Blame Identity/Webenv/App/phpBB/3.0.4/includes/search/search.php

f2e824
f2e824
/**
f2e824
*
f2e824
* @package search
f2e824
* @version $Id: search.php 8782 2008-08-23 17:20:55Z acydburn $
f2e824
* @copyright (c) 2005 phpBB Group
f2e824
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
f2e824
*
f2e824
*/
f2e824
f2e824
/**
f2e824
* @ignore
f2e824
*/
f2e824
if (!defined('IN_PHPBB'))
f2e824
{
f2e824
	exit;
f2e824
}
f2e824
f2e824
/**
f2e824
* @ignore
f2e824
*/
f2e824
define('SEARCH_RESULT_NOT_IN_CACHE', 0);
f2e824
define('SEARCH_RESULT_IN_CACHE', 1);
f2e824
define('SEARCH_RESULT_INCOMPLETE', 2);
f2e824
f2e824
/**
f2e824
* search_backend
f2e824
* optional base class for search plugins providing simple caching based on ACM
f2e824
* and functions to retrieve ignore_words and synonyms
f2e824
* @package search
f2e824
*/
f2e824
class search_backend
f2e824
{
f2e824
	var $ignore_words = array();
f2e824
	var $match_synonym = array();
f2e824
	var $replace_synonym = array();
f2e824
f2e824
	function search_backend(&$error)
f2e824
	{
f2e824
		// This class cannot be used as a search plugin
f2e824
		$error = true;
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Retrieves a language dependend list of words that should be ignored by the search
f2e824
	*/
f2e824
	function get_ignore_words()
f2e824
	{
f2e824
		if (!sizeof($this->ignore_words))
f2e824
		{
f2e824
			global $user, $phpEx;
f2e824
f2e824
			$words = array();
f2e824
f2e824
			if (file_exists("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx"))
f2e824
			{
f2e824
				// include the file containing ignore words
f2e824
				include("{$user->lang_path}{$user->lang_name}/search_ignore_words.$phpEx");
f2e824
			}
f2e824
f2e824
			$this->ignore_words = $words;
f2e824
			unset($words);
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Stores a list of synonyms that should be replaced in $this->match_synonym and $this->replace_synonym and caches them
f2e824
	*/
f2e824
	function get_synonyms()
f2e824
	{
f2e824
		if (!sizeof($this->match_synonym))
f2e824
		{
f2e824
			global $user, $phpEx;
f2e824
f2e824
			$synonyms = array();
f2e824
f2e824
			if (file_exists("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx"))
f2e824
			{
f2e824
				// include the file containing synonyms
f2e824
				include("{$user->lang_path}{$user->lang_name}/search_synonyms.$phpEx");
f2e824
			}
f2e824
f2e824
			$this->match_synonym = array_keys($synonyms);
f2e824
			$this->replace_synonym = array_values($synonyms);
f2e824
f2e824
			unset($synonyms);
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Retrieves cached search results
f2e824
	*
f2e824
	* @param int &$result_count will contain the number of all results for the search (not only for the current page)
f2e824
	* @param array &$id_ary is filled with the ids belonging to the requested page that are stored in the cache
f2e824
	*
f2e824
	* @return int SEARCH_RESULT_NOT_IN_CACHE or SEARCH_RESULT_IN_CACHE or SEARCH_RESULT_INCOMPLETE
f2e824
	*/
f2e824
	function obtain_ids($search_key, &$result_count, &$id_ary, $start, $per_page, $sort_dir)
f2e824
	{
f2e824
		global $cache;
f2e824
f2e824
		if (!($stored_ids = $cache->get('_search_results_' . $search_key)))
f2e824
		{
f2e824
			// no search results cached for this search_key
f2e824
			return SEARCH_RESULT_NOT_IN_CACHE;
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			$result_count = $stored_ids[-1];
f2e824
			$reverse_ids = ($stored_ids[-2] != $sort_dir) ? true : false;
f2e824
			$complete = true;
f2e824
f2e824
			// change the start to the actual end of the current request if the sort direction differs
f2e824
			// from the dirction in the cache and reverse the ids later
f2e824
			if ($reverse_ids)
f2e824
			{
f2e824
				$start = $result_count - $start - $per_page;
f2e824
f2e824
				// the user requested a page past the last index
f2e824
				if ($start < 0)
f2e824
				{
f2e824
					return SEARCH_RESULT_NOT_IN_CACHE;
f2e824
				}
f2e824
			}
f2e824
f2e824
			for ($i = $start, $n = $start + $per_page; ($i < $n) && ($i < $result_count); $i++)
f2e824
			{
f2e824
				if (!isset($stored_ids[$i]))
f2e824
				{
f2e824
					$complete = false;
f2e824
				}
f2e824
				else
f2e824
				{
f2e824
					$id_ary[] = $stored_ids[$i];
f2e824
				}
f2e824
			}
f2e824
			unset($stored_ids);
f2e824
f2e824
			if ($reverse_ids)
f2e824
			{
f2e824
				$id_ary = array_reverse($id_ary);
f2e824
			}
f2e824
f2e824
			if (!$complete)
f2e824
			{
f2e824
				return SEARCH_RESULT_INCOMPLETE;
f2e824
			}
f2e824
			return SEARCH_RESULT_IN_CACHE;
f2e824
		}
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Caches post/topic ids
f2e824
	*
f2e824
	* @param array &$id_ary contains a list of post or topic ids that shall be cached, the first element
f2e824
	* 	must have the absolute index $start in the result set.
f2e824
	*/
f2e824
	function save_ids($search_key, $keywords, $author_ary, $result_count, &$id_ary, $start, $sort_dir)
f2e824
	{
f2e824
		global $cache, $config, $db, $user;
f2e824
f2e824
		$length = min(sizeof($id_ary), $config['search_block_size']);
f2e824
f2e824
		// nothing to cache so exit
f2e824
		if (!$length)
f2e824
		{
f2e824
			return;
f2e824
		}
f2e824
f2e824
		$store_ids = array_slice($id_ary, 0, $length);
f2e824
f2e824
		// create a new resultset if there is none for this search_key yet
f2e824
		// or add the ids to the existing resultset
f2e824
		if (!($store = $cache->get('_search_results_' . $search_key)))
f2e824
		{
f2e824
			// add the current keywords to the recent searches in the cache which are listed on the search page
f2e824
			if (!empty($keywords) || sizeof($author_ary))
f2e824
			{
f2e824
				$sql = 'SELECT search_time
f2e824
					FROM ' . SEARCH_RESULTS_TABLE . '
f2e824
					WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
f2e824
				$result = $db->sql_query($sql);
f2e824
f2e824
				if (!$db->sql_fetchrow($result))
f2e824
				{
f2e824
					$sql_ary = array(
f2e824
						'search_key'		=> $search_key,
f2e824
						'search_time'		=> time(),
f2e824
						'search_keywords'	=> $keywords,
f2e824
						'search_authors'	=> ' ' . implode(' ', $author_ary) . ' '
f2e824
					);
f2e824
f2e824
					$sql = 'INSERT INTO ' . SEARCH_RESULTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
f2e824
					$db->sql_query($sql);
f2e824
				}
f2e824
				$db->sql_freeresult($result);
f2e824
			}
f2e824
f2e824
			$sql = 'UPDATE ' . USERS_TABLE . '
f2e824
				SET user_last_search = ' . time() . '
f2e824
				WHERE user_id = ' . $user->data['user_id'];
f2e824
			$db->sql_query($sql);
f2e824
f2e824
			$store = array(-1 => $result_count, -2 => $sort_dir);
f2e824
			$id_range = range($start, $start + $length - 1);
f2e824
		}
f2e824
		else
f2e824
		{
f2e824
			// we use one set of results for both sort directions so we have to calculate the indizes
f2e824
			// for the reversed array and we also have to reverse the ids themselves
f2e824
			if ($store[-2] != $sort_dir)
f2e824
			{
f2e824
				$store_ids = array_reverse($store_ids);
f2e824
				$id_range = range($store[-1] - $start - $length, $store[-1] - $start - 1);
f2e824
			}
f2e824
			else
f2e824
			{
f2e824
				$id_range = range($start, $start + $length - 1);
f2e824
			}
f2e824
		}
f2e824
f2e824
		$store_ids = array_combine($id_range, $store_ids);
f2e824
f2e824
		// append the ids
f2e824
		if (is_array($store_ids))
f2e824
		{
f2e824
			$store += $store_ids;
f2e824
f2e824
			// if the cache is too big
f2e824
			if (sizeof($store) - 2 > 20 * $config['search_block_size'])
f2e824
			{
f2e824
				// remove everything in front of two blocks in front of the current start index
f2e824
				for ($i = 0, $n = $id_range[0] - 2 * $config['search_block_size']; $i < $n; $i++)
f2e824
				{
f2e824
					if (isset($store[$i]))
f2e824
					{
f2e824
						unset($store[$i]);
f2e824
					}
f2e824
				}
f2e824
f2e824
				// remove everything after two blocks after the current stop index
f2e824
				end($id_range);
f2e824
				for ($i = $store[-1] - 1, $n = current($id_range) + 2 * $config['search_block_size']; $i > $n; $i--)
f2e824
				{
f2e824
					if (isset($store[$i]))
f2e824
					{
f2e824
						unset($store[$i]);
f2e824
					}
f2e824
				}
f2e824
			}
f2e824
			$cache->put('_search_results_' . $search_key, $store, $config['search_store_results']);
f2e824
f2e824
			$sql = 'UPDATE ' . SEARCH_RESULTS_TABLE . '
f2e824
				SET search_time = ' . time() . '
f2e824
				WHERE search_key = \'' . $db->sql_escape($search_key) . '\'';
f2e824
			$db->sql_query($sql);
f2e824
		}
f2e824
f2e824
		unset($store);
f2e824
		unset($store_ids);
f2e824
		unset($id_range);
f2e824
	}
f2e824
f2e824
	/**
f2e824
	* Removes old entries from the search results table and removes searches with keywords that contain a word in $words.
f2e824
	*/
f2e824
	function destroy_cache($words, $authors = false)
f2e824
	{
f2e824
		global $db, $cache, $config;
f2e824
f2e824
		// clear all searches that searched for the specified words
f2e824
		if (sizeof($words))
f2e824
		{
f2e824
			$sql_where = '';
f2e824
			foreach ($words as $word)
f2e824
			{
f2e824
				$sql_where .= " OR search_keywords " . $db->sql_like_expression($db->any_char . $word . $db->any_char);
f2e824
			}
f2e824
f2e824
			$sql = 'SELECT search_key
f2e824
				FROM ' . SEARCH_RESULTS_TABLE . "
f2e824
				WHERE search_keywords LIKE '%*%' $sql_where";
f2e824
			$result = $db->sql_query($sql);
f2e824
f2e824
			while ($row = $db->sql_fetchrow($result))
f2e824
			{
f2e824
				$cache->destroy('_search_results_' . $row['search_key']);
f2e824
			}
f2e824
			$db->sql_freeresult($result);
f2e824
		}
f2e824
f2e824
		// clear all searches that searched for the specified authors
f2e824
		if (is_array($authors) && sizeof($authors))
f2e824
		{
f2e824
			$sql_where = '';
f2e824
			foreach ($authors as $author)
f2e824
			{
f2e824
				$sql_where .= (($sql_where) ? ' OR ' : '') . 'search_authors LIKE \'% ' . (int) $author . ' %\'';
f2e824
			}
f2e824
f2e824
			$sql = 'SELECT search_key
f2e824
				FROM ' . SEARCH_RESULTS_TABLE . "
f2e824
				WHERE $sql_where";
f2e824
			$result = $db->sql_query($sql);
f2e824
f2e824
			while ($row = $db->sql_fetchrow($result))
f2e824
			{
f2e824
				$cache->destroy('_search_results_' . $row['search_key']);
f2e824
			}
f2e824
			$db->sql_freeresult($result);
f2e824
		}
f2e824
f2e824
		$sql = 'DELETE
f2e824
			FROM ' . SEARCH_RESULTS_TABLE . '
f2e824
			WHERE search_time < ' . (time() - $config['search_store_results']);
f2e824
		$db->sql_query($sql);
f2e824
	}
f2e824
}
f2e824
f2e824
?>