Issues (1061)

Sources/ManageSearch.php (3 issues)

Labels
Severity
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 2020 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
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(), template_create_index_progress(), template_create_index_done()
487
 */
488
function CreateMessageIndex()
489
{
490
	global $modSettings, $context, $smcFunc, $db_prefix, $txt;
491
492
	// Scotty, we need more time...
493
	@set_time_limit(600);
494
	if (function_exists('apache_reset_timeout'))
495
		@apache_reset_timeout();
496
497
	$context[$context['admin_menu_name']]['current_subsection'] = 'method';
498
	$context['page_title'] = $txt['search_index_custom'];
499
500
	$messages_per_batch = 50;
501
502
	$index_properties = array(
503
		2 => array(
504
			'column_definition' => 'small',
505
			'step_size' => 1000000,
506
		),
507
		4 => array(
508
			'column_definition' => 'medium',
509
			'step_size' => 1000000,
510
			'max_size' => 16777215,
511
		),
512
		5 => array(
513
			'column_definition' => 'large',
514
			'step_size' => 100000000,
515
			'max_size' => 2000000000,
516
		),
517
	);
518
519
	if (isset($_REQUEST['resume']) && !empty($modSettings['search_custom_index_resume']))
520
	{
521
		$context['index_settings'] = $smcFunc['json_decode']($modSettings['search_custom_index_resume'], true);
522
		$context['start'] = (int) $context['index_settings']['resume_at'];
523
		unset($context['index_settings']['resume_at']);
524
		$context['step'] = 1;
525
	}
526
	else
527
	{
528
		$context['index_settings'] = array(
529
			'bytes_per_word' => isset($_REQUEST['bytes_per_word']) && isset($index_properties[$_REQUEST['bytes_per_word']]) ? (int) $_REQUEST['bytes_per_word'] : 2,
530
		);
531
		$context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
532
		$context['step'] = isset($_REQUEST['step']) ? (int) $_REQUEST['step'] : 0;
533
534
		// admin timeouts are painful when building these long indexes - but only if we actually have such things enabled
535
		if (empty($modSettings['securityDisable']) && $_SESSION['admin_time'] + 3300 < time() && $context['step'] >= 1)
536
			$_SESSION['admin_time'] = time();
537
	}
538
539
	if ($context['step'] !== 0)
540
		checkSession('request');
541
542
	// Step 0: let the user determine how they like their index.
543
	if ($context['step'] === 0)
544
	{
545
		$context['sub_template'] = 'create_index';
546
	}
547
548
	// Step 1: insert all the words.
549
	if ($context['step'] === 1)
550
	{
551
		$context['sub_template'] = 'create_index_progress';
552
553
		if ($context['start'] === 0)
554
		{
555
			db_extend();
556
			$tables = $smcFunc['db_list_tables'](false, $db_prefix . 'log_search_words');
557
			if (!empty($tables))
558
			{
559
				$smcFunc['db_search_query']('drop_words_table', '
560
					DROP TABLE {db_prefix}log_search_words',
561
					array(
562
					)
563
				);
564
			}
565
566
			$smcFunc['db_create_word_search']($index_properties[$context['index_settings']['bytes_per_word']]['column_definition']);
567
568
			// Temporarily switch back to not using a search index.
569
			if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom')
570
				updateSettings(array('search_index' => ''));
571
572
			// Don't let simultanious processes be updating the search index.
573
			if (!empty($modSettings['search_custom_index_config']))
574
				updateSettings(array('search_custom_index_config' => ''));
575
		}
576
577
		$num_messages = array(
578
			'done' => 0,
579
			'todo' => 0,
580
		);
581
582
		$request = $smcFunc['db_query']('', '
583
			SELECT id_msg >= {int:starting_id} AS todo, COUNT(*) AS num_messages
584
			FROM {db_prefix}messages
585
			GROUP BY todo',
586
			array(
587
				'starting_id' => $context['start'],
588
			)
589
		);
590
		while ($row = $smcFunc['db_fetch_assoc']($request))
591
			$num_messages[empty($row['todo']) ? 'done' : 'todo'] = $row['num_messages'];
592
593
		if (empty($num_messages['todo']))
594
		{
595
			$context['step'] = 2;
596
			$context['percentage'] = 80;
597
			$context['start'] = 0;
598
		}
599
		else
600
		{
601
			// Number of seconds before the next step.
602
			$stop = time() + 3;
603
			while (time() < $stop)
604
			{
605
				$inserts = array();
606
				$request = $smcFunc['db_query']('', '
607
					SELECT id_msg, body
608
					FROM {db_prefix}messages
609
					WHERE id_msg BETWEEN {int:starting_id} AND {int:ending_id}
610
					LIMIT {int:limit}',
611
					array(
612
						'starting_id' => $context['start'],
613
						'ending_id' => $context['start'] + $messages_per_batch - 1,
614
						'limit' => $messages_per_batch,
615
					)
616
				);
617
				$forced_break = false;
618
				$number_processed = 0;
619
				while ($row = $smcFunc['db_fetch_assoc']($request))
620
				{
621
					// In theory it's possible for one of these to take friggin ages so add more timeout protection.
622
					if ($stop < time())
623
					{
624
						$forced_break = true;
625
						break;
626
					}
627
628
					$number_processed++;
629
					foreach (text2words($row['body'], $context['index_settings']['bytes_per_word'], true) as $id_word)
630
					{
631
						$inserts[] = array($id_word, $row['id_msg']);
632
					}
633
				}
634
				$num_messages['done'] += $number_processed;
635
				$num_messages['todo'] -= $number_processed;
636
				$smcFunc['db_free_result']($request);
637
638
				$context['start'] += $forced_break ? $number_processed : $messages_per_batch;
639
640
				if (!empty($inserts))
641
					$smcFunc['db_insert']('ignore',
642
						'{db_prefix}log_search_words',
643
						array('id_word' => 'int', 'id_msg' => 'int'),
644
						$inserts,
645
						array('id_word', 'id_msg')
646
					);
647
				if ($num_messages['todo'] === 0)
648
				{
649
					$context['step'] = 2;
650
					$context['start'] = 0;
651
					break;
652
				}
653
				else
654
					updateSettings(array('search_custom_index_resume' => $smcFunc['json_encode'](array_merge($context['index_settings'], array('resume_at' => $context['start'])))));
655
			}
656
657
			// Since there are still two steps to go, 80% is the maximum here.
658
			$context['percentage'] = round($num_messages['done'] / ($num_messages['done'] + $num_messages['todo']), 3) * 80;
659
		}
660
	}
661
662
	// Step 2: removing the words that occur too often and are of no use.
663
	elseif ($context['step'] === 2)
664
	{
665
		if ($context['index_settings']['bytes_per_word'] < 4)
666
			$context['step'] = 3;
667
		else
668
		{
669
			$stop_words = $context['start'] === 0 || empty($modSettings['search_stopwords']) ? array() : explode(',', $modSettings['search_stopwords']);
670
			$stop = time() + 3;
671
			$context['sub_template'] = 'create_index_progress';
672
			$max_messages = ceil(60 * $modSettings['totalMessages'] / 100);
673
674
			while (time() < $stop)
675
			{
676
				$request = $smcFunc['db_query']('', '
677
					SELECT id_word, COUNT(id_word) AS num_words
678
					FROM {db_prefix}log_search_words
679
					WHERE id_word BETWEEN {int:starting_id} AND {int:ending_id}
680
					GROUP BY id_word
681
					HAVING COUNT(id_word) > {int:minimum_messages}',
682
					array(
683
						'starting_id' => $context['start'],
684
						'ending_id' => $context['start'] + $index_properties[$context['index_settings']['bytes_per_word']]['step_size'] - 1,
685
						'minimum_messages' => $max_messages,
686
					)
687
				);
688
				while ($row = $smcFunc['db_fetch_assoc']($request))
689
					$stop_words[] = $row['id_word'];
690
				$smcFunc['db_free_result']($request);
691
692
				updateSettings(array('search_stopwords' => implode(',', $stop_words)));
693
694
				if (!empty($stop_words))
695
					$smcFunc['db_query']('', '
696
						DELETE FROM {db_prefix}log_search_words
697
						WHERE id_word in ({array_int:stop_words})',
698
						array(
699
							'stop_words' => $stop_words,
700
						)
701
					);
702
703
				$context['start'] += $index_properties[$context['index_settings']['bytes_per_word']]['step_size'];
704
				if ($context['start'] > $index_properties[$context['index_settings']['bytes_per_word']]['max_size'])
705
				{
706
					$context['step'] = 3;
707
					break;
708
				}
709
			}
710
			$context['percentage'] = 80 + round($context['start'] / $index_properties[$context['index_settings']['bytes_per_word']]['max_size'], 3) * 20;
711
		}
712
	}
713
714
	// Step 3: remove words not distinctive enough.
715
	if ($context['step'] === 3)
716
	{
717
		$context['sub_template'] = 'create_index_done';
718
719
		updateSettings(array('search_index' => 'custom', 'search_custom_index_config' => $smcFunc['json_encode']($context['index_settings'])));
720
		$smcFunc['db_query']('', '
721
			DELETE FROM {db_prefix}settings
722
			WHERE variable = {string:search_custom_index_resume}',
723
			array(
724
				'search_custom_index_resume' => 'search_custom_index_resume',
725
			)
726
		);
727
	}
728
}
729
730
/**
731
 * Get the installed Search API implementations.
732
 * This function checks for patterns in comments on top of the Search-API files!
733
 * In addition to filenames pattern.
734
 * It loads the search API classes if identified.
735
 * This function is used by EditSearchMethod to list all installed API implementations.
736
 */
