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.

192 lines
9.6 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: ./unapi.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/unapi.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 15-Jul-06, 11:59
  15. // Modified: $Date: 2012-02-27 20:25:30 +0000 (Mon, 27 Feb 2012) $
  16. // $Author: msteffens $
  17. // $Revision: 1337 $
  18. // This script serves as a (faceless) routing page which takes a query to the unAPI server
  19. // and converts the query into a native refbase query which is then passed to 'show.php'.
  20. // More info is given at <http://unapi.refbase.net/>.
  21. // Examples for recognized unAPI queries:
  22. // (URL encoding left out for better readibility, and your server URL will be different,
  23. // of course)
  24. //
  25. // - ask the unAPI server to list all of its supported formats:
  26. // unapi.php
  27. //
  28. // - ask the unAPI server to list all supported formats for a record with serial number 1:
  29. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1
  30. //
  31. // - return record with serial number 1 in various export formats:
  32. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=bibtex
  33. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=endnote
  34. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=ris
  35. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=atom
  36. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=mods
  37. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=oai_dc
  38. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=srw_dc
  39. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=srw_mods
  40. //
  41. // - return record with serial number 1 in various citation formats:
  42. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=rtf
  43. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=pdf
  44. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=latex
  45. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=markdown
  46. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1&format=text
  47. //
  48. // - the unAPI server does also accept multiple record IDs, the given example will return
  49. // records with serial numbers 1, 12, 459 and 600 as citations in RTF format:
  50. // unapi.php?id=http://polaris.ipoe.uni-kiel.de/refs/show.php?records=1,12,459,600&format=rtf
  51. // For more on unAPI, see:
  52. // <http://unapi.info/specs/>
  53. // TODO: - return appropriate HTML status codes
  54. // - improve error handling
  55. // Incorporate some include files:
  56. include 'initialize/db.inc.php'; // 'db.inc.php' is included to hide username and password
  57. include 'includes/include.inc.php'; // include common functions
  58. include 'initialize/ini.inc.php'; // include common variables
  59. include 'includes/unapi.inc.php'; // include functions that deal with unAPI response XML
  60. include_once 'includes/webservice.inc.php'; // include functions that are commonly used with the refbase webservices
  61. // --------------------------------------------------------------------
  62. // START A SESSION:
  63. // call the 'start_session()' function (from 'include.inc.php') which will also read out available session variables:
  64. start_session(true);
  65. // --------------------------------------------------------------------
  66. // Initialize preferred display language:
  67. // (note that 'locales.inc.php' has to be included *after* the call to the 'start_session()' function)
  68. include 'includes/locales.inc.php'; // include the locales
  69. // --------------------------------------------------------------------
  70. // Extract parameters passed to the script:
  71. if (isset($_REQUEST['id']) AND !empty($_REQUEST['id']))
  72. $unapiID = $_REQUEST['id']; // the value must be a permanent URL pointer to a single refbase record (e.g.: 'http://polaris.ipoe.uni-kiel.de/refs/show.php?record=1')
  73. else
  74. $unapiID = "";
  75. // get information how record data shall be returned:
  76. // - 'bibtex' => return as BibTeX data with mime type 'text/plain'
  77. // - 'endnote' => return as Endnote data with mime type 'text/plain'
  78. // - 'ris' => return as RIS data with mime type 'text/plain'
  79. // - 'atom' => return as Atom XML data with mime type 'application/atom+xml'
  80. // - 'mods' => return as MODS XML data with mime type 'application/xml'
  81. // - 'oai_dc' => return as OAI_DC XML data with mime type 'application/xml'
  82. // - 'srw_dc' => return as SRW_DC XML data with mime type 'application/xml'
  83. // - 'srw_mods' => return as SRW_MODS XML data with mime type 'application/xml'
  84. // - 'html' => return as HTML with mime type 'text/html'
  85. // - 'rtf' => return as RTF data with mime type 'application/rtf'
  86. // - 'pdf' => return as PDF data with mime type 'application/pdf'
  87. // - 'latex' => return as LaTeX data with mime type 'application/x-latex'
  88. // - 'markdown' => return as Markdown TEXT data with mime type 'text/plain'
  89. // - 'text' or 'ascii' => return as ASCII TEXT data with mime type 'text/plain'
  90. // TODO: add 'ADS', 'ISI', 'ODF XML' and 'Word XML'
  91. if (isset($_REQUEST['format']) AND preg_match("/^(BibTeX|Endnote|RIS|Atom( XML)?|MODS( XML)?|(OAI_)?DC( XML)?|SRW( XML|_MODS|_DC)?|html|RTF|PDF|LaTeX|Markdown|ASCII|TEXT)$/i", $_REQUEST['format']))
  92. $unapiFormat = $_REQUEST['format'];
  93. else
  94. $unapiFormat = "";
  95. // Set some required parameters based on the requested format:
  96. if (preg_match("/^(BibTeX|Endnote|RIS|Atom( XML)?|MODS( XML)?|(OAI_)?DC( XML)?|SRW( XML|_MODS|_DC)?)$/i", $unapiFormat))
  97. {
  98. $displayType = "Export";
  99. // Standardize XML export format names:
  100. // NOTE: the below regex patterns are potentially too lax and might cause misbehaviour in case any custom export formats have been added
  101. // TODO: add 'ODF XML' and 'Word XML' when they're supported by 'unapi.php'
  102. if (preg_match("/^Atom/i", $unapiFormat))
  103. $exportFormat = "Atom XML";
  104. elseif (preg_match("/^MODS/i", $unapiFormat))
  105. $exportFormat = "MODS XML";
  106. elseif (preg_match("/^(OAI_)?DC/i", $unapiFormat))
  107. $exportFormat = "OAI_DC XML";
  108. elseif (preg_match("/^SRW_DC/i", $unapiFormat))
  109. $exportFormat = "SRW_DC XML";
  110. elseif (preg_match("/^SRW/i", $unapiFormat))
  111. $exportFormat = "SRW_MODS XML";
  112. else
  113. $exportFormat = $unapiFormat;
  114. $citeType = "html";
  115. }
  116. elseif (preg_match("/^(html|RTF|PDF|LaTeX|Markdown|ASCII|TEXT)$/i", $unapiFormat))
  117. {
  118. $displayType = "Cite";
  119. $exportFormat = "";
  120. if (preg_match("/^TEXT/i", $unapiFormat))
  121. $citeType = "ASCII";
  122. else
  123. $citeType = $unapiFormat;
  124. }
  125. else // unrecognized format
  126. {
  127. $displayType = ""; // if the 'submit' parameter is empty, this will produce the default view
  128. $exportFormat = ""; // if no export format was given, 'show.php' will use the default export format which is defined by the '$defaultExportFormat' variable in 'ini.inc.php'
  129. $citeType = "html";
  130. }
  131. // For the context of 'unapi.php' we set some parameters explicitly:
  132. $exportType = "file";
  133. $citeOrder = "author";
  134. $citeStyle = ""; // if no cite style was given, 'show.php' will use the default cite style which is defined by the '$defaultCiteStyle' variable in 'ini.inc.php'
  135. $exportContentType = "application/xml"; // this will be used for unAPI XML response output
  136. // -------------------------------------------------------------------------------------------------------------------
  137. // Check if the correct parameters have been passed:
  138. if (empty($unapiID) OR (!empty($unapiID) AND !isset($_REQUEST['format'])))
  139. {
  140. // if 'unapi.php' was called without the 'format' parameter, we'll return an appropriate unAPI XML response:
  141. // Set the appropriate mimetype & set the character encoding to the one given
  142. // in '$contentTypeCharset' (which is defined in 'ini.inc.php'):
  143. setHeaderContentType($exportContentType, $contentTypeCharset); // function 'setHeaderContentType()' is defined in 'include.inc.php'
  144. echo unapiExplainResponse($unapiID); // function 'unapiExplainResponse()' is defined in 'unapi.inc.php'
  145. }
  146. // Note: error handling should be improved:
  147. // - the script should return "404 Not Found" if the requested identifier is NOT available on the server: header("HTTP/1.1 404 Not Found");
  148. // (currently, an empty file is returned)
  149. // - the script should return "406 Not Acceptable" if the requested identifier is available on the server but is NOT available in the requested format
  150. // (currently, the requested record is displayed in HTML Details view if an unrecognized format was given)
  151. // -------------------------------------------------------------------------------------------------------------------
  152. else // the script was called with the parameters 'id' and 'format'
  153. {
  154. // Build the correct query URL:
  155. // (we skip unnecessary parameters here since 'show.php' will use it's default values for them)
  156. $queryURL = "&submit=" . $displayType . "&exportFormat=" . rawurlencode($exportFormat) . "&exportType=" . $exportType . "&citeOrder=" . $citeOrder . "&citeStyle=" . rawurlencode($citeStyle) . "&citeType=" . $citeType;
  157. // call 'show.php' with the correct query URL in order to output record details in the requested format:
  158. header("Location: $unapiID$queryURL");
  159. }
  160. // -------------------------------------------------------------------------------------------------------------------
  161. ?>