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.

194 lines
9.7 KiB

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