1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Simple Machines Forum (SMF) |
5
|
|
|
* |
6
|
|
|
* @package SMF |
7
|
|
|
* @author Simple Machines https://www.simplemachines.org |
8
|
|
|
* @copyright 2022 Simple Machines and individual contributors |
9
|
|
|
* @license https://www.simplemachines.org/about/smf/license.php BSD |
10
|
|
|
* |
11
|
|
|
* @version 2.1.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
if (!defined('SMF')) |
15
|
|
|
die('No direct access...'); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class fulltext_search |
19
|
|
|
* Used for fulltext index searching |
20
|
|
|
*/ |
21
|
|
|
class fulltext_search extends search_api |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array Which words are banned |
25
|
|
|
*/ |
26
|
|
|
protected $bannedWords = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int The minimum word length |
30
|
|
|
*/ |
31
|
|
|
protected $min_word_length = 4; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array Which databases support this method? |
35
|
|
|
*/ |
36
|
|
|
protected $supported_databases = array('mysql', 'postgresql'); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The constructor function |
40
|
|
|
*/ |
41
|
|
|
public function __construct() |
42
|
|
|
{ |
43
|
|
|
global $modSettings, $db_type; |
44
|
|
|
|
45
|
|
|
// Is this database supported? |
46
|
|
|
if (!in_array($db_type, $this->supported_databases)) |
47
|
|
|
{ |
48
|
|
|
$this->is_supported = false; |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->bannedWords = empty($modSettings['search_banned_words']) ? array() : explode(',', $modSettings['search_banned_words']); |
53
|
|
|
$this->min_word_length = $this->_getMinWordLength(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function supportsMethod($methodName, $query_params = null) |
60
|
|
|
{ |
61
|
|
|
$return = false; |
62
|
|
|
switch ($methodName) |
63
|
|
|
{ |
64
|
|
|
case 'searchSort': |
65
|
|
|
case 'prepareIndexes': |
66
|
|
|
case 'indexedWordQuery': |
67
|
|
|
$return = true; |
68
|
|
|
break; |
69
|
|
|
|
70
|
|
|
// All other methods, too bad dunno you. |
71
|
|
|
default: |
72
|
|
|
$return = false; |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Maybe parent got support |
77
|
|
|
if (!$return) |
78
|
|
|
$return = parent::supportsMethod($methodName, $query_params); |
79
|
|
|
|
80
|
|
|
return $return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* fulltext_search::_getMinWordLength() |
85
|
|
|
* |
86
|
|
|
* What is the minimum word length full text supports? |
87
|
|
|
* |
88
|
|
|
* @return int The minimum word length |
89
|
|
|
*/ |
90
|
|
|
protected function _getMinWordLength() |
91
|
|
|
{ |
92
|
|
|
global $smcFunc, $db_type; |
93
|
|
|
|
94
|
|
|
if ($db_type == 'postgresql') |
95
|
|
|
return 0; |
96
|
|
|
// Try to determine the minimum number of letters for a fulltext search. |
97
|
|
|
$request = $smcFunc['db_search_query']('max_fulltext_length', ' |
98
|
|
|
SHOW VARIABLES |
99
|
|
|
LIKE {string:fulltext_minimum_word_length}', |
100
|
|
|
array( |
101
|
|
|
'fulltext_minimum_word_length' => 'ft_min_word_len', |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
if ($request !== false && $smcFunc['db_num_rows']($request) == 1) |
105
|
|
|
{ |
106
|
|
|
list (, $min_word_length) = $smcFunc['db_fetch_row']($request); |
107
|
|
|
$smcFunc['db_free_result']($request); |
108
|
|
|
} |
109
|
|
|
// 4 is the MySQL default... |
110
|
|
|
else |
111
|
|
|
$min_word_length = 4; |
112
|
|
|
|
113
|
|
|
return $min_word_length; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function searchSort($a, $b) |
120
|
|
|
{ |
121
|
|
|
global $excludedWords, $smcFunc; |
122
|
|
|
|
123
|
|
|
$x = $smcFunc['strlen']($a) - (in_array($a, $excludedWords) ? 1000 : 0); |
124
|
|
|
$y = $smcFunc['strlen']($b) - (in_array($b, $excludedWords) ? 1000 : 0); |
125
|
|
|
|
126
|
|
|
return $x < $y ? 1 : ($x > $y ? -1 : 0); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude, $isExcluded) |
133
|
|
|
{ |
134
|
|
|
global $modSettings, $smcFunc; |
135
|
|
|
|
136
|
|
|
$subwords = text2words($word, null, false); |
137
|
|
|
|
138
|
|
|
if (empty($modSettings['search_force_index'])) |
139
|
|
|
{ |
140
|
|
|
// A boolean capable search engine and not forced to only use an index, we may use a non indexed search |
141
|
|
|
// this is harder on the server so we are restrictive here |
142
|
|
|
if (count($subwords) > 1 && preg_match('~[.:@$]~', $word)) |
143
|
|
|
{ |
144
|
|
|
// using special characters that a full index would ignore and the remaining words are short which would also be ignored |
145
|
|
|
if (($smcFunc['strlen'](current($subwords)) < $this->min_word_length) && ($smcFunc['strlen'](next($subwords)) < $this->min_word_length)) |
146
|
|
|
{ |
147
|
|
|
$wordsSearch['words'][] = trim($word, "/*- "); |
148
|
|
|
$wordsSearch['complex_words'][] = count($subwords) === 1 ? $word : '"' . $word . '"'; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
elseif ($smcFunc['strlen'](trim($word, "/*- ")) < $this->min_word_length) |
152
|
|
|
{ |
153
|
|
|
// short words have feelings too |
154
|
|
|
$wordsSearch['words'][] = trim($word, "/*- "); |
155
|
|
|
$wordsSearch['complex_words'][] = count($subwords) === 1 ? $word : '"' . $word . '"'; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$fulltextWord = count($subwords) === 1 ? $word : '"' . $word . '"'; |
160
|
|
|
$wordsSearch['indexed_words'][] = $fulltextWord; |
161
|
|
|
if ($isExcluded) |
162
|
|
|
$wordsExclude[] = $fulltextWord; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritDoc} |
167
|
|
|
*/ |
168
|
|
|
public function indexedWordQuery(array $words, array $search_data) |
169
|
|
|
{ |
170
|
|
|
global $modSettings, $smcFunc; |
171
|
|
|
|
172
|
|
|
$query_select = array( |
173
|
|
|
'id_msg' => 'm.id_msg', |
174
|
|
|
); |
175
|
|
|
$query_where = array(); |
176
|
|
|
$query_params = $search_data['params']; |
177
|
|
|
|
178
|
|
|
if ($smcFunc['db_title'] === POSTGRE_TITLE) |
179
|
|
|
$modSettings['search_simple_fulltext'] = true; |
180
|
|
|
|
181
|
|
|
if ($query_params['id_search']) |
182
|
|
|
$query_select['id_search'] = '{int:id_search}'; |
183
|
|
|
|
184
|
|
|
$count = 0; |
185
|
|
|
if (empty($modSettings['search_simple_fulltext'])) |
186
|
|
|
foreach ($words['words'] as $regularWord) |
187
|
|
|
{ |
188
|
|
|
$query_where[] = 'm.body' . (in_array($regularWord, $query_params['excluded_words']) ? ' NOT' : '') . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : ' RLIKE ') . '{string:complex_body_' . $count . '}'; |
189
|
|
|
$query_params['complex_body_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($regularWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $regularWord), '\\\'') . '[[:>:]]'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
if ($query_params['user_query']) |
193
|
|
|
$query_where[] = '{raw:user_query}'; |
194
|
|
|
if ($query_params['board_query']) |
195
|
|
|
$query_where[] = 'm.id_board {raw:board_query}'; |
196
|
|
|
|
197
|
|
|
if ($query_params['topic']) |
198
|
|
|
$query_where[] = 'm.id_topic = {int:topic}'; |
199
|
|
|
if ($query_params['min_msg_id']) |
200
|
|
|
$query_where[] = 'm.id_msg >= {int:min_msg_id}'; |
201
|
|
|
if ($query_params['max_msg_id']) |
202
|
|
|
$query_where[] = 'm.id_msg <= {int:max_msg_id}'; |
203
|
|
|
|
204
|
|
|
$count = 0; |
205
|
|
|
if (!empty($query_params['excluded_phrases']) && empty($modSettings['search_force_index'])) |
206
|
|
|
foreach ($query_params['excluded_phrases'] as $phrase) |
207
|
|
|
{ |
208
|
|
|
$query_where[] = 'subject NOT' . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : ' RLIKE ') . '{string:exclude_subject_phrase_' . $count . '}'; |
209
|
|
|
$query_params['exclude_subject_phrase_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]'; |
210
|
|
|
} |
211
|
|
|
$count = 0; |
212
|
|
|
if (!empty($query_params['excluded_subject_words']) && empty($modSettings['search_force_index'])) |
213
|
|
|
foreach ($query_params['excluded_subject_words'] as $excludedWord) |
214
|
|
|
{ |
215
|
|
|
$query_where[] = 'subject NOT' . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : ' RLIKE ') . '{string:exclude_subject_words_' . $count . '}'; |
216
|
|
|
$query_params['exclude_subject_words_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($excludedWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $excludedWord), '\\\'') . '[[:>:]]'; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if (!empty($modSettings['search_simple_fulltext'])) |
220
|
|
|
{ |
221
|
|
|
if ($smcFunc['db_title'] === POSTGRE_TITLE) |
222
|
|
|
{ |
223
|
|
|
$language_ftx = $smcFunc['db_search_language'](); |
224
|
|
|
|
225
|
|
|
$query_where[] = 'to_tsvector({string:language_ftx},body) @@ plainto_tsquery({string:language_ftx},{string:body_match})'; |
226
|
|
|
$query_params['language_ftx'] = $language_ftx; |
227
|
|
|
} |
228
|
|
|
else |
229
|
|
|
$query_where[] = 'MATCH (body) AGAINST ({string:body_match})'; |
230
|
|
|
$query_params['body_match'] = implode(' ', array_diff($words['indexed_words'], $query_params['excluded_index_words'])); |
231
|
|
|
} |
232
|
|
|
else |
233
|
|
|
{ |
234
|
|
|
$query_params['boolean_match'] = ''; |
235
|
|
|
|
236
|
|
|
// remove any indexed words that are used in the complex body search terms |
237
|
|
|
$words['indexed_words'] = array_diff($words['indexed_words'], $words['complex_words']); |
238
|
|
|
|
239
|
|
|
if ($smcFunc['db_title'] === POSTGRE_TITLE) |
240
|
|
|
{ |
241
|
|
|
$row = 0; |
242
|
|
|
foreach ($words['indexed_words'] as $fulltextWord) |
243
|
|
|
{ |
244
|
|
|
$query_params['boolean_match'] .= ($row <> 0 ? '&' : ''); |
245
|
|
|
$query_params['boolean_match'] .= (in_array($fulltextWord, $query_params['excluded_index_words']) ? '!' : '') . $fulltextWord . ' '; |
246
|
|
|
$row++; |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
else |
250
|
|
|
foreach ($words['indexed_words'] as $fulltextWord) |
251
|
|
|
$query_params['boolean_match'] .= (in_array($fulltextWord, $query_params['excluded_index_words']) ? '-' : '+') . $fulltextWord . ' '; |
252
|
|
|
|
253
|
|
|
$query_params['boolean_match'] = substr($query_params['boolean_match'], 0, -1); |
254
|
|
|
|
255
|
|
|
// if we have bool terms to search, add them in |
256
|
|
|
if ($query_params['boolean_match']) |
257
|
|
|
{ |
258
|
|
|
if ($smcFunc['db_title'] === POSTGRE_TITLE) |
259
|
|
|
{ |
260
|
|
|
$language_ftx = $smcFunc['db_search_language'](); |
261
|
|
|
|
262
|
|
|
$query_where[] = 'to_tsvector({string:language_ftx},body) @@ plainto_tsquery({string:language_ftx},{string:boolean_match})'; |
263
|
|
|
$query_params['language_ftx'] = $language_ftx; |
264
|
|
|
} |
265
|
|
|
else |
266
|
|
|
$query_where[] = 'MATCH (body) AGAINST ({string:boolean_match} IN BOOLEAN MODE)'; |
267
|
|
|
} |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
$ignoreRequest = $smcFunc['db_search_query']('insert_into_log_messages_fulltext', ($smcFunc['db_support_ignore'] ? (' |
271
|
|
|
INSERT IGNORE INTO {db_prefix}' . $search_data['insert_into'] . ' |
272
|
|
|
(' . implode(', ', array_keys($query_select)) . ')') : '') . ' |
273
|
|
|
SELECT ' . implode(', ', $query_select) . ' |
274
|
|
|
FROM {db_prefix}messages AS m |
275
|
|
|
WHERE ' . implode(' |
276
|
|
|
AND ', $query_where) . (empty($search_data['max_results']) ? '' : ' |
277
|
|
|
LIMIT ' . ($search_data['max_results'] - $search_data['indexed_results'])), |
278
|
|
|
$query_params |
279
|
|
|
); |
280
|
|
|
|
281
|
|
|
return $ignoreRequest; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
?> |