| Conditions | 49 |
| Paths | > 20000 |
| Total Lines | 316 |
| Code Lines | 144 |
| 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 |
||
| 59 | public function action_search2(): ?bool |
||
| 60 | { |
||
| 61 | global $scripturl, $modSettings, $context, $txt; |
||
| 62 | |||
| 63 | $context['display_mode'] = PmHelper::getDisplayMode(); |
||
| 64 | |||
| 65 | // Make sure the server is able to do this right now |
||
| 66 | if (!empty($modSettings['loadavg_search']) && $modSettings['current_load'] >= $modSettings['loadavg_search']) |
||
| 67 | { |
||
| 68 | throw new Exception('loadavg_search_disabled', false); |
||
| 69 | } |
||
| 70 | |||
| 71 | // Some useful general permissions. |
||
| 72 | $context['can_send_pm'] = allowedTo('pm_send'); |
||
| 73 | |||
| 74 | // Extract all the search parameters if coming in from pagination, etc. |
||
| 75 | $this->_searchParamsFromString(); |
||
| 76 | |||
| 77 | // Set a start for pagination |
||
| 78 | $context['start'] = $this->_req->getQuery('start', 'intval', 0); |
||
| 79 | |||
| 80 | // Set/clean search criteria |
||
| 81 | $this->_prepareSearchParams(); |
||
| 82 | |||
| 83 | $context['folder'] = empty($this->_search_params['sent_only']) ? 'inbox' : 'sent'; |
||
| 84 | |||
| 85 | // Searching for specific members |
||
| 86 | $userQuery = $this->_setUserQuery(); |
||
| 87 | |||
| 88 | // Set up the sorting variables... |
||
| 89 | $this->_setSortParams(); |
||
| 90 | |||
| 91 | // Sort out any labels we may be searching for. |
||
| 92 | $labelQuery = $this->_setLabelQuery(); |
||
| 93 | |||
| 94 | // Unfortunately, searching for words like this is going to be slow, so we're blocking them. |
||
| 95 | $blocklist_words = ['quote', 'the', 'is', 'it', 'are', 'if', 'in']; |
||
| 96 | |||
| 97 | // What are we actually searching for? |
||
| 98 | if (empty($this->_search_params['search'])) |
||
| 99 | { |
||
| 100 | $this->_search_params['search'] = $this->_req->getPost('search', 'trim|strval', ''); |
||
| 101 | } |
||
| 102 | |||
| 103 | // If nothing is left to search on - we set an error! |
||
| 104 | if (!isset($this->_search_params['search']) || $this->_search_params['search'] === '') |
||
| 105 | { |
||
| 106 | $context['search_errors']['invalid_search_string'] = true; |
||
| 107 | } |
||
| 108 | |||
| 109 | // Change non-word characters into spaces. |
||
| 110 | $stripped_query = preg_replace('~(?:[\x0B\0\x{A0}\t\r\s\n(){}\\[\\]<>!@$%^*.,:+=`\~\?/\\\\]+|&(?:amp|lt|gt|quot);)+~u', ' ', $this->_search_params['search']); |
||
| 111 | |||
| 112 | // Make the query lower case since it will case-insensitive anyway. |
||
| 113 | $stripped_query = un_htmlspecialchars(Util::strtolower($stripped_query)); |
||
| 114 | |||
| 115 | // Extract phrase parts first (e.g., some words "this is a phrase" some more words.) |
||
| 116 | preg_match_all('/(?:^|\s)([-]?)"([^"]+)"(?:$|\s)/', $stripped_query, $matches, PREG_PATTERN_ORDER); |
||
| 117 | $phraseArray = $matches[2]; |
||
| 118 | |||
| 119 | // Remove the phrase parts and extract the words. |
||
| 120 | $wordArray = preg_replace('~(?:^|\s)(?:[-]?)"(?:[^"]+)"(?:$|\s)~u', ' ', $this->_search_params['search']); |
||
| 121 | $wordArray = explode(' ', Util::htmlspecialchars(un_htmlspecialchars($wordArray), ENT_QUOTES)); |
||
| 122 | |||
| 123 | // A minus sign in front of a word excludes the word.... so... |
||
| 124 | $excludedWords = []; |
||
| 125 | |||
| 126 | // Check for things like -"some words", but not "-some words". |
||
| 127 | foreach ($matches[1] as $index => $word) |
||
| 128 | { |
||
| 129 | if ($word === '-') |
||
| 130 | { |
||
| 131 | if (($word = trim($phraseArray[$index], "-_' ")) !== '' && !in_array($word, $blocklist_words)) |
||
| 132 | { |
||
| 133 | $excludedWords[] = $word; |
||
| 134 | } |
||
| 135 | |||
| 136 | unset($phraseArray[$index]); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | // Now we look for -test, etc. |
||
| 141 | foreach ($wordArray as $index => $word) |
||
| 142 | { |
||
| 143 | if (str_starts_with(trim($word), '-')) |
||
| 144 | { |
||
| 145 | if (($word = trim($word, "-_' ")) !== '' && !in_array($word, $blocklist_words)) |
||
| 146 | { |
||
| 147 | $excludedWords[] = $word; |
||
| 148 | } |
||
| 149 | |||
| 150 | unset($wordArray[$index]); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | // The remaining words and phrases are all included. |
||
| 155 | $searchArray = array_merge($phraseArray, $wordArray); |
||
| 156 | |||
| 157 | // Trim everything and make sure there are no words that are the same. |
||
| 158 | foreach ($searchArray as $index => $value) |
||
| 159 | { |
||
| 160 | // Skip anything that's close to empty. |
||
| 161 | if (($searchArray[$index] = trim($value, "-_' ")) === '') |
||
| 162 | { |
||
| 163 | unset($searchArray[$index]); |
||
| 164 | } |
||
| 165 | // Skip blocked words. Make sure to note we skipped them as well |
||
| 166 | elseif (in_array($searchArray[$index], $blocklist_words)) |
||
| 167 | { |
||
| 168 | $foundBlockListedWords = true; |
||
| 169 | unset($searchArray[$index]); |
||
| 170 | |||
| 171 | } |
||
| 172 | |||
| 173 | if (isset($searchArray[$index])) |
||
| 174 | { |
||
| 175 | $searchArray[$index] = Util::strtolower(trim($value)); |
||
| 176 | |||
| 177 | if ($searchArray[$index] === '') |
||
| 178 | { |
||
| 179 | unset($searchArray[$index]); |
||
| 180 | } |
||
| 181 | else |
||
| 182 | { |
||
| 183 | // Sort out entities first. |
||
| 184 | $searchArray[$index] = Util::htmlspecialchars($searchArray[$index]); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | $searchArray = array_slice(array_unique($searchArray), 0, 10); |
||
| 190 | |||
| 191 | // This contains *everything* |
||
| 192 | $searchWords = array_merge($searchArray, $excludedWords); |
||
| 193 | |||
| 194 | // Make sure at least one word is being searched for. |
||
| 195 | if (empty($searchArray)) |
||
| 196 | { |
||
| 197 | $context['search_errors']['invalid_search_string' . (empty($foundBlockListedWords) ? '' : '_blocklist')] = true; |
||
| 198 | } |
||
| 199 | |||
| 200 | // Sort out the search query so the user can edit it - if they want. |
||
| 201 | $context['search_params'] = $this->_search_params; |
||
| 202 | if (isset($context['search_params']['search'])) |
||
| 203 | { |
||
| 204 | $context['search_params']['search'] = Util::htmlspecialchars($context['search_params']['search']); |
||
| 205 | } |
||
| 206 | |||
| 207 | if (isset($context['search_params']['userspec'])) |
||
| 208 | { |
||
| 209 | $context['search_params']['userspec'] = Util::htmlspecialchars($context['search_params']['userspec']); |
||
| 210 | } |
||
| 211 | |||
| 212 | // Now we have all the parameters, combine them together for pagination and the like... |
||
| 213 | $context['params'] = $this->_compileURLparams(); |
||
| 214 | |||
| 215 | // Compile the subject query part. |
||
| 216 | $andQueryParts = []; |
||
| 217 | foreach ($searchWords as $index => $word) |
||
| 218 | { |
||
| 219 | if ($word === '') |
||
| 220 | { |
||
| 221 | continue; |
||
| 222 | } |
||
| 223 | |||
| 224 | if ($this->_search_params['subject_only']) |
||
| 225 | { |
||
| 226 | $andQueryParts[] = 'pm.subject' . (in_array($word, $excludedWords) ? ' NOT' : '') . ' LIKE {string:search_' . $index . '}'; |
||
| 227 | } |
||
| 228 | else |
||
| 229 | { |
||
| 230 | $andQueryParts[] = '(pm.subject' . (in_array($word, $excludedWords) ? ' NOT' : '') . ' LIKE {string:search_' . $index . '} ' . (in_array($word, $excludedWords) ? 'AND pm.body NOT' : 'OR pm.body') . ' LIKE {string:search_' . $index . '})'; |
||
| 231 | } |
||
| 232 | |||
| 233 | $this->_searchq_parameters ['search_' . $index] = '%' . strtr($word, ['_' => '\\_', '%' => '\\%']) . '%'; |
||
| 234 | } |
||
| 235 | |||
| 236 | $searchQuery = ' 1=1'; |
||
| 237 | if (!empty($andQueryParts)) |
||
| 238 | { |
||
| 239 | $searchQuery = implode(!empty($this->_search_params['searchtype']) && $this->_search_params['searchtype'] == 2 ? ' OR ' : ' AND ', $andQueryParts); |
||
| 240 | } |
||
| 241 | |||
| 242 | // Age limits? |
||
| 243 | $timeQuery = ''; |
||
| 244 | if (!empty($this->_search_params['minage'])) |
||
| 245 | { |
||
| 246 | $timeQuery .= ' AND pm.msgtime < ' . (time() - $this->_search_params['minage'] * 86400); |
||
| 247 | } |
||
| 248 | |||
| 249 | if (!empty($this->_search_params['maxage'])) |
||
| 250 | { |
||
| 251 | $timeQuery .= ' AND pm.msgtime > ' . (time() - $this->_search_params['maxage'] * 86400); |
||
| 252 | } |
||
| 253 | |||
| 254 | // If we have errors - return back to the first screen... |
||
| 255 | if (!empty($context['search_errors'])) |
||
| 256 | { |
||
| 257 | $this->_req->post->params = $context['params']; |
||
| 258 | |||
| 259 | $this->action_search(); |
||
| 260 | |||
| 261 | return false; |
||
| 262 | } |
||
| 263 | |||
| 264 | // Get the number of results. |
||
| 265 | $numResults = numPMSeachResults($userQuery, $labelQuery, $timeQuery, $searchQuery, $this->_searchq_parameters); |
||
| 266 | |||
| 267 | // Get all the matching message ids, senders and head pm nodes |
||
| 268 | [$foundMessages, $posters, $head_pms] = loadPMSearchMessages($userQuery, $labelQuery, $timeQuery, $searchQuery, $this->_searchq_parameters, $this->_search_params); |
||
| 269 | |||
| 270 | // Find the real head pm when in the conversation view |
||
| 271 | if ($context['display_mode'] === PmHelper::DISPLAY_AS_CONVERSATION && !empty($head_pms)) |
||
| 272 | { |
||
| 273 | $real_pm_ids = loadPMSearchHeads($head_pms); |
||
| 274 | } |
||
| 275 | |||
| 276 | // Load the found user data |
||
| 277 | $posters = array_unique($posters); |
||
| 278 | if (!empty($posters)) |
||
| 279 | { |
||
| 280 | MembersList::load($posters); |
||
| 281 | } |
||
| 282 | |||
| 283 | // Sort out the page index. |
||
| 284 | $context['page_index'] = constructPageIndex('{scripturl}?action=pm;sa=search2;params=' . $context['params'], $context['start'], $numResults, $modSettings['search_results_per_page']); |
||
| 285 | |||
| 286 | $context['message_labels'] = []; |
||
| 287 | $context['message_replied'] = []; |
||
| 288 | $context['personal_messages'] = []; |
||
| 289 | $context['first_label'] = []; |
||
| 290 | |||
| 291 | // If we have results, we have work to do! |
||
| 292 | if (!empty($foundMessages)) |
||
| 293 | { |
||
| 294 | $recipients = []; |
||
| 295 | [$context['message_labels'], $context['message_replied'], $context['message_unread'], $context['first_label']] = loadPMRecipientInfo($foundMessages, $recipients, $context['folder'], true); |
||
| 296 | |||
| 297 | // Prepare for the callback! |
||
| 298 | $search_results = loadPMSearchResults($foundMessages, $this->_search_params); |
||
| 299 | $counter = 0; |
||
| 300 | $bbc_parser = ParserWrapper::instance(); |
||
| 301 | foreach ($search_results as $row) |
||
| 302 | { |
||
| 303 | // If there's no subject, use the default. |
||
| 304 | $row['subject'] = $row['subject'] === '' ? $txt['no_subject'] : $row['subject']; |
||
| 305 | |||
| 306 | // Load this poster context info, if not there, then fill in the essentials... |
||
| 307 | $member = MembersList::get($row['id_member_from']); |
||
| 308 | $member->loadContext(); |
||
| 309 | if ($member->isEmpty()) |
||
| 310 | { |
||
| 311 | $member['name'] = $row['from_name']; |
||
| 312 | $member['id'] = 0; |
||
| 313 | $member['group'] = $txt['guest_title']; |
||
| 314 | $member['link'] = $row['from_name']; |
||
| 315 | $member['email'] = ''; |
||
| 316 | $member['show_email'] = showEmailAddress(0); |
||
| 317 | $member['is_guest'] = true; |
||
| 318 | } |
||
| 319 | |||
| 320 | // Censor anything we don't want to see... |
||
| 321 | $row['body'] = censor($row['body']); |
||
| 322 | $row['subject'] = censor($row['subject']); |
||
| 323 | |||
| 324 | // Parse out any BBC... |
||
| 325 | $row['body'] = $bbc_parser->parsePM($row['body']); |
||
| 326 | |||
| 327 | // Highlight the hits |
||
| 328 | $body_highlighted = ''; |
||
| 329 | $subject_highlighted = ''; |
||
| 330 | foreach ($searchArray as $query) |
||
| 331 | { |
||
| 332 | // Fix the international characters in the keyword too. |
||
| 333 | $query = un_htmlspecialchars($query); |
||
| 334 | $query = trim($query, '\*+'); |
||
| 335 | $query = strtr(Util::htmlspecialchars($query), ['\\\'' => "'"]); |
||
| 336 | |||
| 337 | $body_highlighted = preg_replace_callback('/((<[^>]*)|' . preg_quote(strtr($query, ["'" => ''']), '/') . ')/iu', |
||
| 338 | fn($matches) => $this->_highlighted_callback($matches), $row['body']); |
||
| 339 | $subject_highlighted = preg_replace('/(' . preg_quote($query, '/') . ')/iu', '<strong class="highlight">$1</strong>', $row['subject']); |
||
| 340 | } |
||
| 341 | |||
| 342 | // Set a link using the first label information |
||
| 343 | $href = $scripturl . '?action=pm;f=' . $context['folder'] . (isset($context['first_label'][$row['id_pm']]) ? ';l=' . $context['first_label'][$row['id_pm']] : '') . ';pmid=' . ($context['display_mode'] === PmHelper::DISPLAY_AS_CONVERSATION && isset($real_pm_ids[$head_pms[$row['id_pm']]]) && $context['folder'] === 'inbox' ? $real_pm_ids[$head_pms[$row['id_pm']]] : $row['id_pm']) . '#msg_' . $row['id_pm']; |
||
| 344 | |||
| 345 | $context['personal_messages'][] = [ |
||
| 346 | 'id' => $row['id_pm'], |
||
| 347 | 'member' => $member, |
||
| 348 | 'subject' => $subject_highlighted, |
||
| 349 | 'body' => $body_highlighted, |
||
| 350 | 'time' => standardTime($row['msgtime']), |
||
| 351 | 'html_time' => htmlTime($row['msgtime']), |
||
| 352 | 'timestamp' => forum_time(true, $row['msgtime']), |
||
| 353 | 'recipients' => &$recipients[$row['id_pm']], |
||
| 354 | 'labels' => &$context['message_labels'][$row['id_pm']], |
||
| 355 | 'fully_labeled' => (empty($context['message_labels'][$row['id_pm']]) ? 0 : count($context['message_labels'][$row['id_pm']])) === count($context['labels']), |
||
| 356 | 'is_replied_to' => &$context['message_replied'][$row['id_pm']], |
||
| 357 | 'href' => $href, |
||
| 358 | 'link' => '<a href="' . $href . '">' . $subject_highlighted . '</a>', |
||
| 359 | 'counter' => ++$counter, |
||
| 360 | 'pmbuttons' => $this->_setSearchPmButtons($row['id_pm'], $member), |
||
| 361 | ]; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | |||
| 365 | // Finish off the context. |
||
| 366 | $context['page_title'] = $txt['pm_search_title']; |
||
| 367 | $context['sub_template'] = 'search_results'; |
||
| 368 | $context['menu_data_' . $context['pm_menu_id']]['current_area'] = 'search'; |
||
| 369 | $context['breadcrumbs'][] = [ |
||
| 370 | 'url' => getUrl('action', ['action' => 'pm', 'sa' => 'search']), |
||
| 371 | 'name' => $txt['pm_search_bar_title'], |
||
| 372 | ]; |
||
| 373 | |||
| 374 | return true; |
||
| 375 | } |
||
| 806 |