Conditions | 369 |
Paths | 4 |
Total Lines | 1783 |
Code Lines | 958 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
243 | function PlushSearch2() |
||
244 | { |
||
245 | global $scripturl, $modSettings, $sourcedir, $txt; |
||
246 | global $user_info, $context, $options, $messages_request, $boards_can; |
||
247 | global $excludedWords, $participants, $smcFunc, $cache_enable; |
||
248 | |||
249 | // if comming from the quick search box, and we want to search on members, well we need to do that ;) |
||
250 | if (isset($_REQUEST['search_selection']) && $_REQUEST['search_selection'] === 'members') |
||
251 | redirectexit($scripturl . '?action=mlist;sa=search;fields=name,email;search=' . urlencode($_REQUEST['search'])); |
||
252 | |||
253 | if (!empty($context['load_average']) && !empty($modSettings['loadavg_search']) && $context['load_average'] >= $modSettings['loadavg_search']) |
||
254 | fatal_lang_error('loadavg_search_disabled', false); |
||
255 | |||
256 | // No, no, no... this is a bit hard on the server, so don't you go prefetching it! |
||
257 | if (isset($_SERVER['HTTP_X_MOZ']) && $_SERVER['HTTP_X_MOZ'] == 'prefetch') |
||
258 | { |
||
259 | ob_end_clean(); |
||
260 | send_http_status(403); |
||
261 | die; |
||
|
|||
262 | } |
||
263 | |||
264 | if (isset($_REQUEST['start'])) |
||
265 | $_REQUEST['start'] = (int) $_REQUEST['start']; |
||
266 | |||
267 | $weight_factors = array( |
||
268 | 'frequency' => array( |
||
269 | 'search' => 'COUNT(*) / (MAX(t.num_replies) + 1)', |
||
270 | 'results' => '(t.num_replies + 1)', |
||
271 | ), |
||
272 | 'age' => array( |
||
273 | 'search' => 'CASE WHEN MAX(m.id_msg) < {int:min_msg} THEN 0 ELSE (MAX(m.id_msg) - {int:min_msg}) / {int:recent_message} END', |
||
274 | 'results' => 'CASE WHEN t.id_first_msg < {int:min_msg} THEN 0 ELSE (t.id_first_msg - {int:min_msg}) / {int:recent_message} END', |
||
275 | ), |
||
276 | 'length' => array( |
||
277 | 'search' => 'CASE WHEN MAX(t.num_replies) < {int:huge_topic_posts} THEN MAX(t.num_replies) / {int:huge_topic_posts} ELSE 1 END', |
||
278 | 'results' => 'CASE WHEN t.num_replies < {int:huge_topic_posts} THEN t.num_replies / {int:huge_topic_posts} ELSE 1 END', |
||
279 | ), |
||
280 | 'subject' => array( |
||
281 | 'search' => 0, |
||
282 | 'results' => 0, |
||
283 | ), |
||
284 | 'first_message' => array( |
||
285 | 'search' => 'CASE WHEN MIN(m.id_msg) = MAX(t.id_first_msg) THEN 1 ELSE 0 END', |
||
286 | ), |
||
287 | 'sticky' => array( |
||
288 | 'search' => 'MAX(t.is_sticky)', |
||
289 | 'results' => 't.is_sticky', |
||
290 | ), |
||
291 | ); |
||
292 | |||
293 | call_integration_hook('integrate_search_weights', array(&$weight_factors)); |
||
294 | |||
295 | $weight = array(); |
||
296 | $weight_total = 0; |
||
297 | foreach ($weight_factors as $weight_factor => $value) |
||
298 | { |
||
299 | $weight[$weight_factor] = empty($modSettings['search_weight_' . $weight_factor]) ? 0 : (int) $modSettings['search_weight_' . $weight_factor]; |
||
300 | $weight_total += $weight[$weight_factor]; |
||
301 | } |
||
302 | |||
303 | // Zero weight. Weightless :P. |
||
304 | if (empty($weight_total)) |
||
305 | fatal_lang_error('search_invalid_weights'); |
||
306 | |||
307 | // These vars don't require an interface, they're just here for tweaking. |
||
308 | $recentPercentage = 0.30; |
||
309 | $humungousTopicPosts = 200; |
||
310 | $maxMembersToSearch = 500; |
||
311 | $maxMessageResults = empty($modSettings['search_max_results']) ? 0 : $modSettings['search_max_results'] * 5; |
||
312 | |||
313 | // Start with no errors. |
||
314 | $context['search_errors'] = array(); |
||
315 | |||
316 | // Number of pages hard maximum - normally not set at all. |
||
317 | $modSettings['search_max_results'] = empty($modSettings['search_max_results']) ? 200 * $modSettings['search_results_per_page'] : (int) $modSettings['search_max_results']; |
||
318 | |||
319 | // Maximum length of the string. |
||
320 | $context['search_string_limit'] = 100; |
||
321 | |||
322 | loadLanguage('Search'); |
||
323 | if (!isset($_REQUEST['xml'])) |
||
324 | loadTemplate('Search'); |
||
325 | //If we're doing XML we need to use the results template regardless really. |
||
326 | else |
||
327 | $context['sub_template'] = 'results'; |
||
328 | |||
329 | // Are you allowed? |
||
330 | isAllowedTo('search_posts'); |
||
331 | |||
332 | require_once($sourcedir . '/Display.php'); |
||
333 | require_once($sourcedir . '/Subs-Package.php'); |
||
334 | |||
335 | // Search has a special database set. |
||
336 | db_extend('search'); |
||
337 | |||
338 | // Load up the search API we are going to use. |
||
339 | $searchAPI = findSearchAPI(); |
||
340 | |||
341 | // $search_params will carry all settings that differ from the default search parameters. |
||
342 | // That way, the URLs involved in a search page will be kept as short as possible. |
||
343 | $search_params = array(); |
||
344 | |||
345 | if (isset($_REQUEST['params'])) |
||
346 | { |
||
347 | // Due to IE's 2083 character limit, we have to compress long search strings |
||
348 | $temp_params = base64_decode(str_replace(array('-', '_', '.'), array('+', '/', '='), $_REQUEST['params'])); |
||
349 | |||
350 | // Test for gzuncompress failing |
||
351 | $temp_params2 = @gzuncompress($temp_params); |
||
352 | $temp_params = explode('|"|', (!empty($temp_params2) ? $temp_params2 : $temp_params)); |
||
353 | |||
354 | foreach ($temp_params as $i => $data) |
||
355 | { |
||
356 | @list($k, $v) = explode('|\'|', $data); |
||
357 | $search_params[$k] = $v; |
||
358 | } |
||
359 | |||
360 | if (isset($search_params['brd'])) |
||
361 | $search_params['brd'] = empty($search_params['brd']) ? array() : explode(',', $search_params['brd']); |
||
362 | } |
||
363 | |||
364 | // Store whether simple search was used (needed if the user wants to do another query). |
||
365 | if (!isset($search_params['advanced'])) |
||
366 | $search_params['advanced'] = empty($_REQUEST['advanced']) ? 0 : 1; |
||
367 | |||
368 | // 1 => 'allwords' (default, don't set as param) / 2 => 'anywords'. |
||
369 | if (!empty($search_params['searchtype']) || (!empty($_REQUEST['searchtype']) && $_REQUEST['searchtype'] == 2)) |
||
370 | $search_params['searchtype'] = 2; |
||
371 | |||
372 | // Minimum age of messages. Default to zero (don't set param in that case). |
||
373 | if (!empty($search_params['minage']) || (!empty($_REQUEST['minage']) && $_REQUEST['minage'] > 0)) |
||
374 | $search_params['minage'] = !empty($search_params['minage']) ? (int) $search_params['minage'] : (int) $_REQUEST['minage']; |
||
375 | |||
376 | // Maximum age of messages. Default to infinite (9999 days: param not set). |
||
377 | if (!empty($search_params['maxage']) || (!empty($_REQUEST['maxage']) && $_REQUEST['maxage'] < 9999)) |
||
378 | $search_params['maxage'] = !empty($search_params['maxage']) ? (int) $search_params['maxage'] : (int) $_REQUEST['maxage']; |
||
379 | |||
380 | // Searching a specific topic? |
||
381 | if (!empty($_REQUEST['topic']) || (!empty($_REQUEST['search_selection']) && $_REQUEST['search_selection'] == 'topic')) |
||
382 | { |
||
383 | $search_params['topic'] = empty($_REQUEST['search_selection']) ? (int) $_REQUEST['topic'] : (isset($_REQUEST['sd_topic']) ? (int) $_REQUEST['sd_topic'] : ''); |
||
384 | $search_params['show_complete'] = true; |
||
385 | } |
||
386 | elseif (!empty($search_params['topic'])) |
||
387 | $search_params['topic'] = (int) $search_params['topic']; |
||
388 | |||
389 | if (!empty($search_params['minage']) || !empty($search_params['maxage'])) |
||
390 | { |
||
391 | $request = $smcFunc['db_query']('', ' |
||
392 | SELECT ' . (empty($search_params['maxage']) ? '0, ' : 'COALESCE(MIN(id_msg), -1), ') . (empty($search_params['minage']) ? '0' : 'COALESCE(MAX(id_msg), -1)') . ' |
||
393 | FROM {db_prefix}messages |
||
394 | WHERE 1=1' . ($modSettings['postmod_active'] ? ' |
||
395 | AND approved = {int:is_approved_true}' : '') . (empty($search_params['minage']) ? '' : ' |
||
396 | AND poster_time <= {int:timestamp_minimum_age}') . (empty($search_params['maxage']) ? '' : ' |
||
397 | AND poster_time >= {int:timestamp_maximum_age}'), |
||
398 | array( |
||
399 | 'timestamp_minimum_age' => empty($search_params['minage']) ? 0 : time() - 86400 * $search_params['minage'], |
||
400 | 'timestamp_maximum_age' => empty($search_params['maxage']) ? 0 : time() - 86400 * $search_params['maxage'], |
||
401 | 'is_approved_true' => 1, |
||
402 | ) |
||
403 | ); |
||
404 | list ($minMsgID, $maxMsgID) = $smcFunc['db_fetch_row']($request); |
||
405 | if ($minMsgID < 0 || $maxMsgID < 0) |
||
406 | $context['search_errors']['no_messages_in_time_frame'] = true; |
||
407 | $smcFunc['db_free_result']($request); |
||
408 | } |
||
409 | |||
410 | // Default the user name to a wildcard matching every user (*). |
||
411 | if (!empty($search_params['userspec']) || (!empty($_REQUEST['userspec']) && $_REQUEST['userspec'] != '*')) |
||
412 | $search_params['userspec'] = isset($search_params['userspec']) ? $search_params['userspec'] : $_REQUEST['userspec']; |
||
413 | |||
414 | // If there's no specific user, then don't mention it in the main query. |
||
415 | if (empty($search_params['userspec'])) |
||
416 | $userQuery = ''; |
||
417 | else |
||
418 | { |
||
419 | $userString = strtr($smcFunc['htmlspecialchars']($search_params['userspec'], ENT_QUOTES), array('"' => '"', '%' => '\%', '_' => '\_', '*' => '%', '?' => '_')); |
||
420 | |||
421 | preg_match_all('~"([^"]+)"~', $userString, $matches); |
||
422 | $possible_users = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $userString))); |
||
423 | |||
424 | for ($k = 0, $n = count($possible_users); $k < $n; $k++) |
||
425 | { |
||
426 | $possible_users[$k] = trim($possible_users[$k]); |
||
427 | |||
428 | if (strlen($possible_users[$k]) == 0) |
||
429 | unset($possible_users[$k]); |
||
430 | } |
||
431 | |||
432 | if (empty($possible_users)) |
||
433 | { |
||
434 | $userQuery = ''; |
||
435 | } |
||
436 | else |
||
437 | { |
||
438 | // Create a list of database-escaped search names. |
||
439 | $realNameMatches = array(); |
||
440 | |||
441 | foreach ($possible_users as $possible_user) |
||
442 | $realNameMatches[] = $smcFunc['db_quote']( |
||
443 | '{string:possible_user}', |
||
444 | array( |
||
445 | 'possible_user' => $possible_user |
||
446 | ) |
||
447 | ); |
||
448 | |||
449 | // Retrieve a list of possible members. |
||
450 | $request = $smcFunc['db_query']('', ' |
||
451 | SELECT id_member |
||
452 | FROM {db_prefix}members |
||
453 | WHERE {raw:match_possible_users}', |
||
454 | array( |
||
455 | 'match_possible_users' => 'real_name LIKE ' . implode(' OR real_name LIKE ', $realNameMatches), |
||
456 | ) |
||
457 | ); |
||
458 | |||
459 | // Simply do nothing if there're too many members matching the criteria. |
||
460 | if ($smcFunc['db_num_rows']($request) > $maxMembersToSearch) |
||
461 | { |
||
462 | $userQuery = ''; |
||
463 | } |
||
464 | elseif ($smcFunc['db_num_rows']($request) == 0) |
||
465 | { |
||
466 | $userQuery = $smcFunc['db_quote']( |
||
467 | 'm.id_member = {int:id_member_guest} AND ({raw:match_possible_guest_names})', |
||
468 | array( |
||
469 | 'id_member_guest' => 0, |
||
470 | 'match_possible_guest_names' => 'm.poster_name LIKE ' . implode(' OR m.poster_name LIKE ', $realNameMatches), |
||
471 | ) |
||
472 | ); |
||
473 | } |
||
474 | else |
||
475 | { |
||
476 | $memberlist = array(); |
||
477 | |||
478 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
479 | $memberlist[] = $row['id_member']; |
||
480 | |||
481 | $userQuery = $smcFunc['db_quote']( |
||
482 | '(m.id_member IN ({array_int:matched_members}) OR (m.id_member = {int:id_member_guest} AND ({raw:match_possible_guest_names})))', |
||
483 | array( |
||
484 | 'matched_members' => $memberlist, |
||
485 | 'id_member_guest' => 0, |
||
486 | 'match_possible_guest_names' => 'm.poster_name LIKE ' . implode(' OR m.poster_name LIKE ', $realNameMatches), |
||
487 | ) |
||
488 | ); |
||
489 | } |
||
490 | $smcFunc['db_free_result']($request); |
||
491 | } |
||
492 | } |
||
493 | |||
494 | // If the boards were passed by URL (params=), temporarily put them back in $_REQUEST. |
||
495 | if (!empty($search_params['brd']) && is_array($search_params['brd'])) |
||
496 | $_REQUEST['brd'] = $search_params['brd']; |
||
497 | |||
498 | // Ensure that brd is an array. |
||
499 | if ((!empty($_REQUEST['brd']) && !is_array($_REQUEST['brd'])) || (!empty($_REQUEST['search_selection']) && $_REQUEST['search_selection'] == 'board')) |
||
500 | { |
||
501 | if (!empty($_REQUEST['brd'])) |
||
502 | { |
||
503 | $_REQUEST['brd'] = strpos($_REQUEST['brd'], ',') !== false ? explode(',', $_REQUEST['brd']) : array($_REQUEST['brd']); |
||
504 | } |
||
505 | else |
||
506 | $_REQUEST['brd'] = isset($_REQUEST['sd_brd']) ? array($_REQUEST['sd_brd']) : array(); |
||
507 | } |
||
508 | |||
509 | // Make sure all boards are integers. |
||
510 | if (!empty($_REQUEST['brd'])) |
||
511 | { |
||
512 | foreach ($_REQUEST['brd'] as $id => $brd) |
||
513 | $_REQUEST['brd'][$id] = (int) $brd; |
||
514 | } |
||
515 | |||
516 | // Special case for boards: searching just one topic? |
||
517 | if (!empty($search_params['topic'])) |
||
518 | { |
||
519 | $request = $smcFunc['db_query']('', ' |
||
520 | SELECT t.id_board |
||
521 | FROM {db_prefix}topics AS t |
||
522 | WHERE t.id_topic = {int:search_topic_id} |
||
523 | AND {query_see_topic_board}' . ($modSettings['postmod_active'] ? ' |
||
524 | AND t.approved = {int:is_approved_true}' : '') . ' |
||
525 | LIMIT 1', |
||
526 | array( |
||
527 | 'search_topic_id' => $search_params['topic'], |
||
528 | 'is_approved_true' => 1, |
||
529 | ) |
||
530 | ); |
||
531 | |||
532 | if ($smcFunc['db_num_rows']($request) == 0) |
||
533 | fatal_lang_error('topic_gone', false); |
||
534 | |||
535 | $search_params['brd'] = array(); |
||
536 | list ($search_params['brd'][0]) = $smcFunc['db_fetch_row']($request); |
||
537 | $smcFunc['db_free_result']($request); |
||
538 | } |
||
539 | // Select all boards you've selected AND are allowed to see. |
||
540 | elseif ($user_info['is_admin'] && (!empty($search_params['advanced']) || !empty($_REQUEST['brd']))) |
||
541 | { |
||
542 | $search_params['brd'] = empty($_REQUEST['brd']) ? array() : $_REQUEST['brd']; |
||
543 | } |
||
544 | else |
||
545 | { |
||
546 | $see_board = empty($search_params['advanced']) ? 'query_wanna_see_board' : 'query_see_board'; |
||
547 | |||
548 | $request = $smcFunc['db_query']('', ' |
||
549 | SELECT b.id_board |
||
550 | FROM {db_prefix}boards AS b |
||
551 | WHERE {raw:boards_allowed_to_see} |
||
552 | AND redirect = {string:empty_string}' . (empty($_REQUEST['brd']) ? (!empty($modSettings['recycle_enable']) && $modSettings['recycle_board'] > 0 ? ' |
||
553 | AND b.id_board != {int:recycle_board_id}' : '') : ' |
||
554 | AND b.id_board IN ({array_int:selected_search_boards})'), |
||
555 | array( |
||
556 | 'boards_allowed_to_see' => $user_info[$see_board], |
||
557 | 'empty_string' => '', |
||
558 | 'selected_search_boards' => empty($_REQUEST['brd']) ? array() : $_REQUEST['brd'], |
||
559 | 'recycle_board_id' => $modSettings['recycle_board'], |
||
560 | ) |
||
561 | ); |
||
562 | |||
563 | $search_params['brd'] = array(); |
||
564 | |||
565 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
566 | $search_params['brd'][] = $row['id_board']; |
||
567 | |||
568 | $smcFunc['db_free_result']($request); |
||
569 | |||
570 | // This error should pro'bly only happen for hackers. |
||
571 | if (empty($search_params['brd'])) |
||
572 | $context['search_errors']['no_boards_selected'] = true; |
||
573 | } |
||
574 | |||
575 | if (count($search_params['brd']) != 0) |
||
576 | { |
||
577 | foreach ($search_params['brd'] as $k => $v) |
||
578 | $search_params['brd'][$k] = (int) $v; |
||
579 | |||
580 | // If we've selected all boards, this parameter can be left empty. |
||
581 | $request = $smcFunc['db_query']('', ' |
||
582 | SELECT COUNT(*) |
||
583 | FROM {db_prefix}boards |
||
584 | WHERE redirect = {string:empty_string}', |
||
585 | array( |
||
586 | 'empty_string' => '', |
||
587 | ) |
||
588 | ); |
||
589 | |||
590 | list ($num_boards) = $smcFunc['db_fetch_row']($request); |
||
591 | |||
592 | $smcFunc['db_free_result']($request); |
||
593 | |||
594 | if (count($search_params['brd']) == $num_boards) |
||
595 | { |
||
596 | $boardQuery = ''; |
||
597 | } |
||
598 | elseif (count($search_params['brd']) == $num_boards - 1 && !empty($modSettings['recycle_board']) && !in_array($modSettings['recycle_board'], $search_params['brd'])) |
||
599 | { |
||
600 | $boardQuery = '!= ' . $modSettings['recycle_board']; |
||
601 | } |
||
602 | else |
||
603 | $boardQuery = 'IN (' . implode(', ', $search_params['brd']) . ')'; |
||
604 | } |
||
605 | else |
||
606 | $boardQuery = ''; |
||
607 | |||
608 | $search_params['show_complete'] = !empty($search_params['show_complete']) || !empty($_REQUEST['show_complete']); |
||
609 | |||
610 | $search_params['subject_only'] = !empty($search_params['subject_only']) || !empty($_REQUEST['subject_only']); |
||
611 | |||
612 | $context['compact'] = !$search_params['show_complete']; |
||
613 | |||
614 | // Get the sorting parameters right. Default to sort by relevance descending. |
||
615 | $sort_columns = array( |
||
616 | 'relevance', |
||
617 | 'num_replies', |
||
618 | 'id_msg', |
||
619 | ); |
||
620 | |||
621 | call_integration_hook('integrate_search_sort_columns', array(&$sort_columns)); |
||
622 | |||
623 | if (empty($search_params['sort']) && !empty($_REQUEST['sort'])) |
||
624 | { |
||
625 | list ($search_params['sort'], $search_params['sort_dir']) = array_pad(explode('|', $_REQUEST['sort']), 2, ''); |
||
626 | } |
||
627 | |||
628 | $search_params['sort'] = !empty($search_params['sort']) && in_array($search_params['sort'], $sort_columns) ? $search_params['sort'] : 'relevance'; |
||
629 | |||
630 | if (!empty($search_params['topic']) && $search_params['sort'] === 'num_replies') |
||
631 | $search_params['sort'] = 'id_msg'; |
||
632 | |||
633 | // Sorting direction: descending unless stated otherwise. |
||
634 | $search_params['sort_dir'] = !empty($search_params['sort_dir']) && $search_params['sort_dir'] == 'asc' ? 'asc' : 'desc'; |
||
635 | |||
636 | // Remember current sort type and sort direction |
||
637 | $context['current_sorting'] = $search_params['sort'] . '|' . $search_params['sort_dir']; |
||
638 | |||
639 | // Determine some values needed to calculate the relevance. |
||
640 | $minMsg = (int) ((1 - $recentPercentage) * $modSettings['maxMsgID']); |
||
641 | $recentMsg = $modSettings['maxMsgID'] - $minMsg; |
||
642 | |||
643 | // *** Parse the search query |
||
644 | call_integration_hook('integrate_search_params', array(&$search_params)); |
||
645 | |||
646 | /* |
||
647 | * Unfortunately, searching for words like this is going to be slow, so we're blacklisting them. |
||
648 | * |
||
649 | * @todo Setting to add more here? |
||
650 | * @todo Maybe only blacklist if they are the only word, or "any" is used? |
||
651 | */ |
||
652 | $blacklisted_words = array('img', 'url', 'quote', 'www', 'http', 'the', 'is', 'it', 'are', 'if'); |
||
653 | |||
654 | call_integration_hook('integrate_search_blacklisted_words', array(&$blacklisted_words)); |
||
655 | |||
656 | // What are we searching for? |
||
657 | if (empty($search_params['search'])) |
||
658 | { |
||
659 | if (isset($_GET['search'])) |
||
660 | $search_params['search'] = un_htmlspecialchars($_GET['search']); |
||
661 | |||
662 | elseif (isset($_POST['search'])) |
||
663 | $search_params['search'] = $_POST['search']; |
||
664 | |||
665 | else |
||
666 | $search_params['search'] = ''; |
||
667 | } |
||
668 | |||
669 | // Nothing?? |
||
670 | if (!isset($search_params['search']) || $search_params['search'] == '') |
||
671 | { |
||
672 | $context['search_errors']['invalid_search_string'] = true; |
||
673 | } |
||
674 | // Too long? |
||
675 | elseif ($smcFunc['strlen']($search_params['search']) > $context['search_string_limit']) |
||
676 | { |
||
677 | $context['search_errors']['string_too_long'] = true; |
||
678 | } |
||
679 | |||
680 | // Change non-word characters into spaces. |
||
681 | $stripped_query = preg_replace('~(?:[\x0B\0' . ($context['utf8'] ? '\x{A0}' : '\xA0') . '\t\r\s\n(){}\\[\\]<>!@$%^*.,:+=`\~\?/\\\\]+|&(?:amp|lt|gt|quot);)+~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']); |
||
682 | |||
683 | // Make the query lower case. It's gonna be case insensitive anyway. |
||
684 | $stripped_query = un_htmlspecialchars($smcFunc['strtolower']($stripped_query)); |
||
685 | |||
686 | // This (hidden) setting will do fulltext searching in the most basic way. |
||
687 | if (!empty($modSettings['search_simple_fulltext'])) |
||
688 | $stripped_query = strtr($stripped_query, array('"' => '')); |
||
689 | |||
690 | $no_regexp = preg_match('~&#(?:\d{1,7}|x[0-9a-fA-F]{1,6});~', $stripped_query) === 1; |
||
691 | $is_search_regex = !empty($modSettings['search_match_words']) && !$no_regexp; |
||
692 | |||
693 | // Specify the function to search with. Regex is for word boundaries. |
||
694 | $query_match_type = $is_search_regex ? 'RLIKE' : 'LIKE'; |
||
695 | $word_boundary_wrapper = function(string $str) use ($smcFunc): string |
||
696 | { |
||
697 | return sprintf($smcFunc['db_supports_pcre'] ? '\\b%s\\b' : '[[:<:]]%s[[:>:]]', $str); |
||
698 | }; |
||
699 | $escape_sql_regex = function(string $str): string |
||
700 | { |
||
701 | return addcslashes(preg_replace('/[\[\]$.+*?&^|{}()]/', '[$0]', $str), '\\\''); |
||
702 | }; |
||
703 | |||
704 | // Extract phrase parts first (e.g. some words "this is a phrase" some more words.) |
||
705 | preg_match_all('/(?:^|\s)([-]?)"([^"]+)"(?:$|\s)/', $stripped_query, $matches, PREG_PATTERN_ORDER); |
||
706 | |||
707 | $phraseArray = $matches[2]; |
||
708 | |||
709 | // Remove the phrase parts and extract the words. |
||
710 | $wordArray = preg_replace('~(?:^|\s)[-]?"[^"]+"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']); |
||
711 | |||
712 | $wordArray = explode(' ', $smcFunc['htmlspecialchars'](un_htmlspecialchars($wordArray), ENT_QUOTES)); |
||
713 | |||
714 | // A minus sign in front of a word excludes the word.... so... |
||
715 | $excludedWords = array(); |
||
716 | $excludedIndexWords = array(); |
||
717 | $excludedSubjectWords = array(); |
||
718 | $excludedPhrases = array(); |
||
719 | |||
720 | // .. first, we check for things like -"some words", but not "-some words". |
||
721 | foreach ($matches[1] as $index => $word) |
||
722 | { |
||
723 | if ($word === '-') |
||
724 | { |
||
725 | if (($word = trim($phraseArray[$index], '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) |
||
726 | $excludedWords[] = $word; |
||
727 | unset($phraseArray[$index]); |
||
728 | } |
||
729 | } |
||
730 | |||
731 | // Now we look for -test, etc.... normaller. |
||
732 | foreach ($wordArray as $index => $word) |
||
733 | { |
||
734 | if (strpos(trim($word), '-') === 0) |
||
735 | { |
||
736 | if (($word = trim($word, '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) |
||
737 | $excludedWords[] = $word; |
||
738 | |||
739 | unset($wordArray[$index]); |
||
740 | } |
||
741 | } |
||
742 | |||
743 | // The remaining words and phrases are all included. |
||
744 | $searchArray = array_merge($phraseArray, $wordArray); |
||
745 | |||
746 | $context['search_ignored'] = array(); |
||
747 | // Trim everything and make sure there are no words that are the same. |
||
748 | foreach ($searchArray as $index => $value) |
||
749 | { |
||
750 | // Skip anything practically empty. |
||
751 | if (($searchArray[$index] = trim($value, '-_\' ')) === '') |
||
752 | { |
||
753 | unset($searchArray[$index]); |
||
754 | } |
||
755 | // Skip blacklisted words. Make sure to note we skipped them in case we end up with nothing. |
||
756 | elseif (in_array($searchArray[$index], $blacklisted_words)) |
||
757 | { |
||
758 | $foundBlackListedWords = true; |
||
759 | unset($searchArray[$index]); |
||
760 | } |
||
761 | // Don't allow very, very short words. |
||
762 | elseif ($smcFunc['strlen']($value) < 2) |
||
763 | { |
||
764 | $context['search_ignored'][] = $value; |
||
765 | unset($searchArray[$index]); |
||
766 | } |
||
767 | } |
||
768 | $searchArray = array_slice(array_unique($searchArray), 0, 10); |
||
769 | |||
770 | // Create an array of replacements for highlighting. |
||
771 | $context['mark'] = array(); |
||
772 | |||
773 | foreach ($searchArray as $word) |
||
774 | $context['mark'][$word] = '<strong class="highlight">' . $word . '</strong>'; |
||
775 | |||
776 | // Initialize two arrays storing the words that have to be searched for. |
||
777 | $orParts = array(); |
||
778 | $searchWords = array(); |
||
779 | |||
780 | // Make sure at least one word is being searched for. |
||
781 | if (empty($searchArray)) |
||
782 | { |
||
783 | $context['search_errors']['invalid_search_string' . (!empty($foundBlackListedWords) ? '_blacklist' : '')] = true; |
||
784 | } |
||
785 | // All words/sentences must match. |
||
786 | elseif (empty($search_params['searchtype'])) |
||
787 | { |
||
788 | $orParts[0] = $searchArray; |
||
789 | } |
||
790 | // Any word/sentence must match. |
||
791 | else |
||
792 | { |
||
793 | foreach ($searchArray as $index => $value) |
||
794 | $orParts[$index] = array($value); |
||
795 | } |
||
796 | |||
797 | // Don't allow duplicate error messages if one string is too short. |
||
798 | if (isset($context['search_errors']['search_string_small_words'], $context['search_errors']['invalid_search_string'])) |
||
799 | unset($context['search_errors']['invalid_search_string']); |
||
800 | |||
801 | // Make sure the excluded words are in all or-branches. |
||
802 | foreach ($orParts as $orIndex => $andParts) |
||
803 | foreach ($excludedWords as $word) |
||
804 | $orParts[$orIndex][] = $word; |
||
805 | |||
806 | // Determine the or-branches and the fulltext search words. |
||
807 | foreach ($orParts as $orIndex => $andParts) |
||
808 | { |
||
809 | $searchWords[$orIndex] = array( |
||
810 | 'indexed_words' => array(), |
||
811 | 'words' => array(), |
||
812 | 'subject_words' => array(), |
||
813 | 'all_words' => array(), |
||
814 | 'complex_words' => array(), |
||
815 | ); |
||
816 | |||
817 | // Sort the indexed words (large words -> small words -> excluded words). |
||
818 | if ($searchAPI->supportsMethod('searchSort')) |
||
819 | usort($orParts[$orIndex], 'searchSort'); |
||
820 | |||
821 | foreach ($orParts[$orIndex] as $word) |
||
822 | { |
||
823 | $is_excluded = in_array($word, $excludedWords); |
||
824 | |||
825 | $searchWords[$orIndex]['all_words'][] = $word; |
||
826 | |||
827 | $subjectWords = text2words($word); |
||
828 | if (!$is_excluded || count($subjectWords) === 1) |
||
829 | { |
||
830 | $searchWords[$orIndex]['subject_words'] = array_merge($searchWords[$orIndex]['subject_words'], $subjectWords); |
||
831 | if ($is_excluded) |
||
832 | $excludedSubjectWords = array_merge($excludedSubjectWords, $subjectWords); |
||
833 | } |
||
834 | else |
||
835 | $excludedPhrases[] = $word; |
||
836 | |||
837 | // Have we got indexes to prepare? |
||
838 | if ($searchAPI->supportsMethod('prepareIndexes')) |
||
839 | $searchAPI->prepareIndexes($word, $searchWords[$orIndex], $excludedIndexWords, $is_excluded); |
||
840 | } |
||
841 | |||
842 | // Search_force_index requires all AND parts to have at least one fulltext word. |
||
843 | if (!empty($modSettings['search_force_index']) && empty($searchWords[$orIndex]['indexed_words'])) |
||
844 | { |
||
845 | $context['search_errors']['query_not_specific_enough'] = true; |
||
846 | break; |
||
847 | } |
||
848 | elseif ($search_params['subject_only'] && empty($searchWords[$orIndex]['subject_words']) && empty($excludedSubjectWords)) |
||
849 | { |
||
850 | $context['search_errors']['query_not_specific_enough'] = true; |
||
851 | break; |
||
852 | } |
||
853 | |||
854 | // Make sure we aren't searching for too many indexed words. |
||
855 | else |
||
856 | { |
||
857 | $searchWords[$orIndex]['indexed_words'] = array_slice($searchWords[$orIndex]['indexed_words'], 0, 7); |
||
858 | $searchWords[$orIndex]['subject_words'] = array_slice($searchWords[$orIndex]['subject_words'], 0, 7); |
||
859 | $searchWords[$orIndex]['words'] = array_slice($searchWords[$orIndex]['words'], 0, 4); |
||
860 | } |
||
861 | } |
||
862 | |||
863 | // *** Spell checking |
||
864 | if ($context['show_spellchecking']) |
||
865 | { |
||
866 | require_once($sourcedir . '/Subs-Post.php'); |
||
867 | |||
868 | // Don't hardcode spellchecking functions! |
||
869 | $link = spell_init(); |
||
870 | |||
871 | $did_you_mean = array('search' => array(), 'display' => array()); |
||
872 | $found_misspelling = false; |
||
873 | foreach ($searchArray as $word) |
||
874 | { |
||
875 | if (empty($link)) |
||
876 | continue; |
||
877 | |||
878 | // Don't check phrases. |
||
879 | if (preg_match('~^\w+$~', $word) === 0) |
||
880 | { |
||
881 | $did_you_mean['search'][] = '"' . $word . '"'; |
||
882 | $did_you_mean['display'][] = '"' . $smcFunc['htmlspecialchars']($word) . '"'; |
||
883 | continue; |
||
884 | } |
||
885 | // For some strange reason spell check can crash PHP on decimals. |
||
886 | elseif (preg_match('~\d~', $word) === 1) |
||
887 | { |
||
888 | $did_you_mean['search'][] = $word; |
||
889 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
890 | continue; |
||
891 | } |
||
892 | elseif (spell_check($link, $word)) |
||
893 | { |
||
894 | $did_you_mean['search'][] = $word; |
||
895 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
896 | continue; |
||
897 | } |
||
898 | |||
899 | $suggestions = spell_suggest($link, $word); |
||
900 | foreach ($suggestions as $i => $s) |
||
901 | { |
||
902 | // Search is case insensitive. |
||
903 | if ($smcFunc['strtolower']($s) == $smcFunc['strtolower']($word)) |
||
904 | unset($suggestions[$i]); |
||
905 | |||
906 | // Plus, don't suggest something the user thinks is rude! |
||
907 | elseif ($suggestions[$i] != censorText($s)) |
||
908 | unset($suggestions[$i]); |
||
909 | } |
||
910 | |||
911 | // Anything found? If so, correct it! |
||
912 | if (!empty($suggestions)) |
||
913 | { |
||
914 | $suggestions = array_values($suggestions); |
||
915 | $did_you_mean['search'][] = $suggestions[0]; |
||
916 | $did_you_mean['display'][] = '<em><strong>' . $smcFunc['htmlspecialchars']($suggestions[0]) . '</strong></em>'; |
||
917 | $found_misspelling = true; |
||
918 | } |
||
919 | else |
||
920 | { |
||
921 | $did_you_mean['search'][] = $word; |
||
922 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
923 | } |
||
924 | } |
||
925 | |||
926 | if ($found_misspelling) |
||
927 | { |
||
928 | // Don't spell check excluded words, but add them still... |
||
929 | $temp_excluded = array('search' => array(), 'display' => array()); |
||
930 | |||
931 | foreach ($excludedWords as $word) |
||
932 | { |
||
933 | if (preg_match('~^\w+$~', $word) == 0) |
||
934 | { |
||
935 | $temp_excluded['search'][] = '-"' . $word . '"'; |
||
936 | $temp_excluded['display'][] = '-"' . $smcFunc['htmlspecialchars']($word) . '"'; |
||
937 | } |
||
938 | else |
||
939 | { |
||
940 | $temp_excluded['search'][] = '-' . $word; |
||
941 | $temp_excluded['display'][] = '-' . $smcFunc['htmlspecialchars']($word); |
||
942 | } |
||
943 | } |
||
944 | |||
945 | $did_you_mean['search'] = array_merge($did_you_mean['search'], $temp_excluded['search']); |
||
946 | $did_you_mean['display'] = array_merge($did_you_mean['display'], $temp_excluded['display']); |
||
947 | |||
948 | $temp_params = $search_params; |
||
949 | $temp_params['search'] = implode(' ', $did_you_mean['search']); |
||
950 | |||
951 | if (isset($temp_params['brd'])) |
||
952 | $temp_params['brd'] = implode(',', $temp_params['brd']); |
||
953 | |||
954 | $context['params'] = array(); |
||
955 | |||
956 | foreach ($temp_params as $k => $v) |
||
957 | $context['did_you_mean_params'][] = $k . '|\'|' . $v; |
||
958 | |||
959 | $context['did_you_mean_params'] = base64_encode(implode('|"|', $context['did_you_mean_params'])); |
||
960 | $context['did_you_mean'] = implode(' ', $did_you_mean['display']); |
||
961 | } |
||
962 | } |
||
963 | |||
964 | // Let the user adjust the search query, should they wish? |
||
965 | $context['search_params'] = $search_params; |
||
966 | |||
967 | if (isset($context['search_params']['search'])) |
||
968 | $context['search_params']['search'] = $smcFunc['htmlspecialchars']($context['search_params']['search']); |
||
969 | |||
970 | if (isset($context['search_params']['userspec'])) |
||
971 | $context['search_params']['userspec'] = $smcFunc['htmlspecialchars']($context['search_params']['userspec']); |
||
972 | |||
973 | // Do we have captcha enabled? |
||
974 | if ($user_info['is_guest'] && !empty($modSettings['search_enable_captcha']) && empty($_SESSION['ss_vv_passed']) && (empty($_SESSION['last_ss']) || $_SESSION['last_ss'] != $search_params['search'])) |
||
975 | { |
||
976 | require_once($sourcedir . '/Subs-Editor.php'); |
||
977 | |||
978 | $verificationOptions = array( |
||
979 | 'id' => 'search', |
||
980 | ); |
||
981 | |||
982 | $context['require_verification'] = create_control_verification($verificationOptions, true); |
||
983 | |||
984 | if (is_array($context['require_verification'])) |
||
985 | { |
||
986 | foreach ($context['require_verification'] as $error) |
||
987 | $context['search_errors'][$error] = true; |
||
988 | } |
||
989 | // Don't keep asking for it - they've proven themselves worthy. |
||
990 | else |
||
991 | $_SESSION['ss_vv_passed'] = true; |
||
992 | } |
||
993 | |||
994 | // *** Encode all search params |
||
995 | |||
996 | // All search params have been checked, let's compile them to a single string... made less simple by PHP 4.3.9 and below. |
||
997 | $temp_params = $search_params; |
||
998 | if (isset($temp_params['brd'])) |
||
999 | $temp_params['brd'] = implode(',', $temp_params['brd']); |
||
1000 | |||
1001 | $context['params'] = array(); |
||
1002 | |||
1003 | foreach ($temp_params as $k => $v) |
||
1004 | $context['params'][] = $k . '|\'|' . $v; |
||
1005 | |||
1006 | if (!empty($context['params'])) |
||
1007 | { |
||
1008 | // Due to old IE's 2083 character limit, we have to compress long search strings |
||
1009 | $params = @gzcompress(implode('|"|', $context['params'])); |
||
1010 | |||
1011 | // Gzcompress failed, use try non-gz |
||
1012 | if (empty($params)) |
||
1013 | $params = implode('|"|', $context['params']); |
||
1014 | |||
1015 | // Base64 encode, then replace +/= with uri safe ones that can be reverted |
||
1016 | $context['params'] = str_replace(array('+', '/', '='), array('-', '_', '.'), base64_encode($params)); |
||
1017 | } |
||
1018 | |||
1019 | // ... and add the links to the link tree. |
||
1020 | $context['linktree'][] = array( |
||
1021 | 'url' => $scripturl . '?action=search;params=' . $context['params'], |
||
1022 | 'name' => $txt['search'] |
||
1023 | ); |
||
1024 | $context['linktree'][] = array( |
||
1025 | 'url' => $scripturl . '?action=search2;params=' . $context['params'], |
||
1026 | 'name' => $txt['search_results'] |
||
1027 | ); |
||
1028 | |||
1029 | // *** A last error check |
||
1030 | call_integration_hook('integrate_search_errors'); |
||
1031 | |||
1032 | // One or more search errors? Go back to the first search screen. |
||
1033 | if (!empty($context['search_errors'])) |
||
1034 | { |
||
1035 | $_REQUEST['params'] = $context['params']; |
||
1036 | return PlushSearch1(); |
||
1037 | } |
||
1038 | |||
1039 | // Spam me not, Spam-a-lot? |
||
1040 | if (empty($_SESSION['last_ss']) || $_SESSION['last_ss'] != $search_params['search']) |
||
1041 | spamProtection('search'); |
||
1042 | |||
1043 | // Store the last search string to allow pages of results to be browsed. |
||
1044 | $_SESSION['last_ss'] = $search_params['search']; |
||
1045 | |||
1046 | // *** Reserve an ID for caching the search results. |
||
1047 | $query_params = array_merge($search_params, array( |
||
1048 | 'min_msg_id' => isset($minMsgID) ? (int) $minMsgID : 0, |
||
1049 | 'max_msg_id' => isset($maxMsgID) ? (int) $maxMsgID : 0, |
||
1050 | 'memberlist' => !empty($memberlist) ? $memberlist : array(), |
||
1051 | )); |
||
1052 | |||
1053 | // Can this search rely on the API given the parameters? |
||
1054 | if ($searchAPI->supportsMethod('searchQuery', $query_params)) |
||
1055 | { |
||
1056 | $participants = array(); |
||
1057 | $searchArray = array(); |
||
1058 | |||
1059 | $searchAPI->searchQuery($query_params, $searchWords, $excludedIndexWords, $participants, $searchArray); |
||
1060 | } |
||
1061 | |||
1062 | // Update the cache if the current search term is not yet cached. |
||
1063 | else |
||
1064 | { |
||
1065 | $update_cache = empty($_SESSION['search_cache']) || ($_SESSION['search_cache']['params'] != $context['params']); |
||
1066 | |||
1067 | // Are the result fresh? |
||
1068 | if (!$update_cache && !empty($_SESSION['search_cache']['id_search'])) |
||
1069 | { |
||
1070 | $request = $smcFunc['db_query']('', ' |
||
1071 | SELECT id_search |
||
1072 | FROM {db_prefix}log_search_results |
||
1073 | WHERE id_search = {int:search_id} |
||
1074 | LIMIT 1', |
||
1075 | array( |
||
1076 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
1077 | ) |
||
1078 | ); |
||
1079 | |||
1080 | if ($smcFunc['db_num_rows']($request) === 0) |
||
1081 | $update_cache = true; |
||
1082 | } |
||
1083 | |||
1084 | if ($update_cache) |
||
1085 | { |
||
1086 | // Increase the pointer... |
||
1087 | $modSettings['search_pointer'] = empty($modSettings['search_pointer']) ? 0 : (int) $modSettings['search_pointer']; |
||
1088 | |||
1089 | // ...and store it right off. |
||
1090 | updateSettings(array('search_pointer' => $modSettings['search_pointer'] >= 255 ? 0 : $modSettings['search_pointer'] + 1)); |
||
1091 | |||
1092 | // As long as you don't change the parameters, the cache result is yours. |
||
1093 | $_SESSION['search_cache'] = array( |
||
1094 | 'id_search' => $modSettings['search_pointer'], |
||
1095 | 'num_results' => -1, |
||
1096 | 'params' => $context['params'], |
||
1097 | ); |
||
1098 | |||
1099 | // Clear the previous cache of the final results cache. |
||
1100 | $smcFunc['db_search_query']('delete_log_search_results', ' |
||
1101 | DELETE FROM {db_prefix}log_search_results |
||
1102 | WHERE id_search = {int:search_id}', |
||
1103 | array( |
||
1104 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
1105 | ) |
||
1106 | ); |
||
1107 | |||
1108 | if ($search_params['subject_only']) |
||
1109 | { |
||
1110 | // We do this to try and avoid duplicate keys on databases not supporting INSERT IGNORE. |
||
1111 | $inserts = array(); |
||
1112 | |||
1113 | foreach ($searchWords as $orIndex => $words) |
||
1114 | { |
||
1115 | $subject_query_params = array( |
||
1116 | 'not_redirected' => 0, |
||
1117 | 'never_expires' => 0, |
||
1118 | ); |
||
1119 | $subject_query = array( |
||
1120 | 'from' => '{db_prefix}topics AS t', |
||
1121 | 'inner_join' => array(), |
||
1122 | 'left_join' => array(), |
||
1123 | 'where' => array( |
||
1124 | 't.id_redirect_topic = {int:not_redirected}', |
||
1125 | 't.redirect_expires = {int:never_expires}', |
||
1126 | ), |
||
1127 | ); |
||
1128 | |||
1129 | if ($modSettings['postmod_active']) |
||
1130 | $subject_query['where'][] = 't.approved = {int:is_approved}'; |
||
1131 | |||
1132 | $numTables = 0; |
||
1133 | $prev_join = 0; |
||
1134 | $numSubjectResults = 0; |
||
1135 | |||
1136 | foreach ($words['subject_words'] as $subjectWord) |
||
1137 | { |
||
1138 | $numTables++; |
||
1139 | |||
1140 | if (in_array($subjectWord, $excludedSubjectWords)) |
||
1141 | { |
||
1142 | $subject_query['left_join'][] = '{db_prefix}log_search_subjects AS subj' . $numTables . ' ON (subj' . $numTables . '.word ' . (empty($modSettings['search_match_words']) ? 'LIKE {string:subject_words_' . $numTables . '_wild}' : '= {string:subject_words_' . $numTables . '}') . ' AND subj' . $numTables . '.id_topic = t.id_topic)'; |
||
1143 | |||
1144 | $subject_query['where'][] = '(subj' . $numTables . '.word IS NULL)'; |
||
1145 | } |
||
1146 | else |
||
1147 | { |
||
1148 | $subject_query['inner_join'][] = '{db_prefix}log_search_subjects AS subj' . $numTables . ' ON (subj' . $numTables . '.id_topic = ' . ($prev_join === 0 ? 't' : 'subj' . $prev_join) . '.id_topic)'; |
||
1149 | |||
1150 | $subject_query['where'][] = 'subj' . $numTables . '.word ' . (empty($modSettings['search_match_words']) ? 'LIKE {string:subject_words_' . $numTables . '_wild}' : '= {string:subject_words_' . $numTables . '}'); |
||
1151 | |||
1152 | $prev_join = $numTables; |
||
1153 | } |
||
1154 | |||
1155 | $subject_query_params['subject_words_' . $numTables] = $subjectWord; |
||
1156 | $subject_query_params['subject_words_' . $numTables . '_wild'] = '%' . $subjectWord . '%'; |
||
1157 | } |
||
1158 | |||
1159 | if (!empty($userQuery)) |
||
1160 | { |
||
1161 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
1162 | { |
||
1163 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_topic = t.id_topic)'; |
||
1164 | } |
||
1165 | |||
1166 | $subject_query['where'][] = $userQuery; |
||
1167 | } |
||
1168 | |||
1169 | if (!empty($search_params['topic'])) |
||
1170 | $subject_query['where'][] = 't.id_topic = ' . $search_params['topic']; |
||
1171 | |||
1172 | if (!empty($minMsgID)) |
||
1173 | $subject_query['where'][] = 't.id_first_msg >= ' . $minMsgID; |
||
1174 | |||
1175 | if (!empty($maxMsgID)) |
||
1176 | $subject_query['where'][] = 't.id_last_msg <= ' . $maxMsgID; |
||
1177 | |||
1178 | if (!empty($boardQuery)) |
||
1179 | $subject_query['where'][] = 't.id_board ' . $boardQuery; |
||
1180 | |||
1181 | if (!empty($excludedPhrases)) |
||
1182 | { |
||
1183 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
1184 | { |
||
1185 | $subject_query['inner_join']['m'] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
1186 | } |
||
1187 | |||
1188 | $count = 0; |
||
1189 | |||
1190 | foreach ($excludedPhrases as $phrase) |
||
1191 | { |
||
1192 | $subject_query['where'][] = 'm.subject NOT ' . $query_match_type . ' {string:excluded_phrases_' . $count . '}'; |
||
1193 | |||
1194 | if ($is_search_regex) |
||
1195 | $subject_query_params['excluded_phrases_' . $count++] = $word_boundary_wrapper($escape_sql_regex($phrase)); |
||
1196 | else |
||
1197 | $subject_query_params['excluded_phrases_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($phrase) . '%'; |
||
1198 | } |
||
1199 | } |
||
1200 | |||
1201 | call_integration_hook('integrate_subject_only_search_query', array(&$subject_query, &$subject_query_params)); |
||
1202 | |||
1203 | $relevance = '1000 * ('; |
||
1204 | |||
1205 | foreach ($weight_factors as $type => $value) |
||
1206 | { |
||
1207 | $relevance .= $weight[$type]; |
||
1208 | |||
1209 | if (!empty($value['results'])) |
||
1210 | $relevance .= ' * ' . $value['results']; |
||
1211 | |||
1212 | $relevance .= ' + '; |
||
1213 | } |
||
1214 | |||
1215 | $relevance = substr($relevance, 0, -3) . ') / ' . $weight_total . ' AS relevance'; |
||
1216 | |||
1217 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_subject', |
||
1218 | ($smcFunc['db_support_ignore'] ? ' |
||
1219 | INSERT IGNORE INTO {db_prefix}log_search_results |
||
1220 | (id_search, id_topic, relevance, id_msg, num_matches)' : '') . ' |
||
1221 | SELECT |
||
1222 | {int:id_search}, |
||
1223 | t.id_topic, |
||
1224 | ' . $relevance . ', |
||
1225 | ' . (empty($userQuery) ? 't.id_first_msg' : 'm.id_msg') . ', |
||
1226 | 1 |
||
1227 | FROM ' . $subject_query['from'] . (empty($subject_query['inner_join']) ? '' : ' |
||
1228 | INNER JOIN ' . implode(' |
||
1229 | INNER JOIN ', $subject_query['inner_join'])) . (empty($subject_query['left_join']) ? '' : ' |
||
1230 | LEFT JOIN ' . implode(' |
||
1231 | LEFT JOIN ', $subject_query['left_join'])) . ' |
||
1232 | WHERE ' . implode(' |
||
1233 | AND ', $subject_query['where']) . (empty($modSettings['search_max_results']) ? '' : ' |
||
1234 | LIMIT ' . ($modSettings['search_max_results'] - $numSubjectResults)), |
||
1235 | array_merge($subject_query_params, array( |
||
1236 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1237 | 'min_msg' => $minMsg, |
||
1238 | 'recent_message' => $recentMsg, |
||
1239 | 'huge_topic_posts' => $humungousTopicPosts, |
||
1240 | 'is_approved' => 1, |
||
1241 | )) |
||
1242 | ); |
||
1243 | |||
1244 | // If the database doesn't support IGNORE to make this fast we need to do some tracking. |
||
1245 | if (!$smcFunc['db_support_ignore']) |
||
1246 | { |
||
1247 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1248 | { |
||
1249 | // No duplicates! |
||
1250 | if (isset($inserts[$row[1]])) |
||
1251 | continue; |
||
1252 | |||
1253 | foreach ($row as $key => $value) |
||
1254 | $inserts[$row[1]][] = (int) $row[$key]; |
||
1255 | } |
||
1256 | $smcFunc['db_free_result']($ignoreRequest); |
||
1257 | $numSubjectResults = count($inserts); |
||
1258 | } |
||
1259 | else |
||
1260 | $numSubjectResults += $smcFunc['db_affected_rows'](); |
||
1261 | |||
1262 | if (!empty($modSettings['search_max_results']) && $numSubjectResults >= $modSettings['search_max_results']) |
||
1263 | break; |
||
1264 | } |
||
1265 | |||
1266 | // If there's data to be inserted for non-IGNORE databases do it here! |
||
1267 | if (!empty($inserts)) |
||
1268 | { |
||
1269 | $smcFunc['db_insert']('', |
||
1270 | '{db_prefix}log_search_results', |
||
1271 | array('id_search' => 'int', 'id_topic' => 'int', 'relevance' => 'int', 'id_msg' => 'int', 'num_matches' => 'int'), |
||
1272 | $inserts, |
||
1273 | array('id_search', 'id_topic') |
||
1274 | ); |
||
1275 | } |
||
1276 | |||
1277 | $_SESSION['search_cache']['num_results'] = $numSubjectResults; |
||
1278 | } |
||
1279 | else |
||
1280 | { |
||
1281 | $main_query = array( |
||
1282 | 'select' => array( |
||
1283 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1284 | 'relevance' => '0', |
||
1285 | 'id_topic' => 't.id_topic', |
||
1286 | ), |
||
1287 | 'weights' => array(), |
||
1288 | 'from' => '{db_prefix}topics AS t', |
||
1289 | 'inner_join' => array( |
||
1290 | '{db_prefix}messages AS m ON (m.id_topic = t.id_topic)' |
||
1291 | ), |
||
1292 | 'left_join' => array(), |
||
1293 | 'where' => array( |
||
1294 | 't.id_redirect_topic = {int:not_redirected}', |
||
1295 | 't.redirect_expires = {int:never_expires}', |
||
1296 | ), |
||
1297 | 'group_by' => array(), |
||
1298 | 'parameters' => array( |
||
1299 | 'min_msg' => $minMsg, |
||
1300 | 'recent_message' => $recentMsg, |
||
1301 | 'huge_topic_posts' => $humungousTopicPosts, |
||
1302 | 'is_approved' => 1, |
||
1303 | 'not_redirected' => 0, |
||
1304 | 'never_expires' => 0, |
||
1305 | ), |
||
1306 | ); |
||
1307 | |||
1308 | if (empty($search_params['topic']) && empty($search_params['show_complete'])) |
||
1309 | { |
||
1310 | $main_query['select']['id_msg'] = 'MAX(m.id_msg) AS id_msg'; |
||
1311 | $main_query['select']['num_matches'] = 'COUNT(*) AS num_matches'; |
||
1312 | |||
1313 | $main_query['weights'] = $weight_factors; |
||
1314 | |||
1315 | $main_query['group_by'][] = 't.id_topic'; |
||
1316 | } |
||
1317 | else |
||
1318 | { |
||
1319 | $main_query['select']['id_msg'] = 'm.id_msg'; |
||
1320 | $main_query['select']['num_matches'] = '1 AS num_matches'; |
||
1321 | |||
1322 | $main_query['weights'] = array( |
||
1323 | 'age' => array( |
||
1324 | 'search' => '((m.id_msg - t.id_first_msg) / CASE WHEN t.id_last_msg = t.id_first_msg THEN 1 ELSE t.id_last_msg - t.id_first_msg END)', |
||
1325 | ), |
||
1326 | 'first_message' => array( |
||
1327 | 'search' => 'CASE WHEN m.id_msg = t.id_first_msg THEN 1 ELSE 0 END', |
||
1328 | ), |
||
1329 | ); |
||
1330 | |||
1331 | if (!empty($search_params['topic'])) |
||
1332 | { |
||
1333 | $main_query['where'][] = 't.id_topic = {int:topic}'; |
||
1334 | $main_query['parameters']['topic'] = $search_params['topic']; |
||
1335 | } |
||
1336 | if (!empty($search_params['show_complete'])) |
||
1337 | $main_query['group_by'][] = 't.id_topic, m.id_msg, t.id_first_msg, t.id_last_msg'; |
||
1338 | } |
||
1339 | |||
1340 | // *** Get the subject results. |
||
1341 | $numSubjectResults = 0; |
||
1342 | if (empty($search_params['topic'])) |
||
1343 | { |
||
1344 | $inserts = array(); |
||
1345 | // Create a temporary table to store some preliminary results in. |
||
1346 | $smcFunc['db_search_query']('drop_tmp_log_search_topics', ' |
||
1347 | DROP TABLE IF EXISTS {db_prefix}tmp_log_search_topics', |
||
1348 | array( |
||
1349 | ) |
||
1350 | ); |
||
1351 | $createTemporary = $smcFunc['db_search_query']('create_tmp_log_search_topics', ' |
||
1352 | CREATE TEMPORARY TABLE {db_prefix}tmp_log_search_topics ( |
||
1353 | id_topic int NOT NULL default {string:string_zero}, |
||
1354 | PRIMARY KEY (id_topic) |
||
1355 | ) ENGINE=MEMORY', |
||
1356 | array( |
||
1357 | 'string_zero' => '0', |
||
1358 | ) |
||
1359 | ) !== false; |
||
1360 | |||
1361 | // Clean up some previous cache. |
||
1362 | if (!$createTemporary) |
||
1363 | $smcFunc['db_search_query']('delete_log_search_topics', ' |
||
1364 | DELETE FROM {db_prefix}log_search_topics |
||
1365 | WHERE id_search = {int:search_id}', |
||
1366 | array( |
||
1367 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
1368 | ) |
||
1369 | ); |
||
1370 | |||
1371 | foreach ($searchWords as $orIndex => $words) |
||
1372 | { |
||
1373 | $subject_query = array( |
||
1374 | 'from' => '{db_prefix}topics AS t', |
||
1375 | 'inner_join' => array(), |
||
1376 | 'left_join' => array(), |
||
1377 | 'where' => array( |
||
1378 | 't.id_redirect_topic = {int:not_redirected}', |
||
1379 | 't.redirect_expires = {int:never_expires}', |
||
1380 | ), |
||
1381 | 'params' => array( |
||
1382 | 'not_redirected' => 0, |
||
1383 | 'never_expires' => 0, |
||
1384 | ), |
||
1385 | ); |
||
1386 | |||
1387 | $numTables = 0; |
||
1388 | $prev_join = 0; |
||
1389 | $count = 0; |
||
1390 | $excluded = false; |
||
1391 | foreach ($words['subject_words'] as $subjectWord) |
||
1392 | { |
||
1393 | $numTables++; |
||
1394 | if (in_array($subjectWord, $excludedSubjectWords)) |
||
1395 | { |
||
1396 | if (($subject_query['from'] != '{db_prefix}messages AS m') && !$excluded) |
||
1397 | { |
||
1398 | $subject_query['inner_join']['m'] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
1399 | $excluded = true; |
||
1400 | } |
||
1401 | $subject_query['left_join'][] = '{db_prefix}log_search_subjects AS subj' . $numTables . ' ON (subj' . $numTables . '.word ' . (empty($modSettings['search_match_words']) ? 'LIKE {string:subject_not_' . $count . '}' : '= {string:subject_not_' . $count . '}') . ' AND subj' . $numTables . '.id_topic = t.id_topic)'; |
||
1402 | $subject_query['params']['subject_not_' . $count] = empty($modSettings['search_match_words']) ? '%' . $subjectWord . '%' : $subjectWord; |
||
1403 | |||
1404 | $subject_query['where'][] = '(subj' . $numTables . '.word IS NULL)'; |
||
1405 | $subject_query['where'][] = 'm.body NOT ' . $query_match_type . ' {string:body_not_' . $count . '}'; |
||
1406 | |||
1407 | if ($is_search_regex) |
||
1408 | $subject_query['params']['body_not_' . $count++] = $word_boundary_wrapper($escape_sql_regex($subjectWord)); |
||
1409 | else |
||
1410 | $subject_query['params']['body_not_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($subjectWord) . '%'; |
||
1411 | } |
||
1412 | else |
||
1413 | { |
||
1414 | $subject_query['inner_join'][] = '{db_prefix}log_search_subjects AS subj' . $numTables . ' ON (subj' . $numTables . '.id_topic = ' . ($prev_join === 0 ? 't' : 'subj' . $prev_join) . '.id_topic)'; |
||
1415 | $subject_query['where'][] = 'subj' . $numTables . '.word LIKE {string:subject_like_' . $count . '}'; |
||
1416 | $subject_query['params']['subject_like_' . $count++] = empty($modSettings['search_match_words']) ? '%' . $subjectWord . '%' : $subjectWord; |
||
1417 | $prev_join = $numTables; |
||
1418 | } |
||
1419 | } |
||
1420 | |||
1421 | if (!empty($userQuery)) |
||
1422 | { |
||
1423 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
1424 | { |
||
1425 | $subject_query['inner_join']['m'] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
1426 | } |
||
1427 | $subject_query['where'][] = '{raw:user_query}'; |
||
1428 | $subject_query['params']['user_query'] = $userQuery; |
||
1429 | } |
||
1430 | if (!empty($search_params['topic'])) |
||
1431 | { |
||
1432 | $subject_query['where'][] = 't.id_topic = {int:topic}'; |
||
1433 | $subject_query['params']['topic'] = $search_params['topic']; |
||
1434 | } |
||
1435 | if (!empty($minMsgID)) |
||
1436 | { |
||
1437 | $subject_query['where'][] = 't.id_first_msg >= {int:min_msg_id}'; |
||
1438 | $subject_query['params']['min_msg_id'] = $minMsgID; |
||
1439 | } |
||
1440 | if (!empty($maxMsgID)) |
||
1441 | { |
||
1442 | $subject_query['where'][] = 't.id_last_msg <= {int:max_msg_id}'; |
||
1443 | $subject_query['params']['max_msg_id'] = $maxMsgID; |
||
1444 | } |
||
1445 | if (!empty($boardQuery)) |
||
1446 | { |
||
1447 | $subject_query['where'][] = 't.id_board {raw:board_query}'; |
||
1448 | $subject_query['params']['board_query'] = $boardQuery; |
||
1449 | } |
||
1450 | if (!empty($excludedPhrases)) |
||
1451 | { |
||
1452 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
1453 | { |
||
1454 | $subject_query['inner_join']['m'] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
1455 | } |
||
1456 | $count = 0; |
||
1457 | foreach ($excludedPhrases as $phrase) |
||
1458 | { |
||
1459 | $subject_query['where'][] = 'm.subject NOT ' . $query_match_type . ' {string:exclude_phrase_' . $count . '}'; |
||
1460 | $subject_query['where'][] = 'm.body NOT ' . $query_match_type . ' {string:exclude_phrase_' . $count . '}'; |
||
1461 | |||
1462 | if ($is_search_regex) |
||
1463 | $subject_query['params']['exclude_phrase_' . $count++] = $word_boundary_wrapper($escape_sql_regex($phrase)); |
||
1464 | else |
||
1465 | $subject_query['params']['exclude_phrase_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($phrase) . '%'; |
||
1466 | } |
||
1467 | } |
||
1468 | call_integration_hook('integrate_subject_search_query', array(&$subject_query)); |
||
1469 | |||
1470 | // Nothing to search for? |
||
1471 | if (empty($subject_query['where'])) |
||
1472 | continue; |
||
1473 | |||
1474 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_topics', ($smcFunc['db_support_ignore'] ? (' |
||
1475 | INSERT IGNORE INTO {db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics |
||
1476 | (' . ($createTemporary ? '' : 'id_search, ') . 'id_topic)') : '') . ' |
||
1477 | SELECT ' . ($createTemporary ? '' : $_SESSION['search_cache']['id_search'] . ', ') . 't.id_topic |
||
1478 | FROM ' . $subject_query['from'] . (empty($subject_query['inner_join']) ? '' : ' |
||
1479 | INNER JOIN ' . implode(' |
||
1480 | INNER JOIN ', $subject_query['inner_join'])) . (empty($subject_query['left_join']) ? '' : ' |
||
1481 | LEFT JOIN ' . implode(' |
||
1482 | LEFT JOIN ', $subject_query['left_join'])) . ' |
||
1483 | WHERE ' . implode(' |
||
1484 | AND ', $subject_query['where']) . (empty($modSettings['search_max_results']) ? '' : ' |
||
1485 | LIMIT ' . ($modSettings['search_max_results'] - $numSubjectResults)), |
||
1486 | $subject_query['params'] |
||
1487 | ); |
||
1488 | // Don't do INSERT IGNORE? Manually fix this up! |
||
1489 | if (!$smcFunc['db_support_ignore']) |
||
1490 | { |
||
1491 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1492 | { |
||
1493 | $ind = $createTemporary ? 0 : 1; |
||
1494 | // No duplicates! |
||
1495 | if (isset($inserts[$row[$ind]])) |
||
1496 | continue; |
||
1497 | |||
1498 | $inserts[$row[$ind]] = $row; |
||
1499 | } |
||
1500 | $smcFunc['db_free_result']($ignoreRequest); |
||
1501 | $numSubjectResults = count($inserts); |
||
1502 | } |
||
1503 | else |
||
1504 | $numSubjectResults += $smcFunc['db_affected_rows'](); |
||
1505 | |||
1506 | if (!empty($modSettings['search_max_results']) && $numSubjectResults >= $modSettings['search_max_results']) |
||
1507 | break; |
||
1508 | } |
||
1509 | |||
1510 | // Got some non-MySQL data to plonk in? |
||
1511 | if (!empty($inserts)) |
||
1512 | { |
||
1513 | $smcFunc['db_insert']('', |
||
1514 | ('{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics'), |
||
1515 | $createTemporary ? array('id_topic' => 'int') : array('id_search' => 'int', 'id_topic' => 'int'), |
||
1516 | $inserts, |
||
1517 | $createTemporary ? array('id_topic') : array('id_search', 'id_topic') |
||
1518 | ); |
||
1519 | } |
||
1520 | |||
1521 | if ($numSubjectResults !== 0) |
||
1522 | { |
||
1523 | $main_query['weights']['subject']['search'] = 'CASE WHEN MAX(lst.id_topic) IS NULL THEN 0 ELSE 1 END'; |
||
1524 | $main_query['left_join'][] = '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics AS lst ON (' . ($createTemporary ? '' : 'lst.id_search = {int:id_search} AND ') . 'lst.id_topic = t.id_topic)'; |
||
1525 | if (!$createTemporary) |
||
1526 | $main_query['parameters']['id_search'] = $_SESSION['search_cache']['id_search']; |
||
1527 | } |
||
1528 | } |
||
1529 | |||
1530 | $indexedResults = 0; |
||
1531 | // We building an index? |
||
1532 | if ($searchAPI->supportsMethod('indexedWordQuery', $query_params)) |
||
1533 | { |
||
1534 | $inserts = array(); |
||
1535 | $smcFunc['db_search_query']('drop_tmp_log_search_messages', ' |
||
1536 | DROP TABLE IF EXISTS {db_prefix}tmp_log_search_messages', |
||
1537 | array( |
||
1538 | ) |
||
1539 | ); |
||
1540 | |||
1541 | $createTemporary = $smcFunc['db_search_query']('create_tmp_log_search_messages', ' |
||
1542 | CREATE TEMPORARY TABLE {db_prefix}tmp_log_search_messages ( |
||
1543 | id_msg int NOT NULL default {string:string_zero}, |
||
1544 | PRIMARY KEY (id_msg) |
||
1545 | ) ENGINE=MEMORY', |
||
1546 | array( |
||
1547 | 'string_zero' => '0', |
||
1548 | ) |
||
1549 | ) !== false; |
||
1550 | |||
1551 | // Clear, all clear! |
||
1552 | if (!$createTemporary) |
||
1553 | $smcFunc['db_search_query']('delete_log_search_messages', ' |
||
1554 | DELETE FROM {db_prefix}log_search_messages |
||
1555 | WHERE id_search = {int:id_search}', |
||
1556 | array( |
||
1557 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1558 | ) |
||
1559 | ); |
||
1560 | |||
1561 | foreach ($searchWords as $orIndex => $words) |
||
1562 | { |
||
1563 | // Search for this word, assuming we have some words! |
||
1564 | if (!empty($words['indexed_words'])) |
||
1565 | { |
||
1566 | // Variables required for the search. |
||
1567 | $search_data = array( |
||
1568 | 'insert_into' => ($createTemporary ? 'tmp_' : '') . 'log_search_messages', |
||
1569 | 'no_regexp' => $no_regexp, |
||
1570 | 'max_results' => $maxMessageResults, |
||
1571 | 'indexed_results' => $indexedResults, |
||
1572 | 'params' => array( |
||
1573 | 'id_search' => !$createTemporary ? $_SESSION['search_cache']['id_search'] : 0, |
||
1574 | 'excluded_words' => $excludedWords, |
||
1575 | 'user_query' => !empty($userQuery) ? $userQuery : '', |
||
1576 | 'board_query' => !empty($boardQuery) ? $boardQuery : '', |
||
1577 | 'topic' => !empty($search_params['topic']) ? $search_params['topic'] : 0, |
||
1578 | 'min_msg_id' => !empty($minMsgID) ? $minMsgID : 0, |
||
1579 | 'max_msg_id' => !empty($maxMsgID) ? $maxMsgID : 0, |
||
1580 | 'excluded_phrases' => !empty($excludedPhrases) ? $excludedPhrases : array(), |
||
1581 | 'excluded_index_words' => !empty($excludedIndexWords) ? $excludedIndexWords : array(), |
||
1582 | 'excluded_subject_words' => !empty($excludedSubjectWords) ? $excludedSubjectWords : array(), |
||
1583 | ), |
||
1584 | ); |
||
1585 | |||
1586 | $ignoreRequest = $searchAPI->indexedWordQuery($words, $search_data); |
||
1587 | |||
1588 | if (!$smcFunc['db_support_ignore']) |
||
1589 | { |
||
1590 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1591 | { |
||
1592 | // No duplicates! |
||
1593 | if (isset($inserts[$row[0]])) |
||
1594 | continue; |
||
1595 | |||
1596 | $inserts[$row[0]] = $row; |
||
1597 | } |
||
1598 | $smcFunc['db_free_result']($ignoreRequest); |
||
1599 | $indexedResults = count($inserts); |
||
1600 | } |
||
1601 | else |
||
1602 | $indexedResults += $smcFunc['db_affected_rows'](); |
||
1603 | |||
1604 | if (!empty($maxMessageResults) && $indexedResults >= $maxMessageResults) |
||
1605 | break; |
||
1606 | } |
||
1607 | } |
||
1608 | |||
1609 | // More non-MySQL stuff needed? |
||
1610 | if (!empty($inserts)) |
||
1611 | { |
||
1612 | $smcFunc['db_insert']('', |
||
1613 | '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_messages', |
||
1614 | $createTemporary ? array('id_msg' => 'int') : array('id_msg' => 'int', 'id_search' => 'int'), |
||
1615 | $inserts, |
||
1616 | $createTemporary ? array('id_msg') : array('id_msg', 'id_search') |
||
1617 | ); |
||
1618 | } |
||
1619 | |||
1620 | if (empty($indexedResults) && empty($numSubjectResults) && !empty($modSettings['search_force_index'])) |
||
1621 | { |
||
1622 | $context['search_errors']['query_not_specific_enough'] = true; |
||
1623 | $_REQUEST['params'] = $context['params']; |
||
1624 | return PlushSearch1(); |
||
1625 | } |
||
1626 | elseif (!empty($indexedResults)) |
||
1627 | { |
||
1628 | $main_query['inner_join'][] = '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_messages AS lsm ON (lsm.id_msg = m.id_msg)'; |
||
1629 | if (!$createTemporary) |
||
1630 | { |
||
1631 | $main_query['where'][] = 'lsm.id_search = {int:id_search}'; |
||
1632 | $main_query['parameters']['id_search'] = $_SESSION['search_cache']['id_search']; |
||
1633 | } |
||
1634 | } |
||
1635 | } |
||
1636 | |||
1637 | // Not using an index? All conditions have to be carried over. |
||
1638 | else |
||
1639 | { |
||
1640 | $orWhere = array(); |
||
1641 | $count = 0; |
||
1642 | foreach ($searchWords as $orIndex => $words) |
||
1643 | { |
||
1644 | $where = array(); |
||
1645 | foreach ($words['all_words'] as $regularWord) |
||
1646 | { |
||
1647 | if (in_array($regularWord, $excludedWords)) |
||
1648 | { |
||
1649 | $where[] = 'm.subject NOT ' . $query_match_type . ' {string:all_word_body_' . $count . '}'; |
||
1650 | $where[] = 'm.body NOT ' . $query_match_type . ' {string:all_word_body_' . $count . '}'; |
||
1651 | } |
||
1652 | else |
||
1653 | $where[] = 'm.body ' . $query_match_type . ' {string:all_word_body_' . $count . '}'; |
||
1654 | |||
1655 | if ($is_search_regex) |
||
1656 | $main_query['parameters']['all_word_body_' . $count++] = $word_boundary_wrapper($escape_sql_regex($regularWord)); |
||
1657 | else |
||
1658 | $main_query['parameters']['all_word_body_' . $count++] = '%' . $smcFunc['db_escape_wildcard_string']($regularWord) . '%'; |
||
1659 | } |
||
1660 | if (!empty($where)) |
||
1661 | $orWhere[] = count($where) > 1 ? '(' . implode(' AND ', $where) . ')' : $where[0]; |
||
1662 | } |
||
1663 | if (!empty($orWhere)) |
||
1664 | $main_query['where'][] = count($orWhere) > 1 ? '(' . implode(' OR ', $orWhere) . ')' : $orWhere[0]; |
||
1665 | |||
1666 | if (!empty($userQuery)) |
||
1667 | { |
||
1668 | $main_query['where'][] = '{raw:user_query}'; |
||
1669 | $main_query['parameters']['user_query'] = $userQuery; |
||
1670 | } |
||
1671 | if (!empty($search_params['topic'])) |
||
1672 | { |
||
1673 | $main_query['where'][] = 'm.id_topic = {int:topic}'; |
||
1674 | $main_query['parameters']['topic'] = $search_params['topic']; |
||
1675 | } |
||
1676 | if (!empty($minMsgID)) |
||
1677 | { |
||
1678 | $main_query['where'][] = 'm.id_msg >= {int:min_msg_id}'; |
||
1679 | $main_query['parameters']['min_msg_id'] = $minMsgID; |
||
1680 | } |
||
1681 | if (!empty($maxMsgID)) |
||
1682 | { |
||
1683 | $main_query['where'][] = 'm.id_msg <= {int:max_msg_id}'; |
||
1684 | $main_query['parameters']['max_msg_id'] = $maxMsgID; |
||
1685 | } |
||
1686 | if (!empty($boardQuery)) |
||
1687 | { |
||
1688 | $main_query['where'][] = 'm.id_board {raw:board_query}'; |
||
1689 | $main_query['parameters']['board_query'] = $boardQuery; |
||
1690 | } |
||
1691 | } |
||
1692 | call_integration_hook('integrate_main_search_query', array(&$main_query)); |
||
1693 | |||
1694 | // Did we either get some indexed results, or otherwise did not do an indexed query? |
||
1695 | if (!empty($indexedResults) || !$searchAPI->supportsMethod('indexedWordQuery', $query_params)) |
||
1696 | { |
||
1697 | $relevance = '1000 * ('; |
||
1698 | $new_weight_total = 0; |
||
1699 | foreach ($main_query['weights'] as $type => $value) |
||
1700 | { |
||
1701 | $relevance .= $weight[$type]; |
||
1702 | if (!empty($value['search'])) |
||
1703 | $relevance .= ' * ' . $value['search']; |
||
1704 | $relevance .= ' + '; |
||
1705 | $new_weight_total += $weight[$type]; |
||
1706 | } |
||
1707 | $main_query['select']['relevance'] = substr($relevance, 0, -3) . ') / ' . $new_weight_total . ' AS relevance'; |
||
1708 | |||
1709 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_no_index', ($smcFunc['db_support_ignore'] ? (' |
||
1710 | INSERT IGNORE INTO ' . '{db_prefix}log_search_results |
||
1711 | (' . implode(', ', array_keys($main_query['select'])) . ')') : '') . ' |
||
1712 | SELECT |
||
1713 | ' . implode(', |
||
1714 | ', $main_query['select']) . ' |
||
1715 | FROM ' . $main_query['from'] . (empty($main_query['inner_join']) ? '' : ' |
||
1716 | INNER JOIN ' . implode(' |
||
1717 | INNER JOIN ', $main_query['inner_join'])) . (empty($main_query['left_join']) ? '' : ' |
||
1718 | LEFT JOIN ' . implode(' |
||
1719 | LEFT JOIN ', $main_query['left_join'])) . (!empty($main_query['where']) ? ' |
||
1720 | WHERE ' : '') . implode(' |
||
1721 | AND ', $main_query['where']) . (empty($main_query['group_by']) ? '' : ' |
||
1722 | GROUP BY ' . implode(', ', $main_query['group_by'])) . (empty($modSettings['search_max_results']) ? '' : ' |
||
1723 | LIMIT ' . $modSettings['search_max_results']), |
||
1724 | $main_query['parameters'] |
||
1725 | ); |
||
1726 | |||
1727 | // We love to handle non-good databases that don't support our ignore! |
||
1728 | if (!$smcFunc['db_support_ignore']) |
||
1729 | { |
||
1730 | $inserts = array(); |
||
1731 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1732 | { |
||
1733 | // No duplicates! |
||
1734 | if (isset($inserts[$row[2]])) |
||
1735 | continue; |
||
1736 | |||
1737 | foreach ($row as $key => $value) |
||
1738 | $inserts[$row[2]][] = (int) $row[$key]; |
||
1739 | } |
||
1740 | $smcFunc['db_free_result']($ignoreRequest); |
||
1741 | |||
1742 | // Now put them in! |
||
1743 | if (!empty($inserts)) |
||
1744 | { |
||
1745 | $query_columns = array(); |
||
1746 | foreach ($main_query['select'] as $k => $v) |
||
1747 | $query_columns[$k] = 'int'; |
||
1748 | |||
1749 | $smcFunc['db_insert']('', |
||
1750 | '{db_prefix}log_search_results', |
||
1751 | $query_columns, |
||
1752 | $inserts, |
||
1753 | array('id_search', 'id_topic') |
||
1754 | ); |
||
1755 | } |
||
1756 | $_SESSION['search_cache']['num_results'] += count($inserts); |
||
1757 | } |
||
1758 | else |
||
1759 | $_SESSION['search_cache']['num_results'] = $smcFunc['db_affected_rows'](); |
||
1760 | } |
||
1761 | |||
1762 | // Insert subject-only matches. |
||
1763 | if ($_SESSION['search_cache']['num_results'] < $modSettings['search_max_results'] && $numSubjectResults !== 0) |
||
1764 | { |
||
1765 | $relevance = '1000 * ('; |
||
1766 | foreach ($weight_factors as $type => $value) |
||
1767 | if (isset($value['results'])) |
||
1768 | { |
||
1769 | $relevance .= $weight[$type]; |
||
1770 | if (!empty($value['results'])) |
||
1771 | $relevance .= ' * ' . $value['results']; |
||
1772 | $relevance .= ' + '; |
||
1773 | } |
||
1774 | $relevance = substr($relevance, 0, -3) . ') / ' . $weight_total . ' AS relevance'; |
||
1775 | |||
1776 | $usedIDs = array_flip(empty($inserts) ? array() : array_keys($inserts)); |
||
1777 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_sub_only', ($smcFunc['db_support_ignore'] ? (' |
||
1778 | INSERT IGNORE INTO {db_prefix}log_search_results |
||
1779 | (id_search, id_topic, relevance, id_msg, num_matches)') : '') . ' |
||
1780 | SELECT |
||
1781 | {int:id_search}, |
||
1782 | t.id_topic, |
||
1783 | ' . $relevance . ', |
||
1784 | t.id_first_msg, |
||
1785 | 1 |
||
1786 | FROM {db_prefix}topics AS t |
||
1787 | INNER JOIN {db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics AS lst ON (lst.id_topic = t.id_topic)' |
||
1788 | . ($createTemporary ? '' : ' WHERE lst.id_search = {int:id_search}') |
||
1789 | . (empty($modSettings['search_max_results']) ? '' : ' |
||
1790 | LIMIT ' . ($modSettings['search_max_results'] - $_SESSION['search_cache']['num_results'])), |
||
1791 | array( |
||
1792 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1793 | 'min_msg' => $minMsg, |
||
1794 | 'recent_message' => $recentMsg, |
||
1795 | 'huge_topic_posts' => $humungousTopicPosts, |
||
1796 | ) |
||
1797 | ); |
||
1798 | // Once again need to do the inserts if the database don't support ignore! |
||
1799 | if (!$smcFunc['db_support_ignore']) |
||
1800 | { |
||
1801 | $inserts = array(); |
||
1802 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1803 | { |
||
1804 | // No duplicates! |
||
1805 | if (isset($usedIDs[$row[1]])) |
||
1806 | continue; |
||
1807 | |||
1808 | $usedIDs[$row[1]] = true; |
||
1809 | $inserts[] = $row; |
||
1810 | } |
||
1811 | $smcFunc['db_free_result']($ignoreRequest); |
||
1812 | |||
1813 | // Now put them in! |
||
1814 | if (!empty($inserts)) |
||
1815 | { |
||
1816 | $smcFunc['db_insert']('', |
||
1817 | '{db_prefix}log_search_results', |
||
1818 | array('id_search' => 'int', 'id_topic' => 'int', 'relevance' => 'float', 'id_msg' => 'int', 'num_matches' => 'int'), |
||
1819 | $inserts, |
||
1820 | array('id_search', 'id_topic') |
||
1821 | ); |
||
1822 | } |
||
1823 | $_SESSION['search_cache']['num_results'] += count($inserts); |
||
1824 | } |
||
1825 | else |
||
1826 | $_SESSION['search_cache']['num_results'] += $smcFunc['db_affected_rows'](); |
||
1827 | } |
||
1828 | elseif ($_SESSION['search_cache']['num_results'] == -1) |
||
1829 | $_SESSION['search_cache']['num_results'] = 0; |
||
1830 | } |
||
1831 | } |
||
1832 | |||
1833 | $approve_query = ''; |
||
1834 | if (!empty($modSettings['postmod_active'])) |
||
1835 | { |
||
1836 | // Exclude unapproved topics, but show ones they started. |
||
1837 | if (empty($user_info['mod_cache']['ap'])) |
||
1838 | $approve_query = ' |
||
1839 | AND (t.approved = {int:is_approved} OR t.id_member_started = {int:current_member})'; |
||
1840 | |||
1841 | // Show unapproved topics in boards they have access to. |
||
1842 | elseif ($user_info['mod_cache']['ap'] !== array(0)) |
||
1843 | $approve_query = ' |
||
1844 | AND (t.approved = {int:is_approved} OR t.id_member_started = {int:current_member} OR t.id_board IN ({array_int:approve_boards}))'; |
||
1845 | } |
||
1846 | |||
1847 | // *** Retrieve the results to be shown on the page |
||
1848 | $participants = array(); |
||
1849 | $request = $smcFunc['db_search_query']('', ' |
||
1850 | SELECT lsr.id_topic, lsr.id_msg, lsr.relevance, lsr.num_matches |
||
1851 | FROM {db_prefix}log_search_results AS lsr' . ($search_params['sort'] == 'num_replies' || !empty($approve_query) ? ' |
||
1852 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = lsr.id_topic)' : '') . ' |
||
1853 | WHERE lsr.id_search = {int:id_search}' . $approve_query . ' |
||
1854 | ORDER BY {raw:sort} {raw:sort_dir} |
||
1855 | LIMIT {int:start}, {int:max}', |
||
1856 | array( |
||
1857 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1858 | 'sort' => $search_params['sort'], |
||
1859 | 'sort_dir' => $search_params['sort_dir'], |
||
1860 | 'start' => $_REQUEST['start'], |
||
1861 | 'max' => $modSettings['search_results_per_page'], |
||
1862 | 'is_approved' => 1, |
||
1863 | 'current_member' => $user_info['id'], |
||
1864 | 'approve_boards' => !empty($modSettings['postmod_active']) ? $user_info['mod_cache']['ap'] : array(), |
||
1865 | ) |
||
1866 | ); |
||
1867 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1868 | { |
||
1869 | $context['topics'][$row['id_msg']] = array( |
||
1870 | 'relevance' => round($row['relevance'] / 10, 1) . '%', |
||
1871 | 'num_matches' => $row['num_matches'], |
||
1872 | 'matches' => array(), |
||
1873 | ); |
||
1874 | // By default they didn't participate in the topic! |
||
1875 | $participants[$row['id_topic']] = false; |
||
1876 | } |
||
1877 | $smcFunc['db_free_result']($request); |
||
1878 | } |
||
1879 | |||
1880 | $num_results = 0; |
||
1881 | if (!empty($context['topics'])) |
||
1882 | { |
||
1883 | // Create an array for the permissions. |
||
1884 | $perms = array('post_reply_own', 'post_reply_any'); |
||
1885 | |||
1886 | if (!empty($options['display_quick_mod'])) |
||
1887 | $perms = array_merge($perms, array('lock_any', 'lock_own', 'make_sticky', 'move_any', 'move_own', 'remove_any', 'remove_own', 'merge_any')); |
||
1888 | |||
1889 | $boards_can = boardsAllowedTo($perms, true, false); |
||
1890 | |||
1891 | // How's about some quick moderation? |
||
1892 | if (!empty($options['display_quick_mod'])) |
||
1893 | { |
||
1894 | $context['can_lock'] = in_array(0, $boards_can['lock_any']); |
||
1895 | $context['can_sticky'] = in_array(0, $boards_can['make_sticky']); |
||
1896 | $context['can_move'] = in_array(0, $boards_can['move_any']); |
||
1897 | $context['can_remove'] = in_array(0, $boards_can['remove_any']); |
||
1898 | $context['can_merge'] = in_array(0, $boards_can['merge_any']); |
||
1899 | } |
||
1900 | |||
1901 | $approve_query = ''; |
||
1902 | if (!empty($modSettings['postmod_active'])) |
||
1903 | { |
||
1904 | if (empty($user_info['mod_cache']['ap'])) |
||
1905 | $approve_query = ' |
||
1906 | AND (m.approved = {int:is_approved} OR m.id_member = {int:current_member})'; |
||
1907 | |||
1908 | elseif ($user_info['mod_cache']['ap'] !== array(0)) |
||
1909 | $approve_query = ' |
||
1910 | AND (m.approved = {int:is_approved} OR m.id_member = {int:current_member} OR m.id_board IN ({array_int:approve_boards}))'; |
||
1911 | } |
||
1912 | |||
1913 | // What messages are we using? |
||
1914 | $msg_list = array_keys($context['topics']); |
||
1915 | |||
1916 | // Load the posters... |
||
1917 | $request = $smcFunc['db_query']('', ' |
||
1918 | SELECT id_member |
||
1919 | FROM {db_prefix}messages |
||
1920 | WHERE id_member != {int:no_member} |
||
1921 | AND id_msg IN ({array_int:message_list}) |
||
1922 | LIMIT {int:limit}', |
||
1923 | array( |
||
1924 | 'message_list' => $msg_list, |
||
1925 | 'no_member' => 0, |
||
1926 | 'limit' => count($context['topics']), |
||
1927 | ) |
||
1928 | ); |
||
1929 | $posters = array(); |
||
1930 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1931 | $posters[] = $row['id_member']; |
||
1932 | $smcFunc['db_free_result']($request); |
||
1933 | |||
1934 | call_integration_hook('integrate_search_message_list', array(&$msg_list, &$posters)); |
||
1935 | |||
1936 | if (!empty($posters)) |
||
1937 | loadMemberData(array_unique($posters)); |
||
1938 | |||
1939 | // Get the messages out for the callback - select enough that it can be made to look just like Display. |
||
1940 | $messages_request = $smcFunc['db_query']('', ' |
||
1941 | SELECT |
||
1942 | m.id_msg, m.subject, m.poster_name, m.poster_email, m.poster_time, m.id_member, |
||
1943 | m.icon, m.poster_ip, m.body, m.smileys_enabled, m.modified_time, m.modified_name, |
||
1944 | first_m.id_msg AS first_msg, first_m.subject AS first_subject, first_m.icon AS first_icon, first_m.poster_time AS first_poster_time, |
||
1945 | first_mem.id_member AS first_member_id, COALESCE(first_mem.real_name, first_m.poster_name) AS first_member_name, |
||
1946 | last_m.id_msg AS last_msg, last_m.poster_time AS last_poster_time, last_mem.id_member AS last_member_id, |
||
1947 | COALESCE(last_mem.real_name, last_m.poster_name) AS last_member_name, last_m.icon AS last_icon, last_m.subject AS last_subject, |
||
1948 | t.id_topic, t.is_sticky, t.locked, t.id_poll, t.num_replies, t.num_views, |
||
1949 | b.id_board, b.name AS board_name, c.id_cat, c.name AS cat_name |
||
1950 | FROM {db_prefix}messages AS m |
||
1951 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
1952 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) |
||
1953 | INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) |
||
1954 | INNER JOIN {db_prefix}messages AS first_m ON (first_m.id_msg = t.id_first_msg) |
||
1955 | INNER JOIN {db_prefix}messages AS last_m ON (last_m.id_msg = t.id_last_msg) |
||
1956 | LEFT JOIN {db_prefix}members AS first_mem ON (first_mem.id_member = first_m.id_member) |
||
1957 | LEFT JOIN {db_prefix}members AS last_mem ON (last_mem.id_member = first_m.id_member) |
||
1958 | WHERE m.id_msg IN ({array_int:message_list})' . $approve_query . ' |
||
1959 | ORDER BY ' . $smcFunc['db_custom_order']('m.id_msg', $msg_list) . ' |
||
1960 | LIMIT {int:limit}', |
||
1961 | array( |
||
1962 | 'message_list' => $msg_list, |
||
1963 | 'is_approved' => 1, |
||
1964 | 'current_member' => $user_info['id'], |
||
1965 | 'approve_boards' => !empty($modSettings['postmod_active']) ? $user_info['mod_cache']['ap'] : array(), |
||
1966 | 'limit' => count($context['topics']), |
||
1967 | ) |
||
1968 | ); |
||
1969 | |||
1970 | // How many results will the user be able to see? |
||
1971 | if (!empty($_SESSION['search_cache']['num_results'])) |
||
1972 | $num_results = $_SESSION['search_cache']['num_results']; |
||
1973 | else |
||
1974 | $num_results = $smcFunc['db_num_rows']($messages_request); |
||
1975 | |||
1976 | // If there are no results that means the things in the cache got deleted, so pretend we have no topics anymore. |
||
1977 | if ($num_results == 0) |
||
1978 | $context['topics'] = array(); |
||
1979 | |||
1980 | // If we want to know who participated in what then load this now. |
||
1981 | if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest']) |
||
1982 | { |
||
1983 | $result = $smcFunc['db_query']('', ' |
||
1984 | SELECT id_topic |
||
1985 | FROM {db_prefix}messages |
||
1986 | WHERE id_topic IN ({array_int:topic_list}) |
||
1987 | AND id_member = {int:current_member} |
||
1988 | GROUP BY id_topic |
||
1989 | LIMIT {int:limit}', |
||
1990 | array( |
||
1991 | 'current_member' => $user_info['id'], |
||
1992 | 'topic_list' => array_keys($participants), |
||
1993 | 'limit' => count($participants), |
||
1994 | ) |
||
1995 | ); |
||
1996 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
1997 | $participants[$row['id_topic']] = true; |
||
1998 | |||
1999 | $smcFunc['db_free_result']($result); |
||
2000 | } |
||
2001 | } |
||
2002 | |||
2003 | // Now that we know how many results to expect we can start calculating the page numbers. |
||
2004 | $context['page_index'] = constructPageIndex($scripturl . '?action=search2;params=' . $context['params'], $_REQUEST['start'], $num_results, $modSettings['search_results_per_page'], false); |
||
2005 | |||
2006 | // Consider the search complete! |
||
2007 | if (!empty($cache_enable) && $cache_enable >= 2) |
||
2008 | cache_put_data('search_start:' . ($user_info['is_guest'] ? $user_info['ip'] : $user_info['id']), null, 90); |
||
2009 | |||
2010 | $context['key_words'] = &$searchArray; |
||
2011 | |||
2012 | // Setup the default topic icons... for checking they exist and the like! |
||
2013 | $context['icon_sources'] = array(); |
||
2014 | foreach ($context['stable_icons'] as $icon) |
||
2015 | $context['icon_sources'][$icon] = 'images_url'; |
||
2016 | |||
2017 | $context['sub_template'] = 'results'; |
||
2018 | $context['page_title'] = $txt['search_results']; |
||
2019 | $context['get_topics'] = 'prepareSearchContext'; |
||
2020 | $context['can_restore_perm'] = allowedTo('move_any') && !empty($modSettings['recycle_enable']); |
||
2021 | $context['can_restore'] = false; // We won't know until we handle the context later whether we can actually restore... |
||
2022 | |||
2023 | $context['jump_to'] = array( |
||
2024 | 'label' => addslashes(un_htmlspecialchars($txt['jump_to'])), |
||
2025 | 'board_name' => addslashes(un_htmlspecialchars($txt['select_destination'])), |
||
2026 | ); |
||
2385 | ?> |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.