(a little test for later merges)
[phpbb.git] / phpBB / includes / search / search.php
blob2f20d11495b8092456a6f43a62bb9302e13aed1f
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 * @ignore
22 define('SEARCH_RESULT_NOT_IN_CACHE', 0);
23 define('SEARCH_RESULT_IN_CACHE', 1);
24 define('SEARCH_RESULT_INCOMPLETE', 2);
26 /**
27 * search_backend
28 * optional base class for search plugins providing simple caching based on ACM
29 * and functions to retrieve ignore_words and synonyms
30 * @package search
32 class search_backend
34 var $ignore_words = array();
35 var $match_synonym = array();
36 var $replace_synonym = array();
38 function search_backend(&$error)
40 // This class cannot be used as a search plugin
41 $error = true;
44 /**
45 * Retrieves a language dependend list of words that should be ignored by the search
47 function get_ignore_words()
49 if (!sizeof($this->ignore_words))
51 global $user, $phpEx;
53 $words = array();
55 if (file_exists("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx"))
57 // include the file containing ignore words
58 include("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx");
61 $this->ignore_words = $words;
62 unset($words);
66 /**
67 * Stores a list of synonyms that should be replaced in $this->match_synonym and $this->replace_synonym and caches them
69 function get_synonyms()
71 if (!sizeof($this->match_synonym))
73 global $user, $phpEx;
75 $synonyms = array();
77 if (file_exists("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx"))
79 // include the file containing synonyms
80 include("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx");
83 $this->match_synonym = array_keys($synonyms);
84 $this->replace_synonym = array_values($synonyms);
86 unset($synonyms);
90 /**
91 * Retrieves cached search results
93 * @param int &$result_count will contain the number of all results for the search (not only for the current page)
94 * @param array &$id_ary is filled with the ids belonging to the requested page that are stored in the cache
96 * @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE
98 function obtain_ids($search_key, &$result_count, &$id_ary, $start, $per_page, $sort_dir)
100 global $cache;
102 if (!($stored_ids = $cache->get('_search_results_' . $search_key)))
104 // no search results cached for this search_key
105 return SEARCH_RESULT_NOT_IN_CACHE;
107 else
109 $result_count = $stored_ids[-1];
110 $reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
111 $complete = true;
113 // change the start to the actual end of the current request if the sort direction differs
114 // from the dirction in the cache and reverse the ids later
115 if ($reverse_ids)
117 $start = $result_count - $start - $per_page;
119 // the user requested a page past the last index
120 if ($start < 0)
122 return SEARCH_RESULT_NOT_IN_CACHE;
126 for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
128 if (!isset($stored_ids[$i]))
130 $complete = false;
132 else
134 $id_ary[] = $stored_ids[$i];
137 unset($stored_ids);
139 if ($reverse_ids)
141 $id_ary = array_reverse($id_ary);
144 if (!$complete)
146 return SEARCH_RESULT_INCOMPLETE;
148 return SEARCH_RESULT_IN_CACHE;
153 * Caches post/topic ids
155 * @param array &$id_ary contains a list of post or topic ids that shall be cached, the first element
156 * must have the absolute index $start in the result set.
158 function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir)
160 global $cache, $config, $db, $user;
162 $length = min(sizeof($id_ary), $config['search_block_size']);
164 // nothing to cache so exit
165 if (!$length)
167 return;
170 $store_ids = array_slice($id_ary, 0, $length);
172 // create a new resultset if there is none for this search_key yet
173 // or add the ids to the existing resultset
174 if (!($store = $cache->get('_search_results_' . $search_key)))
176 // add the current keywords to the recent searches in the cache which are listed on the search page
177 if (!empty($keywords) || sizeof($author_ary))
179 $sql = 'SELECT search_time
180 FROM ' . SEARCH_RESULTS_TABLE . '
181 WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
182 $result = $db->sql_query($sql);
184 if (!$db->sql_fetchrow($result))
186 $sql_ary = array(
187 'search_key' => $search_key,
188 'search_time' => time(),
189 'search_keywords' => $keywords,
190 'search_authors' => ' ' . implode(' ', $author_ary) . ' '
193 $sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
194 $db->sql_query($sql);
196 $db->sql_freeresult($result);
199 $sql = 'UPDATE ' . USERS_TABLE . '
200 SET user_last_search = ' . time() . '
201 WHERE user_id = ' . $user->data['user_id'];
202 $db->sql_query($sql);
204 $store = array(-1 => $result_count, -2 => $sort_dir);
205 $id_range = range($start, $start + $length - 1);
207 else
209 // we use one set of results for both sort directions so we have to calculate the indizes
210 // for the reversed array and we also have to reverse the ids themselves
211 if ($store[-2] != $sort_dir)
213 $store_ids = array_reverse($store_ids);
214 $id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
216 else
218 $id_range = range($start, $start + $length - 1);
222 $store_ids = array_combine($id_range, $store_ids);
224 // append the ids
225 if (is_array($store_ids))
227 $store += $store_ids;
229 // if the cache is too big
230 if (sizeof($store) - 2 > 20 * $config['search_block_size'])
232 // remove everything in front of two blocks in front of the current start index
233 for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++)
235 if (isset($store[$i]))
237 unset($store[$i]);
241 // remove everything after two blocks after the current stop index
242 end($id_range);
243 for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--)
245 if (isset($store[$i]))
247 unset($store[$i]);
251 $cache->put('_search_results_' . $search_key, $store, $config['search_store_results']);
253 $sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
254 SET search_time = ' . time() . '
255 WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
256 $db->sql_query($sql);
259 unset($store);
260 unset($store_ids);
261 unset($id_range);
265 * Removes old entries from the search results table and removes searches with keywords that contain a word in $words.
267 function destroy_cache($words, $authors = false)
269 global $db, $cache, $config;
271 // clear all searches that searched for the specified words
272 if (sizeof($words))
274 $sql_where = '';
275 foreach ($words as $word)
277 $sql_where .= " OR search_keywords " . $db->sql_like_expression($db->any_char . $word . $db->any_char);
280 $sql = 'SELECT search_key
281 FROM ' . SEARCH_RESULTS_TABLE . "
282 WHERE search_keywords LIKE '%*%' $sql_where";
283 $result = $db->sql_query($sql);
285 while ($row = $db->sql_fetchrow($result))
287 $cache->destroy('_search_results_' . $row['search_key']);
289 $db->sql_freeresult($result);
292 // clear all searches that searched for the specified authors
293 if (is_array($authors) && sizeof($authors))
295 $sql_where = '';
296 foreach ($authors as $author)
298 $sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors LIKE \'% ' . (int) $author . ' %\'';
301 $sql = 'SELECT search_key
302 FROM ' . SEARCH_RESULTS_TABLE . "
303 WHERE $sql_where";
304 $result = $db->sql_query($sql);
306 while ($row = $db->sql_fetchrow($result))
308 $cache->destroy('_search_results_' . $row['search_key']);
310 $db->sql_freeresult($result);
313 $sql = 'DELETE
314 FROM ' . SEARCH_RESULTS_TABLE . '
315 WHERE search_time < ' . (time() - $config['search_store_results']);
316 $db->sql_query($sql);