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

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