includes/search/fulltext_native.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Sat, 10 Mar 2012 00:11:31 +0400
changeset 14 88d35b740f20
parent 0 ceef6d64c21d
permissions -rw-r--r--
HG ignore
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package search
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* @ignore
indvdum@0
    21
*/
indvdum@0
    22
include_once($phpbb_root_path . 'includes/search/search.' . $phpEx);
indvdum@0
    23
indvdum@0
    24
/**
indvdum@0
    25
* fulltext_native
indvdum@0
    26
* phpBB's own db driven fulltext search, version 2
indvdum@0
    27
* @package search
indvdum@0
    28
*/
indvdum@0
    29
class fulltext_native extends search_backend
indvdum@0
    30
{
indvdum@0
    31
	var $stats = array();
indvdum@0
    32
	var $word_length = array();
indvdum@0
    33
	var $search_query;
indvdum@0
    34
	var $common_words = array();
indvdum@0
    35
indvdum@0
    36
	var $must_contain_ids = array();
indvdum@0
    37
	var $must_not_contain_ids = array();
indvdum@0
    38
	var $must_exclude_one_ids = array();
indvdum@0
    39
indvdum@0
    40
	/**
indvdum@0
    41
	* Initialises the fulltext_native search backend with min/max word length and makes sure the UTF-8 normalizer is loaded.
indvdum@0
    42
	*
indvdum@0
    43
	* @param	boolean|string	&$error	is passed by reference and should either be set to false on success or an error message on failure.
indvdum@0
    44
	*
indvdum@0
    45
	* @access	public
indvdum@0
    46
	*/
indvdum@0
    47
	function fulltext_native(&$error)
indvdum@0
    48
	{
indvdum@0
    49
		global $phpbb_root_path, $phpEx, $config;
indvdum@0
    50
indvdum@0
    51
		$this->word_length = array('min' => $config['fulltext_native_min_chars'], 'max' => $config['fulltext_native_max_chars']);
indvdum@0
    52
indvdum@0
    53
		/**
indvdum@0
    54
		* Load the UTF tools
indvdum@0
    55
		*/
indvdum@0
    56
		if (!class_exists('utf_normalizer'))
indvdum@0
    57
		{
indvdum@0
    58
			include($phpbb_root_path . 'includes/utf/utf_normalizer.' . $phpEx);
indvdum@0
    59
		}
indvdum@0
    60
indvdum@0
    61
indvdum@0
    62
		$error = false;
indvdum@0
    63
	}
indvdum@0
    64
indvdum@0
    65
	/**
indvdum@0
    66
	* This function fills $this->search_query with the cleaned user search query.
indvdum@0
    67
	*
indvdum@0
    68
	* If $terms is 'any' then the words will be extracted from the search query
indvdum@0
    69
	* and combined with | inside brackets. They will afterwards be treated like
indvdum@0
    70
	* an standard search query.
indvdum@0
    71
	*
indvdum@0
    72
	* Then it analyses the query and fills the internal arrays $must_not_contain_ids,
indvdum@0
    73
	* $must_contain_ids and $must_exclude_one_ids which are later used by keyword_search().
indvdum@0
    74
	*
indvdum@0
    75
	* @param	string	$keywords	contains the search query string as entered by the user
indvdum@0
    76
	* @param	string	$terms		is either 'all' (use search query as entered, default words to 'must be contained in post')
indvdum@0
    77
	* 	or 'any' (find all posts containing at least one of the given words)
indvdum@0
    78
	* @return	boolean				false if no valid keywords were found and otherwise true
indvdum@0
    79
	*
indvdum@0
    80
	* @access	public
indvdum@0
    81
	*/
indvdum@0
    82
	function split_keywords($keywords, $terms)
indvdum@0
    83
	{
indvdum@0
    84
		global $db, $user, $config;
indvdum@0
    85
indvdum@0
    86
		$tokens = '+-|()*';
indvdum@0
    87
indvdum@0
    88
		$keywords = trim($this->cleanup($keywords, $tokens));
indvdum@0
    89
indvdum@0
    90
		// allow word|word|word without brackets
indvdum@0
    91
		if ((strpos($keywords, ' ') === false) && (strpos($keywords, '|') !== false) && (strpos($keywords, '(') === false))
indvdum@0
    92
		{
indvdum@0
    93
			$keywords = '(' . $keywords . ')';
indvdum@0
    94
		}
indvdum@0
    95
indvdum@0
    96
		$open_bracket = $space = false;
indvdum@0
    97
		for ($i = 0, $n = strlen($keywords); $i < $n; $i++)
indvdum@0
    98
		{
indvdum@0
    99
			if ($open_bracket !== false)
indvdum@0
   100
			{
indvdum@0
   101
				switch ($keywords[$i])
indvdum@0
   102
				{
indvdum@0
   103
					case ')':
indvdum@0
   104
						if ($open_bracket + 1 == $i)
indvdum@0
   105
						{
indvdum@0
   106
							$keywords[$i - 1] = '|';
indvdum@0
   107
							$keywords[$i] = '|';
indvdum@0
   108
						}
indvdum@0
   109
						$open_bracket = false;
indvdum@0
   110
					break;
indvdum@0
   111
					case '(':
indvdum@0
   112
						$keywords[$i] = '|';
indvdum@0
   113
					break;
indvdum@0
   114
					case '+':
indvdum@0
   115
					case '-':
indvdum@0
   116
					case ' ':
indvdum@0
   117
						$keywords[$i] = '|';
indvdum@0
   118
					break;
indvdum@0
   119
					case '*':
indvdum@0
   120
						if ($i === 0 || ($keywords[$i - 1] !== '*' && strcspn($keywords[$i - 1], $tokens) === 0))
indvdum@0
   121
						{
indvdum@0
   122
							if ($i === $n - 1 || ($keywords[$i + 1] !== '*' && strcspn($keywords[$i + 1], $tokens) === 0))
indvdum@0
   123
							{
indvdum@0
   124
								$keywords = substr($keywords, 0, $i) . substr($keywords, $i + 1);
indvdum@0
   125
							}
indvdum@0
   126
						}
indvdum@0
   127
					break;
indvdum@0
   128
				}
indvdum@0
   129
			}
indvdum@0
   130
			else
indvdum@0
   131
			{
indvdum@0
   132
				switch ($keywords[$i])
indvdum@0
   133
				{
indvdum@0
   134
					case ')':
indvdum@0
   135
						$keywords[$i] = ' ';
indvdum@0
   136
					break;
indvdum@0
   137
					case '(':
indvdum@0
   138
						$open_bracket = $i;
indvdum@0
   139
						$space = false;
indvdum@0
   140
					break;
indvdum@0
   141
					case '|':
indvdum@0
   142
						$keywords[$i] = ' ';
indvdum@0
   143
					break;
indvdum@0
   144
					case '-':
indvdum@0
   145
					case '+':
indvdum@0
   146
						$space = $keywords[$i];
indvdum@0
   147
					break;
indvdum@0
   148
					case ' ':
indvdum@0
   149
						if ($space !== false)
indvdum@0
   150
						{
indvdum@0
   151
							$keywords[$i] = $space;
indvdum@0
   152
						}
indvdum@0
   153
					break;
indvdum@0
   154
					default:
indvdum@0
   155
						$space = false;
indvdum@0
   156
				}
indvdum@0
   157
			}
indvdum@0
   158
		}
indvdum@0
   159
indvdum@0
   160
		if ($open_bracket)
indvdum@0
   161
		{
indvdum@0
   162
			$keywords .= ')';
indvdum@0
   163
		}
indvdum@0
   164
indvdum@0
   165
		$match = array(
indvdum@0
   166
			'#  +#',
indvdum@0
   167
			'#\|\|+#',
indvdum@0
   168
			'#(\+|\-)(?:\+|\-)+#',
indvdum@0
   169
			'#\(\|#',
indvdum@0
   170
			'#\|\)#',
indvdum@0
   171
		);
indvdum@0
   172
		$replace = array(
indvdum@0
   173
			' ',
indvdum@0
   174
			'|',
indvdum@0
   175
			'$1',
indvdum@0
   176
			'(',
indvdum@0
   177
			')',
indvdum@0
   178
		);
indvdum@0
   179
indvdum@0
   180
		$keywords = preg_replace($match, $replace, $keywords);
indvdum@0
   181
		$num_keywords = sizeof(explode(' ', $keywords));
indvdum@0
   182
indvdum@0
   183
		// We limit the number of allowed keywords to minimize load on the database
indvdum@0
   184
		if ($config['max_num_search_keywords'] && $num_keywords > $config['max_num_search_keywords'])
indvdum@0
   185
		{
indvdum@0
   186
			trigger_error($user->lang('MAX_NUM_SEARCH_KEYWORDS_REFINE', $config['max_num_search_keywords'], $num_keywords));
indvdum@0
   187
		}
indvdum@0
   188
indvdum@0
   189
		// $keywords input format: each word separated by a space, words in a bracket are not separated
indvdum@0
   190
indvdum@0
   191
		// the user wants to search for any word, convert the search query
indvdum@0
   192
		if ($terms == 'any')
indvdum@0
   193
		{
indvdum@0
   194
			$words = array();
indvdum@0
   195
indvdum@0
   196
			preg_match_all('#([^\\s+\\-|()]+)(?:$|[\\s+\\-|()])#u', $keywords, $words);
indvdum@0
   197
			if (sizeof($words[1]))
indvdum@0
   198
			{
indvdum@0
   199
				$keywords = '(' . implode('|', $words[1]) . ')';
indvdum@0
   200
			}
indvdum@0
   201
		}
indvdum@0
   202
indvdum@0
   203
		// set the search_query which is shown to the user
indvdum@0
   204
		$this->search_query = $keywords;
indvdum@0
   205
indvdum@0
   206
		$exact_words = array();
indvdum@0
   207
		preg_match_all('#([^\\s+\\-|*()]+)(?:$|[\\s+\\-|()])#u', $keywords, $exact_words);
indvdum@0
   208
		$exact_words = $exact_words[1];
indvdum@0
   209
indvdum@0
   210
		$common_ids = $words = array();
indvdum@0
   211
indvdum@0
   212
		if (sizeof($exact_words))
indvdum@0
   213
		{
indvdum@0
   214
			$sql = 'SELECT word_id, word_text, word_common
indvdum@0
   215
				FROM ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
   216
				WHERE ' . $db->sql_in_set('word_text', $exact_words) . '
indvdum@0
   217
				ORDER BY word_count ASC';
indvdum@0
   218
			$result = $db->sql_query($sql);
indvdum@0
   219
indvdum@0
   220
			// store an array of words and ids, remove common words
indvdum@0
   221
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   222
			{
indvdum@0
   223
				if ($row['word_common'])
indvdum@0
   224
				{
indvdum@0
   225
					$this->common_words[] = $row['word_text'];
indvdum@0
   226
					$common_ids[$row['word_text']] = (int) $row['word_id'];
indvdum@0
   227
					continue;
indvdum@0
   228
				}
indvdum@0
   229
indvdum@0
   230
				$words[$row['word_text']] = (int) $row['word_id'];
indvdum@0
   231
			}
indvdum@0
   232
			$db->sql_freeresult($result);
indvdum@0
   233
		}
indvdum@0
   234
		unset($exact_words);
indvdum@0
   235
indvdum@0
   236
		// now analyse the search query, first split it using the spaces
indvdum@0
   237
		$query = explode(' ', $keywords);
indvdum@0
   238
indvdum@0
   239
		$this->must_contain_ids = array();
indvdum@0
   240
		$this->must_not_contain_ids = array();
indvdum@0
   241
		$this->must_exclude_one_ids = array();
indvdum@0
   242
indvdum@0
   243
		$mode = '';
indvdum@0
   244
		$ignore_no_id = true;
indvdum@0
   245
indvdum@0
   246
		foreach ($query as $word)
indvdum@0
   247
		{
indvdum@0
   248
			if (empty($word))
indvdum@0
   249
			{
indvdum@0
   250
				continue;
indvdum@0
   251
			}
indvdum@0
   252
indvdum@0
   253
			// words which should not be included
indvdum@0
   254
			if ($word[0] == '-')
indvdum@0
   255
			{
indvdum@0
   256
				$word = substr($word, 1);
indvdum@0
   257
indvdum@0
   258
				// a group of which at least one may not be in the resulting posts
indvdum@0
   259
				if ($word[0] == '(')
indvdum@0
   260
				{
indvdum@0
   261
					$word = array_unique(explode('|', substr($word, 1, -1)));
indvdum@0
   262
					$mode = 'must_exclude_one';
indvdum@0
   263
				}
indvdum@0
   264
				// one word which should not be in the resulting posts
indvdum@0
   265
				else
indvdum@0
   266
				{
indvdum@0
   267
					$mode = 'must_not_contain';
indvdum@0
   268
				}
indvdum@0
   269
				$ignore_no_id = true;
indvdum@0
   270
			}
indvdum@0
   271
			// words which have to be included
indvdum@0
   272
			else
indvdum@0
   273
			{
indvdum@0
   274
				// no prefix is the same as a +prefix
indvdum@0
   275
				if ($word[0] == '+')
indvdum@0
   276
				{
indvdum@0
   277
					$word = substr($word, 1);
indvdum@0
   278
				}
indvdum@0
   279
indvdum@0
   280
				// a group of words of which at least one word should be in every resulting post
indvdum@0
   281
				if ($word[0] == '(')
indvdum@0
   282
				{
indvdum@0
   283
					$word = array_unique(explode('|', substr($word, 1, -1)));
indvdum@0
   284
				}
indvdum@0
   285
				$ignore_no_id = false;
indvdum@0
   286
				$mode = 'must_contain';
indvdum@0
   287
			}
indvdum@0
   288
indvdum@0
   289
			if (empty($word))
indvdum@0
   290
			{
indvdum@0
   291
				continue;
indvdum@0
   292
			}
indvdum@0
   293
indvdum@0
   294
			// if this is an array of words then retrieve an id for each
indvdum@0
   295
			if (is_array($word))
indvdum@0
   296
			{
indvdum@0
   297
				$non_common_words = array();
indvdum@0
   298
				$id_words = array();
indvdum@0
   299
				foreach ($word as $i => $word_part)
indvdum@0
   300
				{
indvdum@0
   301
					if (strpos($word_part, '*') !== false)
indvdum@0
   302
					{
indvdum@0
   303
						$id_words[] = '\'' . $db->sql_escape(str_replace('*', '%', $word_part)) . '\'';
indvdum@0
   304
						$non_common_words[] = $word_part;
indvdum@0
   305
					}
indvdum@0
   306
					else if (isset($words[$word_part]))
indvdum@0
   307
					{
indvdum@0
   308
						$id_words[] = $words[$word_part];
indvdum@0
   309
						$non_common_words[] = $word_part;
indvdum@0
   310
					}
indvdum@0
   311
					else
indvdum@0
   312
					{
indvdum@0
   313
						$len = utf8_strlen($word_part);
indvdum@0
   314
						if ($len < $this->word_length['min'] || $len > $this->word_length['max'])
indvdum@0
   315
						{
indvdum@0
   316
							$this->common_words[] = $word_part;
indvdum@0
   317
						}
indvdum@0
   318
					}
indvdum@0
   319
				}
indvdum@0
   320
				if (sizeof($id_words))
indvdum@0
   321
				{
indvdum@0
   322
					sort($id_words);
indvdum@0
   323
					if (sizeof($id_words) > 1)
indvdum@0
   324
					{
indvdum@0
   325
						$this->{$mode . '_ids'}[] = $id_words;
indvdum@0
   326
					}
indvdum@0
   327
					else
indvdum@0
   328
					{
indvdum@0
   329
						$mode = ($mode == 'must_exclude_one') ? 'must_not_contain' : $mode;
indvdum@0
   330
						$this->{$mode . '_ids'}[] = $id_words[0];
indvdum@0
   331
					}
indvdum@0
   332
				}
indvdum@0
   333
				// throw an error if we shall not ignore unexistant words
indvdum@0
   334
				else if (!$ignore_no_id && sizeof($non_common_words))
indvdum@0
   335
				{
indvdum@0
   336
					trigger_error(sprintf($user->lang['WORDS_IN_NO_POST'], implode(', ', $non_common_words)));
indvdum@0
   337
				}
indvdum@0
   338
				unset($non_common_words);
indvdum@0
   339
			}
indvdum@0
   340
			// else we only need one id
indvdum@0
   341
			else if (($wildcard = strpos($word, '*') !== false) || isset($words[$word]))
indvdum@0
   342
			{
indvdum@0
   343
				if ($wildcard)
indvdum@0
   344
				{
indvdum@0
   345
					$len = utf8_strlen(str_replace('*', '', $word));
indvdum@0
   346
					if ($len >= $this->word_length['min'] && $len <= $this->word_length['max'])
indvdum@0
   347
					{
indvdum@0
   348
						$this->{$mode . '_ids'}[] = '\'' . $db->sql_escape(str_replace('*', '%', $word)) . '\'';
indvdum@0
   349
					}
indvdum@0
   350
					else
indvdum@0
   351
					{
indvdum@0
   352
						$this->common_words[] = $word;
indvdum@0
   353
					}
indvdum@0
   354
				}
indvdum@0
   355
				else
indvdum@0
   356
				{
indvdum@0
   357
					$this->{$mode . '_ids'}[] = $words[$word];
indvdum@0
   358
				}
indvdum@0
   359
			}
indvdum@0
   360
			// throw an error if we shall not ignore unexistant words
indvdum@0
   361
			else if (!$ignore_no_id)
indvdum@0
   362
			{
indvdum@0
   363
				if (!isset($common_ids[$word]))
indvdum@0
   364
				{
indvdum@0
   365
					$len = utf8_strlen($word);
indvdum@0
   366
					if ($len >= $this->word_length['min'] && $len <= $this->word_length['max'])
indvdum@0
   367
					{
indvdum@0
   368
						trigger_error(sprintf($user->lang['WORD_IN_NO_POST'], $word));
indvdum@0
   369
					}
indvdum@0
   370
					else
indvdum@0
   371
					{
indvdum@0
   372
						$this->common_words[] = $word;
indvdum@0
   373
					}
indvdum@0
   374
				}
indvdum@0
   375
			}
indvdum@0
   376
			else
indvdum@0
   377
			{
indvdum@0
   378
				$len = utf8_strlen($word);
indvdum@0
   379
				if ($len < $this->word_length['min'] || $len > $this->word_length['max'])
indvdum@0
   380
				{
indvdum@0
   381
					$this->common_words[] = $word;
indvdum@0
   382
				}
indvdum@0
   383
			}
indvdum@0
   384
		}
indvdum@0
   385
indvdum@0
   386
		// we can't search for negatives only
indvdum@0
   387
		if (!sizeof($this->must_contain_ids))
indvdum@0
   388
		{
indvdum@0
   389
			return false;
indvdum@0
   390
		}
indvdum@0
   391
indvdum@0
   392
		if (!empty($this->search_query))
indvdum@0
   393
		{
indvdum@0
   394
			return true;
indvdum@0
   395
		}
indvdum@0
   396
		return false;
indvdum@0
   397
	}
indvdum@0
   398
indvdum@0
   399
	/**
indvdum@0
   400
	* Performs a search on keywords depending on display specific params. You have to run split_keywords() first.
indvdum@0
   401
	*
indvdum@0
   402
	* @param	string		$type				contains either posts or topics depending on what should be searched for
indvdum@0
   403
	* @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)
indvdum@0
   404
	* @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)
indvdum@0
   405
	* @param	array		$sort_by_sql		contains SQL code for the ORDER BY part of a query
indvdum@0
   406
	* @param	string		$sort_key			is the key of $sort_by_sql for the selected sorting
indvdum@0
   407
	* @param	string		$sort_dir			is either a or d representing ASC and DESC
indvdum@0
   408
	* @param	string		$sort_days			specifies the maximum amount of days a post may be old
indvdum@0
   409
	* @param	array		$ex_fid_ary			specifies an array of forum ids which should not be searched
indvdum@0
   410
	* @param	array		$m_approve_fid_ary	specifies an array of forum ids in which the searcher is allowed to view unapproved posts
indvdum@0
   411
	* @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
indvdum@0
   412
	* @param	array		$author_ary			an array of author ids if the author should be ignored during the search the array is empty
indvdum@0
   413
	* @param	string		$author_name		specifies the author match, when ANONYMOUS is also a search-match
indvdum@0
   414
	* @param	array		&$id_ary			passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
indvdum@0
   415
	* @param	int			$start				indicates the first index of the page
indvdum@0
   416
	* @param	int			$per_page			number of ids each page is supposed to contain
indvdum@0
   417
	* @return	boolean|int						total number of results
indvdum@0
   418
	*
indvdum@0
   419
	* @access	public
indvdum@0
   420
	*/
indvdum@0
   421
	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, $author_name, &$id_ary, $start, $per_page)
