Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / search / search.php
blob89d5935258e52f08624413e57ffd71bed4b3c3b1
1 <?php
2 /**
4 * @package search
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * search_backend
21 * optional base class for search plugins providing simple caching based on ACM
22 * and functions to retrieve ignore_words and synonyms
23 * @package search
25 class search_backend
27 const SEARCH_RESULT_NOT_IN_CACHE = 0;
28 const SEARCH_RESULT_IN_CACHE = 1;
29 const SEARCH_RESULT_INCOMPLETE = 2;
31 public $ignore_words = array();
32 public $match_synonym = array();
33 public $replace_synonym = array();
35 function __construct(&$error)
37 // This class cannot be used as a search plugin
38 $error = true;
41 /**
42 * Retrieves a language dependend list of words that should be ignored by the search
44 public function get_ignore_words()
46 if (!sizeof($this->ignore_words))
48 $words = array();
50 if (file_exists(phpbb::$user->lang_path . phpbb::$user->lang_name . '/search_ignore_words.' . PHP_EXT))
52 // include the file containing ignore words
53 include(phpbb::$user->lang_path . phpbb::$user->lang_name . '/search_ignore_words.' . PHP_EXT);
56 $this->ignore_words = $words;
57 unset($words);
61 /**
62 * Stores a list of synonyms that should be replaced in $this->match_synonym and $this->replace_synonym and caches them
64 public function get_synonyms()
66 if (!sizeof($this->match_synonym))
68 $synonyms = array();
70 if (file_exists(phpbb::$user->lang_path . phpbb::$user->lang_name . '/search_synonyms.' . PHP_EXT))
72 // include the file containing synonyms
73 include(phpbb::$user->lang_path . phpbb::$user->lang_name . '/search_synonyms.' . PHP_EXT);
76 $this->match_synonym = array_keys($synonyms);
77 $this->replace_synonym = array_values($synonyms);
79 unset($synonyms);
83 /**
84 * Retrieves cached search results
86 * @param int &$result_count will contain the number of all results for the search (not only for the current page)
87 * @param array &$id_ary is filled with the ids belonging to the requested page that are stored in the cache
89 * @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE
91 protected function obtain_ids($search_key, &$result_count, &$id_ary, $start, $per_page, $sort_dir)
93 if (!($stored_ids = phpbb::$acm->get('search_results_' . $search_key)))
95 // no search results cached for this search_key
96 return self::SEARCH_RESULT_NOT_IN_CACHE;
98 else
100 $result_count = $stored_ids[-1];
101 $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
102 $complete = true;
104 // change the start to the actual end of the current request if the sort direction differs
105 // from the dirction in the cache and reverse the ids later
106 if ($reverse_ids)
108 $start = $result_count - $start - $per_page;
110 // the user requested a page past the last index
111 if ($start < 0)
113 return self::SEARCH_RESULT_NOT_IN_CACHE;
117 for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
119 if (!isset($stored_ids[$i]))
121 $complete = false;
123 else
125 $id_ary[] = $stored_ids[$i];
128 unset($stored_ids);
130 if ($reverse_ids)
132 $id_ary = array_reverse($id_ary);
135 if (!$complete)
137 return self::SEARCH_RESULT_INCOMPLETE;
139 return self::SEARCH_RESULT_IN_CACHE;
144 * Caches post/topic ids
146 * @param array &$id_ary contains a list of post or topic ids that shall be cached, the first element
147 * must have the absolute index $start in the result set.
149 protected function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir)
151 $length = min(sizeof($id_ary), phpbb::$config['search_block_size']);
153 // nothing to cache so exit
154 if (!$length)
156 return;
159 $store_ids = array_slice($id_ary, 0, $length);
161 // create a new resultset if there is none for this search_key yet
162 // or add the ids to the existing resultset
163 if (!($store = phpbb::$acm->get('search_results_' . $search_key)))
165 // add the current keywords to the recent searches in the cache which are listed on the search page
166 if (!empty($keywords) || sizeof($author_ary))
168 $sql = 'SELECT search_time
169 FROM ' . SEARCH_RESULTS_TABLE . '
170 WHERE search_key = \'' . phpbb::$db->sql_escape($search_key) . '\'';
171 $result = phpbb::$db->sql_query($sql);
173 if (!phpbb::$db->sql_fetchrow($result))
175 $sql_ary = array(
176 'search_key' => $search_key,
177 'search_time' => time(),
178 'search_keywords' => $keywords,
179 'search_authors' => ' ' . implode(' ', $author_ary) . ' '
182 $sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . phpbb::$db->sql_build_array('INSERT', $sql_ary);
183 phpbb::$db->sql_query($sql);
185 phpbb::$db->sql_freeresult($result);
188 $sql = 'UPDATE ' . USERS_TABLE . '
189 SET user_last_search = ' . time() . '
190 WHERE user_id = ' . phpbb::$user->data['user_id'];
191 phpbb::$db->sql_query($sql);
193 $store = array(-1 => $result_count, -2 => $sort_dir);
194 $id_range = range($start, $start + $length - 1);
196 else
198 // we use one set of results for both sort directions so we have to calculate the indizes
199 // for the reversed array and we also have to reverse the ids themselves
200 if ($store[-2] != $sort_dir)
202 $store_ids = array_reverse($store_ids);
203 $id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
205 else
207 $id_range = range($start, $start + $length - 1);
211 $store_ids = array_combine($id_range, $store_ids);
213 // append the ids
214 if (is_array($store_ids))
216 $store += $store_ids;
218 // if the cache is too big
219 if (sizeof($store) - 2 > 20 * phpbb::$config['search_block_size'])
221 // remove everything in front of two blocks in front of the current start index
222 for ($i = 0, $n = $id_range[0] - 2 * phpbb::$config['search_block_size']; $i < $n; $i++)
224 if (isset($store[$i]))
226 unset($store[$i]);
230 // remove everything after two blocks after the current stop index
231 end($id_range);
232 for ($i = $store[-1] - 1, $n = current($id_range) + 2 * phpbb::$config['search_block_size']; $i > $n; $i--)
234 if (isset($store[$i]))
236 unset($store[$i]);
240 phpbb::$acm->put('search_results_' . $search_key, $store, phpbb::$config['search_store_results']);
242 $sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
243 SET search_time = ' . time() . '
244 WHERE search_key = \'' . phpbb::$db->sql_escape($search_key) . '\'';
245 phpbb::$db->sql_query($sql);
248 unset($store);
249 unset($store_ids);
250 unset($id_range);
254 * Removes old entries from the search results table and removes searches with keywords that contain a word in $words.
256 public function destroy_cache($words, $authors = false)
258 // clear all searches that searched for the specified words
259 if (sizeof($words))
261 $sql_where = '';
262 foreach ($words as $word)
264 $sql_where .= " OR search_keywords " . phpbb::$db->sql_like_expression(phpbb::$db->any_char . $word . phpbb::$db->any_char);
267 $sql = 'SELECT search_key
268 FROM ' . SEARCH_RESULTS_TABLE . "
269 WHERE search_keywords LIKE '%*%' $sql_where";
270 $result = phpbb::$db->sql_query($sql);
272 while ($row = phpbb::$db->sql_fetchrow($result))
274 phpbb::$acm->destroy('search_results_' . $row['search_key']);
276 phpbb::$db->sql_freeresult($result);
279 // clear all searches that searched for the specified authors
280 if (is_array($authors) && sizeof($authors))
282 $sql_where = '';
283 foreach ($authors as $author)
285 $sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors LIKE \'% ' . (int) $author . ' %\'';
288 $sql = 'SELECT search_key
289 FROM ' . SEARCH_RESULTS_TABLE . "
290 WHERE $sql_where";
291 $result = phpbb::$db->sql_query($sql);
293 while ($row = phpbb::$db->sql_fetchrow($result))
295 phpbb::$acm->destroy('search_results_' . $row['search_key']);
297 phpbb::$db->sql_freeresult($result);
300 $sql = 'DELETE
301 FROM ' . SEARCH_RESULTS_TABLE . '
302 WHERE search_time < ' . (time() - phpbb::$config['search_store_results']);
303 phpbb::$db->sql_query($sql);