PageRenderTime 25ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/php/caching/caching.php

https://bitbucket.org/silverasm/wordseer
PHP | 137 lines | 114 code | 10 blank | 13 comment | 10 complexity | 5c8038ec2b21228e04664ae542e980e2 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. /* Copyright 2012 Aditi Muralidharan. See the file "LICENSE" for the full license governing this code. */
  3. /*******************************************************************************
  4. caching.php
  5. Utilities for caching the the sentences,
  6. phrases, and documents that match a query.
  7. *******************************************************************************/
  8. include_once "../util.php";
  9. $wordseer_instance = getGetParam('instance');
  10. $path = '../../../instances/'.$wordseer_instance.'/config.php';
  11. include_once $path;
  12. //Query parameters
  13. $gov = getGetParam('gov');
  14. $govtype = getGetParam('govtype');
  15. $dep = getGetParam('dep');
  16. $deptype = getGetParam('deptype');
  17. $relation = getGetParam('relation');
  18. $collection = getGetParam('collection');
  19. $statistics = getGetParam('statistics');
  20. $start = getGetParam('start');
  21. $limit = getGetParam('limit');
  22. $metadata = decodeGetJson('metadata');
  23. $searches = (Array) decodeGetJson('search');
  24. $phrases = decodeGetJson('phrases');
  25. $timing = getGetParam('timing');
  26. include_once "../grammaticalsearch/get-search-results.php";
  27. include_once '../document/get-metadata.php';
  28. include_once '../subsets/read.php';
  29. include_once "../phrases/get-phrases.php";
  30. if (strstr($_SERVER['REQUEST_URI'], 'caching.php')) {
  31. makeCacheTables();
  32. if (getGetParam('clear')) {
  33. dispatch_clear_query_cache(getGetParam('query_id'));
  34. } else {
  35. dispatch_caching($searches, $collection, $metadata, $phrases);
  36. }
  37. }
  38. function dispatch_clear_query_cache($query_id) {
  39. $sql = "DELETE FROM cached_filtered_sent_ids WHERE query_id < ($query_id-100);";
  40. mysql_query($sql) or die (mysql_error()." on:
  41. <br> $sql caching.php l 46");
  42. }
  43. /** Creates tables to cache the results of applying the filters for this query.
  44. */
  45. function makeCacheTables() {
  46. $sql = "CREATE TABLE IF NOT EXISTS `cached_filtered_sent_ids` (
  47. `id` int(11) NOT NULL DEFAULT '0',
  48. `document_id` int(11) NOT NULL DEFAULT '0',
  49. `num_matched` int(11) NOT NULL DEFAULT '1',
  50. `num_searches_matched` int(11) NOT NULL DEFAULT '0',
  51. `query_id` int NOT NULL DEFAULT '-1',
  52. PRIMARY KEY (`query_id`, `id`),
  53. KEY `num_matched` (`query_id`, `num_matched`, `id`),
  54. KEY `num_searches_matched` (`query_id`, `num_searches_matched`, `id`)
  55. ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
  56. mysql_query($sql) or die (mysql_error()." on:
  57. <br> $sql caching.php l 54");
  58. }
  59. /** Does the filtering operations necessary for the
  60. query and prints out a new query ID.
  61. */
  62. function dispatch_caching($searches, $collection, $metadata, $phrases) {
  63. global $timing;
  64. global $cache_results;
  65. global $search_result_union;
  66. $search_result_union = (count($searches) > 1);
  67. $cache_results = true;
  68. global $query_id;
  69. $query_id = getNextQueryID();
  70. dispatch_clear_query_cache($query_id);
  71. $gov = '';
  72. $dep = '';
  73. $relation = '';
  74. $govtype = 'word';
  75. $deptype = 'word';
  76. if (count($searches) > 0) {
  77. foreach ($searches as $search) {
  78. if ($search_result_union) {
  79. global $num_search_conditions;
  80. global $num_filter_conditions;
  81. $num_search_conditions = 0;
  82. $num_filter_conditions = 0;
  83. }
  84. $gov = $search['gov'];
  85. $dep = $search['dep'];
  86. $relation = $search['relation'];
  87. $govtype = $search['govtype'];
  88. $deptype = $search['deptype'];
  89. if ($relation == "") {
  90. getSentenceSearchResults($gov, $govtype, $collection,
  91. $metadata, $phrases);
  92. } else {
  93. getDependencySentenceResults($gov, $govtype, $dep, $deptype,
  94. $relation, $collection, false, $metadata, $phrases);
  95. }
  96. if ($timing) displayFilterStatistics();
  97. }
  98. } else {
  99. getSentenceSearchResults($gov, $govtype, $collection, $metadata,
  100. $phrases);
  101. }
  102. echo json_encode(array(
  103. 'ok'=>true,
  104. 'query_id'=>$query_id
  105. ));
  106. }
  107. function getNextQueryID() {
  108. $sql = "CREATE TABLE IF NOT EXISTS `query_cache`(
  109. `query_id` int NOT NULL auto_increment,
  110. PRIMARY KEY (`query_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8";
  111. mysql_query($sql) or die (mysql_error()." on:
  112. <br> $sql <br> at caching/caching.php l 104");
  113. $sql = "SELECT max(query_id) as max from query_cache;";
  114. $result = mysql_query($sql) or die (mysql_error()." on:
  115. <br> $sql <br> at caching/caching.php l 107");
  116. $query_id = 0;
  117. while($row = mysql_fetch_assoc($result)) {
  118. $query_id = $row['max'];
  119. }
  120. $query_id += 1;
  121. $sql = "INSERT INTO query_cache (query_id) VALUES ($query_id);";
  122. mysql_query($sql) or die (mysql_error()." on:
  123. <br> $sql <br> at caching/caching.php l 115");
  124. return $query_id;
  125. }
  126. ?>