Register
phpBB.com Wiki · Home Projects Help

root / trunk / phpBB / includes / search / search.php

1
<?php
2
/**
3
*
4
* @package search
5
* @version $Id: search.php 8786 2008-08-24 10:04:15Z acydburn $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
*
9
*/
10
11
/**
12
* @ignore
13
*/
14
if (!defined('IN_PHPBB'))
15
{
16
	exit;
17
}
18
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
24
*/
25
class search_backend
26
{
27
	const SEARCH_RESULT_NOT_IN_CACHE = 0;
28
	const SEARCH_RESULT_IN_CACHE = 1;
29
	const SEARCH_RESULT_INCOMPLETE = 2;
30
31
	public $ignore_words = array();
32
	public $match_synonym = array();
33
	public $replace_synonym = array();
34
35
	function __construct(&$error)
36
	{
37
		// This class cannot be used as a search plugin
38
		$error = true;
39
	}
40
41
	/**
42
	* Retrieves a language dependend list of words that should be ignored by the search
43
	*/
44
	public function get_ignore_words()
45
	{
46
		if (!sizeof($this->ignore_words))
47
		{
48
			global $user;
49
50
			$words = array();
51
52
			if (file_exists("{$user->lang_path}{$user->lang_name}/search_ignore_words." . PHP_EXT))
53
			{
54
				// include the file containing ignore words
55
				include("{$user->lang_path}{$user->lang_name}/search_ignore_words." . PHP_EXT);
56
			}
57
58
			$this->ignore_words = $words;
59
			unset($words);
60
		}
61
	}
62
63
	/**
64
	* Stores a list of synonyms that should be replaced in $this->match_synonym and $this->replace_synonym and caches them
65
	*/
66
	public function get_synonyms()
67
	{
68
		if (!sizeof($this->match_synonym))
69
		{
70
			global $user;
71
72
			$synonyms = array();
73
74
			if (file_exists("{$user->lang_path}{$user->lang_name}/search_synonyms." . PHP_EXT))
75
			{
76
				// include the file containing synonyms
77
				include("{$user->lang_path}{$user->lang_name}/search_synonyms." . PHP_EXT);
78
			}
79
80
			$this->match_synonym = array_keys($synonyms);
81
			$this->replace_synonym = array_values($synonyms);
82
83
			unset($synonyms);
84
		}
85
	}
86
87
	/**
88
	* Retrieves cached search results
89
	*
90
	* @param int &$result_count will contain the number of all results for the search (not only for the current page)
91
	* @param array &$id_ary is filled with the ids belonging to the requested page that are stored in the cache
92
	*
93
	* @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE
94
	*/
95
	protected function obtain_ids($search_key, &$result_count, &$id_ary, $start, $per_page, $sort_dir)
96
	{
97
		global $cache;
98
99
		if (!($stored_ids = $cache->get('_search_results_' . $search_key)))
100
		{
101
			// no search results cached for this search_key
102
			return self::SEARCH_RESULT_NOT_IN_CACHE;
103
		}
104
		else
105
		{
106
			$result_count = $stored_ids[-1];
107
			$reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
108
			$complete = true;
109
110
			// change the start to the actual end of the current request if the sort direction differs
111
			// from the dirction in the cache and reverse the ids later
112
			if ($reverse_ids)
113
			{
114
				$start = $result_count - $start - $per_page;
115
116
				// the user requested a page past the last index
117
				if ($start < 0)
118
				{
119
					return self::SEARCH_RESULT_NOT_IN_CACHE;
120
				}
121
			}
122
123
			for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
124
			{
125
				if (!isset($stored_ids[$i]))
126
				{
127
					$complete = false;
128
				}
129
				else
130
				{
131
					$id_ary[] = $stored_ids[$i];
132
				}
133
			}
134
			unset($stored_ids);
135
136
			if ($reverse_ids)
137
			{
138
				$id_ary = array_reverse($id_ary);
139
			}
140
141
			if (!$complete)
142
			{
143
				return self::SEARCH_RESULT_INCOMPLETE;
144
			}
145
			return self::SEARCH_RESULT_IN_CACHE;
146
		}
147
	}
148
149
	/**
150
	* Caches post/topic ids
151
	*
152
	* @param array &$id_ary contains a list of post or topic ids that shall be cached, the first element
153
	* 	must have the absolute index $start in the result set.
154
	*/
155
	protected function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir)
