PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/phpBB/phpbb/search/fulltext_mysql.php

http://github.com/phpbb/phpbb
PHP | 1221 lines | 722 code | 145 blank | 354 comment | 111 complexity | c5c626c71ac078aafb6ef6b8ec9305d4 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\search;
  14. /**
  15. * Fulltext search for MySQL
  16. */
  17. class fulltext_mysql extends \phpbb\search\base
  18. {
  19. /**
  20. * Associative array holding index stats
  21. * @var array
  22. */
  23. protected $stats = array();
  24. /**
  25. * Holds the words entered by user, obtained by splitting the entered query on whitespace
  26. * @var array
  27. */
  28. protected $split_words = array();
  29. /**
  30. * Config object
  31. * @var \phpbb\config\config
  32. */
  33. protected $config;
  34. /**
  35. * Database connection
  36. * @var \phpbb\db\driver\driver_interface
  37. */
  38. protected $db;
  39. /**
  40. * phpBB event dispatcher object
  41. * @var \phpbb\event\dispatcher_interface
  42. */
  43. protected $phpbb_dispatcher;
  44. /**
  45. * User object
  46. * @var \phpbb\user
  47. */
  48. protected $user;
  49. /**
  50. * Associative array stores the min and max word length to be searched
  51. * @var array
  52. */
  53. protected $word_length = array();
  54. /**
  55. * Contains tidied search query.
  56. * Operators are prefixed in search query and common words excluded
  57. * @var string
  58. */
  59. protected $search_query;
  60. /**
  61. * Contains common words.
  62. * Common words are words with length less/more than min/max length
  63. * @var array
  64. */
  65. protected $common_words = array();
  66. /**
  67. * Constructor
  68. * Creates a new \phpbb\search\fulltext_mysql, which is used as a search backend
  69. *
  70. * @param string|bool $error Any error that occurs is passed on through this reference variable otherwise false
  71. * @param string $phpbb_root_path Relative path to phpBB root
  72. * @param string $phpEx PHP file extension
  73. * @param \phpbb\auth\auth $auth Auth object
  74. * @param \phpbb\config\config $config Config object
  75. * @param \phpbb\db\driver\driver_interface $db Database object
  76. * @param \phpbb\user $user User object
  77. * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object
  78. */
  79. public function __construct(&$error, $phpbb_root_path, $phpEx, $auth, $config, $db, $user, $phpbb_dispatcher)
  80. {
  81. $this->config = $config;
  82. $this->db = $db;
  83. $this->phpbb_dispatcher = $phpbb_dispatcher;
  84. $this->user = $user;
  85. $this->word_length = array('min' => $this->config['fulltext_mysql_min_word_len'], 'max' => $this->config['fulltext_mysql_max_word_len']);
  86. /**
  87. * Load the UTF tools
  88. */
  89. if (!function_exists('utf8_strlen'))
  90. {
  91. include($phpbb_root_path . 'includes/utf/utf_tools.' . $phpEx);
  92. }
  93. $error = false;
  94. }
  95. /**
  96. * Returns the name of this search backend to be displayed to administrators
  97. *
  98. * @return string Name
  99. */
  100. public function get_name()
  101. {
  102. return 'MySQL Fulltext';
  103. }
  104. /**
  105. * Returns the search_query
  106. *
  107. * @return string search query
  108. */
  109. public function get_search_query()
  110. {
  111. return $this->search_query;
  112. }
  113. /**
  114. * Returns the common_words array
  115. *
  116. * @return array common words that are ignored by search backend
  117. */
  118. public function get_common_words()
  119. {
  120. return $this->common_words;
  121. }
  122. /**
  123. * Returns the word_length array
  124. *
  125. * @return array min and max word length for searching
  126. */
  127. public function get_word_length()
  128. {
  129. return $this->word_length;
  130. }
  131. /**
  132. * Checks for correct MySQL version and stores min/max word length in the config
  133. *
  134. * @return string|bool Language key of the error/incompatibility occurred
  135. */
  136. public function init()
  137. {
  138. if ($this->db->get_sql_layer() != 'mysqli')
  139. {
  140. return $this->user->lang['FULLTEXT_MYSQL_INCOMPATIBLE_DATABASE'];
  141. }
  142. $result = $this->db->sql_query('SHOW TABLE STATUS LIKE \'' . POSTS_TABLE . '\'');
  143. $info = $this->db->sql_fetchrow($result);
  144. $this->db->sql_freeresult($result);
  145. $engine = '';
  146. if (isset($info['Engine']))
  147. {
  148. $engine = $info['Engine'];
  149. }
  150. else if (isset($info['Type']))
  151. {
  152. $engine = $info['Type'];
  153. }
  154. $fulltext_supported = $engine === 'Aria' || $engine === 'MyISAM'
  155. /**
  156. * FULLTEXT is supported on InnoDB since MySQL 5.6.4 according to
  157. * http://dev.mysql.com/doc/refman/5.6/en/innodb-storage-engine.html
  158. * We also require https://bugs.mysql.com/bug.php?id=67004 to be
  159. * fixed for proper overall operation. Hence we require 5.6.8.
  160. */
  161. || $engine === 'InnoDB'
  162. && phpbb_version_compare($this->db->sql_server_info(true), '5.6.8', '>=');
  163. if (!$fulltext_supported)
  164. {
  165. return $this->user->lang['FULLTEXT_MYSQL_NOT_SUPPORTED'];
  166. }
  167. $sql = 'SHOW VARIABLES
  168. LIKE \'%ft\_%\'';
  169. $result = $this->db->sql_query($sql);
  170. $mysql_info = array();
  171. while ($row = $this->db->sql_fetchrow($result))
  172. {
  173. $mysql_info[$row['Variable_name']] = $row['Value'];
  174. }
  175. $this->db->sql_freeresult($result);
  176. if ($engine === 'MyISAM')
  177. {
  178. $this->config->set('fulltext_mysql_max_word_len', $mysql_info['ft_max_word_len']);
  179. $this->config->set('fulltext_mysql_min_word_len', $mysql_info['ft_min_word_len']);
  180. }
  181. else if ($engine === 'InnoDB')
  182. {
  183. $this->config->set('fulltext_mysql_max_word_len', $mysql_info['innodb_ft_max_token_size']);
  184. $this->config->set('fulltext_mysql_min_word_len', $mysql_info['innodb_ft_min_token_size']);
  185. }
  186. return false;
  187. }
  188. /**
  189. * Splits keywords entered by a user into an array of words stored in $this->split_words
  190. * Stores the tidied search query in $this->search_query
  191. *
  192. * @param string &$keywords Contains the keyword as entered by the user
  193. * @param string $terms is either 'all' or 'any'
  194. * @return bool false if no valid keywords were found and otherwise true
  195. */
  196. public function split_keywords(&$keywords, $terms)
  197. {
  198. if ($terms == 'all')
  199. {
  200. $match = array('#\sand\s#iu', '#\sor\s#iu', '#\snot\s#iu', '#(^|\s)\+#', '#(^|\s)-#', '#(^|\s)\|#');
  201. $replace = array(' +', ' |', ' -', ' +', ' -', ' |');
  202. $keywords = preg_replace($match, $replace, $keywords);
  203. }
  204. // Filter out as above
  205. $split_keywords = preg_replace("#[\n\r\t]+#", ' ', trim(htmlspecialchars_decode($keywords)));
  206. // Split words
  207. $split_keywords = preg_replace('#([^\p{L}\p{N}\'*"()])#u', '$1$1', str_replace('\'\'', '\' \'', trim($split_keywords)));
  208. $matches = array();
  209. preg_match_all('#(?:[^\p{L}\p{N}*"()]|^)([+\-|]?(?:[\p{L}\p{N}*"()]+\'?)*[\p{L}\p{N}*"()])(?:[^\p{L}\p{N}*"()]|$)#u', $split_keywords, $matches);
  210. $this->split_words = $matches[1];
  211. // We limit the number of allowed keywords to minimize load on the database
  212. if ($this->config['max_num_search_keywords'] && count($this->split_words) > $this->config['max_num_search_keywords'])
  213. {
  214. trigger_error($this->user->lang('MAX_NUM_SEARCH_KEYWORDS_REFINE', (int) $this->config['max_num_search_keywords'], count($this->split_words)));
  215. }
  216. // to allow phrase search, we need to concatenate quoted words
  217. $tmp_split_words = array();
  218. $phrase = '';
  219. foreach ($this->split_words as $word)
  220. {
  221. if ($phrase)
  222. {
  223. $phrase .= ' ' . $word;
  224. if (strpos($word, '"') !== false && substr_count($word, '"') % 2 == 1)
  225. {
  226. $tmp_split_words[] = $phrase;
  227. $phrase = '';
  228. }
  229. }
  230. else if (strpos($word, '"') !== false && substr_count($word, '"') % 2 == 1)
  231. {
  232. $phrase = $word;
  233. }
  234. else
  235. {
  236. $tmp_split_words[] = $word;
  237. }
  238. }
  239. if ($phrase)
  240. {
  241. $tmp_split_words[] = $phrase;
  242. }
  243. $this->split_words = $tmp_split_words;
  244. unset($tmp_split_words);
  245. unset($phrase);
  246. foreach ($this->split_words as $i => $word)
  247. {
  248. // Check for not allowed search queries for InnoDB.
  249. // We assume similar restrictions for MyISAM, which is usually even
  250. // slower but not as restrictive as InnoDB.
  251. // InnoDB full-text search does not support the use of a leading
  252. // plus sign with wildcard ('+*'), a plus and minus sign
  253. // combination ('+-'), or leading a plus and minus sign combination.
  254. // InnoDB full-text search only supports leading plus or minus signs.
  255. // For example, InnoDB supports '+apple' but does not support 'apple+'.
  256. // Specifying a trailing plus or minus sign causes InnoDB to report
  257. // a syntax error. InnoDB full-text search does not support the use
  258. // of multiple operators on a single search word, as in this example:
  259. // '++apple'. Use of multiple operators on a single search word
  260. // returns a syntax error to standard out.
  261. // Also, ensure that the wildcard character is only used at the
  262. // end of the line as it's intended by MySQL.
  263. if (preg_match('#^(\+[+-]|\+\*|.+[+-]$|.+\*(?!$))#', $word))
  264. {
  265. unset($this->split_words[$i]);
  266. continue;
  267. }
  268. $clean_word = preg_replace('#^[+\-|"]#', '', $word);
  269. // check word length
  270. $clean_len = utf8_strlen(str_replace('*', '', $clean_word));
  271. if (($clean_len < $this->config['fulltext_mysql_min_word_len']) || ($clean_len > $this->config['fulltext_mysql_max_word_len']))
  272. {
  273. $this->common_words[] = $word;
  274. unset($this->split_words[$i]);
  275. }
  276. }
  277. if ($terms == 'any')
  278. {
  279. $this->search_query = '';
  280. foreach ($this->split_words as $word)
  281. {
  282. if ((strpos($word, '+') === 0) || (strpos($word, '-') === 0) || (strpos($word, '|') === 0))
  283. {
  284. $word = substr($word, 1);
  285. }
  286. $this->search_query .= $word . ' ';
  287. }
  288. }
  289. else
  290. {
  291. $this->search_query = '';
  292. foreach ($this->split_words as $word)
  293. {
  294. if ((strpos($word, '+') === 0) || (strpos($word, '-') === 0))
  295. {
  296. $this->search_query .= $word . ' ';
  297. }
  298. else if (strpos($word, '|') === 0)
  299. {
  300. $this->search_query .= substr($word, 1) . ' ';
  301. }
  302. else
  303. {
  304. $this->search_query .= '+' . $word . ' ';
  305. }
  306. }
  307. }
  308. $this->search_query = utf8_htmlspecialchars($this->search_query);
  309. if ($this->search_query)
  310. {
  311. $this->split_words = array_values($this->split_words);
  312. sort($this->split_words);
  313. return true;
  314. }
  315. return false;
  316. }
  317. /**
  318. * Turns text into an array of words
  319. * @param string $text contains post text/subject
  320. */
  321. public function split_message($text)
  322. {
  323. // Split words
  324. $text = preg_replace('#([^\p{L}\p{N}\'*])#u', '$1$1', str_replace('\'\'', '\' \'', trim($text)));
  325. $matches = array();
  326. preg_match_all('#(?:[^\p{L}\p{N}*]|^)([+\-|]?(?:[\p{L}\p{N}*]+\'?)*[\p{L}\p{N}*])(?:[^\p{L}\p{N}*]|$)#u', $text, $matches);
  327. $text = $matches[1];
  328. // remove too short or too long words
  329. $text = array_values($text);
  330. for ($i = 0, $n = count($text); $i < $n; $i++)
  331. {
  332. $text[$i] = trim($text[$i]);
  333. if (utf8_strlen($text[$i]) < $this->config['fulltext_mysql_min_word_len'] || utf8_strlen($text[$i]) > $this->config['fulltext_mysql_max_word_len'])
  334. {
  335. unset($text[$i]);
  336. }
  337. }
  338. return array_values($text);
  339. }
  340. /**
  341. * Performs a search on keywords depending on display specific params. You have to run split_keywords() first
  342. *
  343. * @param string $type contains either posts or topics depending on what should be searched for
  344. * @param string $fields contains either titleonly (topic titles should be searched), msgonly (only message bodies should be searched), firstpost (only subject and body of the first post should be searched) or all (all post bodies and subjects should be searched)
  345. * @param string $terms is either 'all' (use query as entered, words without prefix should default to "have to be in field") or 'any' (ignore search query parts and just return all posts that contain any of the specified words)
  346. * @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
  347. * @param string $sort_key is the key of $sort_by_sql for the selected sorting
  348. * @param string $sort_dir is either a or d representing ASC and DESC
  349. * @param string $sort_days specifies the maximum amount of days a post may be old
  350. * @param array $ex_fid_ary specifies an array of forum ids which should not be searched
  351. * @param string $post_visibility specifies which types of posts the user can view in which forums
  352. * @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
  353. * @param array $author_ary an array of author ids if the author should be ignored during the search the array is empty
  354. * @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
  355. * @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
  356. * @param int $start indicates the first index of the page
  357. * @param int $per_page number of ids each page is supposed to contain
  358. * @return boolean|int total number of results
  359. */
  360. public function keyword_search($type, $fields, $terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
  361. {
  362. // No keywords? No posts
  363. if (!$this->search_query)
  364. {
  365. return false;
  366. }
  367. // generate a search_key from all the options to identify the results
  368. $search_key_array = array(
  369. implode(', ', $this->split_words),
  370. $type,
  371. $fields,
  372. $terms,
  373. $sort_days,
  374. $sort_key,
  375. $topic_id,
  376. implode(',', $ex_fid_ary),
  377. $post_visibility,
  378. implode(',', $author_ary)
  379. );
  380. /**
  381. * Allow changing the search_key for cached results
  382. *
  383. * @event core.search_mysql_by_keyword_modify_search_key
  384. * @var array search_key_array Array with search parameters to generate the search_key
  385. * @var string type Searching type ('posts', 'topics')
  386. * @var string fields Searching fields ('titleonly', 'msgonly', 'firstpost', 'all')
  387. * @var string terms Searching terms ('all', 'any')
  388. * @var int sort_days Time, in days, of the oldest possible post to list
  389. * @var string sort_key The sort type used from the possible sort types
  390. * @var int topic_id Limit the search to this topic_id only
  391. * @var array ex_fid_ary Which forums not to search on
  392. * @var string post_visibility Post visibility data
  393. * @var array author_ary Array of user_id containing the users to filter the results to
  394. * @since 3.1.7-RC1
  395. */
  396. $vars = array(
  397. 'search_key_array',
  398. 'type',
  399. 'fields',
  400. 'terms',
  401. 'sort_days',
  402. 'sort_key',
  403. 'topic_id',
  404. 'ex_fid_ary',
  405. 'post_visibility',
  406. 'author_ary',
  407. );
  408. extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_by_keyword_modify_search_key', compact($vars)));
  409. $search_key = md5(implode('#', $search_key_array));
  410. if ($start < 0)
  411. {
  412. $start = 0;
  413. }
  414. // try reading the results from cache
  415. $result_count = 0;
  416. if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE)
  417. {
  418. return $result_count;
  419. }
  420. $id_ary = array();
  421. $join_topic = ($type == 'posts') ? false : true;
  422. // Build sql strings for sorting
  423. $sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
  424. $sql_sort_table = $sql_sort_join = '';
  425. switch ($sql_sort[0])
  426. {
  427. case 'u':
  428. $sql_sort_table = USERS_TABLE . ' u, ';
  429. $sql_sort_join = ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster ';
  430. break;
  431. case 't':
  432. $join_topic = true;
  433. break;
  434. case 'f':
  435. $sql_sort_table = FORUMS_TABLE . ' f, ';
  436. $sql_sort_join = ' AND f.forum_id = p.forum_id ';
  437. break;
  438. }
  439. // Build some display specific sql strings
  440. switch ($fields)
  441. {
  442. case 'titleonly':
  443. $sql_match = 'p.post_subject';
  444. $sql_match_where = ' AND p.post_id = t.topic_first_post_id';
  445. $join_topic = true;
  446. break;
  447. case 'msgonly':
  448. $sql_match = 'p.post_text';
  449. $sql_match_where = '';
  450. break;
  451. case 'firstpost':
  452. $sql_match = 'p.post_subject, p.post_text';
  453. $sql_match_where = ' AND p.post_id = t.topic_first_post_id';
  454. $join_topic = true;
  455. break;
  456. default:
  457. $sql_match = 'p.post_subject, p.post_text';
  458. $sql_match_where = '';
  459. break;
  460. }
  461. $search_query = $this->search_query;
  462. /**
  463. * Allow changing the query used to search for posts using fulltext_mysql
  464. *
  465. * @event core.search_mysql_keywords_main_query_before
  466. * @var string search_query The parsed keywords used for this search
  467. * @var int result_count The previous result count for the format of the query.
  468. * Set to 0 to force a re-count
  469. * @var bool join_topic Weather or not TOPICS_TABLE should be CROSS JOIN'ED
  470. * @var array author_ary Array of user_id containing the users to filter the results to
  471. * @var string author_name An extra username to search on (!empty(author_ary) must be true, to be relevant)
  472. * @var array ex_fid_ary Which forums not to search on
  473. * @var int topic_id Limit the search to this topic_id only
  474. * @var string sql_sort_table Extra tables to include in the SQL query.
  475. * Used in conjunction with sql_sort_join
  476. * @var string sql_sort_join SQL conditions to join all the tables used together.
  477. * Used in conjunction with sql_sort_table
  478. * @var int sort_days Time, in days, of the oldest possible post to list
  479. * @var string sql_match Which columns to do the search on.
  480. * @var string sql_match_where Extra conditions to use to properly filter the matching process
  481. * @var string sort_by_sql The possible predefined sort types
  482. * @var string sort_key The sort type used from the possible sort types
  483. * @var string sort_dir "a" for ASC or "d" dor DESC for the sort order used
  484. * @var string sql_sort The result SQL when processing sort_by_sql + sort_key + sort_dir
  485. * @var int start How many posts to skip in the search results (used for pagination)
  486. * @since 3.1.5-RC1
  487. */
  488. $vars = array(
  489. 'search_query',
  490. 'result_count',
  491. 'join_topic',
  492. 'author_ary',
  493. 'author_name',
  494. 'ex_fid_ary',
  495. 'topic_id',
  496. 'sql_sort_table',
  497. 'sql_sort_join',
  498. 'sort_days',
  499. 'sql_match',
  500. 'sql_match_where',
  501. 'sort_by_sql',
  502. 'sort_key',
  503. 'sort_dir',
  504. 'sql_sort',
  505. 'start',
  506. );
  507. extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_keywords_main_query_before', compact($vars)));
  508. $sql_select = (!$result_count) ? 'SQL_CALC_FOUND_ROWS ' : '';
  509. $sql_select = ($type == 'posts') ? $sql_select . 'p.post_id' : 'DISTINCT ' . $sql_select . 't.topic_id';
  510. $sql_from = ($join_topic) ? TOPICS_TABLE . ' t, ' : '';
  511. $field = ($type == 'posts') ? 'post_id' : 'topic_id';
  512. if (count($author_ary) && $author_name)
  513. {
  514. // first one matches post of registered users, second one guests and deleted users
  515. $sql_author = ' AND (' . $this->db->sql_in_set('p.poster_id', array_diff($author_ary, array(ANONYMOUS)), false, true) . ' OR p.post_username ' . $author_name . ')';
  516. }
  517. else if (count($author_ary))
  518. {
  519. $sql_author = ' AND ' . $this->db->sql_in_set('p.poster_id', $author_ary);
  520. }
  521. else
  522. {
  523. $sql_author = '';
  524. }
  525. $sql_where_options = $sql_sort_join;
  526. $sql_where_options .= ($topic_id) ? ' AND p.topic_id = ' . $topic_id : '';
  527. $sql_where_options .= ($join_topic) ? ' AND t.topic_id = p.topic_id' : '';
  528. $sql_where_options .= (count($ex_fid_ary)) ? ' AND ' . $this->db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '';
  529. $sql_where_options .= ' AND ' . $post_visibility;
  530. $sql_where_options .= $sql_author;
  531. $sql_where_options .= ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : '';
  532. $sql_where_options .= $sql_match_where;
  533. $sql = "SELECT $sql_select
  534. FROM $sql_from$sql_sort_table" . POSTS_TABLE . " p
  535. WHERE MATCH ($sql_match) AGAINST ('" . $this->db->sql_escape(htmlspecialchars_decode($this->search_query)) . "' IN BOOLEAN MODE)
  536. $sql_where_options
  537. ORDER BY $sql_sort";
  538. $this->db->sql_return_on_error(true);
  539. $result = $this->db->sql_query_limit($sql, $this->config['search_block_size'], $start);
  540. while ($row = $this->db->sql_fetchrow($result))
  541. {
  542. $id_ary[] = (int) $row[$field];
  543. }
  544. $this->db->sql_freeresult($result);
  545. $id_ary = array_unique($id_ary);
  546. // if the total result count is not cached yet, retrieve it from the db
  547. if (!$result_count && count($id_ary))
  548. {
  549. $sql_found_rows = 'SELECT FOUND_ROWS() as result_count';
  550. $result = $this->db->sql_query($sql_found_rows);
  551. $result_count = (int) $this->db->sql_fetchfield('result_count');
  552. $this->db->sql_freeresult($result);
  553. if (!$result_count)
  554. {
  555. return false;
  556. }
  557. }
  558. if ($start >= $result_count)
  559. {
  560. $start = floor(($result_count - 1) / $per_page) * $per_page;
  561. $result = $this->db->sql_query_limit($sql, $this->config['search_block_size'], $start);
  562. while ($row = $this->db->sql_fetchrow($result))
  563. {
  564. $id_ary[] = (int) $row[$field];
  565. }
  566. $this->db->sql_freeresult($result);
  567. $id_ary = array_unique($id_ary);
  568. }
  569. // store the ids, from start on then delete anything that isn't on the current page because we only need ids for one page
  570. $this->save_ids($search_key, implode(' ', $this->split_words), $author_ary, $result_count, $id_ary, $start, $sort_dir);
  571. $id_ary = array_slice($id_ary, 0, (int) $per_page);
  572. return $result_count;
  573. }
  574. /**
  575. * Performs a search on an author's posts without caring about message contents. Depends on display specific params
  576. *
  577. * @param string $type contains either posts or topics depending on what should be searched for
  578. * @param boolean $firstpost_only if true, only topic starting posts will be considered
  579. * @param array $sort_by_sql contains SQL code for the ORDER BY part of a query
  580. * @param string $sort_key is the key of $sort_by_sql for the selected sorting
  581. * @param string $sort_dir is either a or d representing ASC and DESC
  582. * @param string $sort_days specifies the maximum amount of days a post may be old
  583. * @param array $ex_fid_ary specifies an array of forum ids which should not be searched
  584. * @param string $post_visibility specifies which types of posts the user can view in which forums
  585. * @param int $topic_id is set to 0 or a topic id, if it is not 0 then only posts in this topic should be searched
  586. * @param array $author_ary an array of author ids
  587. * @param string $author_name specifies the author match, when ANONYMOUS is also a search-match
  588. * @param array &$id_ary passed by reference, to be filled with ids for the page specified by $start and $per_page, should be ordered
  589. * @param int $start indicates the first index of the page
  590. * @param int $per_page number of ids each page is supposed to contain
  591. * @return boolean|int total number of results
  592. */
  593. public function author_search($type, $firstpost_only, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page)
  594. {
  595. // No author? No posts
  596. if (!count($author_ary))
  597. {
  598. return 0;
  599. }
  600. // generate a search_key from all the options to identify the results
  601. $search_key_array = array(
  602. '',
  603. $type,
  604. ($firstpost_only) ? 'firstpost' : '',
  605. '',
  606. '',
  607. $sort_days,
  608. $sort_key,
  609. $topic_id,
  610. implode(',', $ex_fid_ary),
  611. $post_visibility,
  612. implode(',', $author_ary),
  613. $author_name,
  614. );
  615. /**
  616. * Allow changing the search_key for cached results
  617. *
  618. * @event core.search_mysql_by_author_modify_search_key
  619. * @var array search_key_array Array with search parameters to generate the search_key
  620. * @var string type Searching type ('posts', 'topics')
  621. * @var boolean firstpost_only Flag indicating if only topic starting posts are considered
  622. * @var int sort_days Time, in days, of the oldest possible post to list
  623. * @var string sort_key The sort type used from the possible sort types
  624. * @var int topic_id Limit the search to this topic_id only
  625. * @var array ex_fid_ary Which forums not to search on
  626. * @var string post_visibility Post visibility data
  627. * @var array author_ary Array of user_id containing the users to filter the results to
  628. * @var string author_name The username to search on
  629. * @since 3.1.7-RC1
  630. */
  631. $vars = array(
  632. 'search_key_array',
  633. 'type',
  634. 'firstpost_only',
  635. 'sort_days',
  636. 'sort_key',
  637. 'topic_id',
  638. 'ex_fid_ary',
  639. 'post_visibility',
  640. 'author_ary',
  641. 'author_name',
  642. );
  643. extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_by_author_modify_search_key', compact($vars)));
  644. $search_key = md5(implode('#', $search_key_array));
  645. if ($start < 0)
  646. {
  647. $start = 0;
  648. }
  649. // try reading the results from cache
  650. $result_count = 0;
  651. if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE)
  652. {
  653. return $result_count;
  654. }
  655. $id_ary = array();
  656. // Create some display specific sql strings
  657. if ($author_name)
  658. {
  659. // first one matches post of registered users, second one guests and deleted users
  660. $sql_author = '(' . $this->db->sql_in_set('p.poster_id', array_diff($author_ary, array(ANONYMOUS)), false, true) . ' OR p.post_username ' . $author_name . ')';
  661. }
  662. else
  663. {
  664. $sql_author = $this->db->sql_in_set('p.poster_id', $author_ary);
  665. }
  666. $sql_fora = (count($ex_fid_ary)) ? ' AND ' . $this->db->sql_in_set('p.forum_id', $ex_fid_ary, true) : '';
  667. $sql_topic_id = ($topic_id) ? ' AND p.topic_id = ' . (int) $topic_id : '';
  668. $sql_time = ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : '';
  669. $sql_firstpost = ($firstpost_only) ? ' AND p.post_id = t.topic_first_post_id' : '';
  670. // Build sql strings for sorting
  671. $sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC');
  672. $sql_sort_table = $sql_sort_join = '';
  673. switch ($sql_sort[0])
  674. {
  675. case 'u':
  676. $sql_sort_table = USERS_TABLE . ' u, ';
  677. $sql_sort_join = ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster ';
  678. break;
  679. case 't':
  680. $sql_sort_table = ($type == 'posts' && !$firstpost_only) ? TOPICS_TABLE . ' t, ' : '';
  681. $sql_sort_join = ($type == 'posts' && !$firstpost_only) ? ' AND t.topic_id = p.topic_id ' : '';
  682. break;
  683. case 'f':
  684. $sql_sort_table = FORUMS_TABLE . ' f, ';
  685. $sql_sort_join = ' AND f.forum_id = p.forum_id ';
  686. break;
  687. }
  688. $m_approve_fid_sql = ' AND ' . $post_visibility;
  689. /**
  690. * Allow changing the query used to search for posts by author in fulltext_mysql
  691. *
  692. * @event core.search_mysql_author_query_before
  693. * @var int result_count The previous result count for the format of the query.
  694. * Set to 0 to force a re-count
  695. * @var string sql_sort_table CROSS JOIN'ed table to allow doing the sort chosen
  696. * @var string sql_sort_join Condition to define how to join the CROSS JOIN'ed table specifyed in sql_sort_table
  697. * @var string type Either "posts" or "topics" specifying the type of search being made
  698. * @var array author_ary Array of user_id containing the users to filter the results to
  699. * @var string author_name An extra username to search on
  700. * @var string sql_author SQL WHERE condition for the post author ids
  701. * @var int topic_id Limit the search to this topic_id only
  702. * @var string sql_topic_id SQL of topic_id
  703. * @var string sort_by_sql The possible predefined sort types
  704. * @var string sort_key The sort type used from the possible sort types
  705. * @var string sort_dir "a" for ASC or "d" dor DESC for the sort order used
  706. * @var string sql_sort The result SQL when processing sort_by_sql + sort_key + sort_dir
  707. * @var string sort_days Time, in days, that the oldest post showing can have
  708. * @var string sql_time The SQL to search on the time specifyed by sort_days
  709. * @var bool firstpost_only Wether or not to search only on the first post of the topics
  710. * @var string sql_firstpost The SQL with the conditions to join the tables when using firstpost_only
  711. * @var array ex_fid_ary Forum ids that must not be searched on
  712. * @var array sql_fora SQL query for ex_fid_ary
  713. * @var string m_approve_fid_sql WHERE clause condition on post_visibility restrictions
  714. * @var int start How many posts to skip in the search results (used for pagination)
  715. * @since 3.1.5-RC1
  716. */
  717. $vars = array(
  718. 'result_count',
  719. 'sql_sort_table',
  720. 'sql_sort_join',
  721. 'type',
  722. 'author_ary',
  723. 'author_name',
  724. 'sql_author',
  725. 'topic_id',
  726. 'sql_topic_id',
  727. 'sort_by_sql',
  728. 'sort_key',
  729. 'sort_dir',
  730. 'sql_sort',
  731. 'sort_days',
  732. 'sql_time',
  733. 'firstpost_only',
  734. 'sql_firstpost',
  735. 'ex_fid_ary',
  736. 'sql_fora',
  737. 'm_approve_fid_sql',
  738. 'start',
  739. );
  740. extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_author_query_before', compact($vars)));
  741. // If the cache was completely empty count the results
  742. $calc_results = ($result_count) ? '' : 'SQL_CALC_FOUND_ROWS ';
  743. // Build the query for really selecting the post_ids
  744. if ($type == 'posts')
  745. {
  746. $sql = "SELECT {$calc_results}p.post_id
  747. FROM " . $sql_sort_table . POSTS_TABLE . ' p' . (($firstpost_only) ? ', ' . TOPICS_TABLE . ' t ' : ' ') . "
  748. WHERE $sql_author
  749. $sql_topic_id
  750. $sql_firstpost
  751. $m_approve_fid_sql
  752. $sql_fora
  753. $sql_sort_join
  754. $sql_time
  755. ORDER BY $sql_sort";
  756. $field = 'post_id';
  757. }
  758. else
  759. {
  760. $sql = "SELECT {$calc_results}t.topic_id
  761. FROM " . $sql_sort_table . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p
  762. WHERE $sql_author
  763. $sql_topic_id
  764. $sql_firstpost
  765. $m_approve_fid_sql
  766. $sql_fora
  767. AND t.topic_id = p.topic_id
  768. $sql_sort_join
  769. $sql_time
  770. GROUP BY t.topic_id
  771. ORDER BY $sql_sort";
  772. $field = 'topic_id';
  773. }
  774. // Only read one block of posts from the db and then cache it
  775. $result = $this->db->sql_query_limit($sql, $this->config['search_block_size'], $start);
  776. while ($row = $this->db->sql_fetchrow($result))
  777. {
  778. $id_ary[] = (int) $row[$field];
  779. }
  780. $this->db->sql_freeresult($result);
  781. // retrieve the total result count if needed
  782. if (!$result_count)
  783. {
  784. $sql_found_rows = 'SELECT FOUND_ROWS() as result_count';
  785. $result = $this->db->sql_query($sql_found_rows);
  786. $result_count = (int) $this->db->sql_fetchfield('result_count');
  787. $this->db->sql_freeresult($result);
  788. if (!$result_count)
  789. {
  790. return false;
  791. }
  792. }
  793. if ($start >= $result_count)
  794. {
  795. $start = floor(($result_count - 1) / $per_page) * $per_page;
  796. $result = $this->db->sql_query_limit($sql, $this->config['search_block_size'], $start);
  797. while ($row = $this->db->sql_fetchrow($result))
  798. {
  799. $id_ary[] = (int) $row[$field];
  800. }
  801. $this->db->sql_freeresult($result);
  802. $id_ary = array_unique($id_ary);
  803. }
  804. if (count($id_ary))
  805. {
  806. $this->save_ids($search_key, '', $author_ary, $result_count, $id_ary, $start, $sort_dir);
  807. $id_ary = array_slice($id_ary, 0, $per_page);
  808. return $result_count;
  809. }
  810. return false;
  811. }
  812. /**
  813. * Destroys cached search results, that contained one of the new words in a post so the results won't be outdated
  814. *
  815. * @param string $mode contains the post mode: edit, post, reply, quote ...
  816. * @param int $post_id contains the post id of the post to index
  817. * @param string $message contains the post text of the post
  818. * @param string $subject contains the subject of the post to index
  819. * @param int $poster_id contains the user id of the poster
  820. * @param int $forum_id contains the forum id of parent forum of the post
  821. */
  822. public function index($mode, $post_id, &$message, &$subject, $poster_id, $forum_id)
  823. {
  824. // Split old and new post/subject to obtain array of words
  825. $split_text = $this->split_message($message);
  826. $split_title = ($subject) ? $this->split_message($subject) : array();
  827. $words = array_unique(array_merge($split_text, $split_title));
  828. /**
  829. * Event to modify method arguments and words before the MySQL search index is updated
  830. *
  831. * @event core.search_mysql_index_before
  832. * @var string mode Contains the post mode: edit, post, reply, quote
  833. * @var int post_id The id of the post which is modified/created
  834. * @var string message New or updated post content
  835. * @var string subject New or updated post subject
  836. * @var int poster_id Post author's user id
  837. * @var int forum_id The id of the forum in which the post is located
  838. * @var array words List of words added to the index
  839. * @var array split_text Array of words from the message
  840. * @var array split_title Array of words from the title
  841. * @since 3.2.3-RC1
  842. */
  843. $vars = array(
  844. 'mode',
  845. 'post_id',
  846. 'message',
  847. 'subject',
  848. 'poster_id',
  849. 'forum_id',
  850. 'words',
  851. 'split_text',
  852. 'split_title',
  853. );
  854. extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_index_before', compact($vars)));
  855. unset($split_text);
  856. unset($split_title);
  857. // destroy cached search results containing any of the words removed or added
  858. $this->destroy_cache($words, array($poster_id));
  859. unset($words);
  860. }
  861. /**
  862. * Destroy cached results, that might be outdated after deleting a post
  863. */
  864. public function index_remove($post_ids, $author_ids, $forum_ids)
  865. {
  866. $this->destroy_cache(array(), array_unique($author_ids));
  867. }
  868. /**
  869. * Destroy old cache entries
  870. */
  871. public function tidy()
  872. {
  873. // destroy too old cached search results
  874. $this->destroy_cache(array());
  875. $this->config->set('search_last_gc', time(), false);
  876. }
  877. /**
  878. * Create fulltext index
  879. *
  880. * @return string|bool error string is returned incase of errors otherwise false
  881. */
  882. public function create_index($acp_module, $u_action)
  883. {
  884. // Make sure we can actually use MySQL with fulltext indexes
  885. if ($error = $this->init())
  886. {
  887. return $error;
  888. }
  889. if (empty($this->stats))
  890. {
  891. $this->get_stats();
  892. }
  893. $alter_list = array();
  894. if (!isset($this->stats['post_subject']))
  895. {
  896. $alter_entry = array();
  897. $alter_entry[] = 'MODIFY post_subject varchar(255) COLLATE utf8_unicode_ci DEFAULT \'\' NOT NULL';
  898. $alter_entry[] = 'ADD FULLTEXT (post_subject)';
  899. $alter_list[] = $alter_entry;
  900. }
  901. if (!isset($this->stats['post_content']))
  902. {
  903. $alter_entry = array();
  904. $alter_entry[] = 'MODIFY post_text mediumtext COLLATE utf8_unicode_ci NOT NULL';
  905. $alter_entry[] = 'ADD FULLTEXT post_content (post_text, post_subject)';
  906. $alter_list[] = $alter_entry;
  907. }
  908. $sql_queries = [];
  909. foreach ($alter_list as $alter)
  910. {
  911. $sql_queries[] = 'ALTER TABLE ' . POSTS_TABLE . ' ' . implode(', ', $alter);
  912. }
  913. if (!isset($this->stats['post_text']))
  914. {
  915. $sql_queries[] = 'ALTER TABLE ' . POSTS_TABLE . ' ADD FULLTEXT post_text (post_text)';
  916. }
  917. $stats = $this->stats;
  918. /**
  919. * Event to modify SQL queries before the MySQL search index is created
  920. *
  921. * @event core.search_mysql_create_index_before
  922. * @var array sql_queries Array with queries for creating the search index
  923. * @var array stats Array with statistics of the current index (read only)
  924. * @since 3.2.3-RC1
  925. */
  926. $vars = array(
  927. 'sql_queries',
  928. 'stats',
  929. );
  930. extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_create_index_before', compact($vars)));
  931. foreach ($sql_queries as $sql_query)
  932. {
  933. $this->db->sql_query($sql_query);
  934. }
  935. $this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
  936. return false;
  937. }
  938. /**
  939. * Drop fulltext index
  940. *
  941. * @return string|bool error string is returned incase of errors otherwise false
  942. */
  943. public function delete_index($acp_module, $u_action)
  944. {
  945. // Make sure we can actually use MySQL with fulltext indexes
  946. if ($error = $this->init())
  947. {
  948. return $error;
  949. }
  950. if (empty($this->stats))
  951. {
  952. $this->get_stats();
  953. }
  954. $alter = array();
  955. if (isset($this->stats['post_subject']))
  956. {
  957. $alter[] = 'DROP INDEX post_subject';
  958. }
  959. if (isset($this->stats['post_content']))
  960. {
  961. $alter[] = 'DROP INDEX post_content';
  962. }
  963. if (isset($this->stats['post_text']))
  964. {
  965. $alter[] = 'DROP INDEX post_text';
  966. }
  967. $sql_queries = [];
  968. if (count($alter))
  969. {
  970. $sql_queries[] = 'ALTER TABLE ' . POSTS_TABLE . ' ' . implode(', ', $alter);
  971. }
  972. $stats = $this->stats;
  973. /**
  974. * Event to modify SQL queries before the MySQL search index is deleted
  975. *
  976. * @event core.search_mysql_delete_index_before
  977. * @var array sql_queries Array with queries for deleting the search index
  978. * @var array stats Array with statistics of the current index (read only)
  979. * @since 3.2.3-RC1
  980. */
  981. $vars = array(
  982. 'sql_queries',
  983. 'stats',
  984. );
  985. extract($this->phpbb_dispatcher->trigger_event('core.search_mysql_delete_index_before', compact($vars)));
  986. foreach ($sql_queries as $sql_query)
  987. {
  988. $this->db->sql_query($sql_query);
  989. }
  990. $this->db->sql_query('TRUNCATE TABLE ' . SEARCH_RESULTS_TABLE);
  991. return false;
  992. }
  993. /**
  994. * Returns true if both FULLTEXT indexes exist
  995. */
  996. public function index_created()
  997. {
  998. if (empty($this->stats))
  999. {
  1000. $this->get_stats();
  1001. }
  1002. return isset($this->stats['post_subject']) && isset($this->stats['post_content']) && isset($this->stats['post_text']);
  1003. }
  1004. /**
  1005. * Returns an associative array containing information about the indexes
  1006. */
  1007. public function index_stats()
  1008. {
  1009. if (empty($this->stats))
  1010. {
  1011. $this->get_stats();
  1012. }
  1013. return array(
  1014. $this->user->lang['FULLTEXT_MYSQL_TOTAL_POSTS'] => ($this->index_created()) ? $this->stats['total_posts'] : 0,
  1015. );
  1016. }
  1017. /**
  1018. * Computes the stats and store them in the $this->stats associative array
  1019. */
  1020. protected function get_stats()
  1021. {
  1022. if (strpos($this->db->get_sql_layer(), 'mysql') === false)
  1023. {
  1024. $this->stats = array();
  1025. return;
  1026. }
  1027. $sql = 'SHOW INDEX
  1028. FROM ' . POSTS_TABLE;
  1029. $result = $this->db->sql_query($sql);
  1030. while ($row = $this->db->sql_fetchrow($result))
  1031. {
  1032. // deal with older MySQL versions which didn't use Index_type
  1033. $index_type = (isset($row['Index_type'])) ? $row['Index_type'] : $row['Comment'];
  1034. if ($index_type == 'FULLTEXT')
  1035. {
  1036. if ($row['Key_name'] == 'post_subject')
  1037. {
  1038. $this->stats['post_subject'] = $row;
  1039. }
  1040. else if ($row['Key_name'] == 'post_text')
  1041. {
  1042. $this->stats['post_text'] = $row;
  1043. }
  1044. else if ($row['Key_name'] == 'post_content')
  1045. {
  1046. $this->stats['post_content'] = $row;
  1047. }
  1048. }
  1049. }
  1050. $this->db->sql_freeresult($result);
  1051. $this->stats['total_posts'] = empty($this->stats) ? 0 : $this->db->get_estimated_row_count(POSTS_TABLE);
  1052. }
  1053. /**
  1054. * Display a note, that UTF-8 support is not available with certain versions of PHP
  1055. *
  1056. * @return associative array containing template and config variables
  1057. */
  1058. public function acp()
  1059. {
  1060. $tpl = '
  1061. <dl>
  1062. <dt><label>' . $this->user->lang['MIN_SEARCH_CHARS'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['FULLTEXT_MYSQL_MIN_SEARCH_CHARS_EXPLAIN'] . '</span></dt>
  1063. <dd>' . $this->config['fulltext_mysql_min_word_len'] . '</dd>
  1064. </dl>
  1065. <dl>
  1066. <dt><label>' . $this->user->lang['MAX_SEARCH_CHARS'] . $this->user->lang['COLON'] . '</label><br /><span>' . $this->user->lang['FULLTEXT_MYSQL_MAX_SEARCH_CHARS_EXPLAIN'] . '</span></dt>
  1067. <dd>' . $this->config['fulltext_mysql_max_word_len'] . '</dd>
  1068. </dl>
  1069. ';
  1070. // These are fields required in the config table
  1071. return array(
  1072. 'tpl' => $tpl,
  1073. 'config' => array()
  1074. );
  1075. }
  1076. }