Blame Identity/Models/Html/phpBB/3.0.4/includes/search/search.php

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