| Conditions | 35 |
| Paths | > 20000 |
| Total Lines | 141 |
| Code Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 168 | public function indexedWordQuery(array $words, array $search_data) |
||
| 169 | { |
||
| 170 | global $modSettings, $smcFunc; |
||
| 171 | |||
| 172 | // Specify the function to search with. Regex is for word boundaries. |
||
| 173 | $is_search_regex = !empty($modSettings['search_match_words']) && !$search_data['no_regexp']; |
||
| 174 | $query_match_type = $is_search_regex ? 'RLIKE' : 'LIKE'; |
||
| 175 | $word_boundary_wrapper = function(string $str) use ($smcFunc): string |
||
| 176 | { |
||
| 177 | return sprintf($smcFunc['db_supports_pcre'] ? '\\b%s\\b' : '[[:<:]]%s[[:>:]]', $str); |
||
| 178 | }; |
||
| 179 | $escape_sql_regex = function(string $str): string |
||
| 180 | { |
||
| 181 | return addcslashes(preg_replace('/[\[\]$.+*?&^|{}()]/', '[$0]', $str), '\\\''); |
||
| 182 | }; |
||
| 183 | |||
| 184 | $query_select = array( |
||
| 185 | 'id_msg' => 'm.id_msg', |
||
| 186 | ); |
||
| 187 | $query_where = array(); |
||
| 188 | $query_params = $search_data['params']; |
||
| 189 | |||
| 190 | if ($smcFunc['db_title'] === POSTGRE_TITLE) |
||
| 191 | $modSettings['search_simple_fulltext'] = true; |
||
| 192 | |||
| 193 | if ($query_params['id_search']) |
||
| 194 | $query_select['id_search'] = '{int:id_search}'; |
||
| 195 | |||
| 196 | $count = 0; |
||
| 197 | if (empty($modSettings['search_simple_fulltext'])) |
||
| 198 | foreach ($words['words'] as $regularWord) |
||
| 199 | { |
||
| 200 | if (in_array($regularWord, $query_params['excluded_words'])) |
||
| 201 | $query_where[] = 'm.body NOT ' . $query_match_type . ' {string:complex_body_' . $count . '}'; |
||
| 202 | else |
||
| 203 | $query_where[] = 'm.body ' . $query_match_type . ' {string:complex_body_' . $count . '}'; |
||
| 204 | |||
| 205 | if ($is_search_regex) |
||
| 206 | $query_params['complex_body_' . $count++] = $word_boundary_wrapper($escape_sql_regex($regularWord)); |
||
| 207 | else |
||
| 208 | $query_params['complex_body_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($regularWord) . '%'; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ($query_params['user_query']) |
||
| 212 | $query_where[] = '{raw:user_query}'; |
||
| 213 | if ($query_params['board_query']) |
||
| 214 | $query_where[] = 'm.id_board {raw:board_query}'; |
||
| 215 | |||
| 216 | if ($query_params['topic']) |
||
| 217 | $query_where[] = 'm.id_topic = {int:topic}'; |
||
| 218 | if ($query_params['min_msg_id']) |
||
| 219 | $query_where[] = 'm.id_msg >= {int:min_msg_id}'; |
||
| 220 | if ($query_params['max_msg_id']) |
||
| 221 | $query_where[] = 'm.id_msg <= {int:max_msg_id}'; |
||
| 222 | |||
| 223 | $count = 0; |
||
| 224 | if (!empty($query_params['excluded_phrases']) && empty($modSettings['search_force_index'])) |
||
| 225 | foreach ($query_params['excluded_phrases'] as $phrase) |
||
| 226 | { |
||
| 227 | $query_where[] = 'subject NOT ' . $query_match_type . ' {string:exclude_subject_phrase_' . $count . '}'; |
||
| 228 | |||
| 229 | if ($is_search_regex) |
||
| 230 | $query_params['exclude_subject_phrase_' . $count++] = $word_boundary_wrapper($escape_sql_regex($phrase)); |
||
| 231 | else |
||
| 232 | $query_params['exclude_subject_phrase_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($phrase) . '%'; |
||
| 233 | } |
||
| 234 | $count = 0; |
||
| 235 | if (!empty($query_params['excluded_subject_words']) && empty($modSettings['search_force_index'])) |
||
| 236 | foreach ($query_params['excluded_subject_words'] as $excludedWord) |
||
| 237 | { |
||
| 238 | $query_where[] = 'subject NOT ' . $query_match_type . ' {string:exclude_subject_words_' . $count . '}'; |
||
| 239 | |||
| 240 | if ($is_search_regex) |
||
| 241 | $query_params['exclude_subject_words_' . $count++] = $word_boundary_wrapper($escape_sql_regex($excludedWord)); |
||
| 242 | else |
||
| 243 | $query_params['exclude_subject_words_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($excludedWord) . '%'; |
||
| 244 | } |
||
| 245 | |||
| 246 | if (!empty($modSettings['search_simple_fulltext'])) |
||
| 247 | { |
||
| 248 | if ($smcFunc['db_title'] === POSTGRE_TITLE) |
||
| 249 | { |
||
| 250 | $language_ftx = $smcFunc['db_search_language'](); |
||
| 251 | |||
| 252 | $query_where[] = 'to_tsvector({string:language_ftx},body) @@ plainto_tsquery({string:language_ftx},{string:body_match})'; |
||
| 253 | $query_params['language_ftx'] = $language_ftx; |
||
| 254 | } |
||
| 255 | else |
||
| 256 | $query_where[] = 'MATCH (body) AGAINST ({string:body_match})'; |
||
| 257 | $query_params['body_match'] = implode(' ', array_diff($words['indexed_words'], $query_params['excluded_index_words'])); |
||
| 258 | } |
||
| 259 | else |
||
| 260 | { |
||
| 261 | $query_params['boolean_match'] = ''; |
||
| 262 | |||
| 263 | // remove any indexed words that are used in the complex body search terms |
||
| 264 | $words['indexed_words'] = array_diff($words['indexed_words'], $words['complex_words']); |
||
| 265 | |||
| 266 | if ($smcFunc['db_title'] === POSTGRE_TITLE) |
||
| 267 | { |
||
| 268 | $row = 0; |
||
| 269 | foreach ($words['indexed_words'] as $fulltextWord) |
||
| 270 | { |
||
| 271 | $query_params['boolean_match'] .= ($row <> 0 ? '&' : ''); |
||
| 272 | $query_params['boolean_match'] .= (in_array($fulltextWord, $query_params['excluded_index_words']) ? '!' : '') . $fulltextWord . ' '; |
||
| 273 | $row++; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | else |
||
| 277 | foreach ($words['indexed_words'] as $fulltextWord) |
||
| 278 | $query_params['boolean_match'] .= (in_array($fulltextWord, $query_params['excluded_index_words']) ? '-' : '+') . $fulltextWord . ' '; |
||
| 279 | |||
| 280 | $query_params['boolean_match'] = substr($query_params['boolean_match'], 0, -1); |
||
| 281 | |||
| 282 | // if we have bool terms to search, add them in |
||
| 283 | if ($query_params['boolean_match']) |
||
| 284 | { |
||
| 285 | if ($smcFunc['db_title'] === POSTGRE_TITLE) |
||
| 286 | { |
||
| 287 | $language_ftx = $smcFunc['db_search_language'](); |
||
| 288 | |||
| 289 | $query_where[] = 'to_tsvector({string:language_ftx},body) @@ plainto_tsquery({string:language_ftx},{string:boolean_match})'; |
||
| 290 | $query_params['language_ftx'] = $language_ftx; |
||
| 291 | } |
||
| 292 | else |
||
| 293 | $query_where[] = 'MATCH (body) AGAINST ({string:boolean_match} IN BOOLEAN MODE)'; |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | $ignoreRequest = $smcFunc['db_search_query']('insert_into_log_messages_fulltext', ($smcFunc['db_support_ignore'] ? (' |
||
| 298 | INSERT IGNORE INTO {db_prefix}' . $search_data['insert_into'] . ' |
||
| 299 | (' . implode(', ', array_keys($query_select)) . ')') : '') . ' |
||
| 300 | SELECT ' . implode(', ', $query_select) . ' |
||
| 301 | FROM {db_prefix}messages AS m |
||
| 302 | WHERE ' . implode(' |
||
| 303 | AND ', $query_where) . (empty($search_data['max_results']) ? '' : ' |
||
| 304 | LIMIT ' . ($search_data['max_results'] - $search_data['indexed_results'])), |
||
| 305 | $query_params |
||
| 306 | ); |
||
| 307 | |||
| 308 | return $ignoreRequest; |
||
| 309 | } |
||
| 312 | ?> |