Conditions | 28 |
Paths | > 20000 |
Total Lines | 110 |
Code Lines | 76 |
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 |
||
150 | public function indexedWordQuery(array $words, array $search_data) |
||
151 | { |
||
152 | global $modSettings, $smcFunc; |
||
153 | |||
154 | // Specify the function to search with. Regex is for word boundaries. |
||
155 | $is_search_regex = !empty($modSettings['search_match_words']) && !$search_data['no_regexp']; |
||
156 | $query_match_type = $is_search_regex ? 'RLIKE' : 'LIKE'; |
||
157 | $word_boundary_wrapper = function(string $str) use ($smcFunc): string |
||
158 | { |
||
159 | return sprintf($smcFunc['db_supports_pcre'] ? '\\b%s\\b' : '[[:<:]]%s[[:>:]]', $str); |
||
160 | }; |
||
161 | $escape_sql_regex = function(string $str): string |
||
162 | { |
||
163 | return addcslashes(preg_replace('/[\[\]$.+*?&^|{}()]/', '[$0]', $str), '\\\''); |
||
164 | }; |
||
165 | |||
166 | $query_select = array( |
||
167 | 'id_msg' => 'm.id_msg', |
||
168 | ); |
||
169 | $query_inner_join = array(); |
||
170 | $query_left_join = array(); |
||
171 | $query_where = array(); |
||
172 | $query_params = $search_data['params']; |
||
173 | |||
174 | if ($query_params['id_search']) |
||
175 | $query_select['id_search'] = '{int:id_search}'; |
||
176 | |||
177 | $count = 0; |
||
178 | foreach ($words['words'] as $regularWord) |
||
179 | { |
||
180 | if (in_array($regularWord, $query_params['excluded_words'])) |
||
181 | $query_where[] = 'm.body NOT ' . $query_match_type . ' {string:complex_body_' . $count . '}'; |
||
182 | else |
||
183 | $query_where[] = 'm.body ' . $query_match_type . ' {string:complex_body_' . $count . '}'; |
||
184 | |||
185 | if ($is_search_regex) |
||
186 | $query_params['complex_body_' . $count++] = $word_boundary_wrapper($escape_sql_regex($regularWord)); |
||
187 | else |
||
188 | $query_params['complex_body_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($regularWord) . '%'; |
||
189 | } |
||
190 | |||
191 | if ($query_params['user_query']) |
||
192 | $query_where[] = '{raw:user_query}'; |
||
193 | if ($query_params['board_query']) |
||
194 | $query_where[] = 'm.id_board {raw:board_query}'; |
||
195 | |||
196 | if ($query_params['topic']) |
||
197 | $query_where[] = 'm.id_topic = {int:topic}'; |
||
198 | if ($query_params['min_msg_id']) |
||
199 | $query_where[] = 'm.id_msg >= {int:min_msg_id}'; |
||
200 | if ($query_params['max_msg_id']) |
||
201 | $query_where[] = 'm.id_msg <= {int:max_msg_id}'; |
||
202 | |||
203 | $count = 0; |
||
204 | if (!empty($query_params['excluded_phrases']) && empty($modSettings['search_force_index'])) |
||
205 | foreach ($query_params['excluded_phrases'] as $phrase) |
||
206 | { |
||
207 | $query_where[] = 'subject NOT ' . $query_match_type . ' {string:exclude_subject_words_' . $count . '}'; |
||
208 | |||
209 | if ($is_search_regex) |
||
210 | $query_params['exclude_subject_words_' . $count++] = $word_boundary_wrapper($escape_sql_regex($phrase)); |
||
211 | else |
||
212 | $query_params['exclude_subject_words_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($phrase) . '%'; |
||
213 | } |
||
214 | $count = 0; |
||
215 | if (!empty($query_params['excluded_subject_words']) && empty($modSettings['search_force_index'])) |
||
216 | foreach ($query_params['excluded_subject_words'] as $excludedWord) |
||
217 | { |
||
218 | $query_where[] = 'subject NOT ' . $query_match_type . ' {string:exclude_subject_words_' . $count . '}'; |
||
219 | |||
220 | if ($is_search_regex) |
||
221 | $query_params['exclude_subject_words_' . $count++] = $word_boundary_wrapper($escape_sql_regex($excludedWord)); |
||
222 | else |
||
223 | $query_params['exclude_subject_words_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($excludedWord) . '%'; |
||
224 | } |
||
225 | |||
226 | $numTables = 0; |
||
227 | $prev_join = 0; |
||
228 | foreach ($words['indexed_words'] as $indexedWord) |
||
229 | { |
||
230 | $numTables++; |
||
231 | if (in_array($indexedWord, $query_params['excluded_index_words'])) |
||
232 | { |
||
233 | $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)'; |
||
234 | $query_where[] = '(lsw' . $numTables . '.id_word IS NULL)'; |
||
235 | } |
||
236 | else |
||
237 | { |
||
238 | $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)'; |
||
239 | $query_where[] = 'lsw' . $numTables . '.id_word = ' . $indexedWord; |
||
240 | $prev_join = $numTables; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | $ignoreRequest = $smcFunc['db_search_query']('insert_into_log_messages_fulltext', ($smcFunc['db_support_ignore'] ? (' |
||
245 | INSERT IGNORE INTO {db_prefix}' . $search_data['insert_into'] . ' |
||
246 | (' . implode(', ', array_keys($query_select)) . ')') : '') . ' |
||
247 | SELECT ' . implode(', ', $query_select) . ' |
||
248 | FROM {db_prefix}messages AS m' . (empty($query_inner_join) ? '' : ' |
||
249 | INNER JOIN ' . implode(' |
||
250 | INNER JOIN ', $query_inner_join)) . (empty($query_left_join) ? '' : ' |
||
251 | LEFT JOIN ' . implode(' |
||
252 | LEFT JOIN ', $query_left_join)) . ' |
||
253 | WHERE ' . implode(' |
||
254 | AND ', $query_where) . (empty($search_data['max_results']) ? '' : ' |
||
255 | LIMIT ' . ($search_data['max_results'] - $search_data['indexed_results'])), |
||
256 | $query_params |
||
257 | ); |
||
258 | |||
259 | return $ignoreRequest; |
||
260 | } |
||
337 | ?> |