SearchAPI-Custom.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Simple Machines Forum (SMF)
  4. *
  5. * @package SMF
  6. * @author Simple Machines http://www.simplemachines.org
  7. * @copyright 2011 Simple Machines
  8. * @license http://www.simplemachines.org/about/smf/license.php BSD
  9. *
  10. * @version 2.0
  11. */
  12. if (!defined('SMF'))
  13. die('Hacking attempt...');
  14. /*
  15. int searchSort(string $wordA, string $wordB)
  16. - callback function for usort used to sort the fulltext results.
  17. - the order of sorting is: large words, small words, large words that
  18. are excluded from the search, small words that are excluded.
  19. */
  20. class custom_search
  21. {
  22. // This is the last version of SMF that this was tested on, to protect against API changes.
  23. public $version_compatible = 'SMF 2.0';
  24. // This won't work with versions of SMF less than this.
  25. public $min_smf_version = 'SMF 2.0 Beta 2';
  26. // Is it supported?
  27. public $is_supported = true;
  28. protected $indexSettings = array();
  29. // What words are banned?
  30. protected $bannedWords = array();
  31. // What is the minimum word length?
  32. protected $min_word_length = null;
  33. // What databases support the custom index?
  34. protected $supported_databases = array('mysql', 'postgresql', 'sqlite');
  35. public function __construct()
  36. {
  37. global $modSettings, $db_type;
  38. // Is this database supported?
  39. if (!in_array($db_type, $this->supported_databases))
  40. {
  41. $this->is_supported = false;
  42. return;
  43. }
  44. if (empty($modSettings['search_custom_index_config']))
  45. return;
  46. $this->indexSettings = unserialize($modSettings['search_custom_index_config']);
  47. $this->bannedWords = empty($modSettings['search_stopwords']) ? array() : explode(',', $modSettings['search_stopwords']);
  48. $this->min_word_length = $this->indexSettings['bytes_per_word'];
  49. }
  50. // Check whether the search can be performed by this API.
  51. public function supportsMethod($methodName, $query_params = null)
  52. {
  53. switch ($methodName)
  54. {
  55. case 'isValid':
  56. case 'searchSort':
  57. case 'prepareIndexes':
  58. case 'indexedWordQuery':
  59. return true;
  60. break;
  61. default:
  62. // All other methods, too bad dunno you.
  63. return false;
  64. return;
  65. }
  66. }
  67. // If the settings don't exist we can't continue.
  68. public function isValid()
  69. {
  70. global $modSettings;
  71. return !empty($modSettings['search_custom_index_config']);
  72. }
  73. // This function compares the length of two strings plus a little.
  74. public function searchSort($a, $b)
  75. {
  76. global $modSettings, $excludedWords;
  77. $x = strlen($a) - (in_array($a, $excludedWords) ? 1000 : 0);
  78. $y = strlen($b) - (in_array($b, $excludedWords) ? 1000 : 0);
  79. return $y < $x ? 1 : ($y > $x ? -1 : 0);
  80. }
  81. // Do we have to do some work with the words we are searching for to prepare them?
  82. public function prepareIndexes($word, &$wordsSearch, &$wordsExclude, $isExcluded)
  83. {
  84. global $modSettings, $smcFunc;
  85. $subwords = text2words($word, $this->min_word_length, true);
  86. if (empty($modSettings['search_force_index']))
  87. $wordsSearch['words'][] = $word;
  88. // Excluded phrases don't benefit from being split into subwords.
  89. if (count($subwords) > 1 && $isExcluded)
  90. continue;
  91. else
  92. {
  93. foreach ($subwords as $subword)
  94. {
  95. if ($smcFunc['strlen']($subword) >= $this->min_word_length && !in_array($subword, $this->bannedWords))
  96. {
  97. $wordsSearch['indexed_words'][] = $subword;
  98. if ($isExcluded)
  99. $wordsExclude[] = $subword;
  100. }
  101. }
  102. }
  103. }
  104. // Search for indexed words.
  105. public function indexedWordQuery($words, $search_data)
  106. {
  107. global $modSettings, $smcFunc;
  108. $query_select = array(
  109. 'id_msg' => 'm.id_msg',
  110. );
  111. $query_inner_join = array();
  112. $query_left_join = array();
  113. $query_where = array();
  114. $query_params = $search_data['params'];
  115. if ($query_params['id_search'])
  116. $query_select['id_search'] = '{int:id_search}';
  117. $count = 0;
  118. foreach ($words['words'] as $regularWord)
  119. {
  120. $query_where[] = 'm.body' . (in_array($regularWord, $query_params['excluded_words']) ? ' NOT' : '') . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : ' RLIKE ') . '{string:complex_body_' . $count . '}';
  121. $query_params['complex_body_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($regularWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $regularWord), '\\\'') . '[[:>:]]';
  122. }
  123. if ($query_params['user_query'])
  124. $query_where[] = '{raw:user_query}';
  125. if ($query_params['board_query'])
  126. $query_where[] = 'm.id_board {raw:board_query}';
  127. if ($query_params['topic'])
  128. $query_where[] = 'm.id_topic = {int:topic}';
  129. if ($query_params['min_msg_id'])
  130. $query_where[] = 'm.id_msg >= {int:min_msg_id}';
  131. if ($query_params['max_msg_id'])
  132. $query_where[] = 'm.id_msg <= {int:max_msg_id}';
  133. $count = 0;
  134. if (!empty($query_params['excluded_phrases']) && empty($modSettings['search_force_index']))
  135. foreach ($query_params['excluded_phrases'] as $phrase)
  136. {
  137. $query_where[] = 'subject NOT ' . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : ' RLIKE ') . '{string:exclude_subject_phrase_' . $count . '}';
  138. $query_params['exclude_subject_phrase_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]';
  139. }
  140. $count = 0;
  141. if (!empty($query_params['excluded_subject_words']) && empty($modSettings['search_force_index']))
  142. foreach ($query_params['excluded_subject_words'] as $excludedWord)
  143. {
  144. $query_where[] = 'subject NOT ' . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : ' RLIKE ') . '{string:exclude_subject_words_' . $count . '}';
  145. $query_params['exclude_subject_words_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($excludedWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $excludedWord), '\\\'') . '[[:>:]]';
  146. }
  147. $numTables = 0;
  148. $prev_join = 0;
  149. foreach ($words['indexed_words'] as $indexedWord)
  150. {
  151. $numTables++;
  152. if (in_array($indexedWord, $query_params['excluded_index_words']))
  153. {
  154. $query_left_join[] = '{db_prefix}log_search_words AS lsw' . $numTables . ' ON (lsw' . $numTables . '.id_word = ' . $indexedWord . ' AND lsw' . $numTables . '.id_msg = m.id_msg)';
  155. $query_where[] = '(lsw' . $numTables . '.id_word IS NULL)';
  156. }
  157. else
  158. {
  159. $query_inner_join[] = '{db_prefix}log_search_words AS lsw' . $numTables . ' ON (lsw' . $numTables . '.id_msg = ' . ($prev_join === 0 ? 'm' : 'lsw' . $prev_join) . '.id_msg)';
  160. $query_where[] = 'lsw' . $numTables . '.id_word = ' . $indexedWord;
  161. $prev_join = $numTables;
  162. }
  163. }
  164. $ignoreRequest = $smcFunc['db_search_query']('insert_into_log_messages_fulltext', ($smcFunc['db_support_ignore'] ? ( '
  165. INSERT IGNORE INTO {db_prefix}' . $search_data['insert_into'] . '
  166. (' . implode(', ', array_keys($query_select)) . ')') : '') . '
  167. SELECT ' . implode(', ', $query_select) . '
  168. FROM {db_prefix}messages AS m' . (empty($query_inner_join) ? '' : '
  169. INNER JOIN ' . implode('
  170. INNER JOIN ', $query_inner_join)) . (empty($query_left_join) ? '' : '
  171. LEFT JOIN ' . implode('
  172. LEFT JOIN ', $query_left_join)) . '
  173. WHERE ' . implode('
  174. AND ', $query_where) . (empty($search_data['max_results']) ? '' : '
  175. LIMIT ' . ($search_data['max_results'] - $search_data['indexed_results'])),
  176. $query_params
  177. );
  178. return $ignoreRequest;
  179. }
  180. }
  181. ?>