737
function loadSearchAPIs()
738
{
739
	global $sourcedir, $txt;
740
741
	// Ensure we have class.
742
	require_once($sourcedir . '/Class-SearchAPI.php');
743
744
	$apis = array();
745
	if ($dh = opendir($sourcedir))
746
	{
747
		while (($file = readdir($dh)) !== false)
748
		{
749
			if (is_file($sourcedir . '/' . $file) && preg_match('~^SearchAPI-([A-Za-z\d_]+)\.php$~', $file, $matches))
750
			{
751
				// Check this is definitely a valid API!
752
				$fp = fopen($sourcedir . '/' . $file, 'rb');
753
				$header = fread($fp, 4096);
0 ignored issues
show
It seems like $fp can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

753
				$header = fread(/** @scrutinizer ignore-type */ $fp, 4096);
Loading history...
754
				fclose($fp);
0 ignored issues
show
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

754
				fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
755
756
				if (strpos($header, '* SearchAPI-' . $matches[1] . '.php') !== false)
757
				{
758
					require_once($sourcedir . '/' . $file);
759
760
					$index_name = strtolower($matches[1]);
761
					$search_class_name = $index_name . '_search';
762
					$searchAPI = new $search_class_name();
763
764
					// No Support?  NEXT!
765
					if (!$searchAPI->is_supported)
766
						continue;
767
768
					$apis[$index_name] = array(
769
						'filename' => $file,
770
						'setting_index' => $index_name,
771
						'has_template' => in_array($index_name, array('custom', 'fulltext', 'standard')),
772
						'label' => $index_name && isset($txt['search_index_' . $index_name]) ? $txt['search_index_' . $index_name] : '',
773
						'desc' => $index_name && isset($txt['search_index_' . $index_name . '_desc']) ? $txt['search_index_' . $index_name . '_desc'] : '',
774
					);
775
				}
776
			}
777
		}
778
	}
779
	closedir($dh);
0 ignored issues
show
It seems like $dh can also be of type false; however, parameter $dir_handle of closedir() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

779
	closedir(/** @scrutinizer ignore-type */ $dh);
Loading history...
780
781
	return $apis;
782
}
783
784
/**
785
 * Checks if the message table already has a fulltext index created and returns the key name
786
 * Determines if a db is capable of creating a fulltext index
787
 */
