annotate forum/Sources/SearchAPI-Custom.php @ 87:df86d318892b website

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