You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

261 lines
9.4 KiB

  1. <?php
  2. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  3. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  4. // original author(s).
  5. //
  6. // This code is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  8. // License for more details.
  9. //
  10. // File: ./includes/openurl.inc.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/includes/openurl.inc.php $
  12. // Author(s): Richard Karnesky <mailto:karnesky@gmail.com>
  13. //
  14. // Created: 06-Sep-06, 16:30
  15. // Modified: $Date: 2012-02-29 00:15:45 +0000 (Wed, 29 Feb 2012) $
  16. // $Author: msteffens $
  17. // $Revision: 1349 $
  18. // This include file contains functions that generate OpenURL and COinS data.
  19. // More info about the OpenURL standard (including pointers to further documentation) is available
  20. // at <http://en.wikipedia.org/wiki/OpenURL>. For more info about COinS, see <http://ocoins.info/>.
  21. // TODO: Multiple aus have the same array key, so we apped a number that is later stripped
  22. // Cleanup if possible
  23. // Include refbase markup -> plain text search & replace patterns
  24. include 'includes/transtab_refbase_ascii.inc.php';
  25. function openURL($row, $resolver = "") {
  26. global $openURLResolver; // these variables are defined in 'ini.inc.php'
  27. global $crossRefReqDat;
  28. global $hostInstitutionAbbrevName;
  29. $co = contextObject($row);
  30. $co["sid"] = "refbase:" . $hostInstitutionAbbrevName;
  31. if (empty($resolver))
  32. $resolver = $openURLResolver;
  33. $openURL = $resolver;
  34. if (!preg_match("/\?/", $resolver))
  35. $openURL .= "?";
  36. else
  37. $openURL .= "&amp;";
  38. if (preg_match("#^http://www\.crossref\.org/openurl#", $openURL) && !empty($crossRefReqDat))
  39. $openURL .= "pid=" . rawurlencode($crossRefReqDat) . "&amp;";
  40. $openURL .= "ctx_ver=Z39.88-2004";
  41. foreach ($co as $coKey => $coValue) {
  42. $coKey = preg_replace("/rft./i", "", $coKey);
  43. $coKey = preg_replace("/au[0-9]*/i", "au", $coKey);
  44. $openURL .= "&amp;" . $coKey . "=" . rawurlencode($coValue);
  45. }
  46. return $openURL;
  47. }
  48. function coins($row) {
  49. // fmt_info (type)
  50. $fmt = "info:ofi/fmt:kev:mtx:";
  51. // 'dissertation' is compatible with the 1.0 spec, but not the 0.1 spec
  52. if (!empty($row['thesis']))
  53. $fmt .= "dissertation";
  54. elseif (preg_match("/Journal/", $row['type']))
  55. $fmt .= "journal";
  56. elseif (preg_match("/Patent/", $row['type']))
  57. $fmt .= "patent";
  58. elseif (preg_match("/Book/", $row['type']))
  59. $fmt .= "book";
  60. // 'dc' (dublin core) is compatible with the 1.0 spec, but not the 0.1 spec.
  61. // We default to this, as it is the most generic type.
  62. else
  63. $fmt .= "dc";
  64. $co = contextObject($row);
  65. $coins = "ctx_ver=Z39.88-2004" . "&amp;rft_val_fmt=" . urlencode($fmt);
  66. foreach ($co as $coKey => $coValue) {
  67. // 'urlencode()' differs from 'rawurlencode() (i.e., RFC1738 encoding)
  68. // in that spaces are encoded as plus (+) signs
  69. $coKey = preg_replace("/au[0-9]*/i", "au", $coKey);
  70. // While COinS does not specify encoding, most javascript tools assume that it is UTF-8
  71. // TODO: use function 'detectCharacterEncoding()' instead of the 'mb_*()' functions?
  72. // if (($contentTypeCharset == "ISO-8859-1") AND (detectCharacterEncoding($coValue) != "UTF-8"))
  73. if (mb_detect_encoding($coValue) != "UTF-8" || !(mb_check_encoding($coValue,"UTF-8")))
  74. $coValue = utf8_encode($coValue);
  75. $coins .= "&amp;" . $coKey . "=" . urlencode($coValue);
  76. }
  77. $coins .= "%26ctx_enc%3Dinfo%3Aofi%2Fenc%3AUTF-8";
  78. $coinsSpan = "<span class=\"Z3988\" title=\"" . $coins . "\"></span>";
  79. return $coinsSpan;
  80. }
  81. function contextObject($row) {
  82. global $databaseBaseURL; // defined in 'ini.inc.php'
  83. // The array '$transtab_refbase_ascii' contains search & replace patterns for
  84. // conversion from refbase markup to plain text
  85. global $transtab_refbase_ascii; // defined in 'transtab_refbase_ascii.inc.php'
  86. // Defines search & replace 'actions' that will be applied to all those
  87. // refbase fields that are listed in the corresponding 'fields' element:
  88. $plainTextSearchReplaceActionsArray = array(
  89. array(
  90. 'fields' => array("title", "publication", "abbrev_journal", "address", "keywords", "abstract", "orig_title", "series_title", "abbrev_series_title", "notes"),
  91. 'actions' => $transtab_refbase_ascii
  92. )
  93. );
  94. foreach ($row as $rowFieldName => $rowFieldValue)
  95. // Apply search & replace 'actions' to all fields that are listed in the 'fields'
  96. // element of the arrays contained in '$plainTextSearchReplaceActionsArray':
  97. foreach ($plainTextSearchReplaceActionsArray as $fieldActionsArray)
  98. if (in_array($rowFieldName, $fieldActionsArray['fields']))
  99. // function 'searchReplaceText()' is defined in 'include.inc.php'
  100. $row[$rowFieldName] = searchReplaceText($fieldActionsArray['actions'], $row[$rowFieldName], true);
  101. $co = array();
  102. // rfr_id
  103. $co["rfr_id"] = "info:sid/" . preg_replace("#http://#", "", $databaseBaseURL);
  104. // genre (type)
  105. if (isset($row['type'])) {
  106. if ($row['type'] == "Journal Article")
  107. $co["rft.genre"] = "article";
  108. elseif ($row['type'] == "Book Chapter")
  109. $co["rft.genre"] = "bookitem";
  110. elseif ($row['type'] == "Book Whole")
  111. $co["rft.genre"] = "book";
  112. elseif ($row['type'] == "Conference Article")
  113. $co["rft.genre"] = "proceeding";
  114. elseif ($row['type'] == "Conference Volume")
  115. $co["rft.genre"] = "conference";
  116. elseif ($row['type'] == "Journal")
  117. $co["rft.genre"] = "journal";
  118. elseif ($row['type'] == "Manuscript")
  119. $co["rft.genre"] = "preprint";
  120. elseif ($row['type'] == "Report")
  121. $co["rft.genre"] = "report"; // "report" is only supported by OpenURL v1.0 (but not v0.1)
  122. }
  123. // atitle, btitle, title (title, publication)
  124. if (($row['type'] == "Journal Article") || ($row['type'] == "Book Chapter")) {
  125. if (!empty($row['title']))
  126. $co["rft.atitle"] = $row['title'];
  127. if (!empty($row['publication'])) {
  128. $co["rft.title"] = $row['publication'];
  129. if ($row['type'] == "Book Chapter")
  130. $co["rft.btitle"] = $row['publication'];
  131. }
  132. }
  133. elseif (!empty($row['title']))
  134. $co["rft.title"] = $row['title'];
  135. if (($row['type'] == "Book Whole") && (!empty($row['title'])))
  136. $co["rft.btitle"] = $row['title'];
  137. // stitle (abbrev_journal)
  138. if (!empty($row['abbrev_journal'])) {
  139. $co["rft.stitle"] = $row['abbrev_journal'];
  140. if (empty($row['publication']) && (!isset($co["rft.title"]))) {
  141. // we duplicate the abbreviated journal name to 'rft.title' since the
  142. // CrossRef resolver seems to require 'rft.title' (otherwise it aborts
  143. // with an error: "No journal identifier supplied")
  144. $co["rft.title"] = $row['abbrev_journal'];
  145. }
  146. }
  147. // series (series_title)
  148. if (!empty($row['series_title']))
  149. $co["rft.series"] = $row['series_title'];
  150. // issn
  151. if (!empty($row['issn']))
  152. $co["rft.issn"] = $row['issn'];
  153. // isbn
  154. if (!empty($row['isbn']))
  155. $co["rft.isbn"] = $row['isbn'];
  156. // date (year)
  157. if (!empty($row['year']))
  158. $co["rft.date"] = $row['year'];
  159. // volume
  160. if (!empty($row['volume']))
  161. $co["rft.volume"] = $row['volume'];
  162. // issue
  163. if (!empty($row['issue']))
  164. $co["rft.issue"] = $row['issue'];
  165. // spage, epage, tpages (pages)
  166. // NOTE: lifted from modsxml.inc.php--should throw some into a new include file
  167. if (!empty($row['pages'])) {
  168. if (preg_match("/[0-9] *- *[0-9]/", $row['pages'])) {
  169. list($pagestart, $pageend) = preg_split('/\s*[-]\s*/', $row['pages']);
  170. if ($pagestart < $pageend) {
  171. $co["rft.spage"] = $pagestart;
  172. $co["rft.epage"] = $pageend;
  173. }
  174. }
  175. elseif ($row['type'] == "Book Whole") {
  176. $pagetotal = preg_replace('/^(\d+)\s*pp?\.?$/', "\\1", $row['pages']);
  177. $co["rft.tpages"] = $pagetotal;
  178. }
  179. else
  180. $co["rft.spage"] = $row['pages'];
  181. }
  182. // aulast, aufirst, author (author)
  183. if (!empty($row['author'])) {
  184. $author = $row['author'];
  185. $aulast = extractAuthorsLastName("/ *; */", "/ *, */", 1, $author);
  186. $aufirst = extractAuthorsGivenName("/ *; */", "/ *, */", 1, $author);
  187. if (!empty($aulast))
  188. $co["rft.aulast"] = $aulast;
  189. if (!empty($aufirst))
  190. $co["rft.aufirst"] = $aufirst;
  191. // TODO: cleanup and put this function in include.inc.php?
  192. $authorcount = count(preg_split("/ *; */", $author));
  193. for ($i=0; $i < $authorcount-1; $i++){
  194. $aul = extractAuthorsLastName("/ *; */", "/ *, */", $i+2, $author);
  195. $auf = extractAuthorsGivenName("/ *; */", "/ *, */", $i+2, $author);
  196. if (!empty($aul)) {
  197. $au = $aul;
  198. if (!empty($auf))
  199. $au .= ", ";
  200. }
  201. if (!empty($auf))
  202. $au .= $auf;
  203. if (!empty($au))
  204. $co["rft.au".$i] = $au;
  205. }
  206. }
  207. // pub (publisher)
  208. if (!empty($row['publisher']))
  209. $co["rft.pub"] = $row['publisher'];
  210. // place
  211. if (!empty($row['place']))
  212. $co["rft.place"] = $row['place'];
  213. // id (doi, url)
  214. if (!empty($row['doi']))
  215. $co["rft_id"] = "info:doi/" . $row['doi'];
  216. elseif (!empty($row['url']))
  217. $co["rft_id"] = $row['url'];
  218. return $co;
  219. }
  220. ?>