788
function detectFulltextIndex()
789
{
790
	global $smcFunc, $context, $db_prefix;
791
792
	// We need this for db_get_version
793
	db_extend();
794
795
	if ($smcFunc['db_title'] === POSTGRE_TITLE)
796
	{
797
		$request = $smcFunc['db_query']('', '
798
			SELECT
799
				indexname
800
			FROM pg_tables t
801
				LEFT OUTER JOIN
802
					(SELECT c.relname AS ctablename, ipg.relname AS indexname, indexrelname FROM pg_index x
803
						JOIN pg_class c ON c.oid = x.indrelid
804
						JOIN pg_class ipg ON ipg.oid = x.indexrelid
805
						JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid)
806
					AS foo
807
					ON t.tablename = foo.ctablename
808
			WHERE t.schemaname= {string:schema} and indexname = {string:messages_ftx}',
809
			array(
810
				'schema' => 'public',
811
				'messages_ftx' => $db_prefix . 'messages_ftx',
812
			)
813
		);
814
		while ($row = $smcFunc['db_fetch_assoc']($request))
815
			$context['fulltext_index'][] = $row['indexname'];
816
	}
817
	else
818
	{
819
		$request = $smcFunc['db_query']('', '
820
			SHOW INDEX
821
			FROM {db_prefix}messages',
822
			array(
823
			)
824
		);
825
		$context['fulltext_index'] = array();
826
		if ($request !== false || $smcFunc['db_num_rows']($request) != 0)
827
		{
828
			while ($row = $smcFunc['db_fetch_assoc']($request))
829
				if ($row['Column_name'] == 'body' && (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT' || isset($row['Comment']) && $row['Comment'] == 'FULLTEXT'))
830
					$context['fulltext_index'][] = $row['Key_name'];
831
			$smcFunc['db_free_result']($request);
832
833
			if (is_array($context['fulltext_index']))
834
				$context['fulltext_index'] = array_unique($context['fulltext_index']);
835
		}
836
837
		if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0)
838
			$request = $smcFunc['db_query']('', '
839
				SHOW TABLE STATUS
840
				FROM {string:database_name}
841
				LIKE {string:table_name}',
842
				array(
843
					'database_name' => '`' . strtr($match[1], array('`' => '')) . '`',
844
					'table_name' => str_replace('_', '\_', $match[2]) . 'messages',
845
				)
846
			);
847
		else
848
			$request = $smcFunc['db_query']('', '
849
				SHOW TABLE STATUS
850
				LIKE {string:table_name}',
851
				array(
852
					'table_name' => str_replace('_', '\_', $db_prefix) . 'messages',
853
				)
854
			);
855
856
		if ($request !== false)
857
		{
858
			while ($row = $smcFunc['db_fetch_assoc']($request))
859
				if (isset($row['Engine']) && strtolower($row['Engine']) != 'myisam' && !(strtolower($row['Engine']) == 'innodb' && version_compare($smcFunc['db_get_version'](), '5.6.4', '>=')))
860
					$context['cannot_create_fulltext'] = true;
861
862
			$smcFunc['db_free_result']($request);
863
		}
864
	}
865
}
866
867
?>