indvdum@0
   422
	{
indvdum@0
   423
		global $config, $db;
indvdum@0
   424
indvdum@0
   425
		// No keywords? No posts.
indvdum@0
   426
		if (empty($this->search_query))
indvdum@0
   427
		{
indvdum@0
   428
			return false;
indvdum@0
   429
		}
indvdum@0
   430
indvdum@0
   431
		$must_contain_ids = $this->must_contain_ids;
indvdum@0
   432
		$must_not_contain_ids = $this->must_not_contain_ids;
indvdum@0
   433
		$must_exclude_one_ids = $this->must_exclude_one_ids;
indvdum@0
   434
indvdum@0
   435
		sort($must_contain_ids);
indvdum@0
   436
		sort($must_not_contain_ids);
indvdum@0
   437
		sort($must_exclude_one_ids);
indvdum@0
   438
indvdum@0
   439
		// generate a search_key from all the options to identify the results
indvdum@0
   440
		$search_key = md5(implode('#', array(
indvdum@0
   441
			serialize($must_contain_ids),
indvdum@0
   442
			serialize($must_not_contain_ids),
indvdum@0
   443
			serialize($must_exclude_one_ids),
indvdum@0
   444
			$type,
indvdum@0
   445
			$fields,
indvdum@0
   446
			$terms,
indvdum@0
   447
			$sort_days,
indvdum@0
   448
			$sort_key,
indvdum@0
   449
			$topic_id,
indvdum@0
   450
			implode(',', $ex_fid_ary),
indvdum@0
   451
			implode(',', $m_approve_fid_ary),
indvdum@0
   452
			implode(',', $author_ary),
indvdum@0
   453
			$author_name,
indvdum@0
   454
		)));
indvdum@0
   455
indvdum@0
   456
		// try reading the results from cache
indvdum@0
   457
		$total_results = 0;
indvdum@0
   458
		if ($this->obtain_ids($search_key, $total_results, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE)
indvdum@0
   459
		{
indvdum@0
   460
			return $total_results;
indvdum@0
   461
		}
indvdum@0
   462
indvdum@0
   463
		$id_ary = array();
indvdum@0
   464
indvdum@0
   465
		$sql_where = array();
indvdum@0
   466
		$group_by = false;
indvdum@0
   467
		$m_num = 0;
indvdum@0
   468
		$w_num = 0;
indvdum@0
   469
indvdum@0
   470
		$sql_array = array(
indvdum@0
   471
			'SELECT'	=> ($type == 'posts') ? 'p.post_id' : 'p.topic_id',
indvdum@0
   472
			'FROM'		=> array(
indvdum@0
   473
				SEARCH_WORDMATCH_TABLE	=> array(),
indvdum@0
   474
				SEARCH_WORDLIST_TABLE	=> array(),
indvdum@0
   475
			),
indvdum@0
   476
			'LEFT_JOIN' => array(array(
indvdum@0
   477
				'FROM'	=> array(POSTS_TABLE => 'p'),
indvdum@0
   478
				'ON'	=> 'm0.post_id = p.post_id',
indvdum@0
   479
			)),
indvdum@0
   480
		);
indvdum@0
   481
indvdum@0
   482
		$title_match = '';
indvdum@0
   483
		$left_join_topics = false;
indvdum@0
   484
		$group_by = true;
indvdum@0
   485
		// Build some display specific sql strings
indvdum@0
   486
		switch ($fields)
indvdum@0
   487
		{
indvdum@0
   488
			case 'titleonly':
indvdum@0
   489
				$title_match = 'title_match = 1';
indvdum@0
   490
				$group_by = false;
indvdum@0
   491
			// no break
indvdum@0
   492
			case 'firstpost':
indvdum@0
   493
				$left_join_topics = true;
indvdum@0
   494
				$sql_where[] = 'p.post_id = t.topic_first_post_id';
indvdum@0
   495
			break;
indvdum@0
   496
indvdum@0
   497
			case 'msgonly':
indvdum@0
   498
				$title_match = 'title_match = 0';
indvdum@0
   499
				$group_by = false;
indvdum@0
   500
			break;
indvdum@0
   501
		}
indvdum@0
   502
indvdum@0
   503
		if ($type == 'topics')
indvdum@0
   504
		{
indvdum@0
   505
			$left_join_topics = true;
indvdum@0
   506
			$group_by = true;
indvdum@0
   507
		}
indvdum@0
   508
indvdum@0
   509
		/**
indvdum@0
   510
		* @todo Add a query optimizer (handle stuff like "+(4|3) +4")
indvdum@0
   511
		*/
indvdum@0
   512
indvdum@0
   513
		foreach ($this->must_contain_ids as $subquery)
indvdum@0
   514
		{
indvdum@0
   515
			if (is_array($subquery))
indvdum@0
   516
			{
indvdum@0
   517
				$group_by = true;
indvdum@0
   518
indvdum@0
   519
				$word_id_sql = array();
indvdum@0
   520
				$word_ids = array();
indvdum@0
   521
				foreach ($subquery as $id)
indvdum@0
   522
				{
indvdum@0
   523
					if (is_string($id))
indvdum@0
   524
					{
indvdum@0
   525
						$sql_array['LEFT_JOIN'][] = array(
indvdum@0
   526
							'FROM'	=> array(SEARCH_WORDLIST_TABLE => 'w' . $w_num),
indvdum@0
   527
							'ON'	=> "w$w_num.word_text LIKE $id"
indvdum@0
   528
						);
indvdum@0
   529
						$word_ids[] = "w$w_num.word_id";
indvdum@0
   530
indvdum@0
   531
						$w_num++;
indvdum@0
   532
					}
indvdum@0
   533
					else
indvdum@0
   534
					{
indvdum@0
   535
						$word_ids[] = $id;
indvdum@0
   536
					}
indvdum@0
   537
				}
indvdum@0
   538
indvdum@0
   539
				$sql_where[] = $db->sql_in_set("m$m_num.word_id", $word_ids);
indvdum@0
   540
indvdum@0
   541
				unset($word_id_sql);
indvdum@0
   542
				unset($word_ids);
indvdum@0
   543
			}
indvdum@0
   544
			else if (is_string($subquery))
indvdum@0
   545
			{
indvdum@0
   546
				$sql_array['FROM'][SEARCH_WORDLIST_TABLE][] = 'w' . $w_num;
indvdum@0
   547
indvdum@0
   548
				$sql_where[] = "w$w_num.word_text LIKE $subquery";
indvdum@0
   549
				$sql_where[] = "m$m_num.word_id = w$w_num.word_id";
indvdum@0
   550
indvdum@0
   551
				$group_by = true;
indvdum@0
   552
				$w_num++;
indvdum@0
   553
			}
indvdum@0
   554
			else
indvdum@0
   555
			{
indvdum@0
   556
				$sql_where[] = "m$m_num.word_id = $subquery";
indvdum@0
   557
			}
indvdum@0
   558
indvdum@0
   559
			$sql_array['FROM'][SEARCH_WORDMATCH_TABLE][] = 'm' . $m_num;
indvdum@0
   560
indvdum@0
   561
			if ($title_match)
indvdum@0
   562
			{
indvdum@0
   563
				$sql_where[] = "m$m_num.$title_match";
indvdum@0
   564
			}
indvdum@0
   565
indvdum@0
   566
			if ($m_num != 0)
indvdum@0
   567
			{
indvdum@0
   568
				$sql_where[] = "m$m_num.post_id = m0.post_id";
indvdum@0
   569
			}
indvdum@0
   570
			$m_num++;
indvdum@0
   571
		}
indvdum@0
   572
indvdum@0
   573
		foreach ($this->must_not_contain_ids as $key => $subquery)
indvdum@0
   574
		{
indvdum@0
   575
			if (is_string($subquery))
indvdum@0
   576
			{
indvdum@0
   577
				$sql_array['LEFT_JOIN'][] = array(
indvdum@0
   578
					'FROM'	=> array(SEARCH_WORDLIST_TABLE => 'w' . $w_num),
indvdum@0
   579
					'ON'	=> "w$w_num.word_text LIKE $subquery"
indvdum@0
   580
				);
indvdum@0
   581
indvdum@0
   582
				$this->must_not_contain_ids[$key] = "w$w_num.word_id";
indvdum@0
   583
indvdum@0
   584
				$group_by = true;
indvdum@0
   585
				$w_num++;
indvdum@0
   586
			}
indvdum@0
   587
		}
indvdum@0
   588
indvdum@0
   589
		if (sizeof($this->must_not_contain_ids))
indvdum@0
   590
		{
indvdum@0
   591
			$sql_array['LEFT_JOIN'][] = array(
indvdum@0
   592
				'FROM'	=> array(SEARCH_WORDMATCH_TABLE => 'm' . $m_num),
indvdum@0
   593
				'ON'	=> $db->sql_in_set("m$m_num.word_id", $this->must_not_contain_ids) . (($title_match) ? " AND m$m_num.$title_match" : '') . " AND m$m_num.post_id = m0.post_id"
indvdum@0
   594
			);
indvdum@0
   595
indvdum@0
   596
			$sql_where[] = "m$m_num.word_id IS NULL";
indvdum@0
   597
			$m_num++;
indvdum@0
   598
		}
indvdum@0
   599
indvdum@0
   600
		foreach ($this->must_exclude_one_ids as $ids)
indvdum@0
   601
		{
indvdum@0
   602
			$is_null_joins = array();
indvdum@0
   603
			foreach ($ids as $id)
indvdum@0
   604
			{
indvdum@0
   605
				if (is_string($id))
indvdum@0
   606
				{
indvdum@0
   607
					$sql_array['LEFT_JOIN'][] = array(
indvdum@0
   608
						'FROM'	=> array(SEARCH_WORDLIST_TABLE => 'w' . $w_num),
indvdum@0
   609
						'ON'	=> "w$w_num.word_text LIKE $id"
indvdum@0
   610
					);
indvdum@0
   611
					$id = "w$w_num.word_id";
indvdum@0
   612
indvdum@0
   613
					$group_by = true;
indvdum@0
   614
					$w_num++;
indvdum@0
   615
				}
indvdum@0
   616
indvdum@0
   617
				$sql_array['LEFT_JOIN'][] = array(
indvdum@0
   618
					'FROM'	=> array(SEARCH_WORDMATCH_TABLE => 'm' . $m_num),
indvdum@0
   619
					'ON'	=> "m$m_num.word_id = $id AND m$m_num.post_id = m0.post_id" . (($title_match) ? " AND m$m_num.$title_match" : '')
indvdum@0
   620
				);
indvdum@0
   621
				$is_null_joins[] = "m$m_num.word_id IS NULL";
indvdum@0
   622
indvdum@0
   623
				$m_num++;
indvdum@0
   624
			}
indvdum@0
   625
			$sql_where[] = '(' . implode(' OR ', $is_null_joins) . ')';
indvdum@0
   626
		}
indvdum@0
   627
indvdum@0
   628
		if (!sizeof($m_approve_fid_ary))
indvdum@0
   629
		{
indvdum@0
   630
			$sql_where[] = 'p.post_approved = 1';
indvdum@0
   631
		}
indvdum@0
   632
		else if ($m_approve_fid_ary !== array(-1))
indvdum@0
   633
		{
indvdum@0
   634
			$sql_where[] = '(p.post_approved = 1 OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) . ')';
indvdum@0
   635
		}
indvdum@0
   636
indvdum@0
   637
		if ($topic_id)
indvdum@0
   638
		{
indvdum@0
   639
			$sql_where[] = 'p.topic_id = ' . $topic_id;
indvdum@0
   640
		}
indvdum@0
   641
indvdum@0
   642
		if (sizeof($author_ary))
indvdum@0
   643
		{
indvdum@0
   644
			if ($author_name)
indvdum@0
   645
			{
indvdum@0
   646
				// first one matches post of registered users, second one guests and deleted users
indvdum@0
   647
				$sql_author = '(' . $db->sql_in_set('p.poster_id', array_diff($author_ary, array(ANONYMOUS)), false, true) . ' OR p.post_username ' . $author_name . ')';
indvdum@0
   648
			}
indvdum@0
   649
			else
indvdum@0
   650
			{
indvdum@0
   651
				$sql_author = $db->sql_in_set('p.poster_id', $author_ary);
indvdum@0
   652
			}
indvdum@0
   653
			$sql_where[] = $sql_author;
indvdum@0
   654
		}
indvdum@0
   655
indvdum@0
   656
		if (sizeof($ex_fid_ary))
indvdum@0
   657
		{
indvdum@0
   658
			$sql_where[] = $db->sql_in_set('p.forum_id', $ex_fid_ary, true);
indvdum@0
   659
		}
indvdum@0
   660
indvdum@0
   661
		if ($sort_days)
indvdum@0
   662
		{
indvdum@0
   663
			$sql_where[] = 'p.post_time >= ' . (time() - ($sort_days * 86400));
indvdum@0
   664
		}
indvdum@0
   665
indvdum@0
   666
		$sql_array['WHERE'] = implode(' AND ', $sql_where);
indvdum@0
   667
indvdum@0
   668
		$is_mysql = false;
indvdum@0
   669
		// if the total result count is not cached yet, retrieve it from the db
indvdum@0
   670
		if (!$total_results)
indvdum@0
   671
		{
indvdum@0
   672
			$sql = '';
indvdum@0
   673
			$sql_array_count = $sql_array;
indvdum@0
   674
indvdum@0
   675
			if ($left_join_topics)
indvdum@0
   676
			{
indvdum@0
   677
				$sql_array_count['LEFT_JOIN'][] = array(
indvdum@0
   678
					'FROM'	=> array(TOPICS_TABLE => 't'),
indvdum@0
   679
					'ON'	=> 'p.topic_id = t.topic_id'
indvdum@0
   680
				);
indvdum@0
   681
			}
indvdum@0
   682
indvdum@0
   683
			switch ($db->sql_layer)
indvdum@0
   684
			{
indvdum@0
   685
				case 'mysql4':
indvdum@0
   686
				case 'mysqli':
indvdum@0
   687
indvdum@0
   688
					// 3.x does not support SQL_CALC_FOUND_ROWS
indvdum@0
   689
					// $sql_array['SELECT'] = 'SQL_CALC_FOUND_ROWS ' . $sql_array['SELECT'];
indvdum@0
   690
					$is_mysql = true;
indvdum@0
   691
indvdum@0
   692
				break;
indvdum@0
   693
indvdum@0
   694
				case 'sqlite':
indvdum@0
   695
					$sql_array_count['SELECT'] = ($type == 'posts') ? 'DISTINCT p.post_id' : 'DISTINCT p.topic_id';
indvdum@0
   696
					$sql = 'SELECT COUNT(' . (($type == 'posts') ? 'post_id' : 'topic_id') . ') as total_results
indvdum@0
   697
							FROM (' . $db->sql_build_query('SELECT', $sql_array_count) . ')';
indvdum@0
   698
indvdum@0
   699
				// no break
indvdum@0
   700
indvdum@0
   701
				default:
indvdum@0
   702
					$sql_array_count['SELECT'] = ($type == 'posts') ? 'COUNT(DISTINCT p.post_id) AS total_results' : 'COUNT(DISTINCT p.topic_id) AS total_results';
indvdum@0
   703
					$sql = (!$sql) ? $db->sql_build_query('SELECT', $sql_array_count) : $sql;
indvdum@0
   704
indvdum@0
   705
					$result = $db->sql_query($sql);
indvdum@0
   706
					$total_results = (int) $db->sql_fetchfield('total_results');
indvdum@0
   707
					$db->sql_freeresult($result);
indvdum@0
   708
indvdum@0
   709
					if (!$total_results)
indvdum@0
   710
					{
indvdum@0
   711
						return false;
indvdum@0
   712
					}
indvdum@0
   713
				break;
indvdum@0
   714
			}
indvdum@0
   715
indvdum@0
   716
			unset($sql_array_count, $sql);
indvdum@0
   717
		}
indvdum@0
   718
indvdum@0
   719
		// Build sql strings for sorting
indvdum@0
   720
		$sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
indvdum@0
   721
indvdum@0
   722
		switch ($sql_sort[0])
indvdum@0
   723
		{
indvdum@0
   724
			case 'u':
indvdum@0
   725
				$sql_array['FROM'][USERS_TABLE] = 'u';
indvdum@0
   726
				$sql_where[] = 'u.user_id = p.poster_id ';
indvdum@0
   727
			break;
indvdum@0
   728
indvdum@0
   729
			case 't':
indvdum@0
   730
				$left_join_topics = true;
indvdum@0
   731
			break;
indvdum@0
   732
indvdum@0
   733
			case 'f':
indvdum@0
   734
				$sql_array['FROM'][FORUMS_TABLE] = 'f';
indvdum@0
   735
				$sql_where[] = 'f.forum_id = p.forum_id';
indvdum@0
   736
			break;
indvdum@0
   737
		}
indvdum@0
   738
indvdum@0
   739
		if ($left_join_topics)
indvdum@0
   740
		{
indvdum@0
   741
			$sql_array['LEFT_JOIN'][] = array(
indvdum@0
   742
				'FROM'	=> array(TOPICS_TABLE => 't'),
indvdum@0
   743
				'ON'	=> 'p.topic_id = t.topic_id'
indvdum@0
   744
			);
indvdum@0
   745
		}
indvdum@0
   746
indvdum@0
   747
		$sql_array['WHERE'] = implode(' AND ', $sql_where);
indvdum@0
   748
		$sql_array['GROUP_BY'] = ($group_by) ? (($type == 'posts') ? 'p.post_id' : 'p.topic_id') . ', ' . $sort_by_sql[$sort_key] : '';
indvdum@0
   749
		$sql_array['ORDER_BY'] = $sql_sort;
indvdum@0
   750
indvdum@0
   751
		unset($sql_where, $sql_sort, $group_by);
indvdum@0
   752
indvdum@0
   753
		$sql = $db->sql_build_query('SELECT', $sql_array);
indvdum@0
   754
		$result = $db->sql_query_limit($sql, $config['search_block_size'], $start);
indvdum@0
   755
indvdum@0
   756
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   757
		{
indvdum@0
   758
			$id_ary[] = (int) $row[(($type == 'posts') ? 'post_id' : 'topic_id')];
indvdum@0
   759
		}
indvdum@0
   760
		$db->sql_freeresult($result);
indvdum@0
   761
indvdum@0
   762
		if (!sizeof($id_ary))
indvdum@0
   763
		{
indvdum@0
   764
			return false;
indvdum@0
   765
		}
indvdum@0
   766
indvdum@0
   767
		// if we use mysql and the total result count is not cached yet, retrieve it from the db
indvdum@0
   768
		if (!$total_results && $is_mysql)
indvdum@0
   769
		{
indvdum@0
   770
			// Count rows for the executed queries. Replace $select within $sql with SQL_CALC_FOUND_ROWS, and run it.
indvdum@0
   771
			$sql_array_copy = $sql_array;
indvdum@0
   772
			$sql_array_copy['SELECT'] = 'SQL_CALC_FOUND_ROWS p.post_id ';
indvdum@0
   773
indvdum@0
   774
			$sql = $db->sql_build_query('SELECT', $sql_array_copy);
indvdum@0
   775
			unset($sql_array_copy);
indvdum@0
   776
indvdum@0
   777
			$db->sql_query($sql);
indvdum@0
   778
			$db->sql_freeresult($result);
indvdum@0
   779
indvdum@0
   780
			$sql = 'SELECT FOUND_ROWS() as total_results';
indvdum@0
   781
			$result = $db->sql_query($sql);
indvdum@0
   782
			$total_results = (int) $db->sql_fetchfield('total_results');
indvdum@0
   783
			$db->sql_freeresult($result);
indvdum@0
   784
indvdum@0
   785
			if (!$total_results)
indvdum@0
   786
			{
indvdum@0
   787
				return false;
indvdum@0
   788
			}
indvdum@0
   789
		}
indvdum@0
   790
indvdum@0
   791
		// store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page
indvdum@0
   792
		$this->save_ids($search_key, $this->search_query, $author_ary, $total_results, $id_ary, $start, $sort_dir);
indvdum@0
   793
		$id_ary = array_slice($id_ary, 0, (int) $per_page);
indvdum@0
   794
indvdum@0
   795
		return $total_results;
indvdum@0
   796
	}
