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.0 |
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
|
|
|
// Default the sub-action to 'edit search settings'. |
52
|
|
|
$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'weights'; |
53
|
|
|
|
54
|
|
|
$context['sub_action'] = $_REQUEST['sa']; |
55
|
|
|
|
56
|
|
|
// Create the tabs for the template. |
57
|
|
|
$context[$context['admin_menu_name']]['tab_data'] = array( |
58
|
|
|
'title' => $txt['manage_search'], |
59
|
|
|
'help' => 'search', |
60
|
|
|
'description' => $txt['search_settings_desc'], |
61
|
|
|
'tabs' => array( |
62
|
|
|
'weights' => array( |
63
|
|
|
'description' => $txt['search_weights_desc'], |
64
|
|
|
), |
65
|
|
|
'method' => array( |
66
|
|
|
'description' => $txt['search_method_desc'], |
67
|
|
|
), |
68
|
|
|
'settings' => array( |
69
|
|
|
'description' => $txt['search_settings_desc'], |
70
|
|
|
), |
71
|
|
|
), |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
call_integration_hook('integrate_manage_search', array(&$subActions)); |
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
|
|
|
$smcFunc['db_query']('', ' |
271
|
|
|
ALTER TABLE {db_prefix}messages |
272
|
|
|
DROP INDEX ' . implode(', |
273
|
|
|
DROP INDEX ', $context['fulltext_index']), |
274
|
|
|
array( |
275
|
|
|
'db_error_skip' => true, |
276
|
|
|
) |
277
|
|
|
); |
278
|
|
|
|
279
|
|
|
// Go back to the default search method. |
280
|
|
|
if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext') |
281
|
|
|
updateSettings(array( |
282
|
|
|
'search_index' => '', |
283
|
|
|
)); |
284
|
|
|
redirectexit('action=admin;area=managesearch;sa=method'); |
285
|
|
|
} |
286
|
|
|
elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removecustom') |
287
|
|
|
{ |
288
|
|
|
checkSession('get'); |
289
|
|
|
validateToken('admin-msm', 'get'); |
290
|
|
|
|
291
|
|
|
db_extend(); |
292
|
|
|
$tables = $smcFunc['db_list_tables'](false, $db_prefix . 'log_search_words'); |
293
|
|
|
if (!empty($tables)) |
294
|
|
|
{ |
295
|
|
|
$smcFunc['db_search_query']('drop_words_table', ' |
296
|
|
|
DROP TABLE {db_prefix}log_search_words', |
297
|
|
|
array( |
298
|
|
|
) |
299
|
|
|
); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
updateSettings(array( |
303
|
|
|
'search_custom_index_config' => '', |
304
|
|
|
'search_custom_index_resume' => '', |
305
|
|
|
)); |
306
|
|
|
|
307
|
|
|
// Go back to the default search method. |
308
|
|
|
if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom') |
309
|
|
|
updateSettings(array( |
310
|
|
|
'search_index' => '', |
311
|
|
|
)); |
312
|
|
|
redirectexit('action=admin;area=managesearch;sa=method'); |
313
|
|
|
} |
314
|
|
|
elseif (isset($_POST['save'])) |
315
|
|
|
{ |
316
|
|
|
checkSession(); |
317
|
|
|
validateToken('admin-msmpost'); |
318
|
|
|
|
319
|
|
|
updateSettings(array( |
320
|
|
|
'search_index' => empty($_POST['search_index']) || (!in_array($_POST['search_index'], array('fulltext', 'custom')) && !isset($context['search_apis'][$_POST['search_index']])) ? '' : $_POST['search_index'], |
321
|
|
|
'search_force_index' => isset($_POST['search_force_index']) ? '1' : '0', |
322
|
|
|
'search_match_words' => isset($_POST['search_match_words']) ? '1' : '0', |
323
|
|
|
)); |
324
|
|
|
redirectexit('action=admin;area=managesearch;sa=method'); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$context['table_info'] = array( |
328
|
|
|
'data_length' => 0, |
329
|
|
|
'index_length' => 0, |
330
|
|
|
'fulltext_length' => 0, |
331
|
|
|
'custom_index_length' => 0, |
332
|
|
|
); |
333
|
|
|
|
334
|
|
|
// Get some info about the messages table, to show its size and index size. |
335
|
|
|
if ($db_type == 'mysql') |
336
|
|
|
{ |
337
|
|
|
if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0) |
338
|
|
|
$request = $smcFunc['db_query']('', ' |
339
|
|
|
SHOW TABLE STATUS |
340
|
|
|
FROM {string:database_name} |
341
|
|
|
LIKE {string:table_name}', |
342
|
|
|
array( |
343
|
|
|
'database_name' => '`' . strtr($match[1], array('`' => '')) . '`', |
344
|
|
|
'table_name' => str_replace('_', '\_', $match[2]) . 'messages', |
345
|
|
|
) |
346
|
|
|
); |
347
|
|
|
else |
348
|
|
|
$request = $smcFunc['db_query']('', ' |
349
|
|
|
SHOW TABLE STATUS |
350
|
|
|
LIKE {string:table_name}', |
351
|
|
|
array( |
352
|
|
|
'table_name' => str_replace('_', '\_', $db_prefix) . 'messages', |
353
|
|
|
) |
354
|
|
|
); |
355
|
|
|
if ($request !== false && $smcFunc['db_num_rows']($request) == 1) |
356
|
|
|
{ |
357
|
|
|
// Only do this if the user has permission to execute this query. |
358
|
|
|
$row = $smcFunc['db_fetch_assoc']($request); |
359
|
|
|
$context['table_info']['data_length'] = $row['Data_length']; |
360
|
|
|
$context['table_info']['index_length'] = $row['Index_length']; |
361
|
|
|
$context['table_info']['fulltext_length'] = $row['Index_length']; |
362
|
|
|
$smcFunc['db_free_result']($request); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
// Now check the custom index table, if it exists at all. |
366
|
|
|
if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0) |
367
|
|
|
$request = $smcFunc['db_query']('', ' |
368
|
|
|
SHOW TABLE STATUS |
369
|
|
|
FROM {string:database_name} |
370
|
|
|
LIKE {string:table_name}', |
371
|
|
|
array( |
372
|
|
|
'database_name' => '`' . strtr($match[1], array('`' => '')) . '`', |
373
|
|
|
'table_name' => str_replace('_', '\_', $match[2]) . 'log_search_words', |
374
|
|
|
) |
375
|
|
|
); |
376
|
|
|
else |
377
|
|
|
$request = $smcFunc['db_query']('', ' |
378
|
|
|
SHOW TABLE STATUS |
379
|
|
|
LIKE {string:table_name}', |
380
|
|
|
array( |
381
|
|
|
'table_name' => str_replace('_', '\_', $db_prefix) . 'log_search_words', |
382
|
|
|
) |
383
|
|
|
); |
384
|
|
|
if ($request !== false && $smcFunc['db_num_rows']($request) == 1) |
385
|
|
|
{ |
386
|
|
|
// Only do this if the user has permission to execute this query. |
387
|
|
|
$row = $smcFunc['db_fetch_assoc']($request); |
388
|
|
|
$context['table_info']['index_length'] += $row['Data_length'] + $row['Index_length']; |
389
|
|
|
$context['table_info']['custom_index_length'] = $row['Data_length'] + $row['Index_length']; |
390
|
|
|
$smcFunc['db_free_result']($request); |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
elseif ($db_type == 'postgresql') |
394
|
|
|
{ |
395
|
|
|
// In order to report the sizes correctly we need to perform vacuum (optimize) on the tables we will be using. |
396
|
|
|
//db_extend(); |
397
|
|
|
//$temp_tables = $smcFunc['db_list_tables'](); |
398
|
|
|
//foreach ($temp_tables as $table) |
399
|
|
|
// if ($table == $db_prefix. 'messages' || $table == $db_prefix. 'log_search_words') |
400
|
|
|
// $smcFunc['db_optimize_table']($table); |
401
|
|
|
|
402
|
|
|
// PostGreSql has some hidden sizes. |
403
|
|
|
$request = $smcFunc['db_query']('', ' |
404
|
|
|
SELECT |
405
|
|
|
indexname, |
406
|
|
|
pg_relation_size(quote_ident(t.tablename)::text) AS table_size, |
407
|
|
|
pg_relation_size(quote_ident(indexrelname)::text) AS index_size |
408
|
|
|
FROM pg_tables t |
409
|
|
|
LEFT OUTER JOIN pg_class c ON t.tablename=c.relname |
410
|
|
|
LEFT OUTER JOIN |
411
|
|
|
(SELECT c.relname AS ctablename, ipg.relname AS indexname, indexrelname FROM pg_index x |
412
|
|
|
JOIN pg_class c ON c.oid = x.indrelid |
413
|
|
|
JOIN pg_class ipg ON ipg.oid = x.indexrelid |
414
|
|
|
JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid) |
415
|
|
|
AS foo |
416
|
|
|
ON t.tablename = foo.ctablename |
417
|
|
|
WHERE t.schemaname= {string:schema} and ( |
418
|
|
|
indexname = {string:messages_ftx} OR indexname = {string:log_search_words} )', |
419
|
|
|
array( |
420
|
|
|
'messages_ftx' => $db_prefix . 'messages_ftx', |
421
|
|
|
'log_search_words' => $db_prefix . 'log_search_words', |
422
|
|
|
'schema' => 'public', |
423
|
|
|
) |
424
|
|
|
); |
425
|
|
|
|
426
|
|
|
if ($request !== false && $smcFunc['db_num_rows']($request) > 0) |
427
|
|
|
{ |
428
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
429
|
|
|
{ |
430
|
|
|
if ($row['indexname'] == $db_prefix . 'messages_ftx') |
431
|
|
|
{ |
432
|
|
|
$context['table_info']['data_length'] = (int) $row['table_size']; |
433
|
|
|
$context['table_info']['index_length'] = (int) $row['index_size']; |
434
|
|
|
$context['table_info']['fulltext_length'] = (int) $row['index_size']; |
435
|
|
|
} |
436
|
|
|
elseif ($row['indexname'] == $db_prefix . 'log_search_words') |
437
|
|
|
{ |
438
|
|
|
$context['table_info']['index_length'] = (int) $row['index_size']; |
439
|
|
|
$context['table_info']['custom_index_length'] = (int) $row['index_size']; |
440
|
|
|
} |
441
|
|
|
} |
442
|
|
|
$smcFunc['db_free_result']($request); |
443
|
|
|
} |
444
|
|
|
else |
445
|
|
|
// Didn't work for some reason... |
446
|
|
|
$context['table_info'] = array( |
447
|
|
|
'data_length' => $txt['not_applicable'], |
448
|
|
|
'index_length' => $txt['not_applicable'], |
449
|
|
|
'fulltext_length' => $txt['not_applicable'], |
450
|
|
|
'custom_index_length' => $txt['not_applicable'], |
451
|
|
|
); |
452
|
|
|
} |
453
|
|
|
else |
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
|
|
|
// Format the data and index length in kilobytes. |
462
|
|
|
foreach ($context['table_info'] as $type => $size) |
463
|
|
|
{ |
464
|
|
|
// If it's not numeric then just break. This database engine doesn't support size. |
465
|
|
|
if (!is_numeric($size)) |
466
|
|
|
break; |
467
|
|
|
|
468
|
|
|
$context['table_info'][$type] = comma_format($context['table_info'][$type] / 1024) . ' ' . $txt['search_method_kilobytes']; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
$context['custom_index'] = !empty($modSettings['search_custom_index_config']); |
472
|
|
|
$context['partial_custom_index'] = !empty($modSettings['search_custom_index_resume']) && empty($modSettings['search_custom_index_config']); |
473
|
|
|
$context['double_index'] = !empty($context['fulltext_index']) && $context['custom_index']; |
474
|
|
|
|
475
|
|
|
createToken('admin-msmpost'); |
476
|
|
|
createToken('admin-msm', 'get'); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Create a custom search index for the messages table. |
481
|
|
|
* Called by ?action=admin;area=managesearch;sa=createmsgindex. |
482
|
|
|
* Linked from the EditSearchMethod screen. |
483
|
|
|
* Requires the admin_forum permission. |
484
|
|
|
* Depending on the size of the message table, the process is divided in steps. |
485
|
|
|
* |
486
|
|
|
* @uses template_create_index() |
487
|
|
|
* @uses template_create_index_progress() |
488
|
|
|
* @uses template_create_index_done() |
489
|
|
|
*/ |
490
|
|
|
function CreateMessageIndex() |
491
|
|
|
{ |
492
|
|
|
global $modSettings, $context, $smcFunc, $db_prefix, $txt; |
493
|
|
|
|
494
|
|
|
// Scotty, we need more time... |
495
|
|
|
@set_time_limit(600); |
496
|
|
|
if (function_exists('apache_reset_timeout')) |
497
|
|
|
@apache_reset_timeout(); |
498
|
|
|
|
499
|
|
|
$context[$context['admin_menu_name']]['current_subsection'] = 'method'; |
500
|
|
|
$context['page_title'] = $txt['search_index_custom']; |
501
|
|
|
|
502
|
|
|
$messages_per_batch = 50; |
503
|
|
|
|
504
|
|
|
$index_properties = array( |
505
|
|
|
2 => array( |
506
|
|
|
'column_definition' => 'small', |
507
|
|
|
'step_size' => 1000000, |
508
|
|
|
), |
509
|
|
|
4 => array( |
510
|
|
|
'column_definition' => 'medium', |
511
|
|
|
'step_size' => 1000000, |
512
|
|
|
'max_size' => 16777215, |
513
|
|
|
), |
514
|
|
|
5 => array( |
515
|
|
|
'column_definition' => 'large', |
516
|
|
|
'step_size' => 100000000, |
517
|
|
|
'max_size' => 2000000000, |
518
|
|
|
), |
519
|
|
|
); |
520
|
|
|
|
521
|
|
|
if (isset($_REQUEST['resume']) && !empty($modSettings['search_custom_index_resume'])) |
522
|
|
|
{ |
523
|
|
|
$context['index_settings'] = $smcFunc['json_decode']($modSettings['search_custom_index_resume'], true); |
524
|
|
|
$context['start'] = (int) $context['index_settings']['resume_at']; |
525
|
|
|
unset($context['index_settings']['resume_at']); |
526
|
|
|
$context['step'] = 1; |
527
|
|
|
} |
528
|
|
|
else |
529
|
|
|
{ |
530
|
|
|
$context['index_settings'] = array( |
531
|
|
|
'bytes_per_word' => isset($_REQUEST['bytes_per_word']) && isset($index_properties[$_REQUEST['bytes_per_word']]) ? (int) $_REQUEST['bytes_per_word'] : 2, |
532
|
|
|
); |
533
|
|
|
$context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0; |
534
|
|
|
$context['step'] = isset($_REQUEST['step']) ? (int) $_REQUEST['step'] : 0; |
535
|
|
|
|
536
|
|
|
// admin timeouts are painful when building these long indexes - but only if we actually have such things enabled |
537
|
|
|
if (empty($modSettings['securityDisable']) && $_SESSION['admin_time'] + 3300 < time() && $context['step'] >= 1) |
538
|
|
|
$_SESSION['admin_time'] = time(); |
539
|
|
|
} |
540
|
|
|
|
541
|
|
|
if ($context['step'] !== 0) |
542
|
|
|
checkSession('request'); |
543
|
|
|
|
544
|
|
|
// Step 0: let the user determine how they like their index. |
545
|
|
|
if ($context['step'] === 0) |
546
|
|
|
{ |
547
|
|
|
$context['sub_template'] = 'create_index'; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
// Step 1: insert all the words. |
551
|
|
|
if ($context['step'] === 1) |
552
|
|
|
{ |
553
|
|
|
$context['sub_template'] = 'create_index_progress'; |
554
|
|
|
|
555
|
|
|
if ($context['start'] === 0) |
556
|
|
|
{ |
557
|
|
|
db_extend(); |
558
|
|
|
$tables = $smcFunc['db_list_tables'](false, $db_prefix . 'log_search_words'); |
559
|
|
|
if (!empty($tables)) |
560
|
|
|
{ |
561
|
|
|
$smcFunc['db_search_query']('drop_words_table', ' |
562
|
|
|
DROP TABLE {db_prefix}log_search_words', |
563
|
|
|
array( |
564
|
|
|
) |
565
|
|
|
); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
$smcFunc['db_create_word_search']($index_properties[$context['index_settings']['bytes_per_word']]['column_definition']); |
569
|
|
|
|
570
|
|
|
// Temporarily switch back to not using a search index. |
571
|
|
|
if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom') |
572
|
|
|
updateSettings(array('search_index' => '')); |
573
|
|
|
|
574
|
|
|
// Don't let simultanious processes be updating the search index. |
575
|
|
|
if (!empty($modSettings['search_custom_index_config'])) |
576
|
|
|
updateSettings(array('search_custom_index_config' => '')); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
$num_messages = array( |
580
|
|
|
'done' => 0, |
581
|
|
|
'todo' => 0, |
582
|
|
|
); |
583
|
|
|
|
584
|
|
|
$request = $smcFunc['db_query']('', ' |
585
|
|
|
SELECT id_msg >= {int:starting_id} AS todo, COUNT(*) AS num_messages |
586
|
|
|
FROM {db_prefix}messages |
587
|
|
|
GROUP BY todo', |
588
|
|
|
array( |
589
|
|
|
'starting_id' => $context['start'], |
590
|
|
|
) |
591
|
|
|
); |
592
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
593
|
|
|
$num_messages[empty($row['todo']) ? 'done' : 'todo'] = $row['num_messages']; |
594
|
|
|
|
595
|
|
|
if (empty($num_messages['todo'])) |
596
|
|
|
{ |
597
|
|
|
$context['step'] = 2; |
598
|
|
|
$context['percentage'] = 80; |
599
|
|
|
$context['start'] = 0; |
600
|
|
|
} |
601
|
|
|
else |
602
|
|
|
{ |
603
|
|
|
// Number of seconds before the next step. |
604
|
|
|
$stop = time() + 3; |
605
|
|
|
while (time() < $stop) |
606
|
|
|
{ |
607
|
|
|
$inserts = array(); |
608
|
|
|
$request = $smcFunc['db_query']('', ' |
609
|
|
|
SELECT id_msg, body |
610
|
|
|
FROM {db_prefix}messages |
611
|
|
|
WHERE id_msg BETWEEN {int:starting_id} AND {int:ending_id} |
612
|
|
|
LIMIT {int:limit}', |
613
|
|
|
array( |
614
|
|
|
'starting_id' => $context['start'], |
615
|
|
|
'ending_id' => $context['start'] + $messages_per_batch - 1, |
616
|
|
|
'limit' => $messages_per_batch, |
617
|
|
|
) |
618
|
|
|
); |
619
|
|
|
$forced_break = false; |
620
|
|
|
$number_processed = 0; |
621
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
622
|
|
|
{ |
623
|
|
|
// In theory it's possible for one of these to take friggin ages so add more timeout protection. |
624
|
|
|
if ($stop < time()) |
625
|
|
|
{ |
626
|
|
|
$forced_break = true; |
627
|
|
|
break; |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
$number_processed++; |
631
|
|
|
foreach (text2words($row['body'], $context['index_settings']['bytes_per_word'], true) as $id_word) |
632
|
|
|
{ |
633
|
|
|
$inserts[] = array($id_word, $row['id_msg']); |
634
|
|
|
} |
635
|
|
|
} |
636
|
|
|
$num_messages['done'] += $number_processed; |
637
|
|
|
$num_messages['todo'] -= $number_processed; |
638
|
|
|
$smcFunc['db_free_result']($request); |
639
|
|
|
|
640
|
|
|
$context['start'] += $forced_break ? $number_processed : $messages_per_batch; |
641
|
|
|
|
642
|
|
|
if (!empty($inserts)) |
643
|
|
|
$smcFunc['db_insert']('ignore', |
644
|
|
|
'{db_prefix}log_search_words', |
645
|
|
|
array('id_word' => 'int', 'id_msg' => 'int'), |
646
|
|
|
$inserts, |
647
|
|
|
array('id_word', 'id_msg') |
648
|
|
|
); |
649
|
|
|
if ($num_messages['todo'] === 0) |
650
|
|
|
{ |
651
|
|
|
$context['step'] = 2; |
652
|
|
|
$context['start'] = 0; |
653
|
|
|
break; |
654
|
|
|
} |
655
|
|
|
else |
656
|
|
|
updateSettings(array('search_custom_index_resume' => $smcFunc['json_encode'](array_merge($context['index_settings'], array('resume_at' => $context['start']))))); |
657
|
|
|
} |
658
|
|
|
|
659
|
|
|
// Since there are still two steps to go, 80% is the maximum here. |
660
|
|
|
$context['percentage'] = round($num_messages['done'] / ($num_messages['done'] + $num_messages['todo']), 3) * 80; |
661
|
|
|
} |
662
|
|
|
} |
663
|
|
|
|
664
|
|
|
// Step 2: removing the words that occur too often and are of no use. |
665
|
|
|
elseif ($context['step'] === 2) |
666
|
|
|
{ |
667
|
|
|
if ($context['index_settings']['bytes_per_word'] < 4) |
668
|
|
|
$context['step'] = 3; |
669
|
|
|
else |
670
|
|
|
{ |
671
|
|
|
$stop_words = $context['start'] === 0 || empty($modSettings['search_stopwords']) ? array() : explode(',', $modSettings['search_stopwords']); |
672
|
|
|
$stop = time() + 3; |
673
|
|
|
$context['sub_template'] = 'create_index_progress'; |
674
|
|
|
$max_messages = ceil(60 * $modSettings['totalMessages'] / 100); |
675
|
|
|
|
676
|
|
|
while (time() < $stop) |
677
|
|
|
{ |
678
|
|
|
$request = $smcFunc['db_query']('', ' |
679
|
|
|
SELECT id_word, COUNT(id_word) AS num_words |
680
|
|
|
FROM {db_prefix}log_search_words |
681
|
|
|
WHERE id_word BETWEEN {int:starting_id} AND {int:ending_id} |
682
|
|
|
GROUP BY id_word |
683
|
|
|
HAVING COUNT(id_word) > {int:minimum_messages}', |
684
|
|
|
array( |
685
|
|
|
'starting_id' => $context['start'], |
686
|
|
|
'ending_id' => $context['start'] + $index_properties[$context['index_settings']['bytes_per_word']]['step_size'] - 1, |
687
|
|
|
'minimum_messages' => $max_messages, |
688
|
|
|
) |
689
|
|
|
); |
690
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
691
|
|
|
$stop_words[] = $row['id_word']; |
692
|
|
|
$smcFunc['db_free_result']($request); |
693
|
|
|
|
694
|
|
|
updateSettings(array('search_stopwords' => implode(',', $stop_words))); |
695
|
|
|
|
696
|
|
|
if (!empty($stop_words)) |
697
|
|
|
$smcFunc['db_query']('', ' |
698
|
|
|
DELETE FROM {db_prefix}log_search_words |
699
|
|
|
WHERE id_word in ({array_int:stop_words})', |
700
|
|
|
array( |
701
|
|
|
'stop_words' => $stop_words, |
702
|
|
|
) |
703
|
|
|
); |
704
|
|
|
|
705
|
|
|
$context['start'] += $index_properties[$context['index_settings']['bytes_per_word']]['step_size']; |
706
|
|
|
if ($context['start'] > $index_properties[$context['index_settings']['bytes_per_word']]['max_size']) |
707
|
|
|
{ |
708
|
|
|
$context['step'] = 3; |
709
|
|
|
break; |
710
|
|
|
} |
711
|
|
|
} |
712
|
|
|
$context['percentage'] = 80 + round($context['start'] / $index_properties[$context['index_settings']['bytes_per_word']]['max_size'], 3) * 20; |
713
|
|
|
} |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
// Step 3: remove words not distinctive enough. |
717
|
|
|
if ($context['step'] === 3) |
718
|
|
|
{ |
719
|
|
|
$context['sub_template'] = 'create_index_done'; |
720
|
|
|
|
721
|
|
|
updateSettings(array('search_index' => 'custom', 'search_custom_index_config' => $smcFunc['json_encode']($context['index_settings']))); |
722
|
|
|
$smcFunc['db_query']('', ' |
723
|
|
|
DELETE FROM {db_prefix}settings |
724
|
|
|
WHERE variable = {string:search_custom_index_resume}', |
725
|
|
|
array( |
726
|
|
|
'search_custom_index_resume' => 'search_custom_index_resume', |
727
|
|
|
) |
728
|
|
|
); |
729
|
|
|
} |
730
|
|
|
} |
731
|
|
|
|
732
|
|
|
/** |
733
|
|
|
* Get the installed Search API implementations. |
734
|
|
|
* This function checks for patterns in comments on top of the Search-API files! |
735
|
|
|
* In addition to filenames pattern. |
736
|
|
|
* It loads the search API classes if identified. |
737
|
|
|
* This function is used by EditSearchMethod to list all installed API implementations. |
738
|
|
|
*/ |
739
|
|
|
function loadSearchAPIs() |
740
|
|
|
{ |
741
|
|
|
global $sourcedir, $txt; |
742
|
|
|
|
743
|
|
|
// Ensure we have class. |
744
|
|
|
require_once($sourcedir . '/Class-SearchAPI.php'); |
745
|
|
|
|
746
|
|
|
$apis = array(); |
747
|
|
|
if ($dh = opendir($sourcedir)) |
748
|
|
|
{ |
749
|
|
|
while (($file = readdir($dh)) !== false) |
750
|
|
|
{ |
751
|
|
|
if (is_file($sourcedir . '/' . $file) && preg_match('~^SearchAPI-([A-Za-z\d_]+)\.php$~', $file, $matches)) |
752
|
|
|
{ |
753
|
|
|
// Check this is definitely a valid API! |
754
|
|
|
$fp = fopen($sourcedir . '/' . $file, 'rb'); |
755
|
|
|
$header = fread($fp, 4096); |
756
|
|
|
fclose($fp); |
757
|
|
|
|
758
|
|
|
if (strpos($header, '* SearchAPI-' . $matches[1] . '.php') !== false) |
759
|
|
|
{ |
760
|
|
|
require_once($sourcedir . '/' . $file); |
761
|
|
|
|
762
|
|
|
$index_name = strtolower($matches[1]); |
763
|
|
|
$search_class_name = $index_name . '_search'; |
764
|
|
|
$searchAPI = new $search_class_name(); |
765
|
|
|
|
766
|
|
|
// No Support? NEXT! |
767
|
|
|
if (!$searchAPI->is_supported) |
768
|
|
|
continue; |
769
|
|
|
|
770
|
|
|
$apis[$index_name] = array( |
771
|
|
|
'filename' => $file, |
772
|
|
|
'setting_index' => $index_name, |
773
|
|
|
'has_template' => in_array($index_name, array('custom', 'fulltext', 'standard')), |
774
|
|
|
'label' => $index_name && isset($txt['search_index_' . $index_name]) ? $txt['search_index_' . $index_name] : '', |
775
|
|
|
'desc' => $index_name && isset($txt['search_index_' . $index_name . '_desc']) ? $txt['search_index_' . $index_name . '_desc'] : '', |
776
|
|
|
); |
777
|
|
|
} |
778
|
|
|
} |
779
|
|
|
} |
780
|
|
|
} |
781
|
|
|
closedir($dh); |
782
|
|
|
|
783
|
|
|
return $apis; |
784
|
|
|
} |
785
|
|
|
|
786
|
|
|
/** |
787
|
|
|
* Checks if the message table already has a fulltext index created and returns the key name |
788
|
|
|
* Determines if a db is capable of creating a fulltext index |
789
|
|
|
*/ |
790
|
|
|
function detectFulltextIndex() |
791
|
|
|
{ |
792
|
|
|
global $smcFunc, $context, $db_prefix; |
793
|
|
|
|
794
|
|
|
// We need this for db_get_version |
795
|
|
|
db_extend(); |
796
|
|
|
|
797
|
|
|
if ($smcFunc['db_title'] === POSTGRE_TITLE) |
798
|
|
|
{ |
799
|
|
|
$request = $smcFunc['db_query']('', ' |
800
|
|
|
SELECT |
801
|
|
|
indexname |
802
|
|
|
FROM pg_tables t |
803
|
|
|
LEFT OUTER JOIN |
804
|
|
|
(SELECT c.relname AS ctablename, ipg.relname AS indexname, indexrelname FROM pg_index x |
805
|
|
|
JOIN pg_class c ON c.oid = x.indrelid |
806
|
|
|
JOIN pg_class ipg ON ipg.oid = x.indexrelid |
807
|
|
|
JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid) |
808
|
|
|
AS foo |
809
|
|
|
ON t.tablename = foo.ctablename |
810
|
|
|
WHERE t.schemaname= {string:schema} and indexname = {string:messages_ftx}', |
811
|
|
|
array( |
812
|
|
|
'schema' => 'public', |
813
|
|
|
'messages_ftx' => $db_prefix . 'messages_ftx', |
814
|
|
|
) |
815
|
|
|
); |
816
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
817
|
|
|
$context['fulltext_index'][] = $row['indexname']; |
818
|
|
|
} |
819
|
|
|
else |
820
|
|
|
{ |
821
|
|
|
$request = $smcFunc['db_query']('', ' |
822
|
|
|
SHOW INDEX |
823
|
|
|
FROM {db_prefix}messages', |
824
|
|
|
array( |
825
|
|
|
) |
826
|
|
|
); |
827
|
|
|
$context['fulltext_index'] = array(); |
828
|
|
|
if ($request !== false || $smcFunc['db_num_rows']($request) != 0) |
829
|
|
|
{ |
830
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
831
|
|
|
if ($row['Column_name'] == 'body' && (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT' || isset($row['Comment']) && $row['Comment'] == 'FULLTEXT')) |
832
|
|
|
$context['fulltext_index'][] = $row['Key_name']; |
833
|
|
|
$smcFunc['db_free_result']($request); |
834
|
|
|
|
835
|
|
|
if (is_array($context['fulltext_index'])) |
836
|
|
|
$context['fulltext_index'] = array_unique($context['fulltext_index']); |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0) |
840
|
|
|
$request = $smcFunc['db_query']('', ' |
841
|
|
|
SHOW TABLE STATUS |
842
|
|
|
FROM {string:database_name} |
843
|
|
|
LIKE {string:table_name}', |
844
|
|
|
array( |
845
|
|
|
'database_name' => '`' . strtr($match[1], array('`' => '')) . '`', |
846
|
|
|
'table_name' => str_replace('_', '\_', $match[2]) . 'messages', |
847
|
|
|
) |
848
|
|
|
); |
849
|
|
|
else |
850
|
|
|
$request = $smcFunc['db_query']('', ' |
851
|
|
|
SHOW TABLE STATUS |
852
|
|
|
LIKE {string:table_name}', |
853
|
|
|
array( |
854
|
|
|
'table_name' => str_replace('_', '\_', $db_prefix) . 'messages', |
855
|
|
|
) |
856
|
|
|
); |
857
|
|
|
|
858
|
|
|
if ($request !== false) |
859
|
|
|
{ |
860
|
|
|
while ($row = $smcFunc['db_fetch_assoc']($request)) |
861
|
|
|
if (isset($row['Engine']) && strtolower($row['Engine']) != 'myisam' && !(strtolower($row['Engine']) == 'innodb' && version_compare($smcFunc['db_get_version'](), '5.6.4', '>='))) |
862
|
|
|
$context['cannot_create_fulltext'] = true; |
863
|
|
|
|
864
|
|
|
$smcFunc['db_free_result']($request); |
865
|
|
|
} |
866
|
|
|
} |
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
?> |