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.

824 lines
45 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: ./opensearch.php
  11. // Repository: $HeadURL$
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 04-Feb-06, 21:53
  15. // Modified: $Date: 2012-02-29 00:42:42 +0000 (Wed, 29 Feb 2012) $
  16. // $Author$
  17. // $Revision: 1356 $
  18. // This script serves as a (faceless) routing page which takes an OpenSearch query and
  19. // converts the query into a native refbase query which is then passed to 'show.php'.
  20. // More info is given at <http://opensearch.refbase.net/>.
  21. // Returns an OpenSearch response. Supports the CQL query language, i.e. it allows to
  22. // query all global refbase fields (the given index name must match either one of the
  23. // 'set.index' names listed in the 'sru.php' explain response or match a refbase field
  24. // name directly). If no index name is given 'cql.serverChoice' will be searched by
  25. // default.
  26. // Examples for recognized OpenSearch queries:
  27. //
  28. // - ask the server to return an OpenSearch Description file:
  29. // opensearch.php?operation=explain
  30. //
  31. // - find all records where any of the "main fields" contains 'immunology':
  32. // opensearch.php?query=immunology
  33. // opensearch.php?query=immunology&recordSchema=atom
  34. //
  35. // - find all records where the title field contains either 'ecology' or 'diversity' but
  36. // return only three records starting with record number 4:
  37. // opensearch.php?query=title%20any%20ecology%20diversity&startRecord=4&maximumRecords=3
  38. //
  39. // - ask the server to return JSON-formatted search suggestions for authors whose last names
  40. // begin with either 'Mil' or 'Bel':
  41. // opensearch.php?query=author%20any%20Mil%20Bel&recordSchema=json&operation=suggest
  42. // By default, 'opensearch.php' will output OpenSearch Atom XML ('recordSchema=atom') if not
  43. // specified otherwise in the query. Additionally, 'rss', 'srw_dc', 'srw_mods', 'html' and
  44. // 'json' are currently supported as response formats.
  45. // For more info on OpenSearch, see:
  46. // <http://opensearch.org/>
  47. // TODO: - I18n
  48. // - proper parsing of CQL query string (currently, 'opensearch.php' allows only for a limited set of CQL queries)
  49. // - offer support for the boolean CQL operators 'and/or/not' and parentheses
  50. // (both of the above goals would be accomplished by adopting Rob's CQL-PHP parser, see 'includes/cql.inc.php')
  51. // - if no context set & index name are given in the query, we should search the user's preferred list of "main fields" by default! (cql.serverChoice)
  52. // - currently, 'opensearch.php' does not omit the records list in the response if the OpenSearch query did contain 'maximumRecords=0' (as is the case for an SRU query)
  53. // - finish 'opensearch2xhtml.xsl', and serve it when returning Atom XML
  54. // - finish the form-based query builder (function 'showQueryPage()')
  55. // - what should be done with diagnostics when the client has requested html or json?
  56. // - fix '$citeOrder' issues (see notes in 'rss.php' and below)
  57. // - include OpenSearch elements in RSS & HTML output (see examples at <http://www.opensearch.org/Specifications/OpenSearch/1.1#OpenSearch_response_elements>)
  58. // - it would be nice if users could somehow pass authentication details with the OpenSearch Query
  59. // - rewrite HTML using divs + CSS
  60. // - see also inline comments labeled with "TODO"
  61. // NOTES: - Currently, the JSON response format is only supported when returning search suggestions
  62. // ('operation=suggest'), i.e. you cannot (yet) retrieve full record data in JSON format
  63. // - ATM, querying of user-specific fields does only work with a user being logged in
  64. // Incorporate some include files:
  65. include 'initialize/db.inc.php'; // 'db.inc.php' is included to hide username and password
  66. include 'includes/header.inc.php'; // include header
  67. include 'includes/footer.inc.php'; // include footer
  68. include 'includes/include.inc.php'; // include common functions
  69. include 'initialize/ini.inc.php'; // include common variables
  70. include 'includes/atomxml.inc.php'; // include functions that deal with Atom XML
  71. include 'includes/opensearch.inc.php'; // include functions that return an OpenSearch response
  72. include 'includes/srwxml.inc.php'; // include functions that deal with SRW XML
  73. include_once 'includes/webservice.inc.php'; // include functions that are commonly used with the refbase webservices
  74. // --------------------------------------------------------------------
  75. // Extract the ID of the client from which the query originated:
  76. // this identifier is used to identify queries that originated from the refbase command line clients ("cli-refbase-1.1", "cli-refbase_import-1.0"),
  77. // from a bookmarklet (e.g., "jsb-refbase-1.0") or from a browser such as Firefox that uses 'opensearch.php' for search suggestions ("sug-refbase_suggest-1.0")
  78. // (note that 'client' parameter has to be extracted *before* the call to the 'start_session()' function, since it's value is required by this function)
  79. if (isset($_REQUEST['client']))
  80. $client = $_REQUEST['client'];
  81. else
  82. $client = "";
  83. // START A SESSION:
  84. // call the 'start_session()' function (from 'include.inc.php') which will also read out available session variables:
  85. start_session(true);
  86. // --------------------------------------------------------------------
  87. // Initialize preferred display language:
  88. // (note that 'locales.inc.php' has to be included *after* the call to the 'start_session()' function)
  89. include 'includes/locales.inc.php'; // include the locales
  90. // --------------------------------------------------------------------
  91. // Extract mandatory parameters passed to the script:
  92. if (isset($_REQUEST['query'])) // contains the keywords to be searched for ('{searchTerms}')
  93. $cqlQuery = $_REQUEST['query'];
  94. else
  95. $cqlQuery = "";
  96. // Extract optional parameters passed to the script:
  97. if (isset($_REQUEST['operation']) AND preg_match("/^(explain|suggest|advanced|CQL)$/i", $_REQUEST['operation']))
  98. $operation = $_REQUEST['operation'];
  99. else
  100. $operation = "";
  101. if (isset($_REQUEST['recordSchema']) AND !empty($_REQUEST['recordSchema'])) // contains the desired response format; currently supports 'atom', 'rss', 'srw_dc', 'srw_mods', 'html' and 'json'
  102. $recordSchema = $_REQUEST['recordSchema'];
  103. else
  104. $recordSchema = "atom";
  105. if (isset($_REQUEST['maximumRecords'])) // contains the desired number of search results (OpenSearch equivalent: '{count}')
  106. $showRows = $_REQUEST['maximumRecords'];
  107. else
  108. $showRows = $_SESSION['userRecordsPerPage']; // get the default number of records per page preferred by the current user
  109. if (isset($_REQUEST['startRecord'])) // contains the offset of the first search result, starting with one (OpenSearch equivalent: '{startIndex}')
  110. $rowOffset = ($_REQUEST['startRecord']) - 1; // first row number in a MySQL result set is 0 (not 1)
  111. else
  112. $rowOffset = ""; // if no value to the 'startRecord' parameter is given, we'll output records starting with the first record in the result set
  113. if (isset($_REQUEST['stylesheet'])) // contains the desired stylesheet to be returned for transformation of XML data
  114. $exportStylesheet = $_REQUEST['stylesheet']; // if the 'stylesheet' parameter was given in the query without a value, this will suppress the default stylesheet
  115. else
  116. $exportStylesheet = "DEFAULT"; // the special keyword "DEFAULT" causes a default stylesheet to be assigned below based on the requested operation and response format
  117. // The following parameters are defined by the OpenSearch Query Syntax specification but aren't supported yet:
  118. // if (isset($_REQUEST['startPage'])) // indicates groups (= pages) of search results, starting with one ('{startPage}'); e.g., if 'maximumRecords=10', 'startPage=3' will cause records 21-30 to be returned
  119. // $pageOffset = ($_REQUEST['startPage']);
  120. // else
  121. // $pageOffset = "";
  122. // if (isset($_REQUEST['language'])) // indicates that the client desires results in the specified language ('{language}')
  123. // $language = ($_REQUEST['language']);
  124. // else
  125. // $language = "";
  126. // if (isset($_REQUEST['outputEncoding'])) // indicates that the client desires results in the specified character encoding ('{outputEncoding}')
  127. // $outputEncoding = ($_REQUEST['outputEncoding']);
  128. // else
  129. // $outputEncoding = "";
  130. // if (isset($_REQUEST['inputEncoding'])) // indicates that query parameters are encoded via the specified character encoding ('{inputEncoding}')
  131. // $inputEncoding = ($_REQUEST['inputEncoding']);
  132. // else
  133. // $inputEncoding = "";
  134. // Extract the view type requested by the user (either 'Mobile', 'Print', 'Web' or ''):
  135. // ('' will produce the default 'Web' output style)
  136. if (isset($_REQUEST['viewType']))
  137. $viewType = $_REQUEST['viewType'];
  138. else
  139. $viewType = "";
  140. // --------------------------------------------------------------------
  141. // Set required variables based on the requested response format:
  142. if (preg_match("/^srw([ _]?(mods|dc))?([ _]?xml)?$/i", $recordSchema)) // if SRW XML is requested as response format
  143. {
  144. if (preg_match("/^srw[ _]?dc/i", $recordSchema))
  145. {
  146. $exportFormat = "SRW_DC XML";
  147. if ($exportStylesheet == "DEFAULT")
  148. $exportStylesheet = "srwdc2html.xsl";
  149. }
  150. else
  151. {
  152. $exportFormat = "SRW_MODS XML";
  153. if ($exportStylesheet == "DEFAULT")
  154. $exportStylesheet = "srwmods2html.xsl";
  155. }
  156. $displayType = "Export";
  157. $exportContentType = "application/xml";
  158. $citeOrder = "";
  159. }
  160. elseif (preg_match("/^rss([ _]?xml)?$/i", $recordSchema)) // if RSS XML is requested as response format
  161. {
  162. $exportFormat = "RSS XML";
  163. $displayType = "Export";
  164. $exportContentType = "application/rss+xml";
  165. if ($exportStylesheet == "DEFAULT")
  166. $exportStylesheet = "";
  167. $citeOrder = ""; // TODO/NOTE: currently, 'rss.php' always sorts records like as if '$citeOrder="creation-date"' was given, i.e. it sorts records such that newly added/edited records get listed top of the list; this means that Atom links to alternate formats (such as HTML or SRW XML) might return different records!
  168. }
  169. elseif (preg_match("/^html$/i", $recordSchema)) // if HTML is requested as response format
  170. {
  171. $exportFormat = ""; // since search results won't be routed thru the 'generateExport()' function, '$exportFormat' will be without effect (which is why we leave it blank)
  172. if (preg_match("/^Mobile$/i", $viewType)) // for Mobile view, we enforce the compact Citation view
  173. $displayType = "Cite";
  174. else
  175. $displayType = ""; // if '$displayType' is empty, 'show.php' will use the default view that's given in session variable 'userDefaultView'
  176. $exportContentType = "text/html";
  177. if ($exportStylesheet == "DEFAULT")
  178. $exportStylesheet = "";
  179. $citeOrder = "";
  180. }
  181. elseif (preg_match("/^json$/i", $recordSchema)) // if JSON is requested as response format
  182. {
  183. $exportFormat = "JSON";
  184. $displayType = "Export";
  185. $exportContentType = "application/json";
  186. if ($exportStylesheet == "DEFAULT")
  187. $exportStylesheet = "";
  188. $citeOrder = "";
  189. }
  190. else // by default, OpenSearch Atom XML ('atom') is assumed as response format
  191. {
  192. $exportFormat = "Atom XML";
  193. $displayType = "Export";
  194. $exportContentType = "application/atom+xml";
  195. if ($exportStylesheet == "DEFAULT")
  196. $exportStylesheet = ""; // TODO: finish 'opensearch2xhtml.xsl'
  197. $citeOrder = ""; // TODO/NOTE: '$citeOrder="creation-date"' would sort records such that newly added/edited records get listed top of the list, but then Atom links to alternate formats (such as HTML or SRW XML) would be mismatched!
  198. }
  199. // -------------------------------------------------------------------------------------------------------------------
  200. // Handle the special index 'main_fields':
  201. if (!(preg_match("/^suggest$/i", $operation) AND preg_match("/^(html|json)$/i", $recordSchema)) AND (preg_match("/^main_fields( +(all|any|exact|within) +| *(<>|<=|>=|<|>|=) *)/i", $cqlQuery))) // if the 'main_fields' index is used in conjunction with a non-"suggest" operation
  202. $cqlQuery = preg_replace("/^main_fields(?= +(all|any|exact|within) +| *(<>|<=|>=|<|>|=) *)/i", "cql.serverChoice", $cqlQuery); // replace 'main_fields' index (which, ATM, is only supported for search suggestions) with 'cql.serverChoice'
  203. // Parse CQL query:
  204. $searchArray = parseCQL("1.1", $cqlQuery, $operation); // function 'parseCQL()' is defined in 'webservice.inc.php'
  205. // Build SQL WHERE clause:
  206. $query = ""; // NOTE: although we don't supply a full SQL query here, the variable MUST be named '$query' to have function 'appendToWhereClause()' work correctly
  207. if (!empty($searchArray))
  208. appendToWhereClause($searchArray); // function 'appendToWhereClause()' is defined in 'include.inc.php'
  209. // -------------------------------------------------------------------------------------------------------------------
  210. // Check that mandatory parameters have been passed:
  211. // - if 'opensearch.php' was called with 'operation=explain', we'll return an appropriate OpenSearch description document:
  212. if (preg_match("/^explain$/i", $operation))
  213. {
  214. // Use an appropriate default stylesheet:
  215. if ($exportStylesheet == "DEFAULT")
  216. $exportStylesheet = ""; // TODO: create a stylesheet ('opensearchDescription2html.xsl') that's appropriate for the OpenSearch description
  217. // Set the appropriate mimetype & set the character encoding to the one given
  218. // in '$contentTypeCharset' (which is defined in 'ini.inc.php'):
  219. setHeaderContentType("application/opensearchdescription+xml", $contentTypeCharset); // function 'setHeaderContentType()' is defined in 'include.inc.php'
  220. echo openSearchDescription($exportStylesheet); // function 'openSearchDescription()' is defined in 'opensearch.inc.php'
  221. }
  222. // - if 'opensearch.php' was called with 'operation=suggest' and HTML (or JSON) as the requested response format,
  223. // we'll return search suggestions that match the 'WHERE' clause given in '$query':
  224. elseif (preg_match("/^suggest$/i", $operation) AND preg_match("/^(html|json)$/i", $recordSchema))
  225. {
  226. // Set the appropriate mimetype & set the character encoding to the one given
  227. // in '$contentTypeCharset' (which is defined in 'ini.inc.php'):
  228. setHeaderContentType($exportContentType, $contentTypeCharset);
  229. echo searchSuggestions($cqlQuery, $query);
  230. }
  231. // - If 'opensearch.php' was called without any recognized parameters, we'll present a form where a user can build a query:
  232. elseif (!isset($_REQUEST['query']) AND !isset($_REQUEST['recordSchema']) AND !isset($_REQUEST['maximumRecords']) AND !isset($_REQUEST['startRecord']) AND !isset($_REQUEST['stylesheet']))
  233. showQueryPage($operation, $viewType, $showRows, $rowOffset);
  234. // - If 'opensearch.php' was called without any valid (or with incorrect) parameters, we'll return appropriate 'diagnostics':
  235. elseif (empty($cqlQuery))
  236. returnDiagnostic(7, "query"); // required 'query' parameter is missing
  237. // - Currently, no other schemas than OpenSearch Atom XML, SRW_DC XML, SRW_MODS XML, RSS XML, HTML and JSON are supported:
  238. elseif (!preg_match("/^((atom|rss)([ _]?xml)?|srw([ _]?(mods|dc))?([ _]?xml)?|html|json)$/i",$recordSchema))
  239. returnDiagnostic(66, $recordSchema); // unknown record schema
  240. // -------------------------------------------------------------------------------------------------------------------
  241. else // the script was called at least with the required 'query' parameter
  242. {
  243. // Write the current OpenSearch/CQL query into a session variable:
  244. // (this session variable is used by functions 'atomCollection()' and 'citeRecords()' (in 'cite_html.php') to re-establish the original OpenSearch/CQL query;
  245. // function 'atomCollection()' uses the OpenSearch/CQL query to output 'opensearch.php' URLs instead of 'show.php' URLs)
  246. saveSessionVariable("cqlQuery", $cqlQuery); // function 'saveSessionVariable()' is defined in 'include.inc.php'
  247. // Build the correct query URL:
  248. // (we skip unnecessary parameters here since function 'generateURL()' and 'show.php' will use their default values for them)
  249. $queryParametersArray = array("where" => $query,
  250. "submit" => $displayType,
  251. "viewType" => $viewType,
  252. "exportStylesheet" => $exportStylesheet
  253. );
  254. // NOTE: The 'show.php' script allows anonymous users to query the 'cite_key' field (if a valid 'userID' is included in the query URL).
  255. // However, this requires that the cite key is passed in the 'cite_key' URL parameter. Since 'opensearch.php' uses the 'where'
  256. // parameter to pass its query, anonymous querying of the 'cite_key' field currently does not work for 'opensearch.php'. But
  257. // querying of user-specific fields will work if a user is logged in.
  258. if (isset($_SESSION['loginEmail'])) // we only include the 'userID' parameter if the user is logged in
  259. $queryParametersArray["userID"] = $loginUserID; // for user-specific fields (such as the 'cite_key' field), 'show.php' requires the 'userID' parameter
  260. // call 'show.php' (or 'rss.php' in case of RSS XML) with the correct query URL in order to output record details in the requested format:
  261. $queryURL = generateURL("show.php", $exportFormat, $queryParametersArray, false, $showRows, $rowOffset, "", $citeOrder); // function 'generateURL()' is defined in 'include.inc.php'
  262. header("Location: $queryURL");
  263. }
  264. // -------------------------------------------------------------------------------------------------------------------
  265. // Return a diagnostic error message:
  266. function returnDiagnostic($diagCode, $diagDetails)
  267. {
  268. global $recordSchema;
  269. global $exportContentType;
  270. global $contentTypeCharset; // '$contentTypeCharset' is defined in 'ini.inc.php'
  271. global $exportStylesheet;
  272. // Set the appropriate mimetype & set the character encoding to the one given in '$contentTypeCharset':
  273. setHeaderContentType($exportContentType, $contentTypeCharset); // function 'setHeaderContentType()' is defined in 'include.inc.php'
  274. if (preg_match("/^srw([ _]?(mods|dc))?([ _]?xml)?$/i", $recordSchema))
  275. // Return SRW diagnostics (i.e. SRW error information) wrapped into SRW XML ('searchRetrieveResponse'):
  276. echo srwDiagnostics($diagCode, $diagDetails, $exportStylesheet); // function 'srwDiagnostics()' is defined in 'srwxml.inc.php'
  277. // elseif (preg_match("/^html$/i", $recordSchema))
  278. // TODO!
  279. // elseif (preg_match("/^json$/i", $recordSchema))
  280. // TODO!
  281. else
  282. // Return OpenSearch diagnostics (i.e. OpenSearch error information) wrapped into OpenSearch Atom XML:
  283. echo openSearchDiagnostics($diagCode, $diagDetails, $exportStylesheet); // function 'openSearchDiagnostics()' is defined in 'opensearch.inc.php'
  284. }
  285. // -------------------------------------------------------------------------------------------------------------------
  286. // Return search suggestions that match the 'WHERE' clause given in '$query':
  287. //
  288. // NOTE: Currently, if you specify a multi-item field with 'all' as a relation (as in 'keywords all ...'), only the
  289. // first search term is used to generate search suggestions (though the other search terms will be used to
  290. // restrict the list of search suggestions to only those where the queried field contains ALL search terms).
  291. //
  292. // TODO: - should we support the 'maximumRecords' and 'startRecord' URL parameters for search suggestions?
  293. // - search suggestions for the 'location' field (and possibly other fields) should be omitted if the user isn't logged in!
  294. function searchSuggestions($cqlQuery, $query)
  295. {
  296. global $recordSchema;
  297. global $loginUserID;
  298. global $tableRefs, $tableUserData; // defined in 'db.inc.php'
  299. global $connection;
  300. global $client;
  301. // Extract the first field & search pattern from the 'WHERE' clause:
  302. // (these will be used to retrieve search suggestions)
  303. $origSearchSuggestionsField = preg_replace("/^[ ()]*(\w+).*/i", "\\1", $query);
  304. $searchSuggestionsPattern = preg_replace("/.*? (?:RLIKE|[=<>]+) \"?(.+?)\"?(?=( *\) *?)*( +(AND|OR)\b|$)).*/i", "\\1", $query); // see NOTE above
  305. if (preg_match("/^main_fields$/i", $origSearchSuggestionsField)) // fetch search suggestions for all of the user's "main fields"
  306. $searchSuggestionsFieldsArray = preg_split("/ *, */", $_SESSION['userMainFields']); // get the list of "main fields" preferred by the current user
  307. else
  308. $searchSuggestionsFieldsArray = array($origSearchSuggestionsField); // we only need to fetch search suggestions for one field
  309. $outputDataArray = array(); // make sure that the buffer variable is empty
  310. // Retrieve matching search suggestions for each field given in '$searchSuggestionsFieldsArray':
  311. foreach ($searchSuggestionsFieldsArray as $searchSuggestionsField)
  312. {
  313. if (preg_match("/^main_fields$/i", $origSearchSuggestionsField))
  314. $searchSuggestionsQuery = preg_replace("/\bmain_fields\b/i", $searchSuggestionsField, $query); // replace 'main_fields' (which doesn't exist as SQL field name) with the current field
  315. else
  316. $searchSuggestionsQuery = $query;
  317. // Check whether we need to split field values for this field:
  318. if (preg_match("/^(author|keywords|abstract|address|corporate_author|place|editor|language|summary_language|series_editor|area|expedition|notes|location|call_number|created_by|modified_by|user_keys|user_notes|user_groups|related)$/i", $searchSuggestionsField))
  319. $splitValues = true;
  320. else
  321. $splitValues = false;
  322. // Define split patterns for this field:
  323. if (preg_match("/^(author|corporate_author|editor|series_editor)$/i", $searchSuggestionsField))
  324. $splitPattern = " *[;()/]+ *";
  325. elseif (preg_match("/^abstract$/i", $searchSuggestionsField))
  326. $splitPattern = "\s*[,.()/?!]+\s+|\s+[,.()/?!]\s*|\s+-\s+"; // TODO: can (or should) abstracts be splitted in a better way?
  327. elseif (preg_match("/^(place|notes|location|user_notes|user_groups|related)$/i", $searchSuggestionsField))
  328. $splitPattern = " *[;]+ *";
  329. elseif (preg_match("/^(call_number)$/i", $searchSuggestionsField))
  330. $splitPattern = " *[;@]+ *";
  331. else
  332. $splitPattern = " *[,;()/]+ *";
  333. // Produce the list of search suggestions for this field:
  334. // (function 'selectDistinct()' is defined in 'include.inc.php')
  335. $searchSuggestionsArray = selectDistinct($connection,
  336. $tableRefs,
  337. "serial",
  338. $tableUserData,
  339. "record_id",
  340. "user_id",
  341. $loginUserID,
  342. $searchSuggestionsField,
  343. "",
  344. "",
  345. "",
  346. "",
  347. "serial",
  348. "\".+\" AND $searchSuggestionsQuery", // this is a somewhat hacky workaround that works around current limitations in function 'selectDistinct()'
  349. $splitValues,
  350. $splitPattern,
  351. "ARRAY",
  352. $searchSuggestionsPattern,
  353. false);
  354. if (!empty($searchSuggestionsArray))
  355. {
  356. // Prefix each item with an index name and relation:
  357. //
  358. // NOTE: When the user selects a search suggestion in Firefox's search box, Firefox replaces the
  359. // user-entered data in the browser's search field with the chosen search suggestion. This
  360. // removes any CQL index and relation that was entered by the user (e.g. "keywords any ...")
  361. // and 'cql.serverChoice' will be searched instead. Since this would lead to unexpected (or
  362. // zero) results, we prefix all search suggestions with the index name and the '=' relation.
  363. //
  364. // TODO: This will need to be revised if 'cql.serverChoice' is mapped to the user's preferred list
  365. // of "main fields". Even better would be if browsers would support alternate query URLs for
  366. // each suggestion in the completion list.
  367. if (preg_match("/^json$/i", $recordSchema) AND preg_match("/^sug/i", $client)) // e.g. "sug-refbase_suggest-1.0"
  368. $searchSuggestionsArray = preg_replace('/^/', "$searchSuggestionsField = ", $searchSuggestionsArray);
  369. $outputDataArray = array_merge($outputDataArray, $searchSuggestionsArray); // append this field's search suggestions to the array of found search suggestions
  370. }
  371. }
  372. if (!empty($outputDataArray))
  373. {
  374. if (preg_match("/^main_fields$/i", $origSearchSuggestionsField)) // otherwise, data are already unique and ordered
  375. {
  376. // Remove duplicate values from array:
  377. $outputDataArray = array_unique($outputDataArray);
  378. // Sort in ascending order:
  379. sort($outputDataArray);
  380. }
  381. if (preg_match("/^json$/i", $recordSchema))
  382. $outputData = '"' . implode('", "', $outputDataArray) . '"';
  383. else // unordered HTML list
  384. $outputData = "<li>" . implode("</li><li>", $outputDataArray) . "</li>";
  385. }
  386. else
  387. $outputData = "";
  388. if (preg_match("/^json$/i", $recordSchema)) // return JSON-formatted search suggestions:
  389. return '["' . $cqlQuery . '", [' . $outputData . ']]'; // e.g.: ["fir", ["firefox", "first choice", "mozilla firefox"]]
  390. else // return HTML-formatted search suggestions:
  391. return "<ul>" . $outputData . "</ul>"; // e.g.: <ul><li>firefox</li><li>first choice</li><li>mozilla firefox</li></ul>
  392. }
  393. // -------------------------------------------------------------------------------------------------------------------
  394. // Present a form where a user can build a query:
  395. function showQueryPage($operation, $viewType, $showRows, $rowOffset)
  396. {
  397. global $officialDatabaseName; // defined in 'ini.inc.php'
  398. global $displayType;
  399. global $loc; // defined in 'locales/core.php'
  400. global $client;
  401. // If there's no stored message available:
  402. if (!isset($_SESSION['HeaderString']))
  403. $HeaderString = $loc["SearchDB"].":"; // Provide the default message
  404. else
  405. {
  406. $HeaderString = $_SESSION['HeaderString']; // extract 'HeaderString' session variable (only necessary if register globals is OFF!)
  407. // Note: though we clear the session variable, the current message is still available to this script via '$HeaderString':
  408. deleteSessionVariable("HeaderString"); // function 'deleteSessionVariable()' is defined in 'include.inc.php'
  409. }
  410. // For HTML output, we'll need to reset the value of the '$displayType' variable
  411. // (which, by default, is set to "Export"; see above); otherwise, the 'originalDisplayType'
  412. // parameter in the 'quickSearch' form of the page header would be incorrectly set to "Export"
  413. $displayType = ""; // if '$displayType' is empty, 'show.php' will use the default view that's given in session variable 'userDefaultView'
  414. // Show the login status:
  415. showLogin(); // (function 'showLogin()' is defined in 'include.inc.php')
  416. // DISPLAY header:
  417. // call the 'displayHTMLhead()' and 'showPageHeader()' functions (which are defined in 'header.inc.php'):
  418. displayHTMLhead(encodeHTML($officialDatabaseName) . " -- " . $loc["Search"], "index,follow", "Search the " . encodeHTML($officialDatabaseName), "", true, "", $viewType, array());
  419. if ((!preg_match("/^Mobile$/i", $viewType)) AND (!preg_match("/^inc/i", $client))) // Note: we omit the visible header in mobile view ('viewType=Mobile') and for include mechanisms!
  420. showPageHeader($HeaderString);
  421. // Define variables holding common drop-down elements, i.e. build properly formatted <option> tag elements:
  422. $dropDownConditionals1Array = array("contains" => $loc["contains"],
  423. "does not contain" => $loc["contains not"],
  424. "is equal to" => $loc["equal to"],
  425. "is not equal to" => $loc["equal to not"],
  426. "starts with" => $loc["starts with"],
  427. "ends with" => $loc["ends with"]);
  428. $dropDownItems1 = buildSelectMenuOptions($dropDownConditionals1Array, "//", "\t\t\t", true); // function 'buildSelectMenuOptions()' is defined in 'include.inc.php'
  429. $dropDownConditionals2Array = array("is greater than" => $loc["is greater than"],
  430. "is less than" => $loc["is less than"],
  431. "is within range" => $loc["is within range"],
  432. "is within list" => $loc["is within list"]);
  433. $dropDownItems2 = buildSelectMenuOptions($dropDownConditionals2Array, "//", "\t\t\t", true);
  434. $dropDownFieldNames1Array = array("author" => $loc["DropDownFieldName_Author"],
  435. "address" => $loc["DropDownFieldName_Address"],
  436. "corporate_author" => $loc["DropDownFieldName_CorporateAuthor"],
  437. "thesis" => $loc["DropDownFieldName_Thesis"],
  438. "", // empty array elements function as spacers between groups of drop-down menu items
  439. "title" => $loc["DropDownFieldName_Title"],
  440. "orig_title" => $loc["DropDownFieldName_OrigTitle"],
  441. "",
  442. "year" => $loc["DropDownFieldName_Year"],
  443. "publication" => $loc["DropDownFieldName_Publication"],
  444. "abbrev_journal" => $loc["DropDownFieldName_AbbrevJournal"],
  445. "editor" => $loc["DropDownFieldName_Editor"],
  446. "",
  447. "volume_numeric" => $loc["DropDownFieldName_Volume"], // 'volume_numeric' is used instead of 'volume' in the sort dropdown menus
  448. "issue" => $loc["DropDownFieldName_Issue"],
  449. "pages" => $loc["DropDownFieldName_Pages"],
  450. "",
  451. "series_title" => $loc["DropDownFieldName_SeriesTitle"],
  452. "abbrev_series_title" => $loc["DropDownFieldName_AbbrevSeriesTitle"],
  453. "series_editor" => $loc["DropDownFieldName_SeriesEditor"],
  454. "series_volume_numeric" => $loc["DropDownFieldName_SeriesVolume"], // 'series_volume_numeric' is used instead of 'series_volume' in the sort dropdown menus
  455. "series_issue" => $loc["DropDownFieldName_SeriesIssue"],
  456. "",
  457. "publisher" => $loc["DropDownFieldName_Publisher"],
  458. "place" => $loc["DropDownFieldName_Place"],
  459. "",
  460. "edition" => $loc["DropDownFieldName_Edition"],
  461. "medium" => $loc["DropDownFieldName_Medium"],
  462. "issn" => $loc["DropDownFieldName_Issn"],
  463. "isbn" => $loc["DropDownFieldName_Isbn"],
  464. "",
  465. "language" => $loc["DropDownFieldName_Language"],
  466. "summary_language" => $loc["DropDownFieldName_SummaryLanguage"],
  467. "",
  468. "keywords" => $loc["DropDownFieldName_Keywords"],
  469. "abstract" => $loc["DropDownFieldName_Abstract"],
  470. "",
  471. "area" => $loc["DropDownFieldName_Area"],
  472. "expedition" => $loc["DropDownFieldName_Expedition"],
  473. "conference" => $loc["DropDownFieldName_Conference"],
  474. "",
  475. "doi" => $loc["DropDownFieldName_Doi"],
  476. "url" => $loc["DropDownFieldName_Url"]);
  477. if (isset($_SESSION['loginEmail'])) // we only include the 'file' field if the user is logged in
  478. $dropDownFieldNames1Array["file"] = $loc["DropDownFieldName_File"];
  479. $dropDownFieldNames1Array[] = "";
  480. $dropDownFieldNames1Array["notes"] = $loc["DropDownFieldName_Notes"];
  481. if (isset($_SESSION['loginEmail'])) // we only include the 'location' field if the user is logged in
  482. $dropDownFieldNames1Array["location"] = $loc["DropDownFieldName_Location"];
  483. $dropDownFieldNames2Array = array("call_number" => $loc["DropDownFieldName_CallNumber"],
  484. "",
  485. "serial" => $loc["DropDownFieldName_Serial"],
  486. "type" => $loc["DropDownFieldName_Type"],
  487. "approved" => $loc["DropDownFieldName_Approved"],
  488. "",
  489. "created_date" => $loc["DropDownFieldName_CreatedDate"],
  490. "created_time" => $loc["DropDownFieldName_CreatedTime"]);
  491. if (isset($_SESSION['loginEmail'])) // we only include the 'created_by' field if the user is logged in
  492. $dropDownFieldNames2Array["created_by"] = $loc["DropDownFieldName_CreatedBy"];
  493. $dropDownFieldNames2Array[] = "";
  494. $dropDownFieldNames2Array["modified_date"] = $loc["DropDownFieldName_ModifiedDate"];
  495. $dropDownFieldNames2Array["modified_time"] = $loc["DropDownFieldName_ModifiedTime"];
  496. if (isset($_SESSION['loginEmail'])) // we only include the 'modified_by' field if the user is logged in
  497. $dropDownFieldNames2Array["modified_by"] = $loc["DropDownFieldName_ModifiedBy"];
  498. $dropDownItems3 = buildSelectMenuOptions(array_merge($dropDownFieldNames1Array,$dropDownFieldNames2Array), "//", "\t\t\t", true);
  499. $dropDownConditionals3Array = array("html" => "html",
  500. "atom" => "Atom XML",
  501. "rss" => "RSS XML",
  502. "srw_dc" => "SRW_DC XML",
  503. "srw_mods" => "SRW_MODS XML");
  504. $dropDownItems4 = buildSelectMenuOptions($dropDownConditionals3Array, "//", "\t\t\t", true);
  505. // Map CQL indexes to refbase field names:
  506. $indexNamesArray = mapCQLIndexes(); // function 'mapCQLIndexes()' is defined in 'webservice.inc.php'
  507. // --------------------------------------------------------------------
  508. // TODO: when the simple CQL Query Builder interface is done, a call to 'opensearch.php' (or 'opensearch.php?operation=simple')
  509. // should activate that simple GUI-based interface (currently, it activates the advanced interface that you'd normally only
  510. // get via 'opensearch.php?operation=cql' or 'opensearch.php?operation=advanced')
  511. // if (preg_match("/^(advanced|CQL)$/i", $operation))
  512. showQueryFormAdvanced($dropDownItems1, $dropDownItems2, $dropDownItems3, $dropDownItems4, $showRows, $rowOffset, $indexNamesArray, $viewType); // let's you enter a standard CQL query directly
  513. // else
  514. // showQueryFormSimple($dropDownItems1, $dropDownItems2, $dropDownItems3, $dropDownItems4, $showRows, $rowOffset, $indexNamesArray, $viewType); // let's you build a CQL query via dropdown menues
  515. // --------------------------------------------------------------------
  516. // DISPLAY THE HTML FOOTER:
  517. // call the 'showPageFooter()' and 'displayHTMLfoot()' functions (which are defined in 'footer.inc.php')
  518. if ((!preg_match("/^Mobile$/i", $viewType)) AND (!preg_match("/^inc/i", $client))) // Note: we omit the visible footer in mobile view ('viewType=Mobile') and for include mechanisms!
  519. showPageFooter($HeaderString);
  520. displayHTMLfoot();
  521. }
  522. // -------------------------------------------------------------------------------------------------------------------
  523. // Present a form where a user can build a CQL query via dropdown menues:
  524. //
  525. // TODO: - add a button to add/remove query lines
  526. // - for each form option chosen by the user, a little JavaScript should adopt the underlying CQL query (which finally gets sent to 'opensearch.php' in the 'query' parameter)
  527. // - a 'setup' parameter should allow to pass a full CQL query to 'opensearch.php'; this will be parsed and used to setup the default choice of fields & options
  528. // - offer to save the current choice of fields & options as a CQL query to the 'user_options' table, and reload it upon login using the 'setup' parameter
  529. function showQueryFormSimple($dropDownItems1, $dropDownItems2, $dropDownItems3, $dropDownItems4, $showRows, $rowOffset, $indexNamesArray, $viewType)
  530. {
  531. global $loc; // defined in 'locales/core.php'
  532. // Start <form> and <table> holding the form elements:
  533. ?>
  534. <form action="opensearch.php" method="GET" name="openSearch">
  535. <input type="hidden" name="formType" value="openSearch">
  536. <input type="hidden" name="submit" value="<?php echo $loc["ButtonTitle_Search"]; ?>">
  537. <input type="hidden" name="viewType" value="<?php echo $viewType; ?>">
  538. <table id="queryform" align="center" border="0" cellpadding="0" cellspacing="10" width="95%" summary="This table holds a query form">
  539. <tr>
  540. <td width="120" valign="top">
  541. <div class="sect"><?php echo $loc["Query"]; ?>:</div>
  542. </td><?php
  543. // NOTE: the field selectors and search options don't work yet (see the TODO items at the top of this function)
  544. /*
  545. <td width="140">
  546. <select name="fieldSelector"><?php echo $dropDownItems3; ?>
  547. </select>
  548. </td>
  549. <td width="122">
  550. <select name="fieldConditionalSelector"><?php echo $dropDownItems1; ?>
  551. </select>
  552. </td>
  553. */
  554. ?>
  555. <td colspan="2"><input type="text" name="query" value="" size="60"></td>
  556. </tr>
  557. <tr>
  558. <td>&nbsp;</td>
  559. <td>
  560. <input type="submit" name="submit" value="<?php echo $loc["ButtonTitle_Search"]; ?>" title="<?php echo $loc["DescriptionSearchDB"]; ?>">
  561. </td>
  562. </tr>
  563. </table>
  564. </form><?php
  565. }
  566. // -------------------------------------------------------------------------------------------------------------------
  567. // Present a form where a user can enter a standard CQL query directly:
  568. //
  569. // TODO: use divs + CSS styling (instead of a table-based layout) for _all_ output, especially for 'viewType=Mobile'
  570. function showQueryFormAdvanced($dropDownItems1, $dropDownItems2, $dropDownItems3, $dropDownItems4, $showRows, $rowOffset, $indexNamesArray, $viewType)
  571. {
  572. global $officialDatabaseName; // defined in 'ini.inc.php'
  573. global $loc; // defined in 'locales/core.php'
  574. // Start <form> and <table> holding the form elements:
  575. ?>
  576. <form action="opensearch.php" method="GET" name="openSearch">
  577. <input type="hidden" name="formType" value="openSearch">
  578. <input type="hidden" name="submit" value="<?php echo $loc["ButtonTitle_Search"]; ?>">
  579. <input type="hidden" name="viewType" value="<?php echo $viewType; ?>">
  580. <table id="queryform" align="center" border="0" cellpadding="0" cellspacing="10" width="95%" summary="This table holds the query form">
  581. <tr>
  582. <td width="120" valign="middle">
  583. <div class="sect"><?php
  584. if (preg_match("/^Mobile$/i", $viewType))
  585. echo $officialDatabaseName;
  586. else
  587. echo $loc["CQLQuery"];
  588. ?>:</div>
  589. </td>
  590. <td>
  591. <input type="text" name="query" value="" size="60" title="<?php echo $loc["DescriptionEnterSearchString"]; ?>">
  592. </td>
  593. </tr>
  594. <tr>
  595. <td>&nbsp;</td>
  596. <td>
  597. <input type="submit" name="submit" value="<?php echo $loc["ButtonTitle_Search"]; ?>" title="<?php echo $loc["DescriptionSearchDB"]; ?>">
  598. </td>
  599. </tr>
  600. </table>
  601. <table class="showhide" align="center" border="0" cellpadding="0" cellspacing="10" width="95%">
  602. <tr>
  603. <td class="small" width="120" valign="top">
  604. <a href="javascript:toggleVisibility('searchopt','optToggleimg','optToggletxt','<?php echo rawurlencode($loc["DisplayOptions"]); ?>')"<?php echo addAccessKey("attribute", "search_opt"); ?> title="<?php echo $loc["LinkTitle_ToggleVisibility"] . addAccessKey("title", "search_opt"); ?>">
  605. <img id="optToggleimg" class="toggleimg" src="img/closed.gif" alt="<?php echo $loc["LinkTitle_ToggleVisibility"]; ?>" width="9" height="9" hspace="0" border="0">
  606. <span id="optToggletxt" class="toggletxt"><?php echo $loc["DisplayOptions"]; ?></span>
  607. </a>
  608. </td>
  609. </tr>
  610. </table>
  611. <table id="searchopt" align="center" border="0" cellpadding="0" cellspacing="10" width="95%" summary="This table holds search &amp; display options" style="display: none;">
  612. <tr>
  613. <td width="120" valign="middle">
  614. <div class="sect"><?php echo $loc["DisplayOptions"]; ?>:</div>
  615. </td>
  616. <td width="215" valign="top">
  617. <?php echo $loc["StartAtRecord"]; ?>:&nbsp;&nbsp;
  618. <input type="text" name="startRecord" value="<?php echo ($rowOffset + 1); ?>" size="4" title="<?php echo $loc["DescriptionStartAtRecord"]; ?>">
  619. </td>
  620. <td valign="top">
  621. <?php echo $loc["ShowRecordsPerPage_Prefix"]; ?>&nbsp;&nbsp;&nbsp;<input type="text" id="maximumRecords" name="maximumRecords" value="<?php echo $showRows; ?>" size="4" title="<?php echo $loc["DescriptionShowRecordsPerPage"]; ?>">&nbsp;&nbsp;&nbsp;<?php echo $loc["ShowRecordsPerPage_Suffix"]; ?>
  622. </td>
  623. </tr>
  624. <tr>
  625. <td>&nbsp;</td>
  626. <td valign="top" colspan="2">
  627. <?php echo $loc["Format"]; ?>:&nbsp;&nbsp;
  628. <select name="recordSchema" title="<?php echo $loc["DescriptionSelectCiteFormat"]; ?>"><?php echo $dropDownItems4; ?>
  629. </select>
  630. </td>
  631. </tr>
  632. </table>
  633. <table class="showhide" align="center" border="0" cellpadding="0" cellspacing="10" width="95%">
  634. <tr>
  635. <td class="small" width="120" valign="top">
  636. <a href="javascript:toggleVisibility('helptxt','helpToggleimg','helpToggletxt','<?php echo rawurlencode($loc["HelpAndExamples"]); ?>')"<?php echo addAccessKey("attribute", "search_help"); ?> title="<?php echo $loc["LinkTitle_ToggleVisibility"] . addAccessKey("title", "search_help"); ?>">
  637. <img id="helpToggleimg" class="toggleimg" src="img/closed.gif" alt="<?php echo $loc["LinkTitle_ToggleVisibility"]; ?>" width="9" height="9" hspace="0" border="0">
  638. <span id="helpToggletxt" class="toggletxt"><?php echo $loc["HelpAndExamples"]; ?></span>
  639. </a>
  640. </td>
  641. </tr>
  642. </table>
  643. <table id="helptxt" align="center" border="0" cellpadding="0" cellspacing="10" width="95%" summary="This table holds some help text and example queries" style="display: none;">
  644. <tr>
  645. <td width="120" valign="top">
  646. <div class="sect"><?php echo $loc["Help"]; ?>:</div>
  647. </td>
  648. <td class="helpbody" valign="top">
  649. <div class="even">
  650. This form lets you search the literature database using a standard CQL query (<a href="http://www.loc.gov/standards/sru/specs/cql.html" target="top">Common Query Language</a>). You can simply enter a query term, in which case the <em><?php echo $indexNamesArray["cql.serverChoice"]; ?></em> field will be searched by default. You can also search any other field, some query examples are given below. An introduction to CQL is given <a href="http://zing.z3950.org/cql/intro.html" target="top">here</a>.
  651. </div>
  652. </td>
  653. </tr>
  654. <tr>
  655. <td width="120" valign="top">
  656. <div class="sect"><?php echo $loc["Examples"]; ?>:</div>
  657. </td>
  658. <td class="examples" valign="top">
  659. <div class="even">
  660. Find all records where the <em><?php echo $indexNamesArray["cql.serverChoice"]; ?></em> field contains the word "ecology":
  661. <pre>ecology</pre>
  662. </div>
  663. <div class="odd">
  664. You can use wildcards anywhere in a search term to match one (<code>?</code>) or more (<code>*</code>) unspecified characters. E.g. this finds all records where the <em><?php echo $indexNamesArray["cql.serverChoice"]; ?></em> field contains a word that starts with "ecolog":
  665. <pre>ecolog*</pre>
  666. </div>
  667. <div class="even">
  668. Find all records where the <em>title</em> field contains <code>any</code> of the given words ("ecology" OR "diversity"):
  669. <pre>title any ecology diversity</pre>
  670. </div>
  671. <div class="odd">
  672. Find all records where the <em>author</em> field contains <code>all</code> of the given words ("dieckmann" AND "thomas" AND "sullivan"):
  673. <pre>author all dieckmann thomas sullivan</pre>
  674. </div>
  675. <div class="even">
  676. You can also search for <code>exact</code> field matches. E.g. this finds all records where the <em>publication</em> field equals EXACTLY "Marine Ecology Progress Series":
  677. <pre>publication exact Marine Ecology Progress Series</pre>
  678. </div>
  679. <div class="odd">
  680. For numeric fields, the obvious ordered relations (<code>&lt;</code>, <code>&lt;=</code>, <code>=</code>, <code>&gt;=</code>, <code>&gt;</code>) may be used. E.g. this finds all records where the <em>year</em> field is greater than or equals "2005":
  681. <pre>year &gt;= 2005</pre>
  682. </div>
  683. <div class="even">
  684. For numeric fields, you can match a range using the <code>within</code> relation followed by the lower and upper end of the range. E.g. this finds all records where the <em>volume</em> field contains a number between "10" and "20":
  685. <pre>volume within 10 20</pre>
  686. </div>
  687. </td>
  688. </tr>
  689. </table>
  690. </form><?php
  691. }
  692. // -------------------------------------------------------------------------------------------------------------------
  693. ?>