indvdum@0
   797
indvdum@0
   798
	/**
indvdum@0
   799
	* Performs a search on an author's posts without caring about message contents. Depends on display specific params
indvdum@0
   800
	*
indvdum@0
   801
	* @param	string		$type				contains either posts or topics depending on what should be searched for
indvdum@0
   802
	* @param	boolean		$firstpost_only		if true, only topic starting posts will be considered
indvdum@0
   803
	* @param	array		$sort_by_sql		contains SQL code for the ORDER BY part of a query
indvdum@0
   804
	* @param	string		$sort_key			is the key of $sort_by_sql for the selected sorting
indvdum@0
   805
	* @param	string		$sort_dir			is either a or d representing ASC and DESC
indvdum@0
   806
	* @param	string		$sort_days			specifies the maximum amount of days a post may be old
indvdum@0
   807
	* @param	array		$ex_fid_ary			specifies an array of forum ids which should not be searched
indvdum@0
   808
	* @param	array		$m_approve_fid_ary	specifies an array of forum ids in which the searcher is allowed to view unapproved posts
indvdum@0
   809
	* @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
indvdum@0
   810
	* @param	array		$author_ary			an array of author ids
indvdum@0
   811
	* @param	string		$author_name		specifies the author match, when ANONYMOUS is also a search-match
indvdum@0
   812
	* @param	array		&$id_ary			passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
indvdum@0
   813
	* @param	int			$start				indicates the first index of the page
indvdum@0
   814
	* @param	int			$per_page			number of ids each page is supposed to contain
indvdum@0
   815
	* @return	boolean|int						total number of results
indvdum@0
   816
	*
indvdum@0
   817
	* @access	public
indvdum@0
   818
	*/
