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.

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