forked from elkarte/Elkarte
/
Custom.php
285 lines (242 loc) · 7.77 KB
/
Custom.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
/**
* Search class used when a custom index is used. Handles its creation as well
* as maintaining it as posts are added / removed
*
* @package ElkArte Forum
* @copyright ElkArte Forum contributors
* @license BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
*
* This file contains code covered by:
* copyright: 2011 Simple Machines (http://www.simplemachines.org)
*
* @version 2.0 dev
*
*/
namespace ElkArte\Search\API;
use ElkArte\Database\AbstractResult;
use ElkArte\Helper\Util;
/**
* SearchAPI-Custom.class.php, Custom Search API class .. used when custom ElkArte index is used
*
* @package Search
*/
class Custom extends Standard
{
/** @var string This is the last version of ElkArte that this was tested on, to protect against API changes. */
public $version_compatible = 'ElkArte 2.0';
/** @var string This won't work with versions of ElkArte less than this. */
public $min_elk_version = 'ElkArte 1.0';
/** @var bool Is it supported? */
public $is_supported = true;
/** @var array Index Settings */
protected $indexSettings = [];
/**
* Custom::__construct()
*/
public function __construct($config, $searchParams)
{
global $modSettings;
parent::__construct($config, $searchParams);
// Is this database supported?
if (!in_array($this->_db->title(), $this->supported_databases, true))
{
$this->is_supported = false;
return;
}
if (empty($modSettings['search_custom_index_config']))
{
return;
}
$this->indexSettings = Util::unserialize($modSettings['search_custom_index_config']);
$this->bannedWords = empty($modSettings['search_stopwords']) ? [] : explode(',', $modSettings['search_stopwords']);
}
/**
* If the settings don't exist we can't continue.
*/
public function isValid()
{
global $modSettings;
return !empty($modSettings['search_custom_index_config']);
}
/**
* {@inheritDoc}
*/
public function prepareIndexes($word, &$wordsSearch, &$wordsExclude, $isExcluded, $excludedSubjectWords)
{
global $modSettings;
$subwords = text2words($word, true);
if (empty($modSettings['search_force_index']))
{
$wordsSearch['words'][] = $word;
}
// Excluded phrases don't benefit from being split into subwords.
if (count($subwords) > 1 && $isExcluded)
{
return;
}
foreach ($subwords as $subword)
{
if (Util::strlen($subword) >= $this->min_word_length
&& !in_array($subword, $this->bannedWords, true))
{
$wordsSearch['indexed_words'][] = $subword;
if ($isExcluded !== false)
{
$wordsExclude[] = $subword;
}
}
}
}
/**
* Determines if the word index is being used.
*
* @return bool Returns true if the word index is being used, otherwise returns false.
*/
public function useWordIndex()
{
return true;
}
/**
* Search for indexed words.
*
* @param array $words An array of words
* @param array $search_data An array of search data
*
* @return AbstractResult|boolean
*/
public function indexedWordQuery($words, $search_data)
{
global $modSettings;
$db = database();
// We can't do anything without this
$db_search = db_search();
$query_select = ['id_msg' => 'm.id_msg',];
$query_inner_join = [];
$query_left_join = [];
$query_where = [];
$query_params = $search_data['params'];
if ($query_params['id_search'])
{
$query_select['id_search'] = '{int:id_search}';
}
$count = 0;
foreach ($words['words'] as $regularWord)
{
$query_where[] = 'm.body' . (in_array($regularWord, $query_params['excluded_words'], true) ? ' {not_' : '{') . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? 'ilike} ' : 'rlike} ') . '{string:complex_body_' . $count . '}';
$query_params['complex_body_' . ($count++)] = $this->prepareWord($regularWord, $search_data['no_regexp']);
}
// Modifiers such as specific user or specific board.
$query_where = array_merge($query_where, $this->queryWhereModifiers($query_params));
// Modifiers to exclude words from the subject
$query_where = array_merge($query_where, $this->queryExclusionModifiers($query_params, $search_data));
$numTables = 0;
$prev_join = 0;
foreach ($words['indexed_words'] as $indexedWord)
{
$numTables++;
if (in_array($indexedWord, $query_params['excluded_index_words'], true))
{
$query_left_join[] = '{db_prefix}log_search_words AS lsw' . $numTables . ' ON (lsw' . $numTables . '.id_word = ' . $indexedWord . ' AND lsw' . $numTables . '.id_msg = m.id_msg)';
$query_where[] = '(lsw' . $numTables . '.id_word IS NULL)';
}
else
{
$query_inner_join[] = '{db_prefix}log_search_words AS lsw' . $numTables . ' ON (lsw' . $numTables . '.id_msg = ' . ($prev_join === 0 ? 'm' : 'lsw' . $prev_join) . '.id_msg)';
$query_where[] = 'lsw' . $numTables . '.id_word = ' . $indexedWord;
$prev_join = $numTables;
}
}
return $db_search->search_query('', ($db->support_ignore() ? ('
INSERT IGNORE INTO {db_prefix}' . $search_data['insert_into'] . '
(' . implode(', ', array_keys($query_select)) . ')') : '') . '
SELECT ' . implode(', ', $query_select) . '
FROM {db_prefix}messages AS m' . (empty($query_inner_join) ? '' : '
INNER JOIN ' . implode('
INNER JOIN ', $query_inner_join)) . (empty($query_left_join) ? '' : '
LEFT JOIN ' . implode('
LEFT JOIN ', $query_left_join)) . '
WHERE ' . implode('
AND ', $query_where) . (empty($search_data['max_results']) ? '' : '
LIMIT ' . ($search_data['max_results'] - $search_data['indexed_results'])),
$query_params
);
}
/**
* After a post is made, we update the search index database
*
* @param array $msgOptions Contains the post data
* @param array $topicOptions Not used in this API
* @param array $posterOptions Not used in this API
*/
public function postCreated($msgOptions, $topicOptions, $posterOptions)
{
$db = database();
$inserts = [];
foreach (text2words($msgOptions['body'], true) as $word)
{
$inserts[] = [$word, $msgOptions['id']];
}
if (!empty($inserts))
{
$db->insert('ignore',
'{db_prefix}log_search_words',
['id_word' => 'int', 'id_msg' => 'int'],
$inserts,
['id_word', 'id_msg']
);
}
}
/**
* After a post is modified, we update the search index database.
*
* @param array $msgOptions The post data
* @param array $topicOptions Not used in this API
* @param array $posterOptions Not used in this API
*/
public function postModified($msgOptions, $topicOptions, $posterOptions)
{
global $modSettings;
$db = database();
if (isset($msgOptions['body']))
{
$stopwords = empty($modSettings['search_stopwords']) ? [] : explode(',', $modSettings['search_stopwords']);
$old_body = $msgOptions['old_body'] ?? '';
// Create the new and old index
$old_index = text2words($old_body, true);
$new_index = text2words($msgOptions['body'], true);
// Calculate the words to be added and removed from the index.
$removed_words = array_diff(array_diff($old_index, $new_index), $stopwords);
$inserted_words = array_diff(array_diff($new_index, $old_index), $stopwords);
// Delete the removed words AND the added ones to avoid key constraints.
if (!empty($removed_words))
{
$removed_words = array_merge($removed_words, $inserted_words);
$db->query('', '
DELETE FROM {db_prefix}log_search_words
WHERE id_msg = {int:id_msg}
AND id_word IN ({array_int:removed_words})',
[
'removed_words' => $removed_words,
'id_msg' => $msgOptions['id'],
]
);
}
// Add the new words to be indexed.
if (!empty($inserted_words))
{
$inserts = [];
foreach ($inserted_words as $word)
{
$inserts[] = [$word, $msgOptions['id']];
}
$db->insert('insert',
'{db_prefix}log_search_words',
['id_word' => 'string', 'id_msg' => 'int'],
$inserts,
['id_word', 'id_msg']
);
}
}
}
}