indvdum@0
   819
	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, $author_name, &$id_ary, $start, $per_page)
indvdum@0
   820
	{
indvdum@0
   821
		global $config, $db;
indvdum@0
   822
indvdum@0
   823
		// No author? No posts.
indvdum@0
   824
		if (!sizeof($author_ary))
indvdum@0
   825
		{
indvdum@0
   826
			return 0;
indvdum@0
   827
		}
indvdum@0
   828
indvdum@0
   829
		// generate a search_key from all the options to identify the results
indvdum@0
   830
		$search_key = md5(implode('#', array(
indvdum@0
   831
			'',
indvdum@0
   832
			$type,
indvdum@0
   833
			($firstpost_only) ? 'firstpost' : '',
indvdum@0
   834
			'',
indvdum@0
   835
			'',
indvdum@0
   836
			$sort_days,
indvdum@0
   837
			$sort_key,
indvdum@0
   838
			$topic_id,
indvdum@0
   839
			implode(',', $ex_fid_ary),
indvdum@0
   840
			implode(',', $m_approve_fid_ary),
indvdum@0
   841
			implode(',', $author_ary),
indvdum@0
   842
			$author_name,
indvdum@0
   843
		)));
indvdum@0
   844
indvdum@0
   845
		// try reading the results from cache
indvdum@0
   846
		$total_results = 0;
indvdum@0
   847
		if ($this->obtain_ids($search_key, $total_results, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE)
indvdum@0
   848
		{
indvdum@0
   849
			return $total_results;
indvdum@0
   850
		}
indvdum@0
   851
indvdum@0
   852
		$id_ary = array();
indvdum@0
   853
indvdum@0
   854
		// Create some display specific sql strings
indvdum@0
   855
		if ($author_name)
indvdum@0
   856
		{
indvdum@0
   857
			// first one matches post of registered users, second one guests and deleted users
indvdum@0
   858
			$sql_author = '(' . $db->sql_in_set('p.poster_id', array_diff($author_ary, array(ANONYMOUS)), false, true) . ' OR p.post_username ' . $author_name . ')';
indvdum@0
   859
		}
indvdum@0
   860
		else
indvdum@0
   861
		{
indvdum@0
   862
			$sql_author = $db->sql_in_set('p.poster_id', $author_ary);
indvdum@0
   863
		}
indvdum@0
   864
		$sql_fora		= (sizeof($ex_fid_ary)) ? ' AND ' . $db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '';
indvdum@0
   865
		$sql_time		= ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : '';
indvdum@0
   866
		$sql_topic_id	= ($topic_id) ? ' AND p.topic_id = ' . (int) $topic_id : '';
indvdum@0
   867
		$sql_firstpost = ($firstpost_only) ? ' AND p.post_id = t.topic_first_post_id' : '';
indvdum@0
   868
indvdum@0
   869
		// Build sql strings for sorting
indvdum@0
   870
		$sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
indvdum@0
   871
		$sql_sort_table = $sql_sort_join = '';
indvdum@0
   872
		switch ($sql_sort[0])
indvdum@0
   873
		{
indvdum@0
   874
			case 'u':
indvdum@0
   875
				$sql_sort_table	= USERS_TABLE . ' u, ';
indvdum@0
   876
				$sql_sort_join	= ' AND u.user_id = p.poster_id ';
indvdum@0
   877
			break;
indvdum@0
   878
indvdum@0
   879
			case 't':
indvdum@0
   880
				$sql_sort_table	= ($type == 'posts' && !$firstpost_only) ? TOPICS_TABLE . ' t, ' : '';
indvdum@0
   881
				$sql_sort_join	= ($type == 'posts' && !$firstpost_only) ? ' AND t.topic_id = p.topic_id ' : '';
indvdum@0
   882
			break;
indvdum@0
   883
indvdum@0
   884
			case 'f':
indvdum@0
   885
				$sql_sort_table	= FORUMS_TABLE . ' f, ';
indvdum@0
   886
				$sql_sort_join	= ' AND f.forum_id = p.forum_id ';
indvdum@0
   887
			break;
indvdum@0
   888
		}
indvdum@0
   889
indvdum@0
   890
		if (!sizeof($m_approve_fid_ary))
indvdum@0
   891
		{
indvdum@0
   892
			$m_approve_fid_sql = ' AND p.post_approved = 1';
indvdum@0
   893
		}
indvdum@0
   894
		else if ($m_approve_fid_ary == array(-1))
indvdum@0
   895
		{
indvdum@0
   896
			$m_approve_fid_sql = '';
indvdum@0
   897
		}
indvdum@0
   898
		else
indvdum@0
   899
		{
indvdum@0
   900
			$m_approve_fid_sql = ' AND (p.post_approved = 1 OR ' . $db->sql_in_set('p.forum_id', $m_approve_fid_ary, true) . ')';
indvdum@0
   901
		}
indvdum@0
   902
indvdum@0
   903
		$select = ($type == 'posts') ? 'p.post_id' : 't.topic_id';
indvdum@0
   904
		$is_mysql = false;
indvdum@0
   905
indvdum@0
   906
		// If the cache was completely empty count the results
indvdum@0
   907
		if (!$total_results)
indvdum@0
   908
		{
indvdum@0
   909
			switch ($db->sql_layer)
indvdum@0
   910
			{
indvdum@0
   911
				case 'mysql4':
indvdum@0
   912
				case 'mysqli':
indvdum@0
   913
//					$select = 'SQL_CALC_FOUND_ROWS ' . $select;
indvdum@0
   914
					$is_mysql = true;
indvdum@0
   915
				break;
indvdum@0
   916
indvdum@0
   917
				default:
indvdum@0
   918
					if ($type == 'posts')
indvdum@0
   919
					{
indvdum@0
   920
						$sql = 'SELECT COUNT(p.post_id) as total_results
indvdum@0
   921
							FROM ' . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . "
indvdum@0
   922
							WHERE $sql_author
indvdum@0
   923
								$sql_topic_id
indvdum@0
   924
								$sql_firstpost
indvdum@0
   925
								$m_approve_fid_sql
indvdum@0
   926
								$sql_fora
indvdum@0
   927
								$sql_time";
indvdum@0
   928
					}
indvdum@0
   929
					else
indvdum@0
   930
					{
indvdum@0
   931
						if ($db->sql_layer == 'sqlite')
indvdum@0
   932
						{
indvdum@0
   933
							$sql = 'SELECT COUNT(topic_id) as total_results
indvdum@0
   934
								FROM (SELECT DISTINCT t.topic_id';
indvdum@0
   935
						}
indvdum@0
   936
						else
indvdum@0
   937
						{
indvdum@0
   938
							$sql = 'SELECT COUNT(DISTINCT t.topic_id) as total_results';
indvdum@0
   939
						}
indvdum@0
   940
indvdum@0
   941
						$sql .= ' FROM ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
indvdum@0
   942
							WHERE $sql_author
indvdum@0
   943
								$sql_topic_id
indvdum@0
   944
								$sql_firstpost
indvdum@0
   945
								$m_approve_fid_sql
indvdum@0
   946
								$sql_fora
indvdum@0
   947
								AND t.topic_id = p.topic_id
indvdum@0
   948
								$sql_time" . (($db->sql_layer == 'sqlite') ? ')' : '');
indvdum@0
   949
					}
indvdum@0
   950
					$result = $db->sql_query($sql);
indvdum@0
   951
indvdum@0
   952
					$total_results = (int) $db->sql_fetchfield('total_results');
indvdum@0
   953
					$db->sql_freeresult($result);
indvdum@0
   954
indvdum@0
   955
					if (!$total_results)
indvdum@0
   956
					{
indvdum@0
   957
						return false;
indvdum@0
   958
					}
indvdum@0
   959
				break;
indvdum@0
   960
			}
indvdum@0
   961
		}
indvdum@0
   962
indvdum@0
   963
		// Build the query for really selecting the post_ids
indvdum@0
   964
		if ($type == 'posts')
indvdum@0
   965
		{
indvdum@0
   966
			$sql = "SELECT $select
indvdum@0
   967
				FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t' : '') . "
indvdum@0
   968
				WHERE $sql_author
indvdum@0
   969
					$sql_topic_id
indvdum@0
   970
					$sql_firstpost
indvdum@0
   971
					$m_approve_fid_sql
indvdum@0
   972
					$sql_fora
indvdum@0
   973
					$sql_sort_join
indvdum@0
   974
					$sql_time
indvdum@0
   975
				ORDER BY $sql_sort";
indvdum@0
   976
			$field = 'post_id';
indvdum@0
   977
		}
indvdum@0
   978
		else
indvdum@0
   979
		{
indvdum@0
   980
			$sql = "SELECT $select
indvdum@0
   981
				FROM " . $sql_sort_table . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
indvdum@0
   982
				WHERE $sql_author
indvdum@0
   983
					$sql_topic_id
indvdum@0
   984
					$sql_firstpost
indvdum@0
   985
					$m_approve_fid_sql
indvdum@0
   986
					$sql_fora
indvdum@0
   987
					AND t.topic_id = p.topic_id
indvdum@0
   988
					$sql_sort_join
indvdum@0
   989
					$sql_time
indvdum@0
   990
				GROUP BY t.topic_id, " . $sort_by_sql[$sort_key] . '
indvdum@0
   991
				ORDER BY ' . $sql_sort;
indvdum@0
   992
			$field = 'topic_id';
indvdum@0
   993
		}
indvdum@0
   994
indvdum@0
   995
		// Only read one block of posts from the db and then cache it
indvdum@0
   996
		$result = $db->sql_query_limit($sql, $config['search_block_size'], $start);
indvdum@0
   997
indvdum@0
   998
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   999
		{
indvdum@0
  1000
			$id_ary[] = (int) $row[$field];
indvdum@0
  1001
		}
indvdum@0
  1002
		$db->sql_freeresult($result);
indvdum@0
  1003
indvdum@0
  1004
		if (!$total_results && $is_mysql)
indvdum@0
  1005
		{
indvdum@0
  1006
			// Count rows for the executed queries. Replace $select within $sql with SQL_CALC_FOUND_ROWS, and run it.
indvdum@0
  1007
			$sql = str_replace('SELECT ' . $select, 'SELECT DISTINCT SQL_CALC_FOUND_ROWS p.post_id', $sql);
indvdum@0
  1008
indvdum@0
  1009
			$db->sql_query($sql);
indvdum@0
  1010
			$db->sql_freeresult($result);
indvdum@0
  1011
indvdum@0
  1012
			$sql = 'SELECT FOUND_ROWS() as total_results';
indvdum@0
  1013
			$result = $db->sql_query($sql);
indvdum@0
  1014
			$total_results = (int) $db->sql_fetchfield('total_results');
indvdum@0
  1015
			$db->sql_freeresult($result);
indvdum@0
  1016
indvdum@0
  1017
			if (!$total_results)
indvdum@0
  1018
			{
indvdum@0
  1019
				return false;
indvdum@0
  1020
			}
indvdum@0
  1021
		}
indvdum@0
  1022
indvdum@0
  1023
		if (sizeof($id_ary))
indvdum@0
  1024
		{
indvdum@0
  1025
			$this->save_ids($search_key, '', $author_ary, $total_results, $id_ary, $start, $sort_dir);
indvdum@0
  1026
			$id_ary = array_slice($id_ary, 0, $per_page);
indvdum@0
  1027
indvdum@0
  1028
			return $total_results;
indvdum@0
  1029
		}
indvdum@0
  1030
		return false;
indvdum@0
  1031
	}
indvdum@0
  1032
indvdum@0
  1033
	/**
indvdum@0
  1034
	* Split a text into words of a given length
indvdum@0
  1035
	*
indvdum@0
  1036
	* The text is converted to UTF-8, cleaned up, and split. Then, words that
indvdum@0
  1037
	* conform to the defined length range are returned in an array.
indvdum@0
  1038
	*
indvdum@0
  1039
	* NOTE: duplicates are NOT removed from the return array
indvdum@0
  1040
	*
indvdum@0
  1041
	* @param	string	$text	Text to split, encoded in UTF-8
indvdum@0
  1042
	* @return	array			Array of UTF-8 words
indvdum@0
  1043
	*
indvdum@0
  1044
	* @access	private
indvdum@0
  1045
	*/
indvdum@0
  1046
	function split_message($text)
indvdum@0
  1047
	{
indvdum@0
  1048
		global $phpbb_root_path, $phpEx, $user;
indvdum@0
  1049
indvdum@0
  1050
		$match = $words = array();
indvdum@0
  1051
indvdum@0
  1052
		/**
indvdum@0
  1053
		* Taken from the original code
indvdum@0
  1054
		*/
indvdum@0
  1055
		// Do not index code
indvdum@0
  1056
		$match[] = '#\[code(?:=.*?)?(\:?[0-9a-z]{5,})\].*?\[\/code(\:?[0-9a-z]{5,})\]#is';
indvdum@0
  1057
		// BBcode
indvdum@0
  1058
		$match[] = '#\[\/?[a-z0-9\*\+\-]+(?:=.*?)?(?::[a-z])?(\:?[0-9a-z]{5,})\]#';
indvdum@0
  1059
indvdum@0
  1060
		$min = $this->word_length['min'];
indvdum@0
  1061
		$max = $this->word_length['max'];
indvdum@0
  1062
indvdum@0
  1063
		$isset_min = $min - 1;
indvdum@0
  1064
indvdum@0
  1065
		/**
indvdum@0
  1066
		* Clean up the string, remove HTML tags, remove BBCodes
indvdum@0
  1067
		*/
indvdum@0
  1068
		$word = strtok($this->cleanup(preg_replace($match, ' ', strip_tags($text)), -1), ' ');
indvdum@0
  1069
indvdum@0
  1070
		while (strlen($word))
indvdum@0
  1071
		{
indvdum@0
  1072
			if (strlen($word) > 255 || strlen($word) <= $isset_min)
indvdum@0
  1073
			{
indvdum@0
  1074
				/**
indvdum@0
  1075
				* Words longer than 255 bytes are ignored. This will have to be
indvdum@0
  1076
				* changed whenever we change the length of search_wordlist.word_text
indvdum@0
  1077
				*
indvdum@0
  1078
				* Words shorter than $isset_min bytes are ignored, too
indvdum@0
  1079
				*/
indvdum@0
  1080
				$word = strtok(' ');
indvdum@0
  1081
				continue;
indvdum@0
  1082
			}
indvdum@0
  1083
indvdum@0
  1084
			$len = utf8_strlen($word);
indvdum@0
  1085
indvdum@0
  1086
			/**
indvdum@0
  1087
			* Test whether the word is too short to be indexed.
indvdum@0
  1088
			*
indvdum@0
  1089
			* Note that this limit does NOT apply to CJK and Hangul
indvdum@0
  1090
			*/
indvdum@0
  1091
			if ($len < $min)
indvdum@0
  1092
			{
indvdum@0
  1093
				/**
indvdum@0
  1094
				* Note: this could be optimized. If the codepoint is lower than Hangul's range
indvdum@0
  1095
				* we know that it will also be lower than CJK ranges
indvdum@0
  1096
				*/
indvdum@0
  1097
				if ((strncmp($word, UTF8_HANGUL_FIRST, 3) < 0 || strncmp($word, UTF8_HANGUL_LAST, 3) > 0)
indvdum@0
  1098
				 && (strncmp($word, UTF8_CJK_FIRST, 3) < 0 || strncmp($word, UTF8_CJK_LAST, 3) > 0)
indvdum@0
  1099
				 && (strncmp($word, UTF8_CJK_B_FIRST, 4) < 0 || strncmp($word, UTF8_CJK_B_LAST, 4) > 0))
indvdum@0
  1100
				{
indvdum@0
  1101
					$word = strtok(' ');
indvdum@0
  1102
					continue;
indvdum@0
  1103
				}
indvdum@0
  1104
			}
indvdum@0
  1105
indvdum@0
  1106
			$words[] = $word;
indvdum@0
  1107
			$word = strtok(' ');
indvdum@0
  1108
		}
indvdum@0
  1109
indvdum@0
  1110
		return $words;
indvdum@0
  1111
	}
indvdum@0
  1112
indvdum@0
  1113
	/**
indvdum@0
  1114
	* Updates wordlist and wordmatch tables when a message is posted or changed
indvdum@0
  1115
	*
indvdum@0
  1116
	* @param	string	$mode		Contains the post mode: edit, post, reply, quote
indvdum@0
  1117
	* @param	int		$post_id	The id of the post which is modified/created
indvdum@0
  1118
	* @param	string	&$message	New or updated post content
indvdum@0
  1119
	* @param	string	&$subject	New or updated post subject
indvdum@0
  1120
	* @param	int		$poster_id	Post author's user id
indvdum@0
  1121
	* @param	int		$forum_id	The id of the forum in which the post is located
indvdum@0
  1122
	*
indvdum@0
  1123
	* @access	public
indvdum@0
  1124
	*/
indvdum@0
  1125
	function index($mode, $post_id, &$message, &$subject, $poster_id, $forum_id)
indvdum@0
  1126
	{
indvdum@0
  1127
		global $config, $db, $user;
indvdum@0
  1128
indvdum@0
  1129
		if (!$config['fulltext_native_load_upd'])
indvdum@0
  1130
		{
indvdum@0
  1131
			/**
indvdum@0
  1132
			* The search indexer is disabled, return
indvdum@0
  1133
			*/
indvdum@0
  1134
			return;
indvdum@0
  1135
		}
indvdum@0
  1136
indvdum@0
  1137
		// Split old and new post/subject to obtain array of 'words'
indvdum@0
  1138
		$split_text = $this->split_message($message);
indvdum@0
  1139
		$split_title = $this->split_message($subject);
indvdum@0
  1140
indvdum@0
  1141
		$cur_words = array('post' => array(), 'title' => array());
indvdum@0
  1142
indvdum@0
  1143
		$words = array();
indvdum@0
  1144
		if ($mode == 'edit')
indvdum@0
  1145
		{
indvdum@0
  1146
			$words['add']['post'] = array();
indvdum@0
  1147
			$words['add']['title'] = array();
indvdum@0
  1148
			$words['del']['post'] = array();
indvdum@0
  1149
			$words['del']['title'] = array();
indvdum@0
  1150
indvdum@0
  1151
			$sql = 'SELECT w.word_id, w.word_text, m.title_match
indvdum@0
  1152
				FROM ' . SEARCH_WORDLIST_TABLE . ' w, ' . SEARCH_WORDMATCH_TABLE . " m
indvdum@0
  1153
				WHERE m.post_id = $post_id
indvdum@0
  1154
					AND w.word_id = m.word_id";
indvdum@0
  1155
			$result = $db->sql_query($sql);
indvdum@0
  1156
indvdum@0
  1157
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  1158
			{
indvdum@0
  1159
				$which = ($row['title_match']) ? 'title' : 'post';
indvdum@0
  1160
				$cur_words[$which][$row['word_text']] = $row['word_id'];
indvdum@0
  1161
			}
indvdum@0
  1162
			$db->sql_freeresult($result);
indvdum@0
  1163
indvdum@0
  1164
			$words['add']['post'] = array_diff($split_text, array_keys($cur_words['post']));
indvdum@0
  1165
			$words['add']['title'] = array_diff($split_title, array_keys($cur_words['title']));
indvdum@0
  1166
			$words['del']['post'] = array_diff(array_keys($cur_words['post']), $split_text);
indvdum@0
  1167
			$words['del']['title'] = array_diff(array_keys($cur_words['title']), $split_title);
indvdum@0
  1168
		}
indvdum@0
  1169
		else
indvdum@0
  1170
		{
indvdum@0
  1171
			$words['add']['post'] = $split_text;
indvdum@0
  1172
			$words['add']['title'] = $split_title;
indvdum@0
  1173
			$words['del']['post'] = array();
indvdum@0
  1174
			$words['del']['title'] = array();
indvdum@0
  1175
		}
indvdum@0
  1176
		unset($split_text);
indvdum@0
  1177
		unset($split_title);
indvdum@0
  1178
indvdum@0
  1179
		// Get unique words from the above arrays
indvdum@0
  1180
		$unique_add_words = array_unique(array_merge($words['add']['post'], $words['add']['title']));
indvdum@0
  1181
indvdum@0
  1182
		// We now have unique arrays of all words to be added and removed and
indvdum@0
  1183
		// individual arrays of added and removed words for text and title. What
indvdum@0
  1184
		// we need to do now is add the new words (if they don't already exist)
indvdum@0
  1185
		// and then add (or remove) matches between the words and this post
indvdum@0
  1186
		if (sizeof($unique_add_words))
indvdum@0
  1187
		{
indvdum@0
  1188
			$sql = 'SELECT word_id, word_text
indvdum@0
  1189
				FROM ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
  1190
				WHERE ' . $db->sql_in_set('word_text', $unique_add_words);
indvdum@0
  1191
			$result = $db->sql_query($sql);
indvdum@0
  1192
indvdum@0
  1193
			$word_ids = array();
indvdum@0
  1194
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  1195
			{
indvdum@0
  1196
				$word_ids[$row['word_text']] = $row['word_id'];
indvdum@0
  1197
			}
indvdum@0
  1198
			$db->sql_freeresult($result);
indvdum@0
  1199
			$new_words = array_diff($unique_add_words, array_keys($word_ids));
indvdum@0
  1200
indvdum@0
  1201
			$db->sql_transaction('begin');
indvdum@0
  1202
			if (sizeof($new_words))
indvdum@0
  1203
			{
indvdum@0
  1204
				$sql_ary = array();
indvdum@0
  1205
indvdum@0
  1206
				foreach ($new_words as $word)
indvdum@0
  1207
				{
indvdum@0
  1208
					$sql_ary[] = array('word_text' => (string) $word, 'word_count' => 0);
indvdum@0
  1209
				}
indvdum@0
  1210
				$db->sql_return_on_error(true);
indvdum@0
  1211
				$db->sql_multi_insert(SEARCH_WORDLIST_TABLE, $sql_ary);
indvdum@0
  1212
				$db->sql_return_on_error(false);
indvdum@0
  1213
			}
indvdum@0
  1214
			unset($new_words, $sql_ary);
indvdum@0
  1215
		}
indvdum@0
  1216
		else
indvdum@0
  1217
		{
indvdum@0
  1218
			$db->sql_transaction('begin');
indvdum@0
  1219
		}
indvdum@0
  1220
indvdum@0
  1221
		// now update the search match table, remove links to removed words and add links to new words
indvdum@0
  1222
		foreach ($words['del'] as $word_in => $word_ary)
indvdum@0
  1223
		{
indvdum@0
  1224
			$title_match = ($word_in == 'title') ? 1 : 0;
indvdum@0
  1225
indvdum@0
  1226
			if (sizeof($word_ary))
indvdum@0
  1227
			{
indvdum@0
  1228
				$sql_in = array();
indvdum@0
  1229
				foreach ($word_ary as $word)
indvdum@0
  1230
				{
indvdum@0
  1231
					$sql_in[] = $cur_words[$word_in][$word];
indvdum@0
  1232
				}
indvdum@0
  1233
indvdum@0
  1234
				$sql = 'DELETE FROM ' . SEARCH_WORDMATCH_TABLE . '
indvdum@0
  1235
					WHERE ' . $db->sql_in_set('word_id', $sql_in) . '
indvdum@0
  1236
						AND post_id = ' . intval($post_id) . "
indvdum@0
  1237
						AND title_match = $title_match";
indvdum@0
  1238
				$db->sql_query($sql);
indvdum@0
  1239
indvdum@0
  1240
				$sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
  1241
					SET word_count = word_count - 1
indvdum@0
  1242
					WHERE ' . $db->sql_in_set('word_id', $sql_in) . '
indvdum@0
  1243
						AND word_count > 0';
indvdum@0
  1244
				$db->sql_query($sql);
indvdum@0
  1245
indvdum@0
  1246
				unset($sql_in);
indvdum@0
  1247
			}
indvdum@0
  1248
		}
indvdum@0
  1249
indvdum@0
  1250
		$db->sql_return_on_error(true);
indvdum@0
  1251
		foreach ($words['add'] as $word_in => $word_ary)
indvdum@0
  1252
		{
indvdum@0
  1253
			$title_match = ($word_in == 'title') ? 1 : 0;
indvdum@0
  1254
indvdum@0
  1255
			if (sizeof($word_ary))
indvdum@0
  1256
			{
indvdum@0
  1257
				$sql = 'INSERT INTO ' . SEARCH_WORDMATCH_TABLE . ' (post_id, word_id, title_match)
indvdum@0
  1258
					SELECT ' . (int) $post_id . ', word_id, ' . (int) $title_match . '
indvdum@0
  1259
					FROM ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
  1260
					WHERE ' . $db->sql_in_set('word_text', $word_ary);
indvdum@0
  1261
				$db->sql_query($sql);
indvdum@0
  1262
indvdum@0
  1263
				$sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
  1264
					SET word_count = word_count + 1
indvdum@0
  1265
					WHERE ' . $db->sql_in_set('word_text', $word_ary);
indvdum@0
  1266
				$db->sql_query($sql);
indvdum@0
  1267
			}
indvdum@0
  1268
		}
indvdum@0
  1269
		$db->sql_return_on_error(false);
indvdum@0
  1270
indvdum@0
  1271
		$db->sql_transaction('commit');
indvdum@0
  1272
indvdum@0
  1273
		// destroy cached search results containing any of the words removed or added
indvdum@0
  1274
		$this->destroy_cache(array_unique(array_merge($words['add']['post'], $words['add']['title'], $words['del']['post'], $words['del']['title'])), array($poster_id));
indvdum@0
  1275
indvdum@0
  1276
		unset($unique_add_words);
indvdum@0
  1277
		unset($words);
indvdum@0
  1278
		unset($cur_words);
indvdum@0
  1279
	}
indvdum@0
  1280
indvdum@0
  1281
	/**
indvdum@0
  1282
	* Removes entries from the wordmatch table for the specified post_ids
indvdum@0
  1283
	*/
indvdum@0
  1284
	function index_remove($post_ids, $author_ids, $forum_ids)
indvdum@0
  1285
	{
indvdum@0
  1286
		global $db;
indvdum@0
  1287
indvdum@0
  1288
		if (sizeof($post_ids))
indvdum@0
  1289
		{
indvdum@0
  1290
			$sql = 'SELECT w.word_id, w.word_text, m.title_match
indvdum@0
  1291
				FROM ' . SEARCH_WORDMATCH_TABLE . ' m, ' . SEARCH_WORDLIST_TABLE . ' w
indvdum@0
  1292
				WHERE ' . $db->sql_in_set('m.post_id', $post_ids) . '
indvdum@0
  1293
					AND w.word_id = m.word_id';
indvdum@0
  1294
			$result = $db->sql_query($sql);
indvdum@0
  1295
indvdum@0
  1296
			$message_word_ids = $title_word_ids = $word_texts = array();
indvdum@0
  1297
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  1298
			{
indvdum@0
  1299
				if ($row['title_match'])
indvdum@0
  1300
				{
indvdum@0
  1301
					$title_word_ids[] = $row['word_id'];
indvdum@0
  1302
				}
indvdum@0
  1303
				else
indvdum@0
  1304
				{
indvdum@0
  1305
					$message_word_ids[] = $row['word_id'];
indvdum@0
  1306
				}
indvdum@0
  1307
				$word_texts[] = $row['word_text'];
indvdum@0
  1308
			}
indvdum@0
  1309
			$db->sql_freeresult($result);
indvdum@0
  1310
indvdum@0
  1311
			if (sizeof($title_word_ids))
indvdum@0
  1312
			{
indvdum@0
  1313
				$sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
  1314
					SET word_count = word_count - 1
indvdum@0
  1315
					WHERE ' . $db->sql_in_set('word_id', $title_word_ids) . '
indvdum@0
  1316
						AND word_count > 0';
indvdum@0
  1317
				$db->sql_query($sql);
indvdum@0
  1318
			}
indvdum@0
  1319
indvdum@0
  1320
			if (sizeof($message_word_ids))
indvdum@0
  1321
			{
indvdum@0
  1322
				$sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
  1323
					SET word_count = word_count - 1
indvdum@0
  1324
					WHERE ' . $db->sql_in_set('word_id', $message_word_ids) . '
indvdum@0
  1325
						AND word_count > 0';
indvdum@0
  1326
				$db->sql_query($sql);
indvdum@0
  1327
			}
indvdum@0
  1328
indvdum@0
  1329
			unset($title_word_ids);
indvdum@0
  1330
			unset($message_word_ids);
indvdum@0
  1331
indvdum@0
  1332
			$sql = 'DELETE FROM ' . SEARCH_WORDMATCH_TABLE . '
indvdum@0
  1333
				WHERE ' . $db->sql_in_set('post_id', $post_ids);
indvdum@0
  1334
			$db->sql_query($sql);
indvdum@0
  1335
		}
indvdum@0
  1336
indvdum@0
  1337
		$this->destroy_cache(array_unique($word_texts), $author_ids);
indvdum@0
  1338
	}
indvdum@0
  1339
indvdum@0
  1340
	/**
indvdum@0
  1341
	* Tidy up indexes: Tag 'common words' and remove
indvdum@0
  1342
	* words no longer referenced in the match table
indvdum@0
  1343
	*/
indvdum@0
  1344
	function tidy()
indvdum@0
  1345
	{
indvdum@0
  1346
		global $db, $config;
indvdum@0
  1347
indvdum@0
  1348
		// Is the fulltext indexer disabled? If yes then we need not
indvdum@0
  1349
		// carry on ... it's okay ... I know when I'm not wanted boo hoo
indvdum@0
  1350
		if (!$config['fulltext_native_load_upd'])
indvdum@0
  1351
		{
indvdum@0
  1352
			set_config('search_last_gc', time(), true);
indvdum@0
  1353
			return;
indvdum@0
  1354
		}
indvdum@0
  1355
indvdum@0
  1356
		$destroy_cache_words = array();
indvdum@0
  1357
indvdum@0
  1358
		// Remove common words
indvdum@0
  1359
		if ($config['num_posts'] >= 100 && $config['fulltext_native_common_thres'])
indvdum@0
  1360
		{
indvdum@0
  1361
			$common_threshold = ((double) $config['fulltext_native_common_thres']) / 100.0;
indvdum@0
  1362
			// First, get the IDs of common words
indvdum@0
  1363
			$sql = 'SELECT word_id, word_text
indvdum@0
  1364
				FROM ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
  1365
				WHERE word_count > ' . floor($config['num_posts'] * $common_threshold) . '
indvdum@0
  1366
					OR word_common = 1';
indvdum@0
  1367
			$result = $db->sql_query($sql);
indvdum@0
  1368
indvdum@0
  1369
			$sql_in = array();
indvdum@0
  1370
			while ($row = $db->sql_fetchrow($result))
indvdum@0
  1371
			{
indvdum@0
  1372
				$sql_in[] = $row['word_id'];
indvdum@0
  1373
				$destroy_cache_words[] = $row['word_text'];
indvdum@0
  1374
			}
indvdum@0
  1375
			$db->sql_freeresult($result);
indvdum@0
  1376
indvdum@0
  1377
			if (sizeof($sql_in))
indvdum@0
  1378
			{
indvdum@0
  1379
				// Flag the words
indvdum@0
  1380
				$sql = 'UPDATE ' . SEARCH_WORDLIST_TABLE . '
indvdum@0
  1381
					SET word_common = 1
indvdum@0
  1382
					WHERE ' . $db->sql_in_set('word_id', $sql_in);
indvdum@0
  1383
				$db->sql_query($sql);
indvdum@0
  1384
indvdum@0
  1385
				// by setting search_last_gc to the new time here we make sure that if a user reloads because the
indvdum@0
  1386
				// following query takes too long, he won't run into it again
indvdum@0
  1387
				set_config('search_last_gc', time(), true);
indvdum@0
  1388
indvdum@0
  1389
				// Delete the matches
indvdum@0
  1390
				$sql = 'DELETE FROM ' . SEARCH_WORDMATCH_TABLE . '
indvdum@0
  1391
					WHERE ' . $db->sql_in_set('word_id', $sql_in);
indvdum@0
  1392
				$db->sql_query($sql);
indvdum@0
  1393
			}
indvdum@0
  1394
			unset($sql_in);
indvdum@0
  1395
		}
indvdum@0
  1396
indvdum@0
  1397
		if (sizeof($destroy_cache_words))
indvdum@0
  1398
		{
indvdum@0
  1399
			// destroy cached search results containing any of the words that are now common or were removed
indvdum@0
  1400
			$this->destroy_cache(array_unique($destroy_cache_words));
indvdum@0
  1401
		}
indvdum@0
  1402
indvdum@0
  1403
		set_config('search_last_gc', time(), true);
indvdum@0
  1404
	}
indvdum@0
  1405
indvdum@0
  1406
	/**
indvdum@0
  1407
	* Deletes all words from the index
indvdum@0
  1408
	*/
indvdum@0
  1409
	function delete_index($acp_module, $u_action)
indvdum@0
  1410
	{
indvdum@0
  1411
		global $db;
indvdum@0
  1412
indvdum@0
  1413
		switch ($db->sql_layer)
indvdum@0
  1414
		{
indvdum@0
  1415
			case 'sqlite':
indvdum@0
  1416
			case 'firebird':
indvdum@0
  1417
				$db->sql_query('DELETE FROM ' . SEARCH_WORDLIST_TABLE);
indvdum@0
  1418
				$db->sql_query('DELETE FROM ' . SEARCH_WORDMATCH_TABLE);
indvdum@0
  1419
				$db->sql_query('DELETE FROM ' . SEARCH_RESULTS_TABLE);
indvdum@0
  1420
			break;
indvdum@0
  1421
indvdum@0
  1422
			default:
indvdum@0
  1423
				$db->sql_query('TRUNCATE TABLE ' . SEARCH_WORDLIST_TABLE);
indvdum@0
  1424
				$db->sql_query('TRUNCATE TABLE ' . SEARCH_WORDMATCH_TABLE);
indvdum@0
  1425
				$db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
indvdum@0
  1426
			break;
indvdum@0
  1427
		}
indvdum@0
  1428
	}
indvdum@0
  1429
indvdum@0
  1430
	/**
indvdum@0
  1431
	* Returns true if both FULLTEXT indexes exist
indvdum@0
  1432
	*/
indvdum@0
  1433
	function index_created()
indvdum@0
  1434
	{
indvdum@0
  1435
		if (!sizeof($this->stats))
indvdum@0
  1436
		{
indvdum@0
  1437
			$this->get_stats();
indvdum@0
  1438
		}
indvdum@0
  1439
indvdum@0
  1440
		return ($this->stats['total_words'] && $this->stats['total_matches']) ? true : false;
indvdum@0
  1441
	}
indvdum@0
  1442
indvdum@0
  1443
	/**
indvdum@0
  1444
	* Returns an associative array containing information about the indexes
indvdum@0
  1445
	*/
indvdum@0
  1446
	function index_stats()
indvdum@0
  1447
	{
indvdum@0
  1448
		global $user;
indvdum@0
  1449
indvdum@0
  1450
		if (!sizeof($this->stats))
indvdum@0
  1451
		{
indvdum@0
  1452
			$this->get_stats();
indvdum@0
  1453
		}
indvdum@0
  1454
indvdum@0
  1455
		return array(
indvdum@0
  1456
			$user->lang['TOTAL_WORDS']		=> $this->stats['total_words'],
indvdum@0
  1457
			$user->lang['TOTAL_MATCHES']	=> $this->stats['total_matches']);
indvdum@0
  1458
	}
indvdum@0
  1459
indvdum@0
  1460
	function get_stats()
indvdum@0
  1461
	{
indvdum@0
  1462
		global $db;
indvdum@0
  1463
indvdum@0
  1464
		$sql = 'SELECT COUNT(*) as total_words
indvdum@0
  1465
			FROM ' . SEARCH_WORDLIST_TABLE;
indvdum@0
  1466
		$result = $db->sql_query($sql);
indvdum@0
  1467
		$this->stats['total_words'] = (int) $db->sql_fetchfield('total_words');
indvdum@0
  1468
		$db->sql_freeresult($result);
indvdum@0
  1469
indvdum@0
  1470
		$sql = 'SELECT COUNT(*) as total_matches
indvdum@0
  1471
			FROM ' . SEARCH_WORDMATCH_TABLE;
indvdum@0
  1472
		$result = $db->sql_query($sql);
indvdum@0
  1473
		$this->stats['total_matches'] = (int) $db->sql_fetchfield('total_matches');
indvdum@0
  1474
		$db->sql_freeresult($result);
indvdum@0
  1475
	}
indvdum@0
  1476
indvdum@0
  1477
	/**
indvdum@0
  1478
	* Clean up a text to remove non-alphanumeric characters
indvdum@0
  1479
	*
indvdum@0
  1480
	* This method receives a UTF-8 string, normalizes and validates it, replaces all
indvdum@0
  1481
	* non-alphanumeric characters with strings then returns the result.
indvdum@0
  1482
	*
indvdum@0
  1483
	* Any number of "allowed chars" can be passed as a UTF-8 string in NFC.
indvdum@0
  1484
	*
indvdum@0
  1485
	* @param	string	$text			Text to split, in UTF-8 (not normalized or sanitized)
indvdum@0
  1486
	* @param	string	$allowed_chars	String of special chars to allow
indvdum@0
  1487
	* @param	string	$encoding		Text encoding
indvdum@0
  1488
	* @return	string					Cleaned up text, only alphanumeric chars are left
indvdum@0
  1489
	*
indvdum@0
  1490
	* @todo normalizer::cleanup being able to be used?
indvdum@0
  1491
	*/
indvdum@0
  1492
	function cleanup($text, $allowed_chars = null, $encoding = 'utf-8')
indvdum@0
  1493
	{
indvdum@0
  1494
		global $phpbb_root_path, $phpEx;
indvdum@0
  1495
		static $conv = array(), $conv_loaded = array();
indvdum@0
  1496
		$words = $allow = array();
indvdum@0
  1497
indvdum@0
  1498
		// Convert the text to UTF-8
indvdum@0
  1499
		$encoding = strtolower($encoding);
indvdum@0
  1500
		if ($encoding != 'utf-8')
indvdum@0
  1501
		{
indvdum@0
  1502
			$text = utf8_recode($text, $encoding);
indvdum@0
  1503
		}
indvdum@0
  1504
indvdum@0
  1505
		$utf_len_mask = array(
indvdum@0
  1506
			"\xC0"	=>	2,
indvdum@0
  1507
			"\xD0"	=>	2,
indvdum@0
  1508
			"\xE0"	=>	3,
indvdum@0
  1509
			"\xF0"	=>	4
indvdum@0
  1510
		);
indvdum@0
  1511
indvdum@0
  1512
		/**
indvdum@0
  1513
		* Replace HTML entities and NCRs
indvdum@0
  1514
		*/
indvdum@0
  1515
		$text = htmlspecialchars_decode(utf8_decode_ncr($text), ENT_QUOTES);
indvdum@0
  1516
indvdum@0
  1517
		/**
indvdum@0
  1518
		* Load the UTF-8 normalizer
indvdum@0
  1519
		*
indvdum@0
  1520
		* If we use it more widely, an instance of that class should be held in a
indvdum@0
  1521
		* a global variable instead
indvdum@0
  1522
		*/
indvdum@0
  1523
		utf_normalizer::nfc($text);
indvdum@0
  1524
indvdum@0
  1525
		/**
indvdum@0
  1526
		* The first thing we do is:
indvdum@0
  1527
		*
indvdum@0
  1528
		* - convert ASCII-7 letters to lowercase
indvdum@0
  1529
		* - remove the ASCII-7 non-alpha characters
indvdum@0
  1530
		* - remove the bytes that should not appear in a valid UTF-8 string: 0xC0,
indvdum@0
  1531
		*   0xC1 and 0xF5-0xFF
indvdum@0
  1532
		*
indvdum@0
  1533
		* @todo in theory, the third one is already taken care of during normalization and those chars should have been replaced by Unicode replacement chars
indvdum@0
  1534
		*/
indvdum@0
  1535
		$sb_match	= "ISTCPAMELRDOJBNHFGVWUQKYXZ\r\n\t!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~\x00\x01\x02\x03\x04\x05\x06\x07\x08\x0B\x0C\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\xC0\xC1\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";
indvdum@0
  1536
		$sb_replace	= 'istcpamelrdojbnhfgvwuqkyxz                                                                              ';
indvdum@0
  1537
indvdum@0
  1538
		/**
indvdum@0
  1539
		* This is the list of legal ASCII chars, it is automatically extended
indvdum@0
  1540
		* with ASCII chars from $allowed_chars
indvdum@0
  1541
		*/
indvdum@0
  1542
		$legal_ascii = ' eaisntroludcpmghbfvq10xy2j9kw354867z';
indvdum@0
  1543
indvdum@0
  1544
		/**
indvdum@0
  1545
		* Prepare an array containing the extra chars to allow
indvdum@0
  1546
		*/
indvdum@0
  1547
		if (isset($allowed_chars[0]))
indvdum@0
  1548
		{
indvdum@0
  1549
			$pos = 0;
indvdum@0
  1550
			$len = strlen($allowed_chars);
indvdum@0
  1551
			do
indvdum@0
  1552
			{
indvdum@0
  1553
				$c = $allowed_chars[$pos];
indvdum@0
  1554
indvdum@0
  1555
				if ($c < "\x80")
indvdum@0
  1556
				{
indvdum@0
  1557
					/**
indvdum@0
  1558
					* ASCII char
indvdum@0
  1559
					*/
indvdum@0
  1560
					$sb_pos = strpos($sb_match, $c);
indvdum@0
  1561
					if (is_int($sb_pos))
indvdum@0
  1562
					{
indvdum@0
  1563
						/**
indvdum@0
  1564
						* Remove the char from $sb_match and its corresponding
indvdum@0
  1565
						* replacement in $sb_replace
indvdum@0
  1566
						*/
indvdum@0
  1567
						$sb_match = substr($sb_match, 0, $sb_pos) . substr($sb_match, $sb_pos + 1);
indvdum@0
  1568
						$sb_replace = substr($sb_replace, 0, $sb_pos) . substr($sb_replace, $sb_pos + 1);
indvdum@0
  1569
						$legal_ascii .= $c;
indvdum@0
  1570
					}
indvdum@0
  1571
indvdum@0
  1572
					++$pos;
indvdum@0
  1573
				}
indvdum@0
  1574
				else
indvdum@0
  1575
				{
indvdum@0
  1576
					/**
indvdum@0
  1577
					* UTF-8 char
indvdum@0
  1578
					*/
indvdum@0
  1579
					$utf_len = $utf_len_mask[$c & "\xF0"];
indvdum@0
  1580
					$allow[substr($allowed_chars, $pos, $utf_len)] = 1;
indvdum@0
  1581
					$pos += $utf_len;
indvdum@0
  1582
				}
indvdum@0
  1583
			}
indvdum@0
  1584
			while ($pos < $len);
indvdum@0
  1585
		}
indvdum@0
  1586
indvdum@0
  1587
		$text = strtr($text, $sb_match, $sb_replace);
indvdum@0
  1588
		$ret = '';
indvdum@0
  1589
indvdum@0
  1590
		$pos = 0;
indvdum@0
  1591
		$len = strlen($text);
indvdum@0
  1592
indvdum@0
  1593
		do
indvdum@0
  1594
		{
indvdum@0
  1595
			/**
indvdum@0
  1596
			* Do all consecutive ASCII chars at once
indvdum@0
  1597
			*/
indvdum@0
  1598
			if ($spn = strspn($text, $legal_ascii, $pos))
indvdum@0
  1599
			{
indvdum@0
  1600
				$ret .= substr($text, $pos, $spn);
indvdum@0
  1601
				$pos += $spn;
indvdum@0
  1602
			}
indvdum@0
  1603
indvdum@0
  1604
			if ($pos >= $len)
indvdum@0
  1605
			{
indvdum@0
  1606
				return $ret;
indvdum@0
  1607
			}
indvdum@0
  1608
indvdum@0
  1609
			/**
indvdum@0
  1610
			* Capture the UTF char
indvdum@0
  1611
			*/
indvdum@0
  1612
			$utf_len = $utf_len_mask[$text[$pos] & "\xF0"];
indvdum@0
  1613
			$utf_char = substr($text, $pos, $utf_len);
indvdum@0
  1614
			$pos += $utf_len;
indvdum@0
  1615
indvdum@0
  1616
			if (($utf_char >= UTF8_HANGUL_FIRST && $utf_char <= UTF8_HANGUL_LAST)
indvdum@0
  1617
			 || ($utf_char >= UTF8_CJK_FIRST && $utf_char <= UTF8_CJK_LAST)
indvdum@0
  1618
			 || ($utf_char >= UTF8_CJK_B_FIRST && $utf_char <= UTF8_CJK_B_LAST))
indvdum@0
  1619
			{
indvdum@0
  1620
				/**
indvdum@0
  1621
				* All characters within these ranges are valid
indvdum@0
  1622
				*
indvdum@0
  1623
				* We separate them with a space in order to index each character
indvdum@0
  1624
				* individually
indvdum@0
  1625
				*/
indvdum@0
  1626
				$ret .= ' ' . $utf_char . ' ';
indvdum@0
  1627
				continue;
indvdum@0
  1628
			}
indvdum@0
  1629
indvdum@0
  1630
			if (isset($allow[$utf_char]))
indvdum@0
  1631
			{
indvdum@0
  1632
				/**
indvdum@0
  1633
				* The char is explicitly allowed
indvdum@0
  1634
				*/
indvdum@0
  1635
				$ret .= $utf_char;
indvdum@0
  1636
				continue;
indvdum@0
  1637
			}
indvdum@0
  1638
indvdum@0
  1639
			if (isset($conv[$utf_char]))
indvdum@0
  1640
			{
indvdum@0
  1641
				/**
indvdum@0
  1642
				* The char is mapped to something, maybe to itself actually
indvdum@0
  1643
				*/
indvdum@0
  1644
				$ret .= $conv[$utf_char];
indvdum@0
  1645
				continue;
indvdum@0
  1646
			}
indvdum@0
  1647
indvdum@0
  1648
			/**
indvdum@0
  1649
			* The char isn't mapped, but did we load its conversion table?
indvdum@0
  1650
			*
indvdum@0
  1651
			* The search indexer table is split into blocks. The block number of
indvdum@0
  1652
			* each char is equal to its codepoint right-shifted for 11 bits. It
indvdum@0
  1653
			* means that out of the 11, 16 or 21 meaningful bits of a 2-, 3- or
indvdum@0
  1654
			* 4- byte sequence we only keep the leftmost 0, 5 or 10 bits. Thus,
indvdum@0
  1655
			* all UTF chars encoded in 2 bytes are in the same first block.
indvdum@0
  1656
			*/
indvdum@0
  1657
			if (isset($utf_char[2]))
indvdum@0
  1658
			{
indvdum@0
  1659
				if (isset($utf_char[3]))
indvdum@0
  1660
				{
indvdum@0
  1661
					/**
indvdum@0
  1662
					* 1111 0nnn 10nn nnnn 10nx xxxx 10xx xxxx
indvdum@0
  1663
					* 0000 0111 0011 1111 0010 0000
indvdum@0
  1664
					*/
indvdum@0
  1665
					$idx = ((ord($utf_char[0]) & 0x07) << 7) | ((ord($utf_char[1]) & 0x3F) << 1) | ((ord($utf_char[2]) & 0x20) >> 5);
indvdum@0
  1666
				}
indvdum@0
  1667
				else
indvdum@0
  1668
				{
indvdum@0
  1669
					/**
indvdum@0
  1670
					* 1110 nnnn 10nx xxxx 10xx xxxx
indvdum@0
  1671
					* 0000 0111 0010 0000
indvdum@0
  1672
					*/
indvdum@0
  1673
					$idx = ((ord($utf_char[0]) & 0x07) << 1) | ((ord($utf_char[1]) & 0x20) >> 5);
indvdum@0
  1674
				}
indvdum@0
  1675
			}
indvdum@0
  1676
			else
indvdum@0
  1677
			{
indvdum@0
  1678
				/**
indvdum@0
  1679
				* 110x xxxx 10xx xxxx
indvdum@0
  1680
				* 0000 0000 0000 0000
indvdum@0
  1681
				*/
indvdum@0
  1682
				$idx = 0;
indvdum@0
  1683
			}
indvdum@0
  1684
indvdum@0
  1685
			/**
indvdum@0
  1686
			* Check if the required conv table has been loaded already
indvdum@0
  1687
			*/
indvdum@0
  1688
			if (!isset($conv_loaded[$idx]))
indvdum@0
  1689
			{
indvdum@0
  1690
				$conv_loaded[$idx] = 1;
indvdum@0
  1691
				$file = $phpbb_root_path . 'includes/utf/data/search_indexer_' . $idx . '.' . $phpEx;
indvdum@0
  1692
indvdum@0
  1693
				if (file_exists($file))
indvdum@0
  1694
				{
indvdum@0
  1695
					$conv += include($file);
indvdum@0
  1696
				}
indvdum@0
  1697
			}
indvdum@0
  1698
indvdum@0
  1699
			if (isset($conv[$utf_char]))
indvdum@0
  1700
			{
indvdum@0
  1701
				$ret .= $conv[$utf_char];
indvdum@0
  1702
			}
indvdum@0
  1703
			else
indvdum@0
  1704
			{
indvdum@0
  1705
				/**
indvdum@0
  1706
				* We add an entry to the conversion table so that we
indvdum@0
  1707
				* don't have to convert to codepoint and perform the checks
indvdum@0
  1708
				* that are above this block
indvdum@0
  1709
				*/
indvdum@0
  1710
				$conv[$utf_char] = ' ';
indvdum@0
  1711
				$ret .= ' ';
indvdum@0
  1712
			}
indvdum@0
  1713
		}
indvdum@0
  1714
		while (1);
indvdum@0
  1715
indvdum@0
  1716
		return $ret;
indvdum@0
  1717
	}
indvdum@0
  1718
indvdum@0
  1719
	/**
indvdum@0
  1720
	* Returns a list of options for the ACP to display
indvdum@0
  1721
	*/
indvdum@0
  1722
	function acp()
indvdum@0
  1723
	{
indvdum@0
  1724
		global $user, $config;
indvdum@0
  1725
indvdum@0
  1726
indvdum@0
  1727
		/**
indvdum@0
  1728
		* if we need any options, copied from fulltext_native for now, will have to be adjusted or removed
indvdum@0
  1729
		*/
indvdum@0
  1730
indvdum@0
  1731
		$tpl = '
indvdum@0
  1732
		<dl>
indvdum@0
  1733
			<dt><label for="fulltext_native_load_upd">' . $user->lang['YES_SEARCH_UPDATE'] . ':</label><br /><span>' . $user->lang['YES_SEARCH_UPDATE_EXPLAIN'] . '</span></dt>
indvdum@0
  1734
			<dd><label><input type="radio" id="fulltext_native_load_upd" name="config[fulltext_native_load_upd]" value="1"' . (($config['fulltext_native_load_upd']) ? ' checked="checked"' : '') . ' class="radio" /> ' . $user->lang['YES'] . '</label><label><input type="radio" name="config[fulltext_native_load_upd]" value="0"' . ((!$config['fulltext_native_load_upd']) ? ' checked="checked"' : '') . ' class="radio" /> ' . $user->lang['NO'] . '</label></dd>
indvdum@0
  1735
		</dl>
indvdum@0
  1736
		<dl>
indvdum@0
  1737
			<dt><label for="fulltext_native_min_chars">' . $user->lang['MIN_SEARCH_CHARS'] . ':</label><br /><span>' . $user->lang['MIN_SEARCH_CHARS_EXPLAIN'] . '</span></dt>
indvdum@0
  1738
			<dd><input id="fulltext_native_min_chars" type="text" size="3" maxlength="3" name="config[fulltext_native_min_chars]" value="' . (int) $config['fulltext_native_min_chars'] . '" /></dd>
indvdum@0
  1739
		</dl>
indvdum@0
  1740
		<dl>
indvdum@0
  1741
			<dt><label for="fulltext_native_max_chars">' . $user->lang['MAX_SEARCH_CHARS'] . ':</label><br /><span>' . $user->lang['MAX_SEARCH_CHARS_EXPLAIN'] . '</span></dt>
indvdum@0
  1742
			<dd><input id="fulltext_native_max_chars" type="text" size="3" maxlength="3" name="config[fulltext_native_max_chars]" value="' . (int) $config['fulltext_native_max_chars'] . '" /></dd>
indvdum@0
  1743
		</dl>
indvdum@0
  1744
		<dl>
indvdum@0
  1745
			<dt><label for="fulltext_native_common_thres">' . $user->lang['COMMON_WORD_THRESHOLD'] . ':</label><br /><span>' . $user->lang['COMMON_WORD_THRESHOLD_EXPLAIN'] . '</span></dt>
indvdum@0
  1746
			<dd><input id="fulltext_native_common_thres" type="text" size="3" maxlength="3" name="config[fulltext_native_common_thres]" value="' . (double) $config['fulltext_native_common_thres'] . '" /> %</dd>
indvdum@0
  1747
		</dl>
indvdum@0
  1748
		';
indvdum@0
  1749
indvdum@0
  1750
		// These are fields required in the config table
indvdum@0
  1751
		return array(
indvdum@0
  1752
			'tpl'		=> $tpl,
indvdum@0
  1753
			'config'	=> array('fulltext_native_load_upd' => 'bool', 'fulltext_native_min_chars' => 'integer:0:255', 'fulltext_native_max_chars' => 'integer:0:255', 'fulltext_native_common_thres' => 'double:0:100')
indvdum@0
  1754
		);
indvdum@0
  1755
	}
indvdum@0
  1756
}
indvdum@0
  1757
indvdum@0
  1758
?>