/src/php/caching/caching.php
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
- <?php
- /* Copyright 2012 Aditi Muralidharan. See the file "LICENSE" for the full license governing this code. */
- /*******************************************************************************
- caching.php
- Utilities for caching the the sentences,
- phrases, and documents that match a query.
- *******************************************************************************/
- include_once "../util.php";
- $wordseer_instance = getGetParam('instance');
- $path = '../../../instances/'.$wordseer_instance.'/config.php';
- include_once $path;
- //Query parameters
- $gov = getGetParam('gov');
- $govtype = getGetParam('govtype');
- $dep = getGetParam('dep');
- $deptype = getGetParam('deptype');
- $relation = getGetParam('relation');
- $collection = getGetParam('collection');
- $statistics = getGetParam('statistics');
- $start = getGetParam('start');
- $limit = getGetParam('limit');
- $metadata = decodeGetJson('metadata');
- $searches = (Array) decodeGetJson('search');
- $phrases = decodeGetJson('phrases');
- $timing = getGetParam('timing');
- include_once "../grammaticalsearch/get-search-results.php";
- include_once '../document/get-metadata.php';
- include_once '../subsets/read.php';
- include_once "../phrases/get-phrases.php";
- if (strstr($_SERVER['REQUEST_URI'], 'caching.php')) {
- makeCacheTables();
- if (getGetParam('clear')) {
- dispatch_clear_query_cache(getGetParam('query_id'));
- } else {
- dispatch_caching($searches, $collection, $metadata, $phrases);
- }
- }
- function dispatch_clear_query_cache($query_id) {
- $sql = "DELETE FROM cached_filtered_sent_ids WHERE query_id < ($query_id-100);";
- mysql_query($sql) or die (mysql_error()." on:
- <br> $sql caching.php l 46");
- }
- /** Creates tables to cache the results of applying the filters for this query.
- */
- function makeCacheTables() {
- $sql = "CREATE TABLE IF NOT EXISTS `cached_filtered_sent_ids` (
- `id` int(11) NOT NULL DEFAULT '0',
- `document_id` int(11) NOT NULL DEFAULT '0',
- `num_matched` int(11) NOT NULL DEFAULT '1',
- `num_searches_matched` int(11) NOT NULL DEFAULT '0',
- `query_id` int NOT NULL DEFAULT '-1',
- PRIMARY KEY (`query_id`, `id`),
- KEY `num_matched` (`query_id`, `num_matched`, `id`),
- KEY `num_searches_matched` (`query_id`, `num_searches_matched`, `id`)
- ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
- mysql_query($sql) or die (mysql_error()." on:
- <br> $sql caching.php l 54");
- }
- /** Does the filtering operations necessary for the
- query and prints out a new query ID.
- */
- function dispatch_caching($searches, $collection, $metadata, $phrases) {
- global $timing;
- global $cache_results;
- global $search_result_union;
- $search_result_union = (count($searches) > 1);
- $cache_results = true;
- global $query_id;
- $query_id = getNextQueryID();
- dispatch_clear_query_cache($query_id);
- $gov = '';
- $dep = '';
- $relation = '';
- $govtype = 'word';
- $deptype = 'word';
- if (count($searches) > 0) {
- foreach ($searches as $search) {
- if ($search_result_union) {
- global $num_search_conditions;
- global $num_filter_conditions;
- $num_search_conditions = 0;
- $num_filter_conditions = 0;
- }
- $gov = $search['gov'];
- $dep = $search['dep'];
- $relation = $search['relation'];
- $govtype = $search['govtype'];
- $deptype = $search['deptype'];
- if ($relation == "") {
- getSentenceSearchResults($gov, $govtype, $collection,
- $metadata, $phrases);
- } else {
- getDependencySentenceResults($gov, $govtype, $dep, $deptype,
- $relation, $collection, false, $metadata, $phrases);
- }
- if ($timing) displayFilterStatistics();
- }
- } else {
- getSentenceSearchResults($gov, $govtype, $collection, $metadata,
- $phrases);
- }
-
- echo json_encode(array(
- 'ok'=>true,
- 'query_id'=>$query_id
- ));
- }
- function getNextQueryID() {
- $sql = "CREATE TABLE IF NOT EXISTS `query_cache`(
- `query_id` int NOT NULL auto_increment,
- PRIMARY KEY (`query_id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8";
- mysql_query($sql) or die (mysql_error()." on:
- <br> $sql <br> at caching/caching.php l 104");
- $sql = "SELECT max(query_id) as max from query_cache;";
- $result = mysql_query($sql) or die (mysql_error()." on:
- <br> $sql <br> at caching/caching.php l 107");
- $query_id = 0;
- while($row = mysql_fetch_assoc($result)) {
- $query_id = $row['max'];
- }
- $query_id += 1;
- $sql = "INSERT INTO query_cache (query_id) VALUES ($query_id);";
- mysql_query($sql) or die (mysql_error()." on:
- <br> $sql <br> at caching/caching.php l 115");
- return $query_id;
- }
- ?>