1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/search/search.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,320 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package search
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +/**
1.23 +* @ignore
1.24 +*/
1.25 +define('SEARCH_RESULT_NOT_IN_CACHE', 0);
1.26 +define('SEARCH_RESULT_IN_CACHE', 1);
1.27 +define('SEARCH_RESULT_INCOMPLETE', 2);
1.28 +
1.29 +/**
1.30 +* search_backend
1.31 +* optional base class for search plugins providing simple caching based on ACM
1.32 +* and functions to retrieve ignore_words and synonyms
1.33 +* @package search
1.34 +*/
1.35 +class search_backend
1.36 +{
1.37 + var $ignore_words = array();
1.38 + var $match_synonym = array();
1.39 + var $replace_synonym = array();
1.40 +
1.41 + function search_backend(&$error)
1.42 + {
1.43 + // This class cannot be used as a search plugin
1.44 + $error = true;
1.45 + }
1.46 +
1.47 + /**
1.48 + * Retrieves a language dependend list of words that should be ignored by the search
1.49 + */
1.50 + function get_ignore_words()
1.51 + {
1.52 + if (!sizeof($this->ignore_words))
1.53 + {
1.54 + global $user, $phpEx;
1.55 +
1.56 + $words = array();
1.57 +
1.58 + if (file_exists("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx"))
1.59 + {
1.60 + // include the file containing ignore words
1.61 + include("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx");
1.62 + }
1.63 +
1.64 + $this->ignore_words = $words;
1.65 + unset($words);
1.66 + }
1.67 + }
1.68 +
1.69 + /**
1.70 + * Stores a list of synonyms that should be replaced in $this->match_synonym and $this->replace_synonym and caches them
1.71 + */
1.72 + function get_synonyms()
1.73 + {
1.74 + if (!sizeof($this->match_synonym))
1.75 + {
1.76 + global $user, $phpEx;
1.77 +
1.78 + $synonyms = array();
1.79 +
1.80 + if (file_exists("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx"))
1.81 + {
1.82 + // include the file containing synonyms
1.83 + include("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx");
1.84 + }
1.85 +
1.86 + $this->match_synonym = array_keys($synonyms);
1.87 + $this->replace_synonym = array_values($synonyms);
1.88 +
1.89 + unset($synonyms);
1.90 + }
1.91 + }
1.92 +
1.93 + /**
1.94 + * Retrieves cached search results
1.95 + *
1.96 + * @param int &$result_count will contain the number of all results for the search (not only for the current page)
1.97 + * @param array &$id_ary is filled with the ids belonging to the requested page that are stored in the cache
1.98 + *
1.99 + * @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE
1.100 + */
1.101 + function obtain_ids($search_key, &$result_count, &$id_ary, $start, $per_page, $sort_dir)
1.102 + {
1.103 + global $cache;
1.104 +
1.105 + if (!($stored_ids = $cache->get('_search_results_' . $search_key)))
1.106 + {
1.107 + // no search results cached for this search_key
1.108 + return SEARCH_RESULT_NOT_IN_CACHE;
1.109 + }
1.110 + else
1.111 + {
1.112 + $result_count = $stored_ids[-1];
1.113 + $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
1.114 + $complete = true;
1.115 +
1.116 + // change the start to the actual end of the current request if the sort direction differs
1.117 + // from the dirction in the cache and reverse the ids later
1.118 + if ($reverse_ids)
1.119 + {
1.120 + $start = $result_count - $start - $per_page;
1.121 +
1.122 + // the user requested a page past the last index
1.123 + if ($start < 0)
1.124 + {
1.125 + return SEARCH_RESULT_NOT_IN_CACHE;
1.126 + }
1.127 + }
1.128 +
1.129 + for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
1.130 + {
1.131 + if (!isset($stored_ids[$i]))
1.132 + {
1.133 + $complete = false;
1.134 + }
1.135 + else
1.136 + {
1.137 + $id_ary[] = $stored_ids[$i];
1.138 + }
1.139 + }
1.140 + unset($stored_ids);
1.141 +
1.142 + if ($reverse_ids)
1.143 + {
1.144 + $id_ary = array_reverse($id_ary);
1.145 + }
1.146 +
1.147 + if (!$complete)
1.148 + {
1.149 + return SEARCH_RESULT_INCOMPLETE;
1.150 + }
1.151 + return SEARCH_RESULT_IN_CACHE;
1.152 + }
1.153 + }
1.154 +
1.155 + /**
1.156 + * Caches post/topic ids
1.157 + *
1.158 + * @param array &$id_ary contains a list of post or topic ids that shall be cached, the first element
1.159 + * must have the absolute index $start in the result set.
1.160 + */
1.161 + function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir)
1.162 + {
1.163 + global $cache, $config, $db, $user;
1.164 +
1.165 + $length = min(sizeof($id_ary), $config['search_block_size']);
1.166 +
1.167 + // nothing to cache so exit
1.168 + if (!$length)
1.169 + {
1.170 + return;
1.171 + }
1.172 +
1.173 + $store_ids = array_slice($id_ary, 0, $length);
1.174 +
1.175 + // create a new resultset if there is none for this search_key yet
1.176 + // or add the ids to the existing resultset
1.177 + if (!($store = $cache->get('_search_results_' . $search_key)))
1.178 + {
1.179 + // add the current keywords to the recent searches in the cache which are listed on the search page
1.180 + if (!empty($keywords) || sizeof($author_ary))
1.181 + {
1.182 + $sql = 'SELECT search_time
1.183 + FROM ' . SEARCH_RESULTS_TABLE . '
1.184 + WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
1.185 + $result = $db->sql_query($sql);
1.186 +
1.187 + if (!$db->sql_fetchrow($result))
1.188 + {
1.189 + $sql_ary = array(
1.190 + 'search_key' => $search_key,
1.191 + 'search_time' => time(),
1.192 + 'search_keywords' => $keywords,
1.193 + 'search_authors' => ' ' . implode(' ', $author_ary) . ' '
1.194 + );
1.195 +
1.196 + $sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1.197 + $db->sql_query($sql);
1.198 + }
1.199 + $db->sql_freeresult($result);
1.200 + }
1.201 +
1.202 + $sql = 'UPDATE ' . USERS_TABLE . '
1.203 + SET user_last_search = ' . time() . '
1.204 + WHERE user_id = ' . $user->data['user_id'];
1.205 + $db->sql_query($sql);
1.206 +
1.207 + $store = array(-1 => $result_count, -2 => $sort_dir);
1.208 + $id_range = range($start, $start + $length - 1);
1.209 + }
1.210 + else
1.211 + {
1.212 + // we use one set of results for both sort directions so we have to calculate the indizes
1.213 + // for the reversed array and we also have to reverse the ids themselves
1.214 + if ($store[-2] != $sort_dir)
1.215 + {
1.216 + $store_ids = array_reverse($store_ids);
1.217 + $id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
1.218 + }
1.219 + else
1.220 + {
1.221 + $id_range = range($start, $start + $length - 1);
1.222 + }
1.223 + }
1.224 +
1.225 + $store_ids = array_combine($id_range, $store_ids);
1.226 +
1.227 + // append the ids
1.228 + if (is_array($store_ids))
1.229 + {
1.230 + $store += $store_ids;
1.231 +
1.232 + // if the cache is too big
1.233 + if (sizeof($store) - 2 > 20 * $config['search_block_size'])
1.234 + {
1.235 + // remove everything in front of two blocks in front of the current start index
1.236 + for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++)
1.237 + {
1.238 + if (isset($store[$i]))
1.239 + {
1.240 + unset($store[$i]);
1.241 + }
1.242 + }
1.243 +
1.244 + // remove everything after two blocks after the current stop index
1.245 + end($id_range);
1.246 + for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--)
1.247 + {
1.248 + if (isset($store[$i]))
1.249 + {
1.250 + unset($store[$i]);
1.251 + }
1.252 + }
1.253 + }
1.254 + $cache->put('_search_results_' . $search_key, $store, $config['search_store_results']);
1.255 +
1.256 + $sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
1.257 + SET search_time = ' . time() . '
1.258 + WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
1.259 + $db->sql_query($sql);
1.260 + }
1.261 +
1.262 + unset($store);
1.263 + unset($store_ids);
1.264 + unset($id_range);
1.265 + }
1.266 +
1.267 + /**
1.268 + * Removes old entries from the search results table and removes searches with keywords that contain a word in $words.
1.269 + */
1.270 + function destroy_cache($words, $authors = false)
1.271 + {
1.272 + global $db, $cache, $config;
1.273 +
1.274 + // clear all searches that searched for the specified words
1.275 + if (sizeof($words))
1.276 + {
1.277 + $sql_where = '';
1.278 + foreach ($words as $word)
1.279 + {
1.280 + $sql_where .= " OR search_keywords " . $db->sql_like_expression($db->any_char . $word . $db->any_char);
1.281 + }
1.282 +
1.283 + $sql = 'SELECT search_key
1.284 + FROM ' . SEARCH_RESULTS_TABLE . "
1.285 + WHERE search_keywords LIKE '%*%' $sql_where";
1.286 + $result = $db->sql_query($sql);
1.287 +
1.288 + while ($row = $db->sql_fetchrow($result))
1.289 + {
1.290 + $cache->destroy('_search_results_' . $row['search_key']);
1.291 + }
1.292 + $db->sql_freeresult($result);
1.293 + }
1.294 +
1.295 + // clear all searches that searched for the specified authors
1.296 + if (is_array($authors) && sizeof($authors))
1.297 + {
1.298 + $sql_where = '';
1.299 + foreach ($authors as $author)
1.300 + {
1.301 + $sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors LIKE \'% ' . (int) $author . ' %\'';
1.302 + }
1.303 +
1.304 + $sql = 'SELECT search_key
1.305 + FROM ' . SEARCH_RESULTS_TABLE . "
1.306 + WHERE $sql_where";
1.307 + $result = $db->sql_query($sql);
1.308 +
1.309 + while ($row = $db->sql_fetchrow($result))
1.310 + {
1.311 + $cache->destroy('_search_results_' . $row['search_key']);
1.312 + }
1.313 + $db->sql_freeresult($result);
1.314 + }
1.315 +
1.316 + $sql = 'DELETE
1.317 + FROM ' . SEARCH_RESULTS_TABLE . '
1.318 + WHERE search_time < ' . (time() - $config['search_store_results']);
1.319 + $db->sql_query($sql);
1.320 + }
1.321 +}
1.322 +
1.323 +?>
1.324 \ No newline at end of file