Blame Extras/phpBB/3.0.4/includes/search/search.php

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