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

4c79b5
4c79b5
/**
4c79b5
*
4c79b5
* @package search
4c79b5
* @version $Id: fulltext_mysql.php 8814 2008-09-04 12:01:47Z 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
include_once($phpbb_root_path . 'includes/search/search.' . $phpEx);
4c79b5
4c79b5
/**
4c79b5
* fulltext_mysql
4c79b5
* Fulltext search for MySQL
4c79b5
* @package search
4c79b5
*/
4c79b5
class fulltext_mysql extends search_backend
4c79b5
{
4c79b5
	var $stats = array();
4c79b5
	var $word_length = array();
4c79b5
	var $split_words = array();
4c79b5
	var $search_query;
4c79b5
	var $common_words = array();
4c79b5
	var $pcre_properties = false;
4c79b5
	var $mbstring_regex = false;
4c79b5
4c79b5
	function fulltext_mysql(&$error)
4c79b5
	{
4c79b5
		global $config;
4c79b5
4c79b5
		$this->word_length = array('min' => $config['fulltext_mysql_min_word_len'], 'max' => $config['fulltext_mysql_max_word_len']);
4c79b5
4c79b5
		if (version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>=')))
4c79b5
		{
4c79b5
			// While this is the proper range of PHP versions, PHP may not be linked with the bundled PCRE lib and instead with an older version
4c79b5
			if (@preg_match('/\p{L}/u', 'a') !== false)
4c79b5
			{
4c79b5
				$this->pcre_properties = true;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if (function_exists('mb_ereg'))
4c79b5
		{
4c79b5
			$this->mbstring_regex = true;
4c79b5
			mb_regex_encoding('UTF-8');
4c79b5
		}
4c79b5
4c79b5
		$error = false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Checks for correct MySQL version and stores min/max word length in the config
4c79b5
	*/
4c79b5
	function init()
4c79b5
	{
4c79b5
		global $db, $user;
4c79b5
4c79b5
		if ($db->sql_layer != 'mysql4' && $db->sql_layer != 'mysqli')
4c79b5
		{
4c79b5
			return $user->lang['FULLTEXT_MYSQL_INCOMPATIBLE_VERSION'];
4c79b5
		}
4c79b5
4c79b5
		$result = $db->sql_query('SHOW TABLE STATUS LIKE \'' . POSTS_TABLE . '\'');
4c79b5
		$info = $db->sql_fetchrow($result);
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		$engine = '';
4c79b5
		if (isset($info['Engine']))
4c79b5
		{
4c79b5
			$engine = $info['Engine'];
4c79b5
		}
4c79b5
		else if (isset($info['Type']))
4c79b5
		{
4c79b5
			$engine = $info['Type'];
4c79b5
		}
4c79b5
4c79b5
		if ($engine != 'MyISAM')
4c79b5
		{
4c79b5
			return $user->lang['FULLTEXT_MYSQL_NOT_MYISAM'];
4c79b5
		}
4c79b5
4c79b5
		$sql = 'SHOW VARIABLES
4c79b5
			LIKE \'ft\_%\'';
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		$mysql_info = array();
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$mysql_info[$row['Variable_name']] = $row['Value'];
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		set_config('fulltext_mysql_max_word_len', $mysql_info['ft_max_word_len']);
4c79b5
		set_config('fulltext_mysql_min_word_len', $mysql_info['ft_min_word_len']);
4c79b5
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Splits keywords entered by a user into an array of words stored in $this->split_words
4c79b5
	* Stores the tidied search query in $this->search_query
4c79b5
	*
4c79b5
	* @param string &$keywords Contains the keyword as entered by the user
4c79b5
	* @param string $terms is either 'all' or 'any'
4c79b5
	* @return bool false if no valid keywords were found and otherwise true
4c79b5
	*/
4c79b5
	function split_keywords(&$keywords, $terms)
4c79b5
	{
4c79b5
		global $config;
4c79b5
4c79b5
		if ($terms == 'all')
4c79b5
		{
4c79b5
			$match		= array('#\sand\s#iu', '#\sor\s#iu', '#\snot\s#iu', '#\+#', '#-#', '#\|#');
4c79b5
			$replace	= array(' +', ' |', ' -', ' +', ' -', ' |');
4c79b5
4c79b5
			$keywords = preg_replace($match, $replace, $keywords);
4c79b5
		}
4c79b5
4c79b5
		// Filter out as above
4c79b5
		$split_keywords = preg_replace("#[\n\r\t]+#", ' ', trim(htmlspecialchars_decode($keywords)));
4c79b5
4c79b5
		// Split words
4c79b5
		if ($this->pcre_properties)
4c79b5
		{
4c79b5
			$split_keywords = preg_replace('#([^\p{L}\p{N}\'*"()])#u', '$1$1', str_replace('\'\'', '\' \'', trim($split_keywords)));
4c79b5
		}
4c79b5
		else if ($this->mbstring_regex)
4c79b5
		{
4c79b5
			$split_keywords = mb_ereg_replace('([^\w\'*"()])', '\\1\\1', str_replace('\'\'', '\' \'', trim($split_keywords)));
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$split_keywords = preg_replace('#([^\w\'*"()])#u', '$1$1', str_replace('\'\'', '\' \'', trim($split_keywords)));
4c79b5
		}
4c79b5
4c79b5
		if ($this->pcre_properties)
4c79b5
		{
4c79b5
			$matches = array();
4c79b5
			preg_match_all('#(?:[^\p{L}\p{N}*"()]|^)([+\-|]?(?:[\p{L}\p{N}*"()]+\'?)*[\p{L}\p{N}*"()])(?:[^\p{L}\p{N}*"()]|$)#u', $split_keywords, $matches);
4c79b5
			$this->split_words = $matches[1];
4c79b5
		}
4c79b5
		else if ($this->mbstring_regex)
4c79b5
		{
4c79b5
			mb_ereg_search_init($split_keywords, '(?:[^\w*"()]|^)([+\-|]?(?:[\w*"()]+\'?)*[\w*"()])(?:[^\w*"()]|$)');
4c79b5
4c79b5
			while (($word = mb_ereg_search_regs()))
4c79b5
			{
4c79b5
				$this->split_words[] = $word[1];
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$matches = array();
4c79b5
			preg_match_all('#(?:[^\w*"()]|^)([+\-|]?(?:[\w*"()]+\'?)*[\w*"()])(?:[^\w*"()]|$)#u', $split_keywords, $matches);
4c79b5
			$this->split_words = $matches[1];
4c79b5
		}
4c79b5
4c79b5
		// to allow phrase search, we need to concatenate quoted words
4c79b5
		$tmp_split_words = array();
4c79b5
		$phrase = '';
4c79b5
		foreach ($this->split_words as $word)
4c79b5
		{
4c79b5
			if ($phrase)
4c79b5
			{
4c79b5
				$phrase .= ' ' . $word;
4c79b5
				if (strpos($word, '"') !== false && substr_count($word, '"') % 2 == 1)
4c79b5
				{
4c79b5
					$tmp_split_words[] = $phrase;
4c79b5
					$phrase = '';
4c79b5
				}
4c79b5
			}
4c79b5
			else if (strpos($word, '"') !== false && substr_count($word, '"') % 2 == 1)
4c79b5
			{
4c79b5
				$phrase = $word;
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$tmp_split_words[] = $word . ' ';
4c79b5
			}
4c79b5
		}
4c79b5
		if ($phrase)
4c79b5
		{
4c79b5
			$tmp_split_words[] = $phrase;
4c79b5
		}
4c79b5
4c79b5
		$this->split_words = $tmp_split_words;
4c79b5
4c79b5
		unset($tmp_split_words);
4c79b5
		unset($phrase);
4c79b5
4c79b5
		foreach ($this->split_words as $i => $word)
4c79b5
		{
4c79b5
			$clean_word = preg_replace('#^[+\-|"]#', '', $word);
4c79b5
4c79b5
			// check word length
4c79b5
			$clean_len = utf8_strlen(str_replace('*', '', $clean_word));
4c79b5
			if (($clean_len < $config['fulltext_mysql_min_word_len']) || ($clean_len > $config['fulltext_mysql_max_word_len']))
4c79b5
			{
4c79b5
				$this->common_words[] = $word;
4c79b5
				unset($this->split_words[$i]);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if ($terms == 'any')
4c79b5
		{
4c79b5
			$this->search_query = '';
4c79b5
			foreach ($this->split_words as $word)
4c79b5
			{
4c79b5
				if ((strpos($word, '+') === 0) || (strpos($word, '-') === 0) || (strpos($word, '|') === 0))
4c79b5
				{
4c79b5
					$word = substr($word, 1);
4c79b5
				}
4c79b5
				$this->search_query .= $word . ' ';
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$this->search_query = '';
4c79b5
			foreach ($this->split_words as $word)
4c79b5
			{
4c79b5
				if ((strpos($word, '+') === 0) || (strpos($word, '-') === 0))
4c79b5
				{
4c79b5
					$this->search_query .= $word . ' ';
4c79b5
				}
4c79b5
				else if (strpos($word, '|') === 0)
4c79b5
				{
4c79b5
					$this->search_query .= substr($word, 1) . ' ';
4c79b5
				}
4c79b5
				else
4c79b5
				{
4c79b5
					$this->search_query .= '+' . $word . ' ';
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		$this->search_query = utf8_htmlspecialchars($this->search_query);
4c79b5
4c79b5
		if ($this->search_query)
4c79b5
		{
4c79b5
			$this->split_words = array_values($this->split_words);
4c79b5
			sort($this->split_words);
4c79b5
			return true;
4c79b5
		}
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Turns text into an array of words
4c79b5
	*/
4c79b5
	function split_message($text)
4c79b5
	{
4c79b5
		global $config;
4c79b5
4c79b5
		// Split words
4c79b5
		if ($this->pcre_properties)
4c79b5
		{
4c79b5
			$text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text)));
4c79b5
		}
4c79b5
		else if ($this->mbstring_regex)
4c79b5
		{
4c79b5
			$text = mb_ereg_replace('([^\w\'*])', '\\1\\1', str_replace('\'\'', '\' \'', trim($text)));
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$text = preg_replace('#([^\w\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text)));
4c79b5
		}
4c79b5
4c79b5
		if ($this->pcre_properties)
4c79b5
		{
4c79b5
			$matches = array();
4c79b5
			preg_match_all('#(?:[^\p{L}\p{N}*]|^)([+\-|]?(?:[\p{L}\p{N}*]+\'?)*[\p{L}\p{N}*])(?:[^\p{L}\p{N}*]|$)#u', $text, $matches);
4c79b5
			$text = $matches[1];
4c79b5
		}
4c79b5
		else if ($this->mbstring_regex)
4c79b5
		{
4c79b5
			mb_ereg_search_init($text, '(?:[^\w*]|^)([+\-|]?(?:[\w*]+\'?)*[\w*])(?:[^\w*]|$)');
4c79b5
4c79b5
			$text = array();
4c79b5
			while (($word = mb_ereg_search_regs()))
4c79b5
			{
4c79b5
				$text[] = $word[1];
4c79b5
			}
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$matches = array();
4c79b5
			preg_match_all('#(?:[^\w*]|^)([+\-|]?(?:[\w*]+\'?)*[\w*])(?:[^\w*]|$)#u', $text, $matches);
4c79b5
			$text = $matches[1];
4c79b5
		}
4c79b5
4c79b5
		// remove too short or too long words
4c79b5
		$text = array_values($text);
4c79b5
		for ($i = 0, $n = sizeof($text); $i < $n; $i++)
4c79b5
		{
4c79b5
			$text[$i] = trim($text[$i]);
4c79b5
			if (utf8_strlen($text[$i]) < $config['fulltext_mysql_min_word_len'] || utf8_strlen($text[$i]) > $config['fulltext_mysql_max_word_len'])
4c79b5
			{
4c79b5
				unset($text[$i]);
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		return array_values($text);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Performs a search on keywords depending on display specific params. You have to run split_keywords() first.
4c79b5
	*
4c79b5
	* @param	string		$type				contains either posts or topics depending on what should be searched for
4c79b5
	* @param	string		&$fields			contains either titleonly (topic titles should be searched), msgonly (only message bodies should be searched), firstpost (only subject and body of the first post should be searched) or all (all post bodies and subjects should be searched)
4c79b5
	* @param	string		&$terms				is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words)
4c79b5
	* @param	array		&$sort_by_sql		contains SQL code for the ORDER BY part of a query
4c79b5
	* @param	string		&$sort_key			is the key of $sort_by_sql for the selected sorting
4c79b5
	* @param	string		&$sort_dir			is either a or d representing ASC and DESC
4c79b5
	* @param	string		&$sort_days			specifies the maximum amount of days a post may be old
4c79b5
	* @param	array		&$ex_fid_ary		specifies an array of forum ids which should not be searched
4c79b5
	* @param	array		&$m_approve_fid_ary	specifies an array of forum ids in which the searcher is allowed to view unapproved posts
4c79b5
	* @param	int			&$topic_id			is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
4c79b5
	* @param	array		&$author_ary		an array of author ids if the author should be ignored during the search the array is empty
4c79b5
	* @param	array		&$id_ary			passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
4c79b5
	* @param	int			$start				indicates the first index of the page
4c79b5
	* @param	int			$per_page			number of ids each page is supposed to contain
4c79b5
	* @return	boolean|int						total number of results
4c79b5
	*
4c79b5
	* @access	public
4c79b5
	*/
4c79b5
	function keyword_search($type, &$fields, &$terms, &$sort_by_sql, &$sort_key, &$sort_dir, &$sort_days, &$ex_fid_ary, &$m_approve_fid_ary, &$topic_id, &$author_ary, &$id_ary, $start, $per_page)
4c79b5
	{
4c79b5
		global $config, $db;
4c79b5
4c79b5
		// No keywords? No posts.
4c79b5
		if (!$this->search_query)
4c79b5
		{
4c79b5
			return false;
4c79b5
		}
4c79b5
4c79b5
		// generate a search_key from all the options to identify the results
4c79b5
		$search_key = md5(implode('#', array(
4c79b5
			implode(', ', $this->split_words),
4c79b5
			$type,
4c79b5
			$fields,
4c79b5
			$terms,
4c79b5
			$sort_days,
4c79b5
			$sort_key,
4c79b5
			$topic_id,
4c79b5
			implode(',', $ex_fid_ary),
4c79b5
			implode(',', $m_approve_fid_ary),
4c79b5
			implode(',', $author_ary)
4c79b5
		)));
4c79b5
4c79b5
		// try reading the results from cache
4c79b5
		$result_count = 0;
4c79b5
		if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE)
4c79b5
		{
4c79b5
			return $result_count;
4c79b5
		}
4c79b5
4c79b5
		$id_ary = array();
4c79b5
4c79b5
		$join_topic = ($type == 'posts') ? false : true;
4c79b5
4c79b5
		// Build sql strings for sorting
4c79b5
		$sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
4c79b5
		$sql_sort_table = $sql_sort_join = '';
4c79b5
4c79b5
		switch ($sql_sort[0])
4c79b5
		{
4c79b5
			case 'u':
4c79b5
				$sql_sort_table	= USERS_TABLE . ' u, ';
4c79b5
				$sql_sort_join	= ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster ';
4c79b5
			break;
4c79b5
4c79b5
			case 't':
4c79b5
				$join_topic = true;
4c79b5
			break;
4c79b5
4c79b5
			case 'f':
4c79b5
				$sql_sort_table	= FORUMS_TABLE . ' f, ';
4c79b5
				$sql_sort_join	= ' AND f.forum_id = p.forum_id ';
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		// Build some display specific sql strings
4c79b5
		switch ($fields)
4c79b5
		{
4c79b5
			case 'titleonly':
4c79b5
				$sql_match = 'p.post_subject';
4c79b5
				$sql_match_where = ' AND p.post_id = t.topic_first_post_id';
4c79b5
				$join_topic = true;
4c79b5
			break;
4c79b5
4c79b5
			case 'msgonly':
4c79b5
				$sql_match = 'p.post_text';
4c79b5
				$sql_match_where = '';
4c79b5
			break;
4c79b5
4c79b5
			case 'firstpost':
4c79b5
				$sql_match = 'p.post_subject, p.post_text';
4c79b5
				$sql_match_where = ' AND p.post_id = t.topic_first_post_id';
4c79b5
				$join_topic = true;
4c79b5
			break;
4c79b5
4c79b5
			default:
4c79b5
				$sql_match = 'p.post_subject, p.post_text';
4c79b5
				$sql_match_where = '';
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		if (!sizeof($m_approve_fid_ary))
4c79b5
		{
4c79b5
			$m_approve_fid_sql = ' AND p.post_approved = 1';
4c79b5
		}
4c79b5
		else if ($m_approve_fid_ary === array(-1))
4c79b5
		{
4c79b5
			$m_approve_fid_sql = '';
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$m_approve_fid_sql = ' AND (p.post_approved = 1 OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) . ')';
4c79b5
		}
4c79b5
4c79b5
		$sql_select			= (!$result_count) ? 'SQL_CALC_FOUND_ROWS ' : '';
4c79b5
		$sql_select			= ($type == 'posts') ? $sql_select . 'p.post_id' : 'DISTINCT ' . $sql_select . 't.topic_id';
4c79b5
		$sql_from			= ($join_topic) ? TOPICS_TABLE . ' t, ' : '';
4c79b5
		$field				= ($type == 'posts') ? 'post_id' : 'topic_id';
4c79b5
		$sql_author			= (sizeof($author_ary) == 1) ? ' = ' . $author_ary[0] : 'IN (' . implode(', ', $author_ary) . ')';
4c79b5
4c79b5
		$sql_where_options = $sql_sort_join;
4c79b5
		$sql_where_options .= ($topic_id) ? ' AND p.topic_id = ' . $topic_id : '';
4c79b5
		$sql_where_options .= ($join_topic) ? ' AND t.topic_id = p.topic_id' : '';
4c79b5
		$sql_where_options .= (sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '';
4c79b5
		$sql_where_options .= $m_approve_fid_sql;
4c79b5
		$sql_where_options .= (sizeof($author_ary)) ? ' AND p.poster_id ' . $sql_author : '';
4c79b5
		$sql_where_options .= ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : '';
4c79b5
		$sql_where_options .= $sql_match_where;
4c79b5
4c79b5
		$sql = "SELECT $sql_select
4c79b5
			FROM $sql_from$sql_sort_table" . POSTS_TABLE . " p
4c79b5
			WHERE MATCH ($sql_match) AGAINST ('" . $db->sql_escape(htmlspecialchars_decode($this->search_query)) . "' IN BOOLEAN MODE)
4c79b5
				$sql_where_options
4c79b5
			ORDER BY $sql_sort";
4c79b5
		$result = $db->sql_query_limit($sql, $config['search_block_size'], $start);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$id_ary[] = $row[$field];
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		$id_ary = array_unique($id_ary);
4c79b5
4c79b5
		if (!sizeof($id_ary))
4c79b5
		{
4c79b5
			return false;
4c79b5
		}
4c79b5
4c79b5
		// if the total result count is not cached yet, retrieve it from the db
4c79b5
		if (!$result_count)
4c79b5
		{
4c79b5
			$sql = 'SELECT FOUND_ROWS() as result_count';
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$result_count = (int) $db->sql_fetchfield('result_count');
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			if (!$result_count)
4c79b5
			{
4c79b5
				return false;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		// store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page
4c79b5
		$this->save_ids($search_key, implode(' ', $this->split_words), $author_ary, $result_count, $id_ary, $start, $sort_dir);
4c79b5
		$id_ary = array_slice($id_ary, 0, (int) $per_page);
4c79b5
4c79b5
		return $result_count;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Performs a search on an author's posts without caring about message contents. Depends on display specific params
4c79b5
	*
4c79b5
	* @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
4c79b5
	* @param int $start indicates the first index of the page
4c79b5
	* @param int $per_page number of ids each page is supposed to contain
4c79b5
	* @return total number of results
4c79b5
	*/
4c79b5
	function author_search($type, $firstpost_only, &$sort_by_sql, &$sort_key, &$sort_dir, &$sort_days, &$ex_fid_ary, &$m_approve_fid_ary, &$topic_id, &$author_ary, &$id_ary, $start, $per_page)
4c79b5
	{
4c79b5
		global $config, $db;
4c79b5
4c79b5
		// No author? No posts.
4c79b5
		if (!sizeof($author_ary))
4c79b5
		{
4c79b5
			return 0;
4c79b5
		}
4c79b5
4c79b5
		// generate a search_key from all the options to identify the results
4c79b5
		$search_key = md5(implode('#', array(
4c79b5
			'',
4c79b5
			$type,
4c79b5
			($firstpost_only) ? 'firstpost' : '',
4c79b5
			'',
4c79b5
			'',
4c79b5
			$sort_days,
4c79b5
			$sort_key,
4c79b5
			$topic_id,
4c79b5
			implode(',', $ex_fid_ary),
4c79b5
			implode(',', $m_approve_fid_ary),
4c79b5
			implode(',', $author_ary)
4c79b5
		)));
4c79b5
4c79b5
		// try reading the results from cache
4c79b5
		$result_count = 0;
4c79b5
		if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE)
4c79b5
		{
4c79b5
			return $result_count;
4c79b5
		}
4c79b5
4c79b5
		$id_ary = array();
4c79b5
4c79b5
		// Create some display specific sql strings
4c79b5
		$sql_author		= $db->sql_in_set('p.poster_id', $author_ary);
4c79b5
		$sql_fora		= (sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '';
4c79b5
		$sql_topic_id	= ($topic_id) ? ' AND p.topic_id = ' . (int) $topic_id : '';
4c79b5
		$sql_time		= ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : '';
4c79b5
		$sql_firstpost = ($firstpost_only) ? ' AND p.post_id = t.topic_first_post_id' : '';
4c79b5
4c79b5
		// Build sql strings for sorting
4c79b5
		$sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
4c79b5
		$sql_sort_table = $sql_sort_join = '';
4c79b5
		switch ($sql_sort[0])
4c79b5
		{
4c79b5
			case 'u':
4c79b5
				$sql_sort_table	= USERS_TABLE . ' u, ';
4c79b5
				$sql_sort_join	= ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster ';
4c79b5
			break;
4c79b5
4c79b5
			case 't':
4c79b5
				$sql_sort_table	= ($type == 'posts' && !$firstpost_only) ? TOPICS_TABLE . ' t, ' : '';
4c79b5
				$sql_sort_join	= ($type == 'posts' && !$firstpost_only) ? ' AND t.topic_id = p.topic_id ' : '';
4c79b5
			break;
4c79b5
4c79b5
			case 'f':
4c79b5
				$sql_sort_table	= FORUMS_TABLE . ' f, ';
4c79b5
				$sql_sort_join	= ' AND f.forum_id = p.forum_id ';
4c79b5
			break;
4c79b5
		}
4c79b5
4c79b5
		if (!sizeof($m_approve_fid_ary))
4c79b5
		{
4c79b5
			$m_approve_fid_sql = ' AND p.post_approved = 1';
4c79b5
		}
4c79b5
		else if ($m_approve_fid_ary == array(-1))
4c79b5
		{
4c79b5
			$m_approve_fid_sql = '';
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$m_approve_fid_sql = ' AND (p.post_approved = 1 OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) . ')';
4c79b5
		}
4c79b5
4c79b5
		// If the cache was completely empty count the results
4c79b5
		$calc_results = ($result_count) ? '' : 'SQL_CALC_FOUND_ROWS ';
4c79b5
4c79b5
		// Build the query for really selecting the post_ids
4c79b5
		if ($type == 'posts')
4c79b5
		{
4c79b5
			$sql = "SELECT {$calc_results}p.post_id
4c79b5
				FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . "
4c79b5
				WHERE $sql_author
4c79b5
					$sql_topic_id
4c79b5
					$sql_firstpost
4c79b5
					$m_approve_fid_sql
4c79b5
					$sql_fora
4c79b5
					$sql_sort_join
4c79b5
					$sql_time
4c79b5
				ORDER BY $sql_sort";
4c79b5
			$field = 'post_id';
4c79b5
		}
4c79b5
		else
4c79b5
		{
4c79b5
			$sql = "SELECT {$calc_results}t.topic_id
4c79b5
				FROM " . $sql_sort_table . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
4c79b5
				WHERE $sql_author
4c79b5
					$sql_topic_id
4c79b5
					$sql_firstpost
4c79b5
					$m_approve_fid_sql
4c79b5
					$sql_fora
4c79b5
					AND t.topic_id = p.topic_id
4c79b5
					$sql_sort_join
4c79b5
					$sql_time
4c79b5
				GROUP BY t.topic_id
4c79b5
				ORDER BY $sql_sort";
4c79b5
			$field = 'topic_id';
4c79b5
		}
4c79b5
4c79b5
		// Only read one block of posts from the db and then cache it
4c79b5
		$result = $db->sql_query_limit($sql, $config['search_block_size'], $start);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			$id_ary[] = $row[$field];
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		// retrieve the total result count if needed
4c79b5
		if (!$result_count)
4c79b5
		{
4c79b5
			$sql = 'SELECT FOUND_ROWS() as result_count';
4c79b5
			$result = $db->sql_query($sql);
4c79b5
			$result_count = (int) $db->sql_fetchfield('result_count');
4c79b5
			$db->sql_freeresult($result);
4c79b5
4c79b5
			if (!$result_count)
4c79b5
			{
4c79b5
				return false;
4c79b5
			}
4c79b5
		}
4c79b5
4c79b5
		if (sizeof($id_ary))
4c79b5
		{
4c79b5
			$this->save_ids($search_key, '', $author_ary, $result_count, $id_ary, $start, $sort_dir);
4c79b5
			$id_ary = array_slice($id_ary, 0, $per_page);
4c79b5
4c79b5
			return $result_count;
4c79b5
		}
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Destroys cached search results, that contained one of the new words in a post so the results won't be outdated.
4c79b5
	*
4c79b5
	* @param string $mode contains the post mode: edit, post, reply, quote ...
4c79b5
	*/
4c79b5
	function index($mode, $post_id, &$message, &$subject, $poster_id, $forum_id)
4c79b5
	{
4c79b5
		global $db;
4c79b5
4c79b5
		// Split old and new post/subject to obtain array of words
4c79b5
		$split_text = $this->split_message($message);
4c79b5
		$split_title = ($subject) ? $this->split_message($subject) : array();
4c79b5
4c79b5
		$words = array_unique(array_merge($split_text, $split_title));
4c79b5
4c79b5
		unset($split_text);
4c79b5
		unset($split_title);
4c79b5
4c79b5
		// destroy cached search results containing any of the words removed or added
4c79b5
		$this->destroy_cache($words, array($poster_id));
4c79b5
4c79b5
		unset($words);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Destroy cached results, that might be outdated after deleting a post
4c79b5
	*/
4c79b5
	function index_remove($post_ids, $author_ids, $forum_ids)
4c79b5
	{
4c79b5
		$this->destroy_cache(array(), $author_ids);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Destroy old cache entries
4c79b5
	*/
4c79b5
	function tidy()
4c79b5
	{
4c79b5
		global $db, $config;
4c79b5
4c79b5
		// destroy too old cached search results
4c79b5
		$this->destroy_cache(array());
4c79b5
4c79b5
		set_config('search_last_gc', time(), true);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Create fulltext index
4c79b5
	*/
4c79b5
	function create_index($acp_module, $u_action)
4c79b5
	{
4c79b5
		global $db;
4c79b5
4c79b5
		// Make sure we can actually use MySQL with fulltext indexes
4c79b5
		if ($error = $this->init())
4c79b5
		{
4c79b5
			return $error;
4c79b5
		}
4c79b5
4c79b5
		if (empty($this->stats))
4c79b5
		{
4c79b5
			$this->get_stats();
4c79b5
		}
4c79b5
4c79b5
		$alter = array();
4c79b5
4c79b5
		if (!isset($this->stats['post_subject']))
4c79b5
		{
4c79b5
			if ($db->sql_layer == 'mysqli' || version_compare($db->sql_server_info(true), '4.1.3', '>='))
4c79b5
			{
4c79b5
				//$alter[] = 'MODIFY post_subject varchar(100) COLLATE utf8_unicode_ci DEFAULT \'\' NOT NULL';
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$alter[] = 'MODIFY post_subject text NOT NULL';
4c79b5
			}
4c79b5
			$alter[] = 'ADD FULLTEXT (post_subject)';
4c79b5
		}
4c79b5
4c79b5
		if (!isset($this->stats['post_text']))
4c79b5
		{
4c79b5
			if ($db->sql_layer == 'mysqli' || version_compare($db->sql_server_info(true), '4.1.3', '>='))
4c79b5
			{
4c79b5
				$alter[] = 'MODIFY post_text mediumtext COLLATE utf8_unicode_ci NOT NULL';
4c79b5
			}
4c79b5
			else
4c79b5
			{
4c79b5
				$alter[] = 'MODIFY post_text mediumtext NOT NULL';
4c79b5
			}
4c79b5
			$alter[] = 'ADD FULLTEXT (post_text)';
4c79b5
		}
4c79b5
4c79b5
		if (!isset($this->stats['post_content']))
4c79b5
		{
4c79b5
			$alter[] = 'ADD FULLTEXT post_content (post_subject, post_text)';
4c79b5
		}
4c79b5
4c79b5
		if (sizeof($alter))
4c79b5
		{
4c79b5
			$db->sql_query('ALTER TABLE ' . POSTS_TABLE . ' ' . implode(', ', $alter));
4c79b5
		}
4c79b5
4c79b5
		$db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
4c79b5
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Drop fulltext index
4c79b5
	*/
4c79b5
	function delete_index($acp_module, $u_action)
4c79b5
	{
4c79b5
		global $db;
4c79b5
4c79b5
		// Make sure we can actually use MySQL with fulltext indexes
4c79b5
		if ($error = $this->init())
4c79b5
		{
4c79b5
			return $error;
4c79b5
		}
4c79b5
4c79b5
		if (empty($this->stats))
4c79b5
		{
4c79b5
			$this->get_stats();
4c79b5
		}
4c79b5
4c79b5
		$alter = array();
4c79b5
4c79b5
		if (isset($this->stats['post_subject']))
4c79b5
		{
4c79b5
			$alter[] = 'DROP INDEX post_subject';
4c79b5
		}
4c79b5
4c79b5
		if (isset($this->stats['post_text']))
4c79b5
		{
4c79b5
			$alter[] = 'DROP INDEX post_text';
4c79b5
		}
4c79b5
4c79b5
		if (isset($this->stats['post_content']))
4c79b5
		{
4c79b5
			$alter[] = 'DROP INDEX post_content';
4c79b5
		}
4c79b5
4c79b5
		if (sizeof($alter))
4c79b5
		{
4c79b5
			$db->sql_query('ALTER TABLE ' . POSTS_TABLE . ' ' . implode(', ', $alter));
4c79b5
		}
4c79b5
4c79b5
		$db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
4c79b5
4c79b5
		return false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Returns true if both FULLTEXT indexes exist
4c79b5
	*/
4c79b5
	function index_created()
4c79b5
	{
4c79b5
		if (empty($this->stats))
4c79b5
		{
4c79b5
			$this->get_stats();
4c79b5
		}
4c79b5
4c79b5
		return (isset($this->stats['post_text']) && isset($this->stats['post_subject']) && isset($this->stats['post_content'])) ? true : false;
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Returns an associative array containing information about the indexes
4c79b5
	*/
4c79b5
	function index_stats()
4c79b5
	{
4c79b5
		global $user;
4c79b5
4c79b5
		if (empty($this->stats))
4c79b5
		{
4c79b5
			$this->get_stats();
4c79b5
		}
4c79b5
4c79b5
		return array(
4c79b5
			$user->lang['FULLTEXT_MYSQL_TOTAL_POSTS']			=> ($this->index_created()) ? $this->stats['total_posts'] : 0,
4c79b5
		);
4c79b5
	}
4c79b5
4c79b5
	function get_stats()
4c79b5
	{
4c79b5
		global $db;
4c79b5
4c79b5
		if (strpos($db->sql_layer, 'mysql') === false)
4c79b5
		{
4c79b5
			$this->stats = array();
4c79b5
			return;
4c79b5
		}
4c79b5
4c79b5
		$sql = 'SHOW INDEX
4c79b5
			FROM ' . POSTS_TABLE;
4c79b5
		$result = $db->sql_query($sql);
4c79b5
4c79b5
		while ($row = $db->sql_fetchrow($result))
4c79b5
		{
4c79b5
			// deal with older MySQL versions which didn't use Index_type
4c79b5
			$index_type = (isset($row['Index_type'])) ? $row['Index_type'] : $row['Comment'];
4c79b5
4c79b5
			if ($index_type == 'FULLTEXT')
4c79b5
			{
4c79b5
				if ($row['Key_name'] == 'post_text')
4c79b5
				{
4c79b5
					$this->stats['post_text'] = $row;
4c79b5
				}
4c79b5
				else if ($row['Key_name'] == 'post_subject')
4c79b5
				{
4c79b5
					$this->stats['post_subject'] = $row;
4c79b5
				}
4c79b5
				else if ($row['Key_name'] == 'post_content')
4c79b5
				{
4c79b5
					$this->stats['post_content'] = $row;
4c79b5
				}
4c79b5
			}
4c79b5
		}
4c79b5
		$db->sql_freeresult($result);
4c79b5
4c79b5
		$sql = 'SELECT COUNT(post_id) as total_posts
4c79b5
			FROM ' . POSTS_TABLE;
4c79b5
		$result = $db->sql_query($sql);
4c79b5
		$this->stats['total_posts'] = (int) $db->sql_fetchfield('total_posts');
4c79b5
		$db->sql_freeresult($result);
4c79b5
	}
4c79b5
4c79b5
	/**
4c79b5
	* Display a note, that UTF-8 support is not available with certain versions of PHP
4c79b5
	*/
4c79b5
	function acp()
4c79b5
	{
4c79b5
		global $user, $config;
4c79b5
4c79b5
		$tpl = '
4c79b5
		
4c79b5
			
<label>' . $user->lang['FULLTEXT_MYSQL_PCRE'] . '</label>
' . $user->lang['FULLTEXT_MYSQL_PCRE_EXPLAIN'] . '
4c79b5
			
' . (($this->pcre_properties) ? $user->lang['YES'] : $user->lang['NO']) . ' (PHP ' . PHP_VERSION . ')
4c79b5
		
4c79b5
		
4c79b5
			
<label>' . $user->lang['FULLTEXT_MYSQL_MBSTRING'] . '</label>
' . $user->lang['FULLTEXT_MYSQL_MBSTRING_EXPLAIN'] . '
4c79b5
			
' . (($this->mbstring_regex) ? $user->lang['YES'] : $user->lang['NO']). '
4c79b5
		
4c79b5
		';
4c79b5
4c79b5
		// These are fields required in the config table
4c79b5
		return array(
4c79b5
			'tpl'		=> $tpl,
4c79b5
			'config'	=> array()
4c79b5
		);
4c79b5
	}
4c79b5
}
4c79b5
4c79b5
?>