PageRenderTime 83ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/sources/admin/ManageSearch.php

https://github.com/Arantor/Elkarte
PHP | 1059 lines | 814 code | 117 blank | 128 comment | 122 complexity | 26f767b3aea9f4ceebc22e779a56c7b9 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * @name ElkArte Forum
  4. * @copyright ElkArte Forum contributors
  5. * @license BSD http://opensource.org/licenses/BSD-3-Clause
  6. *
  7. * This software is a derived product, based on:
  8. *
  9. * Simple Machines Forum (SMF)
  10. * copyright: 2011 Simple Machines (http://www.simplemachines.org)
  11. * license: BSD, See included LICENSE.TXT for terms and conditions.
  12. *
  13. * @version 1.0 Alpha
  14. *
  15. * The admin screen to change the search settings.
  16. *
  17. */
  18. if (!defined('ELKARTE'))
  19. die('No access...');
  20. /**
  21. * Main entry point for the admin search settings screen.
  22. * It checks permissions, and it forwards to the appropriate function based on
  23. * the given sub-action.
  24. * Defaults to sub-action 'settings'.
  25. * Called by ?action=admin;area=managesearch.
  26. * Requires the admin_forum permission.
  27. *
  28. * @uses ManageSearch template.
  29. * @uses Search language file.
  30. */
  31. function ManageSearch()
  32. {
  33. global $context, $txt, $scripturl;
  34. isAllowedTo('admin_forum');
  35. loadLanguage('Search');
  36. loadTemplate('ManageSearch');
  37. db_extend('search');
  38. $subActions = array(
  39. 'settings' => 'EditSearchSettings',
  40. 'weights' => 'EditWeights',
  41. 'method' => 'EditSearchMethod',
  42. 'createfulltext' => 'EditSearchMethod',
  43. 'removecustom' => 'EditSearchMethod',
  44. 'removefulltext' => 'EditSearchMethod',
  45. 'createmsgindex' => 'CreateMessageIndex',
  46. 'managesphinx' => 'EditSphinxSettings',
  47. );
  48. call_integration_hook('integrate_manage_search', array($subActions));
  49. // Default the sub-action to 'edit search settings'.
  50. $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'weights';
  51. $context['sub_action'] = $_REQUEST['sa'];
  52. // Create the tabs for the template.
  53. $context[$context['admin_menu_name']]['tab_data'] = array(
  54. 'title' => $txt['manage_search'],
  55. 'help' => 'search',
  56. 'description' => $txt['search_settings_desc'],
  57. 'tabs' => array(
  58. 'weights' => array(
  59. 'description' => $txt['search_weights_desc'],
  60. ),
  61. 'method' => array(
  62. 'description' => $txt['search_method_desc'],
  63. ),
  64. 'settings' => array(
  65. 'description' => $txt['search_settings_desc'],
  66. ),
  67. ),
  68. );
  69. // Call the right function for this sub-acton.
  70. $subActions[$_REQUEST['sa']]();
  71. }
  72. /**
  73. * Edit some general settings related to the search function.
  74. * Called by ?action=admin;area=managesearch;sa=settings.
  75. * Requires the admin_forum permission.
  76. *
  77. * @param $return_config
  78. * @uses ManageSearch template, 'modify_settings' sub-template.
  79. */
  80. function EditSearchSettings($return_config = false)
  81. {
  82. global $txt, $context, $scripturl, $modSettings;
  83. // What are we editing anyway?
  84. $config_vars = array(
  85. // Permission...
  86. array('permissions', 'search_posts'),
  87. // Some simple settings.
  88. array('check', 'simpleSearch'),
  89. array('check', 'search_dropdown'),
  90. array('int', 'search_results_per_page'),
  91. array('int', 'search_max_results', 'subtext' => $txt['search_max_results_disable']),
  92. '',
  93. // Some limitations.
  94. array('int', 'search_floodcontrol_time', 'subtext' => $txt['search_floodcontrol_time_desc'], 6, 'postinput' => $txt['seconds']),
  95. );
  96. call_integration_hook('integrate_modify_search_settings', array($config_vars));
  97. // Perhaps the search method wants to add some settings?
  98. require_once(SUBSDIR . '/Search.subs.php');
  99. $searchAPI = findSearchAPI();
  100. if (is_callable(array($searchAPI, 'searchSettings')))
  101. call_user_func_array($searchAPI->searchSettings, array(&$config_vars));
  102. if ($return_config)
  103. return $config_vars;
  104. $context['page_title'] = $txt['search_settings_title'];
  105. $context['sub_template'] = 'show_settings';
  106. // We'll need this for the settings.
  107. require_once(ADMINDIR . '/ManageServer.php');
  108. // A form was submitted.
  109. if (isset($_REQUEST['save']))
  110. {
  111. checkSession();
  112. call_integration_hook('integrate_save_search_settings');
  113. saveDBSettings($config_vars);
  114. redirectexit('action=admin;area=managesearch;sa=settings;' . $context['session_var'] . '=' . $context['session_id']);
  115. }
  116. // Prep the template!
  117. $context['post_url'] = $scripturl . '?action=admin;area=managesearch;save;sa=settings';
  118. $context['settings_title'] = $txt['search_settings_title'];
  119. // We need this for the in-line permissions
  120. createToken('admin-mp');
  121. prepareDBSettingContext($config_vars);
  122. }
  123. /**
  124. * Edit the relative weight of the search factors.
  125. * Called by ?action=admin;area=managesearch;sa=weights.
  126. * Requires the admin_forum permission.
  127. *
  128. * @uses ManageSearch template, 'modify_weights' sub-template.
  129. */
  130. function EditWeights()
  131. {
  132. global $txt, $context, $modSettings;
  133. $context['page_title'] = $txt['search_weights_title'];
  134. $context['sub_template'] = 'modify_weights';
  135. $factors = array(
  136. 'search_weight_frequency',
  137. 'search_weight_age',
  138. 'search_weight_length',
  139. 'search_weight_subject',
  140. 'search_weight_first_message',
  141. 'search_weight_sticky',
  142. );
  143. call_integration_hook('integrate_modify_search_weights', array($factors));
  144. // A form was submitted.
  145. if (isset($_POST['save']))
  146. {
  147. checkSession();
  148. validateToken('admin-msw');
  149. call_integration_hook('integrate_save_search_weights');
  150. $changes = array();
  151. foreach ($factors as $factor)
  152. $changes[$factor] = (int) $_POST[$factor];
  153. updateSettings($changes);
  154. }
  155. $context['relative_weights'] = array('total' => 0);
  156. foreach ($factors as $factor)
  157. $context['relative_weights']['total'] += isset($modSettings[$factor]) ? $modSettings[$factor] : 0;
  158. foreach ($factors as $factor)
  159. $context['relative_weights'][$factor] = round(100 * (isset($modSettings[$factor]) ? $modSettings[$factor] : 0) / $context['relative_weights']['total'], 1);
  160. createToken('admin-msw');
  161. }
  162. /**
  163. * Edit the search method and search index used.
  164. * Calculates the size of the current search indexes in use.
  165. * Allows to create and delete a fulltext index on the messages table.
  166. * Allows to delete a custom index (that CreateMessageIndex() created).
  167. * Called by ?action=admin;area=managesearch;sa=method.
  168. * Requires the admin_forum permission.
  169. *
  170. * @uses ManageSearch template, 'select_search_method' sub-template.
  171. */
  172. function EditSearchMethod()
  173. {
  174. global $txt, $context, $modSettings, $smcFunc, $db_type, $db_prefix;
  175. $context[$context['admin_menu_name']]['current_subsection'] = 'method';
  176. $context['page_title'] = $txt['search_method_title'];
  177. $context['sub_template'] = 'select_search_method';
  178. $context['supports_fulltext'] = $smcFunc['db_search_support']('fulltext');
  179. // Load any apis.
  180. $context['search_apis'] = loadSearchAPIs();
  181. // Detect whether a fulltext index is set.
  182. if ($context['supports_fulltext'])
  183. detectFulltextIndex();
  184. if (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'createfulltext')
  185. {
  186. checkSession('get');
  187. validateToken('admin-msm', 'get');
  188. // Make sure it's gone before creating it.
  189. $smcFunc['db_query']('', '
  190. ALTER TABLE {db_prefix}messages
  191. DROP INDEX body',
  192. array(
  193. 'db_error_skip' => true,
  194. )
  195. );
  196. $smcFunc['db_query']('', '
  197. ALTER TABLE {db_prefix}messages
  198. ADD FULLTEXT body (body)',
  199. array(
  200. )
  201. );
  202. $context['fulltext_index'] = 'body';
  203. }
  204. elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removefulltext' && !empty($context['fulltext_index']))
  205. {
  206. checkSession('get');
  207. validateToken('admin-msm', 'get');
  208. $smcFunc['db_query']('', '
  209. ALTER TABLE {db_prefix}messages
  210. DROP INDEX ' . implode(',
  211. DROP INDEX ', $context['fulltext_index']),
  212. array(
  213. 'db_error_skip' => true,
  214. )
  215. );
  216. $context['fulltext_index'] = '';
  217. // Go back to the default search method.
  218. if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'fulltext')
  219. updateSettings(array(
  220. 'search_index' => '',
  221. ));
  222. }
  223. elseif (!empty($_REQUEST['sa']) && $_REQUEST['sa'] == 'removecustom')
  224. {
  225. checkSession('get');
  226. validateToken('admin-msm', 'get');
  227. db_extend();
  228. $tables = $smcFunc['db_list_tables'](false, $db_prefix . 'log_search_words');
  229. if (!empty($tables))
  230. {
  231. $smcFunc['db_search_query']('drop_words_table', '
  232. DROP TABLE {db_prefix}log_search_words',
  233. array(
  234. )
  235. );
  236. }
  237. updateSettings(array(
  238. 'search_custom_index_config' => '',
  239. 'search_custom_index_resume' => '',
  240. ));
  241. // Go back to the default search method.
  242. if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom')
  243. updateSettings(array(
  244. 'search_index' => '',
  245. ));
  246. }
  247. elseif (isset($_POST['save']))
  248. {
  249. checkSession();
  250. validateToken('admin-msmpost');
  251. updateSettings(array(
  252. 'search_index' => empty($_POST['search_index']) || (!in_array($_POST['search_index'], array('fulltext', 'custom')) && !isset($context['search_apis'][$_POST['search_index']])) ? '' : $_POST['search_index'],
  253. 'search_force_index' => isset($_POST['search_force_index']) ? '1' : '0',
  254. 'search_match_words' => isset($_POST['search_match_words']) ? '1' : '0',
  255. ));
  256. }
  257. $context['table_info'] = array(
  258. 'data_length' => 0,
  259. 'index_length' => 0,
  260. 'fulltext_length' => 0,
  261. 'custom_index_length' => 0,
  262. );
  263. // Get some info about the messages table, to show its size and index size.
  264. if ($db_type == 'mysql')
  265. {
  266. if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0)
  267. $request = $smcFunc['db_query']('', '
  268. SHOW TABLE STATUS
  269. FROM {string:database_name}
  270. LIKE {string:table_name}',
  271. array(
  272. 'database_name' => '`' . strtr($match[1], array('`' => '')) . '`',
  273. 'table_name' => str_replace('_', '\_', $match[2]) . 'messages',
  274. )
  275. );
  276. else
  277. $request = $smcFunc['db_query']('', '
  278. SHOW TABLE STATUS
  279. LIKE {string:table_name}',
  280. array(
  281. 'table_name' => str_replace('_', '\_', $db_prefix) . 'messages',
  282. )
  283. );
  284. if ($request !== false && $smcFunc['db_num_rows']($request) == 1)
  285. {
  286. // Only do this if the user has permission to execute this query.
  287. $row = $smcFunc['db_fetch_assoc']($request);
  288. $context['table_info']['data_length'] = $row['Data_length'];
  289. $context['table_info']['index_length'] = $row['Index_length'];
  290. $context['table_info']['fulltext_length'] = $row['Index_length'];
  291. $smcFunc['db_free_result']($request);
  292. }
  293. // Now check the custom index table, if it exists at all.
  294. if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0)
  295. $request = $smcFunc['db_query']('', '
  296. SHOW TABLE STATUS
  297. FROM {string:database_name}
  298. LIKE {string:table_name}',
  299. array(
  300. 'database_name' => '`' . strtr($match[1], array('`' => '')) . '`',
  301. 'table_name' => str_replace('_', '\_', $match[2]) . 'log_search_words',
  302. )
  303. );
  304. else
  305. $request = $smcFunc['db_query']('', '
  306. SHOW TABLE STATUS
  307. LIKE {string:table_name}',
  308. array(
  309. 'table_name' => str_replace('_', '\_', $db_prefix) . 'log_search_words',
  310. )
  311. );
  312. if ($request !== false && $smcFunc['db_num_rows']($request) == 1)
  313. {
  314. // Only do this if the user has permission to execute this query.
  315. $row = $smcFunc['db_fetch_assoc']($request);
  316. $context['table_info']['index_length'] += $row['Data_length'] + $row['Index_length'];
  317. $context['table_info']['custom_index_length'] = $row['Data_length'] + $row['Index_length'];
  318. $smcFunc['db_free_result']($request);
  319. }
  320. }
  321. elseif ($db_type == 'postgresql')
  322. {
  323. // In order to report the sizes correctly we need to perform vacuum (optimize) on the tables we will be using.
  324. db_extend();
  325. $temp_tables = $smcFunc['db_list_tables']();
  326. foreach ($temp_tables as $table)
  327. if ($table == $db_prefix. 'messages' || $table == $db_prefix. 'log_search_words')
  328. $smcFunc['db_optimize_table']($table);
  329. // PostGreSql has some hidden sizes.
  330. $request = $smcFunc['db_query']('', '
  331. SELECT relname, relpages * 8 *1024 AS "KB" FROM pg_class
  332. WHERE relname = {string:messages} OR relname = {string:log_search_words}
  333. ORDER BY relpages DESC',
  334. array(
  335. 'messages' => $db_prefix. 'messages',
  336. 'log_search_words' => $db_prefix. 'log_search_words',
  337. )
  338. );
  339. if ($request !== false && $smcFunc['db_num_rows']($request) > 0)
  340. {
  341. while ($row = $smcFunc['db_fetch_assoc']($request))
  342. {
  343. if ($row['relname'] == $db_prefix . 'messages')
  344. {
  345. $context['table_info']['data_length'] = (int) $row['KB'];
  346. $context['table_info']['index_length'] = (int) $row['KB'];
  347. // Doesn't support fulltext
  348. $context['table_info']['fulltext_length'] = $txt['not_applicable'];
  349. }
  350. elseif ($row['relname'] == $db_prefix. 'log_search_words')
  351. {
  352. $context['table_info']['index_length'] = (int) $row['KB'];
  353. $context['table_info']['custom_index_length'] = (int) $row['KB'];
  354. }
  355. }
  356. $smcFunc['db_free_result']($request);
  357. }
  358. else
  359. // Didn't work for some reason...
  360. $context['table_info'] = array(
  361. 'data_length' => $txt['not_applicable'],
  362. 'index_length' => $txt['not_applicable'],
  363. 'fulltext_length' => $txt['not_applicable'],
  364. 'custom_index_length' => $txt['not_applicable'],
  365. );
  366. }
  367. else
  368. $context['table_info'] = array(
  369. 'data_length' => $txt['not_applicable'],
  370. 'index_length' => $txt['not_applicable'],
  371. 'fulltext_length' => $txt['not_applicable'],
  372. 'custom_index_length' => $txt['not_applicable'],
  373. );
  374. // Format the data and index length in kilobytes.
  375. foreach ($context['table_info'] as $type => $size)
  376. {
  377. // If it's not numeric then just break. This database engine doesn't support size.
  378. if (!is_numeric($size))
  379. break;
  380. $context['table_info'][$type] = comma_format($context['table_info'][$type] / 1024) . ' ' . $txt['search_method_kilobytes'];
  381. }
  382. $context['custom_index'] = !empty($modSettings['search_custom_index_config']);
  383. $context['partial_custom_index'] = !empty($modSettings['search_custom_index_resume']) && empty($modSettings['search_custom_index_config']);
  384. $context['double_index'] = !empty($context['fulltext_index']) && $context['custom_index'];
  385. createToken('admin-msmpost');
  386. createToken('admin-msm', 'get');
  387. }
  388. /**
  389. * Create a custom search index for the messages table.
  390. * Called by ?action=admin;area=managesearch;sa=createmsgindex.
  391. * Linked from the EditSearchMethod screen.
  392. * Requires the admin_forum permission.
  393. * Depending on the size of the message table, the process is divided in steps.
  394. *
  395. * @uses ManageSearch template, 'create_index', 'create_index_progress', and 'create_index_done'
  396. * sub-templates.
  397. */
  398. function CreateMessageIndex()
  399. {
  400. global $modSettings, $context, $smcFunc, $db_prefix, $txt;
  401. // Scotty, we need more time...
  402. @set_time_limit(600);
  403. if (function_exists('apache_reset_timeout'))
  404. @apache_reset_timeout();
  405. $context[$context['admin_menu_name']]['current_subsection'] = 'method';
  406. $context['page_title'] = $txt['search_index_custom'];
  407. $messages_per_batch = 50;
  408. $index_properties = array(
  409. 2 => array(
  410. 'column_definition' => 'small',
  411. 'step_size' => 1000000,
  412. ),
  413. 4 => array(
  414. 'column_definition' => 'medium',
  415. 'step_size' => 1000000,
  416. 'max_size' => 16777215,
  417. ),
  418. 5 => array(
  419. 'column_definition' => 'large',
  420. 'step_size' => 100000000,
  421. 'max_size' => 2000000000,
  422. ),
  423. );
  424. if (isset($_REQUEST['resume']) && !empty($modSettings['search_custom_index_resume']))
  425. {
  426. $context['index_settings'] = unserialize($modSettings['search_custom_index_resume']);
  427. $context['start'] = (int) $context['index_settings']['resume_at'];
  428. unset($context['index_settings']['resume_at']);
  429. $context['step'] = 1;
  430. }
  431. else
  432. {
  433. $context['index_settings'] = array(
  434. 'bytes_per_word' => isset($_REQUEST['bytes_per_word']) && isset($index_properties[$_REQUEST['bytes_per_word']]) ? (int) $_REQUEST['bytes_per_word'] : 2,
  435. );
  436. $context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
  437. $context['step'] = isset($_REQUEST['step']) ? (int) $_REQUEST['step'] : 0;
  438. // admin timeouts are painful when building these long indexes
  439. if ($_SESSION['admin_time'] + 3300 < time() && $context['step'] >= 1)
  440. $_SESSION['admin_time'] = time();
  441. }
  442. if ($context['step'] !== 0)
  443. checkSession('request');
  444. // Step 0: let the user determine how they like their index.
  445. if ($context['step'] === 0)
  446. {
  447. $context['sub_template'] = 'create_index';
  448. }
  449. // Step 1: insert all the words.
  450. if ($context['step'] === 1)
  451. {
  452. $context['sub_template'] = 'create_index_progress';
  453. if ($context['start'] === 0)
  454. {
  455. db_extend();
  456. $tables = $smcFunc['db_list_tables'](false, $db_prefix . 'log_search_words');
  457. if (!empty($tables))
  458. {
  459. $smcFunc['db_search_query']('drop_words_table', '
  460. DROP TABLE {db_prefix}log_search_words',
  461. array(
  462. )
  463. );
  464. }
  465. $smcFunc['db_create_word_search']($index_properties[$context['index_settings']['bytes_per_word']]['column_definition']);
  466. // Temporarily switch back to not using a search index.
  467. if (!empty($modSettings['search_index']) && $modSettings['search_index'] == 'custom')
  468. updateSettings(array('search_index' => ''));
  469. // Don't let simultanious processes be updating the search index.
  470. if (!empty($modSettings['search_custom_index_config']))
  471. updateSettings(array('search_custom_index_config' => ''));
  472. }
  473. $num_messages = array(
  474. 'done' => 0,
  475. 'todo' => 0,
  476. );
  477. $request = $smcFunc['db_query']('', '
  478. SELECT id_msg >= {int:starting_id} AS todo, COUNT(*) AS num_messages
  479. FROM {db_prefix}messages
  480. GROUP BY todo',
  481. array(
  482. 'starting_id' => $context['start'],
  483. )
  484. );
  485. while ($row = $smcFunc['db_fetch_assoc']($request))
  486. $num_messages[empty($row['todo']) ? 'done' : 'todo'] = $row['num_messages'];
  487. if (empty($num_messages['todo']))
  488. {
  489. $context['step'] = 2;
  490. $context['percentage'] = 80;
  491. $context['start'] = 0;
  492. }
  493. else
  494. {
  495. // Number of seconds before the next step.
  496. $stop = time() + 3;
  497. while (time() < $stop)
  498. {
  499. $inserts = array();
  500. $request = $smcFunc['db_query']('', '
  501. SELECT id_msg, body
  502. FROM {db_prefix}messages
  503. WHERE id_msg BETWEEN {int:starting_id} AND {int:ending_id}
  504. LIMIT {int:limit}',
  505. array(
  506. 'starting_id' => $context['start'],
  507. 'ending_id' => $context['start'] + $messages_per_batch - 1,
  508. 'limit' => $messages_per_batch,
  509. )
  510. );
  511. $forced_break = false;
  512. $number_processed = 0;
  513. while ($row = $smcFunc['db_fetch_assoc']($request))
  514. {
  515. // In theory it's possible for one of these to take friggin ages so add more timeout protection.
  516. if ($stop < time())
  517. {
  518. $forced_break = true;
  519. break;
  520. }
  521. $number_processed++;
  522. foreach (text2words($row['body'], $context['index_settings']['bytes_per_word'], true) as $id_word)
  523. {
  524. $inserts[] = array($id_word, $row['id_msg']);
  525. }
  526. }
  527. $num_messages['done'] += $number_processed;
  528. $num_messages['todo'] -= $number_processed;
  529. $smcFunc['db_free_result']($request);
  530. $context['start'] += $forced_break ? $number_processed : $messages_per_batch;
  531. if (!empty($inserts))
  532. $smcFunc['db_insert']('ignore',
  533. '{db_prefix}log_search_words',
  534. array('id_word' => 'int', 'id_msg' => 'int'),
  535. $inserts,
  536. array('id_word', 'id_msg')
  537. );
  538. if ($num_messages['todo'] === 0)
  539. {
  540. $context['step'] = 2;
  541. $context['start'] = 0;
  542. break;
  543. }
  544. else
  545. updateSettings(array('search_custom_index_resume' => serialize(array_merge($context['index_settings'], array('resume_at' => $context['start'])))));
  546. }
  547. // Since there are still two steps to go, 80% is the maximum here.
  548. $context['percentage'] = round($num_messages['done'] / ($num_messages['done'] + $num_messages['todo']), 3) * 80;
  549. }
  550. }
  551. // Step 2: removing the words that occur too often and are of no use.
  552. elseif ($context['step'] === 2)
  553. {
  554. if ($context['index_settings']['bytes_per_word'] < 4)
  555. $context['step'] = 3;
  556. else
  557. {
  558. $stop_words = $context['start'] === 0 || empty($modSettings['search_stopwords']) ? array() : explode(',', $modSettings['search_stopwords']);
  559. $stop = time() + 3;
  560. $context['sub_template'] = 'create_index_progress';
  561. $max_messages = ceil(60 * $modSettings['totalMessages'] / 100);
  562. while (time() < $stop)
  563. {
  564. $request = $smcFunc['db_query']('', '
  565. SELECT id_word, COUNT(id_word) AS num_words
  566. FROM {db_prefix}log_search_words
  567. WHERE id_word BETWEEN {int:starting_id} AND {int:ending_id}
  568. GROUP BY id_word
  569. HAVING COUNT(id_word) > {int:minimum_messages}',
  570. array(
  571. 'starting_id' => $context['start'],
  572. 'ending_id' => $context['start'] + $index_properties[$context['index_settings']['bytes_per_word']]['step_size'] - 1,
  573. 'minimum_messages' => $max_messages,
  574. )
  575. );
  576. while ($row = $smcFunc['db_fetch_assoc']($request))
  577. $stop_words[] = $row['id_word'];
  578. $smcFunc['db_free_result']($request);
  579. updateSettings(array('search_stopwords' => implode(',', $stop_words)));
  580. if (!empty($stop_words))
  581. $smcFunc['db_query']('', '
  582. DELETE FROM {db_prefix}log_search_words
  583. WHERE id_word in ({array_int:stop_words})',
  584. array(
  585. 'stop_words' => $stop_words,
  586. )
  587. );
  588. $context['start'] += $index_properties[$context['index_settings']['bytes_per_word']]['step_size'];
  589. if ($context['start'] > $index_properties[$context['index_settings']['bytes_per_word']]['max_size'])
  590. {
  591. $context['step'] = 3;
  592. break;
  593. }
  594. }
  595. $context['percentage'] = 80 + round($context['start'] / $index_properties[$context['index_settings']['bytes_per_word']]['max_size'], 3) * 20;
  596. }
  597. }
  598. // Step 3: remove words not distinctive enough.
  599. if ($context['step'] === 3)
  600. {
  601. $context['sub_template'] = 'create_index_done';
  602. updateSettings(array('search_index' => 'custom', 'search_custom_index_config' => serialize($context['index_settings'])));
  603. $smcFunc['db_query']('', '
  604. DELETE FROM {db_prefix}settings
  605. WHERE variable = {string:search_custom_index_resume}',
  606. array(
  607. 'search_custom_index_resume' => 'search_custom_index_resume',
  608. )
  609. );
  610. }
  611. }
  612. /**
  613. * Get the installed Search API implementations.
  614. * This function checks for patterns in comments on top of the Search-API files!
  615. * In addition to filenames pattern.
  616. * It loads the search API classes if identified.
  617. * This function is used by EditSearchMethod to list all installed API implementations.
  618. */
  619. function loadSearchAPIs()
  620. {
  621. global $txt;
  622. $apis = array();
  623. if ($dh = opendir(SOURCEDIR))
  624. {
  625. while (($file = readdir($dh)) !== false)
  626. {
  627. if (is_file(SOURCEDIR . '/' . $file) && preg_match('~^SearchAPI-([A-Za-z\d_]+)\.class\.php$~', $file, $matches))
  628. {
  629. // Check that this is definitely a valid API!
  630. $fp = fopen(SOURCEDIR . '/' . $file, 'rb');
  631. $header = fread($fp, 4096);
  632. fclose($fp);
  633. if (strpos($header, '* SearchAPI-' . $matches[1] . '.class.php') !== false)
  634. {
  635. require_once(SOURCEDIR . '/' . $file);
  636. $index_name = strtolower($matches[1]);
  637. $search_class_name = $index_name . '_search';
  638. $searchAPI = new $search_class_name();
  639. // No Support? NEXT!
  640. if (!$searchAPI->is_supported)
  641. continue;
  642. $apis[$index_name] = array(
  643. 'filename' => $file,
  644. 'setting_index' => $index_name,
  645. 'has_template' => in_array($index_name, array('custom', 'fulltext', 'standard')),
  646. 'label' => $index_name && isset($txt['search_index_' . $index_name]) ? $txt['search_index_' . $index_name] : '',
  647. 'desc' => $index_name && isset($txt['search_index_' . $index_name . '_desc']) ? $txt['search_index_' . $index_name . '_desc'] : '',
  648. );
  649. }
  650. }
  651. }
  652. }
  653. closedir($dh);
  654. return $apis;
  655. }
  656. /**
  657. * Checks if the message table already has a fulltext index created and returns the key name
  658. * Determines if a db is capable of creating a fulltext index
  659. */
  660. function detectFulltextIndex()
  661. {
  662. global $smcFunc, $context, $db_prefix;
  663. $request = $smcFunc['db_query']('', '
  664. SHOW INDEX
  665. FROM {db_prefix}messages',
  666. array(
  667. )
  668. );
  669. $context['fulltext_index'] = '';
  670. if ($request !== false || $smcFunc['db_num_rows']($request) != 0)
  671. {
  672. while ($row = $smcFunc['db_fetch_assoc']($request))
  673. if ($row['Column_name'] == 'body' && (isset($row['Index_type']) && $row['Index_type'] == 'FULLTEXT' || isset($row['Comment']) && $row['Comment'] == 'FULLTEXT'))
  674. $context['fulltext_index'][] = $row['Key_name'];
  675. $smcFunc['db_free_result']($request);
  676. if (is_array($context['fulltext_index']))
  677. $context['fulltext_index'] = array_unique($context['fulltext_index']);
  678. }
  679. if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) !== 0)
  680. $request = $smcFunc['db_query']('', '
  681. SHOW TABLE STATUS
  682. FROM {string:database_name}
  683. LIKE {string:table_name}',
  684. array(
  685. 'database_name' => '`' . strtr($match[1], array('`' => '')) . '`',
  686. 'table_name' => str_replace('_', '\_', $match[2]) . 'messages',
  687. )
  688. );
  689. else
  690. $request = $smcFunc['db_query']('', '
  691. SHOW TABLE STATUS
  692. LIKE {string:table_name}',
  693. array(
  694. 'table_name' => str_replace('_', '\_', $db_prefix) . 'messages',
  695. )
  696. );
  697. if ($request !== false)
  698. {
  699. while ($row = $smcFunc['db_fetch_assoc']($request))
  700. if ((isset($row['Type']) && strtolower($row['Type']) != 'myisam') || (isset($row['Engine']) && strtolower($row['Engine']) != 'myisam'))
  701. $context['cannot_create_fulltext'] = true;
  702. $smcFunc['db_free_result']($request);
  703. }
  704. }
  705. /**
  706. * Edit settings related to the sphinx or sphinxQL search function.
  707. * Called by ?action=admin;area=managesearch;sa=sphinx.
  708. */
  709. function EditSphinxSettings()
  710. {
  711. global $txt, $context, $modSettings;
  712. // saving the settings
  713. if (isset($_POST['save']))
  714. {
  715. checkSession();
  716. validateToken('admin-mssphinx');
  717. updateSettings(array(
  718. 'sphinx_data_path' => rtrim($_POST['sphinx_data_path'], '/'),
  719. 'sphinx_log_path' => rtrim($_POST['sphinx_log_path'], '/'),
  720. 'sphinx_stopword_path' => $_POST['sphinx_stopword_path'],
  721. 'sphinx_indexer_mem' => (int) $_POST['sphinx_indexer_mem'],
  722. 'sphinx_searchd_server' => $_POST['sphinx_searchd_server'],
  723. 'sphinx_searchd_port' => (int) $_POST['sphinx_searchd_port'],
  724. 'sphinxql_searchd_port' => (int) $_POST['sphinxql_searchd_port'],
  725. 'sphinx_max_results' => (int) $_POST['sphinx_max_results'],
  726. ));
  727. }
  728. // checking if we can connect?
  729. elseif (isset($_POST['checkconnect']))
  730. {
  731. checkSession();
  732. validateToken('admin-mssphinx');
  733. // If they have not picked sphinx yet, let them know, but we can still check connections
  734. if (empty($modSettings['search_index']) || ($modSettings['search_index'] !== 'sphinx' && $modSettings['search_index'] !== 'sphinxql'))
  735. {
  736. $context['settings_message'][] = $txt['sphinx_test_not_selected'];
  737. $context['error_type'] = 'notice';
  738. }
  739. // try to connect via Sphinx API?
  740. if ($modSettings['search_index'] === 'sphinx' || empty($modSettings['search_index']))
  741. {
  742. if (@file_exists(SOURCEDIR . '/sphinxapi.php'))
  743. {
  744. include_once(SOURCEDIR . '/sphinxapi.php');
  745. $mySphinx = new SphinxClient();
  746. $mySphinx->SetServer($modSettings['sphinx_searchd_server'], (int) $modSettings['sphinx_searchd_port']);
  747. $mySphinx->SetLimits(0, (int) $modSettings['sphinx_max_results']);
  748. $mySphinx->SetMatchMode(SPH_MATCH_BOOLEAN);
  749. $mySphinx->SetSortMode(SPH_SORT_ATTR_ASC, 'id_topic');
  750. $request = $mySphinx->Query('test', 'elkarte_index');
  751. if ($request === false)
  752. {
  753. $context['settings_message'][] = $txt['sphinx_test_connect_failed'];
  754. $context['error_type'] = 'serious';
  755. }
  756. else
  757. $context['settings_message'][] = $txt['sphinx_test_passed'];
  758. }
  759. else
  760. {
  761. $context['settings_message'][] = $txt['sphinx_test_api_missing'];
  762. $context['error_type'] = 'serious';
  763. }
  764. }
  765. // try to connect via SphinxQL
  766. if ($modSettings['search_index'] === 'sphinxql' || empty($modSettings['search_index']))
  767. {
  768. if (!empty($modSettings['sphinx_searchd_server']) && !empty($modSettings['sphinxql_searchd_port']))
  769. {
  770. $result = mysql_connect(($modSettings['sphinx_searchd_server'] === 'localhost' ? '127.0.0.1' : $modSettings['sphinx_searchd_server']) . ':' . (int) $modSettings['sphinxql_searchd_port']);
  771. if ($result === false)
  772. {
  773. $context['settings_message'][] = $txt['sphinxql_test_connect_failed'];
  774. $context['error_type'] = 'serious';
  775. }
  776. else
  777. $context['settings_message'][] = $txt['sphinxql_test_passed'];
  778. }
  779. else
  780. {
  781. $context['settings_message'][] = $txt['sphinxql_test_connect_failed'];
  782. $context['error_type'] = 'serious';
  783. }
  784. }
  785. }
  786. elseif (isset($_POST['createconfig']))
  787. {
  788. checkSession();
  789. validateToken('admin-mssphinx');
  790. CreateSphinxConfig();
  791. }
  792. // Setup for the template
  793. $context['page_title'] = $txt['search_sphinx'];
  794. $context['page_description'] = $txt['sphinx_description'];
  795. $context['sub_template'] = 'manage_sphinx';
  796. createToken('admin-mssphinx');
  797. }
  798. function CreateSphinxConfig()
  799. {
  800. global $context, $db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_character_set, $modSettings;
  801. $humungousTopicPosts = 200;
  802. // set up to ouput a file to the users browser
  803. ob_end_clean();
  804. header('Pragma: ');
  805. if (!$context['browser']['is_gecko'])
  806. header('Content-Transfer-Encoding: binary');
  807. header('Connection: close');
  808. header('Content-Disposition: attachment; filename="sphinx.conf"');
  809. header('Content-Type: application/octet-stream');
  810. $weight_factors = array(
  811. 'age',
  812. 'length',
  813. 'first_message',
  814. 'sticky',
  815. );
  816. $weight = array();
  817. $weight_total = 0;
  818. foreach ($weight_factors as $weight_factor)
  819. {
  820. $weight[$weight_factor] = empty($modSettings['search_weight_' . $weight_factor]) ? 0 : (int) $modSettings['search_weight_' . $weight_factor];
  821. $weight_total += $weight[$weight_factor];
  822. }
  823. // weightless, then use defaults
  824. if ($weight_total === 0)
  825. {
  826. $weight = array(
  827. 'age' => 25,
  828. 'length' => 25,
  829. 'first_message' => 25,
  830. 'sticky' => 25,
  831. );
  832. $weight_total = 100;
  833. }
  834. // check paths are set, if not use some defaults
  835. $modSettings['sphinx_data_path'] = empty($modSettings['sphinx_data_path']) ? '/var/sphinx/data' : $modSettings['sphinx_data_path'];
  836. $modSettings['sphinx_log_path'] = empty($modSettings['sphinx_log_path']) ? '/var/sphinx/log' : $modSettings['sphinx_log_path'];
  837. // output our minimal configuration file to get them started
  838. echo '#
  839. # Sphinx configuration file (sphinx.conf), configured for Elkarte
  840. #
  841. # This is the minimum needed clean, simple, functional
  842. #
  843. # By default the location of this file would probably be:
  844. # /usr/local/etc/sphinx.conf
  845. #
  846. source elkarte_source
  847. {
  848. type = mysql
  849. sql_host = ', $db_server, '
  850. sql_user = ', $db_user, '
  851. sql_pass = ', $db_passwd, '
  852. sql_db = ', $db_name, '
  853. sql_port = 3306', empty($db_character_set) ? '' : '
  854. sql_query_pre = SET NAMES ' . $db_character_set, '
  855. sql_query_pre = \
  856. REPLACE INTO ', $db_prefix, 'settings (variable, value) \
  857. SELECT \'sphinx_indexed_msg_until\', MAX(id_msg) \
  858. FROM ', $db_prefix, 'messages
  859. sql_query_range = \
  860. SELECT 1, value \
  861. FROM ', $db_prefix, 'settings \
  862. WHERE variable = \'sphinx_indexed_msg_until\'
  863. sql_range_step = 1000
  864. sql_query = \
  865. SELECT \
  866. m.id_msg, m.id_topic, m.id_board, IF(m.id_member = 0, 4294967295, m.id_member) AS id_member, m.poster_time, m.body, m.subject, \
  867. t.num_replies + 1 AS num_replies, CEILING(1000000 * ( \
  868. IF(m.id_msg < 0.7 * s.value, 0, (m.id_msg - 0.7 * s.value) / (0.3 * s.value)) * ' . $weight['age'] . ' + \
  869. IF(t.num_replies < 200, t.num_replies / 200, 1) * ' . $weight['length'] . ' + \
  870. IF(m.id_msg = t.id_first_msg, 1, 0) * ' . $weight['first_message'] . ' + \
  871. IF(t.is_sticky = 0, 0, 1) * ' . $weight['sticky'] . ' \
  872. ) / ' . $weight_total . ') AS relevance \
  873. FROM ', $db_prefix, 'messages AS m, ', $db_prefix, 'topics AS t, ', $db_prefix, 'settings AS s \
  874. WHERE t.id_topic = m.id_topic \
  875. AND s.variable = \'maxMsgID\' \
  876. AND m.id_msg BETWEEN $start AND $end
  877. sql_attr_uint = id_topic
  878. sql_attr_uint = id_board
  879. sql_attr_uint = id_member
  880. sql_attr_timestamp = poster_time
  881. sql_attr_timestamp = relevance
  882. sql_attr_timestamp = num_replies
  883. sql_query_info = \
  884. SELECT * \
  885. FROM ', $db_prefix, 'messages \
  886. WHERE id_msg = $id
  887. }
  888. source elkarte_delta_source : elkarte_source
  889. {
  890. sql_query_pre = ', isset($db_character_set) ? 'SET NAMES ' . $db_character_set : '', '
  891. sql_query_range = \
  892. SELECT s1.value, s2.value \
  893. FROM ', $db_prefix, 'settings AS s1, ', $db_prefix, 'settings AS s2 \
  894. WHERE s1.variable = \'sphinx_indexed_msg_until\' \
  895. AND s2.variable = \'maxMsgID\'
  896. }
  897. index elkarte_base_index
  898. {
  899. html_strip = 1
  900. source = elkarte_source
  901. path = ', $modSettings['sphinx_data_path'], '/elkarte_sphinx_base.index', empty($modSettings['sphinx_stopword_path']) ? '' : '
  902. stopwords = ' . $modSettings['sphinx_stopword_path'], '
  903. min_word_len = 2
  904. charset_type = utf-8
  905. charset_table = 0..9, A..Z->a..z, _, a..z
  906. }
  907. index elkarte_delta_index : elkarte_base_index
  908. {
  909. source = elkarte_delta_source
  910. path = ', $modSettings['sphinx_data_path'], '/elkarte_sphinx_delta.index
  911. }
  912. index elkarte_index
  913. {
  914. type = distributed
  915. local = elkarte_base_index
  916. local = elkarte_delta_index
  917. }
  918. indexer
  919. {
  920. mem_limit = ', (empty($modSettings['sphinx_indexer_mem']) ? 32 : (int) $modSettings['sphinx_indexer_mem']), 'M
  921. }
  922. searchd
  923. {
  924. listen = ', (empty($modSettings['sphinx_searchd_port']) ? 3312 : (int) $modSettings['sphinx_searchd_port']), '
  925. listen = ', (empty($modSettings['sphinxql_searchd_port']) ? 3313 : (int) $modSettings['sphinxql_searchd_port']), ':mysql41
  926. log = ', $modSettings['sphinx_log_path'], '/searchd.log
  927. query_log = ', $modSettings['sphinx_log_path'], '/query.log
  928. read_timeout = 5
  929. max_children = 30
  930. pid_file = ', $modSettings['sphinx_data_path'], '/searchd.pid
  931. max_matches = ', (empty($modSettings['sphinx_max_results']) ? 3312 : (int) $modSettings['sphinx_max_results']), '
  932. }
  933. ';
  934. obExit(false, false);
  935. }