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

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