| Total Complexity | 70 |
| Total Lines | 283 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like custom_search often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use custom_search, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class custom_search extends search_api |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var array Index settings |
||
| 25 | */ |
||
| 26 | protected $indexSettings = array(); |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array An array of banned words |
||
| 30 | */ |
||
| 31 | protected $bannedWords = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int|null Minimum word length (null for no minimum) |
||
| 35 | */ |
||
| 36 | protected $min_word_length = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array Which databases support this method |
||
| 40 | */ |
||
| 41 | protected $supported_databases = array('mysql', 'postgresql'); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Constructor function |
||
| 45 | */ |
||
| 46 | public function __construct() |
||
| 47 | { |
||
| 48 | global $smcFunc, $modSettings, $db_type; |
||
| 49 | |||
| 50 | // Is this database supported? |
||
| 51 | if (!in_array($db_type, $this->supported_databases)) |
||
| 52 | { |
||
| 53 | $this->is_supported = false; |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | if (empty($modSettings['search_custom_index_config'])) |
||
| 58 | return; |
||
| 59 | |||
| 60 | $this->indexSettings = $smcFunc['json_decode']($modSettings['search_custom_index_config'], true); |
||
| 61 | |||
| 62 | $this->bannedWords = empty($modSettings['search_stopwords']) ? array() : explode(',', $modSettings['search_stopwords']); |
||
| 63 | $this->min_word_length = $this->indexSettings['bytes_per_word']; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritDoc} |
||
| 68 | */ |
||
| 69 | public function supportsMethod($methodName, $query_params = null) |
||
| 70 | { |
||
| 71 | $return = false; |
||
| 72 | switch ($methodName) |
||
| 73 | { |
||
| 74 | case 'isValid': |
||
| 75 | case 'searchSort': |
||
| 76 | case 'prepareIndexes': |
||
| 77 | case 'indexedWordQuery': |
||
| 78 | case 'postCreated': |
||
| 79 | case 'postModified': |
||
| 80 | $return = true; |
||
| 81 | break; |
||
| 82 | |||
| 83 | // All other methods, too bad dunno you. |
||
| 84 | default: |
||
| 85 | $return = false; |
||
| 86 | } |
||
| 87 | |||
| 88 | // Maybe parent got support |
||
| 89 | if (!$return) |
||
| 90 | $return = parent::supportsMethod($methodName, $query_params); |
||
| 91 | |||
| 92 | return $return; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritDoc} |
||
| 97 | */ |
||
| 98 | public function isValid() |
||
| 99 | { |
||
| 100 | global $modSettings; |
||
| 101 | |||
| 102 | return !empty($modSettings['search_custom_index_config']); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritDoc} |
||
| 107 | */ |
||
| 108 | public function searchSort($a, $b) |
||
| 109 | { |
||
| 110 | global $excludedWords; |
||
| 111 | |||
| 112 | $x = strlen($a) - (in_array($a, $excludedWords) ? 1000 : 0); |
||
| 113 | $y = strlen($b) - (in_array($b, $excludedWords) ? 1000 : 0); |
||
| 114 | |||
| 115 | return $y < $x ? 1 : ($y > $x ? -1 : 0); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritDoc} |
||
| 120 | */ |
||
| 121 | public function prepareIndexes($word, array &$wordsSearch, array &$wordsExclude, $isExcluded) |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritDoc} |
||
| 149 | */ |
||
| 150 | public function indexedWordQuery(array $words, array $search_data) |
||
| 151 | { |
||
| 152 | global $modSettings, $smcFunc; |
||
| 153 | |||
| 154 | $query_select = array( |
||
| 155 | 'id_msg' => 'm.id_msg', |
||
| 156 | ); |
||
| 157 | $query_inner_join = array(); |
||
| 158 | $query_left_join = array(); |
||
| 159 | $query_where = array(); |
||
| 160 | $query_params = $search_data['params']; |
||
| 161 | |||
| 162 | if ($query_params['id_search']) |
||
| 163 | $query_select['id_search'] = '{int:id_search}'; |
||
| 164 | |||
| 165 | $count = 0; |
||
| 166 | foreach ($words['words'] as $regularWord) |
||
| 167 | { |
||
| 168 | $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 . '}'; |
||
| 169 | $query_params['complex_body_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($regularWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $regularWord), '\\\'') . '[[:>:]]'; |
||
| 170 | } |
||
| 171 | |||
| 172 | if ($query_params['user_query']) |
||
| 173 | $query_where[] = '{raw:user_query}'; |
||
| 174 | if ($query_params['board_query']) |
||
| 175 | $query_where[] = 'm.id_board {raw:board_query}'; |
||
| 176 | |||
| 177 | if ($query_params['topic']) |
||
| 178 | $query_where[] = 'm.id_topic = {int:topic}'; |
||
| 179 | if ($query_params['min_msg_id']) |
||
| 180 | $query_where[] = 'm.id_msg >= {int:min_msg_id}'; |
||
| 181 | if ($query_params['max_msg_id']) |
||
| 182 | $query_where[] = 'm.id_msg <= {int:max_msg_id}'; |
||
| 183 | |||
| 184 | $count = 0; |
||
| 185 | if (!empty($query_params['excluded_phrases']) && empty($modSettings['search_force_index'])) |
||
| 186 | foreach ($query_params['excluded_phrases'] as $phrase) |
||
| 187 | { |
||
| 188 | $query_where[] = 'subject NOT ' . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : ' RLIKE ') . '{string:exclude_subject_phrase_' . $count . '}'; |
||
| 189 | $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), '\\\'') . '[[:>:]]'; |
||
| 190 | } |
||
| 191 | $count = 0; |
||
| 192 | if (!empty($query_params['excluded_subject_words']) && empty($modSettings['search_force_index'])) |
||
| 193 | foreach ($query_params['excluded_subject_words'] as $excludedWord) |
||
| 194 | { |
||
| 195 | $query_where[] = 'subject NOT ' . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : ' RLIKE ') . '{string:exclude_subject_words_' . $count . '}'; |
||
| 196 | $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), '\\\'') . '[[:>:]]'; |
||
| 197 | } |
||
| 198 | |||
| 199 | $numTables = 0; |
||
| 200 | $prev_join = 0; |
||
| 201 | foreach ($words['indexed_words'] as $indexedWord) |
||
| 202 | { |
||
| 203 | $numTables++; |
||
| 204 | if (in_array($indexedWord, $query_params['excluded_index_words'])) |
||
| 205 | { |
||
| 206 | $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)'; |
||
| 207 | $query_where[] = '(lsw' . $numTables . '.id_word IS NULL)'; |
||
| 208 | } |
||
| 209 | else |
||
| 210 | { |
||
| 211 | $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)'; |
||
| 212 | $query_where[] = 'lsw' . $numTables . '.id_word = ' . $indexedWord; |
||
| 213 | $prev_join = $numTables; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | $ignoreRequest = $smcFunc['db_search_query']('insert_into_log_messages_fulltext', ($smcFunc['db_support_ignore'] ? (' |
||
| 218 | INSERT IGNORE INTO {db_prefix}' . $search_data['insert_into'] . ' |
||
| 219 | (' . implode(', ', array_keys($query_select)) . ')') : '') . ' |
||
| 220 | SELECT ' . implode(', ', $query_select) . ' |
||
| 221 | FROM {db_prefix}messages AS m' . (empty($query_inner_join) ? '' : ' |
||
| 222 | INNER JOIN ' . implode(' |
||
| 223 | INNER JOIN ', $query_inner_join)) . (empty($query_left_join) ? '' : ' |
||
| 224 | LEFT JOIN ' . implode(' |
||
| 225 | LEFT JOIN ', $query_left_join)) . ' |
||
| 226 | WHERE ' . implode(' |
||
| 227 | AND ', $query_where) . (empty($search_data['max_results']) ? '' : ' |
||
| 228 | LIMIT ' . ($search_data['max_results'] - $search_data['indexed_results'])), |
||
| 229 | $query_params |
||
| 230 | ); |
||
| 231 | |||
| 232 | return $ignoreRequest; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritDoc} |
||
| 237 | */ |
||
| 238 | public function postCreated(array &$msgOptions, array &$topicOptions, array &$posterOptions) |
||
| 254 | ); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritDoc} |
||
| 259 | */ |
||
| 260 | public function postModified(array &$msgOptions, array &$topicOptions, array &$posterOptions) |
||
| 310 | ?> |