1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The admin screen to change the search settings. |
5
|
|
|
* |
6
|
|
|
* Simple Machines Forum (SMF) |
7
|
|
|
* |
8
|
|
|
* @package SMF |
9
|
|
|
* @author Simple Machines https://www.simplemachines.org |
10
|
|
|
* @copyright 2022 Simple Machines and individual contributors |
11
|
|
|
* @license https://www.simplemachines.org/about/smf/license.php BSD |
12
|
|
|
* |
13
|
|
|
* @version 2.1.3 |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
if (!defined('SMF')) |
17
|
|
|
die('No direct access...'); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Main entry point for the admin search settings screen. |
21
|
|
|
* It checks permissions, and it forwards to the appropriate function based on |
22
|
|
|
* the given sub-action. |
23
|
|
|
* Defaults to sub-action 'settings'. |
24
|
|
|
* Called by ?action=admin;area=managesearch. |
25
|
|
|
* Requires the admin_forum permission. |
26
|
|
|
* |
27
|
|
|
* Uses ManageSearch template. |
28
|
|
|
* Uses Search language file. |
29
|
|
|
*/ |
30
|
|
|
function ManageSearch() |
31
|
|
|
{ |
32
|
|
|
global $context, $txt; |
33
|
|
|
|
34
|
|
|
isAllowedTo('admin_forum'); |
35
|
|
|
|
36
|
|
|
loadLanguage('Search'); |
37
|
|
|
loadTemplate('ManageSearch'); |
38
|
|
|
|
39
|
|
|
db_extend('search'); |
40
|
|
|
|
41
|
|
|
$subActions = array( |
42
|
|
|
'settings' => 'EditSearchSettings', |
43
|
|
|
'weights' => 'EditWeights', |
44
|
|
|
'method' => 'EditSearchMethod', |
45
|
|
|
'createfulltext' => 'EditSearchMethod', |
46
|
|
|
'removecustom' => 'EditSearchMethod', |
47
|
|
|
'removefulltext' => 'EditSearchMethod', |
48
|
|
|
'createmsgindex' => 'CreateMessageIndex', |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
// Create the tabs for the template. |
52
|
|
|
$context[$context['admin_menu_name']]['tab_data'] = array( |
53
|
|
|
'title' => $txt['manage_search'], |
54
|
|
|
'help' => 'search', |
55
|
|
|
'description' => $txt['search_settings_desc'], |
56
|
|
|
'tabs' => array( |
57
|
|
|
'weights' => array( |
58
|
|
|
'description' => $txt['search_weights_desc'], |
59
|
|
|
), |
60
|
|
|
'method' => array( |
61
|
|
|
'description' => $txt['search_method_desc'], |
62
|
|
|
), |
63
|
|
|
'settings' => array( |
64
|
|
|
'description' => $txt['search_settings_desc'], |
65
|
|
|
), |
66
|
|
|
), |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
call_integration_hook('integrate_manage_search', array(&$subActions)); |
70
|
|
|
|
71
|
|
|
// Default the sub-action to 'edit search settings'. |
72
|
|
|
$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'weights'; |
73
|
|
|
|
74
|
|
|
$context['sub_action'] = $_REQUEST['sa']; |
75
|
|
|
|
76
|
|
|
// Call the right function for this sub-action. |
77
|
|
|
call_helper($subActions[$_REQUEST['sa']]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Edit some general settings related to the search function. |
82
|
|
|
* Called by ?action=admin;area=managesearch;sa=settings. |
83
|
|
|
* Requires the admin_forum permission. |
84
|
|
|
* @uses template_show_settings() |
85
|
|
|
* |
86
|
|
|
* @param bool $return_config Whether or not to return the config_vars array (used for admin search) |
87
|
|
|
* @return void|array Returns nothing or returns the $config_vars array if $return_config is true |
88
|
|
|
*/ |
89
|
|
|
function EditSearchSettings($return_config = false) |
90
|
|
|
{ |
91
|
|
|
global $txt, $context, $scripturl, $sourcedir, $modSettings; |
92
|
|
|
|
93
|
|
|
// What are we editing anyway? |
94
|
|
|
$config_vars = array( |
95
|
|
|
// Permission... |
96
|
|
|
array('permissions', 'search_posts'), |
97
|
|
|
// Some simple settings. |
98
|
|
|
array('int', 'search_results_per_page'), |
99
|
|
|
array('int', 'search_max_results', 'subtext' => $txt['search_max_results_disable']), |
100
|
|
|
'', |
101
|
|
|
|
102
|
|
|
// Some limitations. |
103
|
|
|
array('int', 'search_floodcontrol_time', 'subtext' => $txt['search_floodcontrol_time_desc'], 6, 'postinput' => $txt['seconds']), |
104
|
|
|
); |
105
|
|
|
|
106
|
|
|
call_integration_hook('integrate_modify_search_settings', array(&$config_vars)); |
107
|
|
|
|
108
|
|
|
// Perhaps the search method wants to add some settings? |
109
|
|
|
require_once($sourcedir . '/Search.php'); |
110
|
|
|
$searchAPI = findSearchAPI(); |
111
|
|
|
if (is_callable(array($searchAPI, 'searchSettings'))) |
112
|
|
|
call_user_func_array(array($searchAPI, 'searchSettings'), array(&$config_vars)); |
113
|
|
|
|
114
|
|
|
if ($return_config) |
115
|
|
|
return $config_vars; |
116
|
|
|
|
117
|
|
|
$context['page_title'] = $txt['search_settings_title']; |
118
|
|
|
$context['sub_template'] = 'show_settings'; |
119
|
|
|
|
120
|
|
|
// We'll need this for the settings. |
121
|
|
|
require_once($sourcedir . '/ManageServer.php'); |
122
|
|
|
|
123
|
|
|
// A form was submitted. |
124
|
|
|
if (isset($_REQUEST['save'])) |
125
|
|
|
{ |
126
|
|
|
checkSession(); |
127
|
|
|
|
128
|
|
|
call_integration_hook('integrate_save_search_settings'); |
129
|
|
|
|
130
|
|
|
if (empty($_POST['search_results_per_page'])) |
131
|
|
|
$_POST['search_results_per_page'] = !empty($modSettings['search_results_per_page']) ? $modSettings['search_results_per_page'] : $modSettings['defaultMaxMessages']; |
132
|
|
|
saveDBSettings($config_vars); |
133
|
|
|
$_SESSION['adm-save'] = true; |
134
|
|
|
redirectexit('action=admin;area=managesearch;sa=settings;' . $context['session_var'] . '=' . $context['session_id']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Prep the template! |
138
|
|
|
$context['post_url'] = $scripturl . '?action=admin;area=managesearch;save;sa=settings'; |
139
|
|
|
$context['settings_title'] = $txt['search_settings_title']; |
140
|
|
|
|
141
|
|
|
// We need this for the in-line permissions |
142
|
|
|
createToken('admin-mp'); |
143
|
|
|
|
144
|
|
|
prepareDBSettingContext($config_vars); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Edit the relative weight of the search factors. |
149
|
|
|
* Called by ?action=admin;area=managesearch;sa=weights. |
150
|
|
|
* Requires the admin_forum permission. |
151
|
|
|
* |
152
|
|
|
* @uses template_modify_weights() |
153
|
|
|
*/ |
154
|
|
|
function EditWeights() |
155
|
|
|
{ |
156
|
|
|
global $txt, $context, $modSettings; |
157
|
|
|
|
158
|
|
|
$context['page_title'] = $txt['search_weights_title']; |
159
|
|
|
$context['sub_template'] = 'modify_weights'; |
160
|
|
|
|
161
|
|
|
$factors = array( |
162
|
|
|
'search_weight_frequency', |
163
|
|
|
'search_weight_age', |
164
|
|
|
'search_weight_length', |
165
|
|
|
'search_weight_subject', |
166
|
|
|
'search_weight_first_message', |
167
|
|
|
'search_weight_sticky', |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
call_integration_hook('integrate_modify_search_weights', array(&$factors)); |
171
|
|
|
|
172
|
|
|
// A form was submitted. |
173
|
|
|
if (isset($_POST['save'])) |
174
|
|
|
{ |
175
|
|
|
checkSession(); |
176
|
|
|
validateToken('admin-msw'); |
177
|
|
|
|
178
|
|
|
call_integration_hook('integrate_save_search_weights'); |
179
|
|
|
|
180
|
|
|
$changes = array(); |
181
|
|
|
foreach ($factors as $factor) |
182
|
|
|
$changes[$factor] = (int) $_POST[$factor]; |
183
|
|
|
updateSettings($changes); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$context['relative_weights'] = array('total' => 0); |
187
|
|
|
foreach ($factors as $factor) |
188
|
|
|
$context['relative_weights']['total'] += isset($modSettings[$factor]) ? $modSettings[$factor] : 0; |
189
|
|
|
|
190
|
|
|
foreach ($factors as $factor) |
191
|
|
|
$context['relative_weights'][$factor] = round(100 * (isset($modSettings[$factor]) ? $modSettings[$factor] : 0) / $context['relative_weights']['total'], 1); |
192
|
|
|
|
193
|
|
|
createToken('admin-msw'); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Edit the search method and search index used. |
198
|
|
|
* Calculates the size of the current search indexes in use. |
199
|
|
|
* Allows to create and delete a fulltext index on the messages table. |
200
|
|
|
* Allows to delete a custom index (that CreateMessageIndex() created). |
201
|
|
|
* Called by ?action=admin;area=managesearch;sa=method. |
202
|
|
|
* Requires the admin_forum permission. |
203
|
|
|
* |
204
|
|
|
* @uses template_select_search_method() |
205
|
|
|
*/ |
206
|
|
|
function EditSearchMethod() |
207
|
|
|
{ |
208
|
|
|
global $txt, $context, $modSettings, $smcFunc, $db_type, $db_prefix; |
209
|
|
|
|
210
|
|
|
$context['page_title'] = $txt['search_method_title']; |
211
|
|
|
$context['sub_template'] = 'select_search_method'; |
212
|
|
|
$context['supports_fulltext'] = $smcFunc['db_search_support']('fulltext'); |
213
|
|
|
|
214
|
|
|
// Load any apis. |
215
|
|
|
$context['search_apis'] = loadSearchAPIs(); |
216
|
|
|
|
217
|
|
|
// Detect whether a fulltext index is set. |
218
|
|
|
if ($context['supports_fulltext']) |
219
|
|
|
detectFulltextIndex(); |
220
|
|
|
|
221
|
|
|
if (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'createfulltext') |
222
|
|
|
{ |
223
|
|
|
checkSession('get'); |
224
|
|
|
validateToken('admin-msm', 'get'); |
225
|
|
|
|
226
|
|
|
if ($db_type == 'postgresql') |
227
|
|
|
{ |
228
|
|
|
$smcFunc['db_query']('', ' |
229
|
|
|
DROP INDEX IF EXISTS {db_prefix}messages_ftx', |
230
|
|
|
array( |
231
|
|
|
'db_error_skip' => true, |
232
|
|
|
) |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
$language_ftx = $smcFunc['db_search_language'](); |
236
|
|
|
|
237
|
|
|
$smcFunc['db_query']('', ' |
238
|
|
|
CREATE INDEX {db_prefix}messages_ftx ON {db_prefix}messages |
239
|
|
|
USING gin(to_tsvector({string:language},body))', |
240
|
|
|
array( |
241
|
|
|
'language' => $language_ftx |
242
|
|
|
) |
243
|
|
|
); |
244
|
|
|
} |
245
|
|
|
else |
246
|
|
|
{ |
247
|
|
|
// Make sure it's gone before creating it. |
248
|
|
|
$smcFunc['db_query']('', ' |
249
|
|
|
ALTER TABLE {db_prefix}messages |
250
|
|
|
DROP INDEX body', |
251
|
|
|
array( |
252
|
|
|
'db_error_skip' => true, |
253
|
|
|
) |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
$smcFunc['db_query']('', ' |
257
|
|
|
ALTER TABLE {db_prefix}messages |
258
|
|
|
ADD FULLTEXT body (body)', |
259
|
|
|
array( |
260
|
|
|
) |
261
|
|
|
); |
262
|
|
|
} |
263
|
|
|
redirectexit('action=admin;area=managesearch;sa=method'); |
264
|
|
|
} |
265
|
|
|
elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removefulltext' && !empty($context['fulltext_index'])) |
266
|
|
|
{ |
267
|
|
|
checkSession('get'); |
268
|
|
|
validateToken('admin-msm', 'get'); |
269
|
|
|
|
270
|
|
|
if ($db_type == 'postgresql') |
271
|
|
|
$smcFunc['db_query']('', ' |
272
|
|
|
DROP INDEX IF EXISTS {db_prefix}messages_ftx', |
273
|
|
|
array( |
274
|
|
|
'db_error_skip' => true, |
275
|
|
|
) |
276
|
|
|
); |
277
|
|
|
else |
278
|
|
|
$smcFunc['db_query']('', ' |
279
|
|
|
ALTER TABLE {db_prefix}messages |
280
|
|
|
DROP INDEX ' . implode(', |
281
|
|
|
DROP INDEX ', $context['fulltext_index']), |
282
|
|
|
array( |
283
|
|
|
'db_error_skip' => true, |
284
|
|
|
) |
285
|
|
|
); |
286
|
|
|
|
287
|
|
|
// Go back to the default search method. |
288
|
|
|
if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext') |
289
|
|
|
updateSettings(array( |
290
|
|
|
'search_index' => '', |
291
|
|
|
)); |
292
|
|
|
redirectexit('action=admin;area=managesearch;sa=method'); |
293
|
|
|
} |
294
|
|
|
elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removecustom') |
295
|
|
|
{ |
296
|
|
|
checkSession('get'); |
297
|
|
|
validateToken('admin-msm', 'get'); |
298
|
|
|
|
299
|
|
|
db_extend(); |
300
|
|
|
$tables = $smcFunc['db_list_tables'](false, $db_prefix . 'log_search_words'); |
301
|
|
|
if (!empty($tables)) |
302
|
|
|
{ |
303
|
|
|
$smcFunc['db_search_query']('drop_words_table', ' |
304
|
|
|
DROP TABLE {db_prefix}log_search_words', |
305
|
|
|
array( |
306
|
|
|
) |
307
|
|
|
); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
updateSettings(array( |
311
|
|
|
'search_custom_index_config' => '', |
312
|
|
|
'search_custom_index_resume' => '', |
313
|
|
|
)); |
314
|
|
|
|
315
|
|
|
// Go back to the default search method. |
316
|
|
|
if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom') |
317
|
|
|
updateSettings(array( |
318
|
|
|
'search_index' => '', |
319
|
|
|
)); |
320
|
|
|
redirectexit('action=admin;area=managesearch;sa=method'); |
321
|
|
|
} |
322
|
|
|
elseif (isset($_POST['save'])) |
323
|
|
|
{ |
324
|
|
|
checkSession(); |
325
|
|
|
validateToken('admin-msmpost'); |
326
|
|
|
|
327
|
|
|
updateSettings(array( |
328
|
|
|
'search_index' => empty($_POST['search_index']) || (!in_array($_POST['search_index'], array('fulltext', 'custom')) && !isset($context['search_apis'][$_POST['search_index']])) ? '' : $_POST['search_index'], |
329
|
|
|
'search_force_index' => isset($_POST['search_force_index']) ? '1' : '0', |
330
|
|
|
'search_match_words' => isset($_POST['search_match_words']) ? '1' : '0', |
331
|
|
|
)); |
332
|
|
|
redirectexit('action=admin;area=managesearch;sa=method'); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
$context['table_info'] = array( |
336
|
|
|
'data_length' => 0, |
337
|
|
|
'index_length' => 0, |
338
|
|
|
'fulltext_length' => 0, |
339
|
|
|
'custom_index_length' => 0, |
340
|
|
|
); |
341
|
|
|
|
342
|
|
|
// Get some info about the messages table, to show its size and index size. |
343
|
|
|
if ($db_type == 'mysql') |
344
|
|
|
{ |
345
|
|
|
if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0) |
346
|
|
|
$request = $smcFunc['db_query']('', ' |
347
|
|
|
SHOW TABLE STATUS |
348
|
|
|
FROM {string:database_name} |
349
|
|
|
LIKE {string:table_name}', |
350
|
|
|
array( |
351
|
|
|
'database_name' => '`' . strtr($match[1], array('`' => '')) . '`', |
352
|
|
|
'table_name' => str_replace('_', '\_', $match[2]) . 'messages', |
353
|
|
|
) |
354
|
|
|
); |
355
|
|
|
else |
356
|
|
|
$request = $smcFunc['db_query']('', ' |
357
|
|
|
SHOW TABLE STATUS |
358
|
|
|
LIKE {string:table_name}', |
359
|
|
|
array( |
360
|
|
|
'table_name' => str_replace('_', '\_', $db_prefix) . 'messages', |
361
|
|
|
) |
362
|
|
|
); |
363
|
|
|
if ($request !== false && $smcFunc['db_num_rows']($request) == 1) |
364
|
|
|
{ |
365
|
|
|
// Only do this if the user has permission to execute this query. |
366
|
|
|
$row = $smcFunc['db_fetch_assoc']($request); |
367
|
|
|
$context['table_info']['data_length'] = $row['Data_length']; |
368
|
|
|
$context['table_info']['index_length'] = $row['Index_length']; |
369
|
|
|
$context['table_info']['fulltext_length'] = $row['Index_length']; |
370
|
|
|
$smcFunc['db_free_result']($request); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
// Now check the custom index table, if it exists at all. |
374
|
|
|
if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0) |
375
|
|
|
$request = $smcFunc['db_query']('', ' |
376
|
|
|
SHOW TABLE STATUS |
377
|
|
|
FROM {string:database_name} |
378
|
|
|
LIKE {string:table_name}', |
379
|
|
|
array( |
380
|
|
|
'database_name' => '`' . strtr($match[1], array('`' => '')) . '`', |
381
|
|
|
'table_name' => str_replace('_', '\_', $match[2]) . 'log_search_words', |
382
|
|
|
) |
383
|
|
|
); |
384
|
|
|
else |
385
|
|
|
$request = $smcFunc['db_query']('', ' |
386
|
|
|
SHOW TABLE STATUS |
387
|
|
|
LIKE {string:table_name}', |
388
|
|
|
array( |
389
|
|
|
'table_name' => str_replace('_', '\_', $db_prefix) . 'log_search_words', |
390
|
|
|
) |
391
|
|
|
); |
392
|
|
|
if ($request !== false && $smcFunc['db_num_rows']($request) == 1) |
393
|
|
|
{ |
394
|
|
|
// Only do this if the user has permission to execute this query. |
395
|
|
|
$row = $smcFunc['db_fetch_assoc']($request); |
396
|
|
|
$context['table_info']['index_length'] += $row['Data_length'] + $row['Index_length']; |
397
|
|
|
$context['table_info']['custom_index_length'] = $row['Data_length'] + $row['Index_length']; |
398
|
|
|
$smcFunc['db_free_result']($request); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
elseif ($db_type == 'postgresql') |
402
|
|
|
{ |
403
|
|
|
// In order to report the sizes correctly we need to perform vacuum (optimize) on the tables we will be using. |
404
|
|
|
//db_extend(); |
405
|
|
|
//$temp_tables = $smcFunc['db_list_tables'](); |
406
|
|
|
//foreach ($temp_tables as $table) |
407
|
|
|
// if ($table == $db_prefix. 'messages' || $table == $db_prefix. 'log_search_words') |
408
|
|
|
// $smcFunc['db_optimize_table']($table); |
409
|
|
|
|
410
|
|
|
// PostGreSql has some hidden sizes. |
411
|
|
|
$request = $smcFunc['db_query']('', ' |
412
|
|
|
SELECT |
413
|
|
|
indexname, |
414
|
|
|
pg_relation_size(quote_ident(t.tablename)::text) AS table_size, |
415
|
|
|
pg_relation_size(quote_ident(indexrelname)::text) AS index_size |
416
|
|
|
FROM pg_tables t |
417
|
|
|
LEFT OUTER JOIN pg_class c ON t.tablename=c.relname |
418
|
|
|
LEFT OUTER JOIN |
419
|
|
|
(SELECT c.relname AS ctablename, ipg.relname AS indexname, indexrelname FROM pg_index x |
420
|
|
|
JOIN pg_class c ON c.oid = x.indrelid |
421
|
|
|
JOIN pg_class ipg ON ipg.oid = x.indexrelid |
422
|
|
|
JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid) |
423
|
|
|
AS foo |
424
|
|
|
ON t.tablename = foo.ctablename |
425
|
|
|
WHERE t.schemaname= {string:schema} and ( |
426
|
|
|
indexname = {string:messages_ftx} OR indexname = {string:log_search_words} )', |
427
|
|
|
array( |
428
|
|
|
'messages_ftx' => $db_prefix . 'messages_ftx', |
429
|
|
|
'log_search_words' => $db_prefix . 'log_search_words', |
430
|
|
|
'schema' => 'public', |
431
|
|
|
) |
432
|
|
|
); |
433
|
|
|
|
434
|
|
|
if ($request !== false && $smcFunc['db_num_rows']($request) > 0) |
435
|
|
|
{ |
436
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
437
|
|
|
{ |
438
|
|
|
if ($row['indexname'] == $db_prefix . 'messages_ftx') |
439
|
|
|
{ |
440
|
|
|
$context['table_info']['data_length'] = (int) $row['table_size']; |
441
|
|
|
$context['table_info']['index_length'] = (int) $row['index_size']; |
442
|
|
|
$context['table_info']['fulltext_length'] = (int) $row['index_size']; |
443
|
|
|
} |
444
|
|
|
elseif ($row['indexname'] == $db_prefix . 'log_search_words') |
445
|
|
|
{ |
446
|
|
|
$context['table_info']['index_length'] = (int) $row['index_size']; |
447
|
|
|
$context['table_info']['custom_index_length'] = (int) $row['index_size']; |
448
|
|
|
} |
449
|
|
|
} |
450
|
|
|
$smcFunc['db_free_result']($request); |
451
|
|
|
} |
452
|
|
|
else |
453
|
|
|
// Didn't work for some reason... |
454
|
|
|
$context['table_info'] = array( |
455
|
|
|
'data_length' => $txt['not_applicable'], |
456
|
|
|
'index_length' => $txt['not_applicable'], |
457
|
|
|
'fulltext_length' => $txt['not_applicable'], |
458
|
|
|
'custom_index_length' => $txt['not_applicable'], |
459
|
|
|
); |
460
|
|
|
} |
461
|
|
|
else |
462
|
|
|
$context['table_info'] = array( |
463
|
|
|
'data_length' => $txt['not_applicable'], |
464
|
|
|
'index_length' => $txt['not_applicable'], |
465
|
|
|
'fulltext_length' => $txt['not_applicable'], |
466
|
|
|
'custom_index_length' => $txt['not_applicable'], |
467
|
|
|
); |
468
|
|
|
|
469
|
|
|
// Format the data and index length in kilobytes. |
470
|
|
|
foreach ($context['table_info'] as $type => $size) |
471
|
|
|
{ |
472
|
|
|
// If it's not numeric then just break. This database engine doesn't support size. |
473
|
|
|
if (!is_numeric($size)) |
474
|
|
|
break; |
475
|
|
|
|
476
|
|
|
$context['table_info'][$type] = comma_format($context['table_info'][$type] / 1024) . ' ' . $txt['search_method_kilobytes']; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
$context['custom_index'] = !empty($modSettings['search_custom_index_config']); |
480
|
|
|
$context['partial_custom_index'] = !empty($modSettings['search_custom_index_resume']) && empty($modSettings['search_custom_index_config']); |
481
|
|
|
$context['double_index'] = !empty($context['fulltext_index']) && $context['custom_index']; |
482
|
|
|
|
483
|
|
|
createToken('admin-msmpost'); |
484
|
|
|
createToken('admin-msm', 'get'); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Create a custom search index for the messages table. |
489
|
|
|
* Called by ?action=admin;area=managesearch;sa=createmsgindex. |
490
|
|
|
* Linked from the EditSearchMethod screen. |
491
|
|
|
* Requires the admin_forum permission. |
492
|
|
|
* Depending on the size of the message table, the process is divided in steps. |
493
|
|
|
* |
494
|
|
|
* @uses template_create_index() |
495
|
|
|
* @uses template_create_index_progress() |
496
|
|
|
* @uses template_create_index_done() |
497
|
|
|
*/ |
498
|
|
|
function CreateMessageIndex() |
499
|
|
|
{ |
500
|
|
|
global $modSettings, $context, $smcFunc, $db_prefix, $txt; |
501
|
|
|
|
502
|
|
|
// Scotty, we need more time... |
503
|
|
|
@set_time_limit(600); |
504
|
|
|
if (function_exists('apache_reset_timeout')) |
505
|
|
|
@apache_reset_timeout(); |
506
|
|
|
|
507
|
|
|
$context[$context['admin_menu_name']]['current_subsection'] = 'method'; |
508
|
|
|
$context['page_title'] = $txt['search_index_custom']; |
509
|
|
|
|
510
|
|
|
$messages_per_batch = 50; |
511
|
|
|
|
512
|
|
|
$index_properties = array( |
513
|
|
|
2 => array( |
514
|
|
|
'column_definition' => 'small', |
515
|
|
|
'step_size' => 1000000, |
516
|
|
|
), |
517
|
|
|
4 => array( |
518
|
|
|
'column_definition' => 'medium', |
519
|
|
|
'step_size' => 1000000, |
520
|
|
|
'max_size' => 16777215, |
521
|
|
|
), |
522
|
|
|
5 => array( |
523
|
|
|
'column_definition' => 'large', |
524
|
|
|
'step_size' => 100000000, |
525
|
|
|
'max_size' => 2000000000, |
526
|
|
|
), |
527
|
|
|
); |
528
|
|
|
|
529
|
|
|
if (isset($_REQUEST['resume']) && !empty($modSettings['search_custom_index_resume'])) |
530
|
|
|
{ |
531
|
|
|
$context['index_settings'] = $smcFunc['json_decode']($modSettings['search_custom_index_resume'], true); |
532
|
|
|
$context['start'] = (int) $context['index_settings']['resume_at']; |
533
|
|
|
unset($context['index_settings']['resume_at']); |
534
|
|
|
$context['step'] = 1; |
535
|
|
|
} |
536
|
|
|
else |
537
|
|
|
{ |
538
|
|
|
$context['index_settings'] = array( |
539
|
|
|
'bytes_per_word' => isset($_REQUEST['bytes_per_word']) && isset($index_properties[$_REQUEST['bytes_per_word']]) ? (int) $_REQUEST['bytes_per_word'] : 2, |
540
|
|
|
); |
541
|
|
|
$context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0; |
542
|
|
|
$context['step'] = isset($_REQUEST['step']) ? (int) $_REQUEST['step'] : 0; |
543
|
|
|
|
544
|
|
|
// admin timeouts are painful when building these long indexes - but only if we actually have such things enabled |
545
|
|
|
if (empty($modSettings['securityDisable']) && $_SESSION['admin_time'] + 3300 < time() && $context['step'] >= 1) |
546
|
|
|
$_SESSION['admin_time'] = time(); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
if ($context['step'] !== 0) |
550
|
|
|
checkSession('request'); |
551
|
|
|
|
552
|
|
|
// Step 0: let the user determine how they like their index. |
553
|
|
|
if ($context['step'] === 0) |
554
|
|
|
{ |
555
|
|
|
$context['sub_template'] = 'create_index'; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
// Step 1: insert all the words. |
559
|
|
|
if ($context['step'] === 1) |
560
|
|
|
{ |
561
|
|
|
$context['sub_template'] = 'create_index_progress'; |
562
|
|
|
|
563
|
|
|
if ($context['start'] === 0) |
564
|
|
|
{ |
565
|
|
|
db_extend(); |
566
|
|
|
$tables = $smcFunc['db_list_tables'](false, $db_prefix . 'log_search_words'); |
567
|
|
|
if (!empty($tables)) |
568
|
|
|
{ |
569
|
|
|
$smcFunc['db_search_query']('drop_words_table', ' |
570
|
|
|
DROP TABLE {db_prefix}log_search_words', |
571
|
|
|
array( |
572
|
|
|
) |
573
|
|
|
); |
574
|
|
|
} |
575
|
|
|
|
576
|
|
|
$smcFunc['db_create_word_search']($index_properties[$context['index_settings']['bytes_per_word']]['column_definition']); |
577
|
|
|
|
578
|
|
|
// Temporarily switch back to not using a search index. |
579
|
|
|
if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom') |
580
|
|
|
updateSettings(array('search_index' => '')); |
581
|
|
|
|
582
|
|
|
// Don't let simultanious processes be updating the search index. |
583
|
|
|
if (!empty($modSettings['search_custom_index_config'])) |
584
|
|
|
updateSettings(array('search_custom_index_config' => '')); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
$num_messages = array( |
588
|
|
|
'done' => 0, |
589
|
|
|
'todo' => 0, |
590
|
|
|
); |
591
|
|
|
|
592
|
|
|
$request = $smcFunc['db_query']('', ' |
593
|
|
|
SELECT id_msg >= {int:starting_id} AS todo, COUNT(*) AS num_messages |
594
|
|
|
FROM {db_prefix}messages |
595
|
|
|
GROUP BY todo', |
596
|
|
|
array( |
597
|
|
|
'starting_id' => $context['start'], |
598
|
|
|
) |
599
|
|
|
); |
600
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
601
|
|
|
$num_messages[empty($row['todo']) ? 'done' : 'todo'] = $row['num_messages']; |
602
|
|
|
|
603
|
|
|
if (empty($num_messages['todo'])) |
604
|
|
|
{ |
605
|
|
|
$context['step'] = 2; |
606
|
|
|
$context['percentage'] = 80; |
607
|
|
|
$context['start'] = 0; |
608
|
|
|
} |
609
|
|
|
else |
610
|
|
|
{ |
611
|
|
|
// Number of seconds before the next step. |
612
|
|
|
$stop = time() + 3; |
613
|
|
|
while (time() < $stop) |
614
|
|
|
{ |
615
|
|
|
$inserts = array(); |
616
|
|
|
$request = $smcFunc['db_query']('', ' |
617
|
|
|
SELECT id_msg, body |
618
|
|
|
FROM {db_prefix}messages |
619
|
|
|
WHERE id_msg BETWEEN {int:starting_id} AND {int:ending_id} |
620
|
|
|
LIMIT {int:limit}', |
621
|
|
|
array( |
622
|
|
|
'starting_id' => $context['start'], |
623
|
|
|
'ending_id' => $context['start'] + $messages_per_batch - 1, |
624
|
|
|
'limit' => $messages_per_batch, |
625
|
|
|
) |
626
|
|
|
); |
627
|
|
|
$forced_break = false; |
628
|
|
|
$number_processed = 0; |
629
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
630
|
|
|
{ |
631
|
|
|
// In theory it's possible for one of these to take friggin ages so add more timeout protection. |
632
|
|
|
if ($stop < time()) |
633
|
|
|
{ |
634
|
|
|
$forced_break = true; |
635
|
|
|
break; |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
$number_processed++; |
639
|
|
|
foreach (text2words($row['body'], $context['index_settings']['bytes_per_word'], true) as $id_word) |
640
|
|
|
{ |
641
|
|
|
$inserts[] = array($id_word, $row['id_msg']); |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
$num_messages['done'] += $number_processed; |
645
|
|
|
$num_messages['todo'] -= $number_processed; |
646
|
|
|
$smcFunc['db_free_result']($request); |
647
|
|
|
|
648
|
|
|
$context['start'] += $forced_break ? $number_processed : $messages_per_batch; |
649
|
|
|
|
650
|
|
|
if (!empty($inserts)) |
651
|
|
|
$smcFunc['db_insert']('ignore', |
652
|
|
|
'{db_prefix}log_search_words', |
653
|
|
|
array('id_word' => 'int', 'id_msg' => 'int'), |
654
|
|
|
$inserts, |
655
|
|
|
array('id_word', 'id_msg') |
656
|
|
|
); |
657
|
|
|
if ($num_messages['todo'] === 0) |
658
|
|
|
{ |
659
|
|
|
$context['step'] = 2; |
660
|
|
|
$context['start'] = 0; |
661
|
|
|
break; |
662
|
|
|
} |
663
|
|
|
else |
664
|
|
|
updateSettings(array('search_custom_index_resume' => $smcFunc['json_encode'](array_merge($context['index_settings'], array('resume_at' => $context['start']))))); |
665
|
|
|
} |
666
|
|
|
|
667
|
|
|
// Since there are still two steps to go, 80% is the maximum here. |
668
|
|
|
$context['percentage'] = round($num_messages['done'] / ($num_messages['done'] + $num_messages['todo']), 3) * 80; |
669
|
|
|
} |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
// Step 2: removing the words that occur too often and are of no use. |
673
|
|
|
elseif ($context['step'] === 2) |
674
|
|
|
{ |
675
|
|
|
if ($context['index_settings']['bytes_per_word'] < 4) |
676
|
|
|
$context['step'] = 3; |
677
|
|
|
else |
678
|
|
|
{ |
679
|
|
|
$stop_words = $context['start'] === 0 || empty($modSettings['search_stopwords']) ? array() : explode(',', $modSettings['search_stopwords']); |
680
|
|
|
$stop = time() + 3; |
681
|
|
|
$context['sub_template'] = 'create_index_progress'; |
682
|
|
|
$max_messages = ceil(60 * $modSettings['totalMessages'] / 100); |
683
|
|
|
|
684
|
|
|
while (time() < $stop) |
685
|
|
|
{ |
686
|
|
|
$request = $smcFunc['db_query']('', ' |
687
|
|
|
SELECT id_word, COUNT(id_word) AS num_words |
688
|
|
|
FROM {db_prefix}log_search_words |
689
|
|
|
WHERE id_word BETWEEN {int:starting_id} AND {int:ending_id} |
690
|
|
|
GROUP BY id_word |
691
|
|
|
HAVING COUNT(id_word) > {int:minimum_messages}', |
692
|
|
|
array( |
693
|
|
|
'starting_id' => $context['start'], |
694
|
|
|
'ending_id' => $context['start'] + $index_properties[$context['index_settings']['bytes_per_word']]['step_size'] - 1, |
695
|
|
|
'minimum_messages' => $max_messages, |
696
|
|
|
) |
697
|
|
|
); |
698
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
699
|
|
|
$stop_words[] = $row['id_word']; |
700
|
|
|
$smcFunc['db_free_result']($request); |
701
|
|
|
|
702
|
|
|
updateSettings(array('search_stopwords' => implode(',', $stop_words))); |
703
|
|
|
|
704
|
|
|
if (!empty($stop_words)) |
705
|
|
|
$smcFunc['db_query']('', ' |
706
|
|
|
DELETE FROM {db_prefix}log_search_words |
707
|
|
|
WHERE id_word in ({array_int:stop_words})', |
708
|
|
|
array( |
709
|
|
|
'stop_words' => $stop_words, |
710
|
|
|
) |
711
|
|
|
); |
712
|
|
|
|
713
|
|
|
$context['start'] += $index_properties[$context['index_settings']['bytes_per_word']]['step_size']; |
714
|
|
|
if ($context['start'] > $index_properties[$context['index_settings']['bytes_per_word']]['max_size']) |
715
|
|
|
{ |
716
|
|
|
$context['step'] = 3; |
717
|
|
|
break; |
718
|
|
|
} |
719
|
|
|
} |
720
|
|
|
$context['percentage'] = 80 + round($context['start'] / $index_properties[$context['index_settings']['bytes_per_word']]['max_size'], 3) * 20; |
721
|
|
|
} |
722
|
|
|
} |
723
|
|
|
|
724
|
|
|
// Step 3: remove words not distinctive enough. |
725
|
|
|
if ($context['step'] === 3) |
726
|
|
|
{ |
727
|
|
|
$context['sub_template'] = 'create_index_done'; |
728
|
|
|
|
729
|
|
|
updateSettings(array('search_index' => 'custom', 'search_custom_index_config' => $smcFunc['json_encode']($context['index_settings']))); |
730
|
|
|
$smcFunc['db_query']('', ' |
731
|
|
|
DELETE FROM {db_prefix}settings |
732
|
|
|
WHERE variable = {string:search_custom_index_resume}', |
733
|
|
|
array( |
734
|
|
|
'search_custom_index_resume' => 'search_custom_index_resume', |
735
|
|
|
) |
736
|
|
|
); |
737
|
|
|
} |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
/** |
741
|
|
|
* Get the installed Search API implementations. |
742
|
|
|
* This function checks for patterns in comments on top of the Search-API files! |
743
|
|
|
* In addition to filenames pattern. |
744
|
|
|
* It loads the search API classes if identified. |
745
|
|
|
* This function is used by EditSearchMethod to list all installed API implementations. |
746
|
|
|
*/ |
747
|
|
|
function loadSearchAPIs() |
748
|
|
|
{ |
749
|
|
|
global $sourcedir, $txt; |
750
|
|
|
|
751
|
|
|
// Ensure we have class. |
752
|
|
|
require_once($sourcedir . '/Class-SearchAPI.php'); |
753
|
|
|
|
754
|
|
|
$apis = array(); |
755
|
|
|
if ($dh = opendir($sourcedir)) |
756
|
|
|
{ |
757
|
|
|
while (($file = readdir($dh)) !== false) |
758
|
|
|
{ |
759
|
|
|
if (is_file($sourcedir . '/' . $file) && preg_match('~^SearchAPI-([A-Za-z\d_]+)\.php$~', $file, $matches)) |
760
|
|
|
{ |
761
|
|
|
// Check this is definitely a valid API! |
762
|
|
|
$fp = fopen($sourcedir . '/' . $file, 'rb'); |
763
|
|
|
$header = fread($fp, 4096); |
764
|
|
|
fclose($fp); |
765
|
|
|
|
766
|
|
|
if (strpos($header, '* SearchAPI-' . $matches[1] . '.php') !== false) |
767
|
|
|
{ |
768
|
|
|
require_once($sourcedir . '/' . $file); |
769
|
|
|
|
770
|
|
|
$index_name = strtolower($matches[1]); |
771
|
|
|
$search_class_name = $index_name . '_search'; |
772
|
|
|
$searchAPI = new $search_class_name(); |
773
|
|
|
|
774
|
|
|
// No Support? NEXT! |
775
|
|
|
if (!$searchAPI->is_supported) |
776
|
|
|
continue; |
777
|
|
|
|
778
|
|
|
$apis[$index_name] = array( |
779
|
|
|
'filename' => $file, |
780
|
|
|
'setting_index' => $index_name, |
781
|
|
|
'has_template' => in_array($index_name, array('custom', 'fulltext', 'standard')), |
782
|
|
|
'label' => $index_name && isset($txt['search_index_' . $index_name]) ? $txt['search_index_' . $index_name] : '', |
783
|
|
|
'desc' => $index_name && isset($txt['search_index_' . $index_name . '_desc']) ? $txt['search_index_' . $index_name . '_desc'] : '', |
784
|
|
|
); |
785
|
|
|
} |
786
|
|
|
} |
787
|
|
|
} |
788
|
|
|
} |
789
|
|
|
closedir($dh); |
790
|
|
|
|
791
|
|
|
return $apis; |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* Checks if the message table already has a fulltext index created and returns the key name |
796
|
|
|
* Determines if a db is capable of creating a fulltext index |
797
|
|
|
*/ |
798
|
|
|
function detectFulltextIndex() |
799
|
|
|
{ |
800
|
|
|
global $smcFunc, $context, $db_prefix; |
801
|
|
|
|
802
|
|
|
// We need this for db_get_version |
803
|
|
|
db_extend(); |
804
|
|
|
|
805
|
|
|
if ($smcFunc['db_title'] === POSTGRE_TITLE) |
806
|
|
|
{ |
807
|
|
|
$request = $smcFunc['db_query']('', ' |
808
|
|
|
SELECT |
809
|
|
|
indexname |
810
|
|
|
FROM pg_tables t |
811
|
|
|
LEFT OUTER JOIN |
812
|
|
|
(SELECT c.relname AS ctablename, ipg.relname AS indexname, indexrelname FROM pg_index x |
813
|
|
|
JOIN pg_class c ON c.oid = x.indrelid |
814
|
|
|
JOIN pg_class ipg ON ipg.oid = x.indexrelid |
815
|
|
|
JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid) |
816
|
|
|
AS foo |
817
|
|
|
ON t.tablename = foo.ctablename |
818
|
|
|
WHERE t.schemaname= {string:schema} and indexname = {string:messages_ftx}', |
819
|
|
|
array( |
820
|
|
|
'schema' => 'public', |
821
|
|
|
'messages_ftx' => $db_prefix . 'messages_ftx', |
822
|
|
|
) |
823
|
|
|
); |
824
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
825
|
|
|
$context['fulltext_index'][] = $row['indexname']; |
826
|
|
|
} |
827
|
|
|
else |
828
|
|
|
{ |
829
|
|
|
$request = $smcFunc['db_query']('', ' |
830
|
|
|
SHOW INDEX |
831
|
|
|
FROM {db_prefix}messages', |
832
|
|
|
array( |
833
|
|
|
) |
834
|
|
|
); |
835
|
|
|
$context['fulltext_index'] = array(); |
836
|
|
|
if ($request !== false || $smcFunc['db_num_rows']($request) != 0) |
837
|
|
|
{ |
838
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
839
|
|
|
if ($row['Column_name'] == 'body' && (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT' || isset($row['Comment']) && $row['Comment'] == 'FULLTEXT')) |
840
|
|
|
$context['fulltext_index'][] = $row['Key_name']; |
841
|
|
|
$smcFunc['db_free_result']($request); |
842
|
|
|
|
843
|
|
|
if (is_array($context['fulltext_index'])) |
844
|
|
|
$context['fulltext_index'] = array_unique($context['fulltext_index']); |
845
|
|
|
} |
846
|
|
|
|
847
|
|
|
if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0) |
848
|
|
|
$request = $smcFunc['db_query']('', ' |
849
|
|
|
SHOW TABLE STATUS |
850
|
|
|
FROM {string:database_name} |
851
|
|
|
LIKE {string:table_name}', |
852
|
|
|
array( |
853
|
|
|
'database_name' => '`' . strtr($match[1], array('`' => '')) . '`', |
854
|
|
|
'table_name' => str_replace('_', '\_', $match[2]) . 'messages', |
855
|
|
|
) |
856
|
|
|
); |
857
|
|
|
else |
858
|
|
|
$request = $smcFunc['db_query']('', ' |
859
|
|
|
SHOW TABLE STATUS |
860
|
|
|
LIKE {string:table_name}', |
861
|
|
|
array( |
862
|
|
|
'table_name' => str_replace('_', '\_', $db_prefix) . 'messages', |
863
|
|
|
) |
864
|
|
|
); |
865
|
|
|
|
866
|
|
|
if ($request !== false) |
867
|
|
|
{ |
868
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
869
|
|
|
if (isset($row['Engine']) && strtolower($row['Engine']) != 'myisam' && !(strtolower($row['Engine']) == 'innodb' && version_compare($smcFunc['db_get_version'](), '5.6.4', '>='))) |
870
|
|
|
$context['cannot_create_fulltext'] = true; |
871
|
|
|
|
872
|
|
|
$smcFunc['db_free_result']($request); |
873
|
|
|
} |
874
|
|
|
} |
875
|
|
|
} |
876
|
|
|
|
877
|
|
|
?> |