156
	{
157
		global $cache, $config, $db, $user;
158
159
		$length = min(sizeof($id_ary), $config['search_block_size']);
160
161
		// nothing to cache so exit
162
		if (!$length)
163
		{
164
			return;
165
		}
166
167
		$store_ids = array_slice($id_ary, 0, $length);
168
169
		// create a new resultset if there is none for this search_key yet
170
		// or add the ids to the existing resultset
171
		if (!($store = $cache->get('_search_results_' . $search_key)))
172
		{
173
			// add the current keywords to the recent searches in the cache which are listed on the search page
174
			if (!empty($keywords) || sizeof($author_ary))
175
			{
176
				$sql = 'SELECT search_time
177
					FROM ' . SEARCH_RESULTS_TABLE . '
178
					WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
179
				$result = $db->sql_query($sql);
180
181
				if (!$db->sql_fetchrow($result))
182
				{
183
					$sql_ary = array(
184
						'search_key'		=> $search_key,
185
						'search_time'		=> time(),
186
						'search_keywords'	=> $keywords,
187
						'search_authors'	=> ' ' . implode(' ', $author_ary) . ' '
188
					);
189
190
					$sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
191
					$db->sql_query($sql);
192
				}
193
				$db->sql_freeresult($result);
194
			}
195
196
			$sql = 'UPDATE ' . USERS_TABLE . '
197
				SET user_last_search = ' . time() . '
198
				WHERE user_id = ' . $user->data['user_id'];
199
			$db->sql_query($sql);
200
201
			$store = array(-1 => $result_count, -2 => $sort_dir);
202
			$id_range = range($start, $start + $length - 1);
203
		}
204
		else
205
		{
206
			// we use one set of results for both sort directions so we have to calculate the indizes
207
			// for the reversed array and we also have to reverse the ids themselves
208
			if ($store[-2] != $sort_dir)
209
			{
210
				$store_ids = array_reverse($store_ids);
211
				$id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
212
			}
213
			else
214
			{
215
				$id_range = range($start, $start + $length - 1);
216
			}
217
		}
218
219
		$store_ids = array_combine($id_range, $store_ids);
220
221
		// append the ids
222
		if (is_array($store_ids))
223
		{
224
			$store += $store_ids;
225
226
			// if the cache is too big
227
			if (sizeof($store) - 2 > 20 * $config['search_block_size'])
228
			{
229
				// remove everything in front of two blocks in front of the current start index
230
				for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++)
231
				{
232
					if (isset($store[$i]))
233
					{
234
						unset($store[$i]);
235
					}
236
				}
237
238
				// remove everything after two blocks after the current stop index
239
				end($id_range);
240
				for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--)
241
				{
242
					if (isset($store[$i]))
243
					{
244
						unset($store[$i]);
245
					}
246
				}
247
			}
248
			$cache->put('_search_results_' . $search_key, $store, $config['search_store_results']);
249
250
			$sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
251
				SET search_time = ' . time() . '
252
				WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
253
			$db->sql_query($sql);
254
		}
255
256
		unset($store);
257
		unset($store_ids);
258
		unset($id_range);
259
	}
260
261
	/**
262
	* Removes old entries from the search results table and removes searches with keywords that contain a word in $words.
263
	*/
264
	public function destroy_cache($words, $authors = false)
265
	{
266
		global $db, $cache, $config;
267
268
		// clear all searches that searched for the specified words
269
		if (sizeof($words))
270
		{
271
			$sql_where = '';
272
			foreach ($words as $word)
273
			{
274
				$sql_where .= " OR search_keywords " . $db->sql_like_expression($db->any_char . $word . $db->any_char);
275
			}
276
277
			$sql = 'SELECT search_key
278
				FROM ' . SEARCH_RESULTS_TABLE . "
279
				WHERE search_keywords LIKE '%*%' $sql_where";
280
			$result = $db->sql_query($sql);
281
282
			while ($row = $db->sql_fetchrow($result))
283
			{
284
				$cache->destroy('_search_results_' . $row['search_key']);
285
			}
286
			$db->sql_freeresult($result);
287
		}
288
289
		// clear all searches that searched for the specified authors
290
		if (is_array($authors) && sizeof($authors))
291
		{
292
			$sql_where = '';
293
			foreach ($authors as $author)
294
			{
295
				$sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors LIKE \'% ' . (int) $author . ' %\'';
296
			}
297
298
			$sql = 'SELECT search_key
299
				FROM ' . SEARCH_RESULTS_TABLE . "
300
				WHERE $sql_where";
301
			$result = $db->sql_query($sql);
302
303
			while ($row = $db->sql_fetchrow($result))
304
			{
305
				$cache->destroy('_search_results_' . $row['search_key']);
306
			}
307
			$db->sql_freeresult($result);
308
		}
309
310
		$sql = 'DELETE
311
			FROM ' . SEARCH_RESULTS_TABLE . '
312
			WHERE search_time < ' . (time() - $config['search_store_results']);
313
		$db->sql_query($sql);
314
	}
315
}
316
317
?>