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