detectFulltextIndex()   D
last analyzed

Complexity

Conditions 19
Paths 26

Size

Total Lines 75
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 34
nop 0
dl 0
loc 75
rs 4.5166
c 0
b 0
f 0
nc 26

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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

751
				$header = fread(/** @scrutinizer ignore-type */ $fp, 4096);
Loading history...
752
				fclose($fp);
0 ignored issues
show
Bug introduced by
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

752
				fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
753
754
				if (strpos($header, '* SearchAPI-' . $matches[1] . '.php') !== false)
755
				{
756
					require_once($sourcedir . '/' . $file);
757
758
					$index_name = strtolower($matches[1]);
759
					$search_class_name = $index_name . '_search';
760
					$searchAPI = new $search_class_name();
761
762
					// No Support?  NEXT!
763
					if (!$searchAPI->is_supported)
764
						continue;
765
766
					$apis[$index_name] = array(
767
						'filename' => $file,
768
						'setting_index' => $index_name,
769
						'has_template' => in_array($index_name, array('custom', 'fulltext', 'standard')),
770
						'label' => $index_name && isset($txt['search_index_' . $index_name]) ? $txt['search_index_' . $index_name] : '',
771
						'desc' => $index_name && isset($txt['search_index_' . $index_name . '_desc']) ? $txt['search_index_' . $index_name . '_desc'] : '',
772
					);
773
				}
774
			}
775
		}
776
	}
777
	closedir($dh);
0 ignored issues
show
Bug introduced by
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

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