Conditions | 384 |
Paths | 4 |
Total Lines | 1754 |
Code Lines | 940 |
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 |
||
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 | |||
692 | // Extract phrase parts first (e.g. some words "this is a phrase" some more words.) |
||
693 | preg_match_all('/(?:^|\s)([-]?)"([^"]+)"(?:$|\s)/', $stripped_query, $matches, PREG_PATTERN_ORDER); |
||
694 | |||
695 | $phraseArray = $matches[2]; |
||
696 | |||
697 | // Remove the phrase parts and extract the words. |
||
698 | $wordArray = preg_replace('~(?:^|\s)(?:[-]?)"(?:[^"]+)"(?:$|\s)~' . ($context['utf8'] ? 'u' : ''), ' ', $search_params['search']); |
||
699 | |||
700 | $wordArray = explode(' ', $smcFunc['htmlspecialchars'](un_htmlspecialchars($wordArray), ENT_QUOTES)); |
||
701 | |||
702 | // A minus sign in front of a word excludes the word.... so... |
||
703 | $excludedWords = array(); |
||
704 | $excludedIndexWords = array(); |
||
705 | $excludedSubjectWords = array(); |
||
706 | $excludedPhrases = array(); |
||
707 | |||
708 | // .. first, we check for things like -"some words", but not "-some words". |
||
709 | foreach ($matches[1] as $index => $word) |
||
710 | { |
||
711 | if ($word === '-') |
||
712 | { |
||
713 | if (($word = trim($phraseArray[$index], '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) |
||
714 | $excludedWords[] = $word; |
||
715 | unset($phraseArray[$index]); |
||
716 | } |
||
717 | } |
||
718 | |||
719 | // Now we look for -test, etc.... normaller. |
||
720 | foreach ($wordArray as $index => $word) |
||
721 | { |
||
722 | if (strpos(trim($word), '-') === 0) |
||
723 | { |
||
724 | if (($word = trim($word, '-_\' ')) !== '' && !in_array($word, $blacklisted_words)) |
||
725 | $excludedWords[] = $word; |
||
726 | |||
727 | unset($wordArray[$index]); |
||
728 | } |
||
729 | } |
||
730 | |||
731 | // The remaining words and phrases are all included. |
||
732 | $searchArray = array_merge($phraseArray, $wordArray); |
||
733 | |||
734 | $context['search_ignored'] = array(); |
||
735 | // Trim everything and make sure there are no words that are the same. |
||
736 | foreach ($searchArray as $index => $value) |
||
737 | { |
||
738 | // Skip anything practically empty. |
||
739 | if (($searchArray[$index] = trim($value, '-_\' ')) === '') |
||
740 | { |
||
741 | unset($searchArray[$index]); |
||
742 | } |
||
743 | // Skip blacklisted words. Make sure to note we skipped them in case we end up with nothing. |
||
744 | elseif (in_array($searchArray[$index], $blacklisted_words)) |
||
745 | { |
||
746 | $foundBlackListedWords = true; |
||
747 | unset($searchArray[$index]); |
||
748 | } |
||
749 | // Don't allow very, very short words. |
||
750 | elseif ($smcFunc['strlen']($value) < 2) |
||
751 | { |
||
752 | $context['search_ignored'][] = $value; |
||
753 | unset($searchArray[$index]); |
||
754 | } |
||
755 | } |
||
756 | $searchArray = array_slice(array_unique($searchArray), 0, 10); |
||
757 | |||
758 | // Create an array of replacements for highlighting. |
||
759 | $context['mark'] = array(); |
||
760 | |||
761 | foreach ($searchArray as $word) |
||
762 | $context['mark'][$word] = '<strong class="highlight">' . $word . '</strong>'; |
||
763 | |||
764 | // Initialize two arrays storing the words that have to be searched for. |
||
765 | $orParts = array(); |
||
766 | $searchWords = array(); |
||
767 | |||
768 | // Make sure at least one word is being searched for. |
||
769 | if (empty($searchArray)) |
||
770 | { |
||
771 | $context['search_errors']['invalid_search_string' . (!empty($foundBlackListedWords) ? '_blacklist' : '')] = true; |
||
772 | } |
||
773 | // All words/sentences must match. |
||
774 | elseif (empty($search_params['searchtype'])) |
||
775 | { |
||
776 | $orParts[0] = $searchArray; |
||
777 | } |
||
778 | // Any word/sentence must match. |
||
779 | else |
||
780 | { |
||
781 | foreach ($searchArray as $index => $value) |
||
782 | $orParts[$index] = array($value); |
||
783 | } |
||
784 | |||
785 | // Don't allow duplicate error messages if one string is too short. |
||
786 | if (isset($context['search_errors']['search_string_small_words'], $context['search_errors']['invalid_search_string'])) |
||
787 | unset($context['search_errors']['invalid_search_string']); |
||
788 | |||
789 | // Make sure the excluded words are in all or-branches. |
||
790 | foreach ($orParts as $orIndex => $andParts) |
||
791 | foreach ($excludedWords as $word) |
||
792 | $orParts[$orIndex][] = $word; |
||
793 | |||
794 | // Determine the or-branches and the fulltext search words. |
||
795 | foreach ($orParts as $orIndex => $andParts) |
||
796 | { |
||
797 | $searchWords[$orIndex] = array( |
||
798 | 'indexed_words' => array(), |
||
799 | 'words' => array(), |
||
800 | 'subject_words' => array(), |
||
801 | 'all_words' => array(), |
||
802 | 'complex_words' => array(), |
||
803 | ); |
||
804 | |||
805 | // Sort the indexed words (large words -> small words -> excluded words). |
||
806 | if ($searchAPI->supportsMethod('searchSort')) |
||
807 | usort($orParts[$orIndex], 'searchSort'); |
||
808 | |||
809 | foreach ($orParts[$orIndex] as $word) |
||
810 | { |
||
811 | $is_excluded = in_array($word, $excludedWords); |
||
812 | |||
813 | $searchWords[$orIndex]['all_words'][] = $word; |
||
814 | |||
815 | $subjectWords = text2words($word); |
||
816 | if (!$is_excluded || count($subjectWords) === 1) |
||
817 | { |
||
818 | $searchWords[$orIndex]['subject_words'] = array_merge($searchWords[$orIndex]['subject_words'], $subjectWords); |
||
819 | if ($is_excluded) |
||
820 | $excludedSubjectWords = array_merge($excludedSubjectWords, $subjectWords); |
||
821 | } |
||
822 | else |
||
823 | $excludedPhrases[] = $word; |
||
824 | |||
825 | // Have we got indexes to prepare? |
||
826 | if ($searchAPI->supportsMethod('prepareIndexes')) |
||
827 | $searchAPI->prepareIndexes($word, $searchWords[$orIndex], $excludedIndexWords, $is_excluded); |
||
828 | } |
||
829 | |||
830 | // Search_force_index requires all AND parts to have at least one fulltext word. |
||
831 | if (!empty($modSettings['search_force_index']) && empty($searchWords[$orIndex]['indexed_words'])) |
||
832 | { |
||
833 | $context['search_errors']['query_not_specific_enough'] = true; |
||
834 | break; |
||
835 | } |
||
836 | elseif ($search_params['subject_only'] && empty($searchWords[$orIndex]['subject_words']) && empty($excludedSubjectWords)) |
||
837 | { |
||
838 | $context['search_errors']['query_not_specific_enough'] = true; |
||
839 | break; |
||
840 | } |
||
841 | |||
842 | // Make sure we aren't searching for too many indexed words. |
||
843 | else |
||
844 | { |
||
845 | $searchWords[$orIndex]['indexed_words'] = array_slice($searchWords[$orIndex]['indexed_words'], 0, 7); |
||
846 | $searchWords[$orIndex]['subject_words'] = array_slice($searchWords[$orIndex]['subject_words'], 0, 7); |
||
847 | $searchWords[$orIndex]['words'] = array_slice($searchWords[$orIndex]['words'], 0, 4); |
||
848 | } |
||
849 | } |
||
850 | |||
851 | // *** Spell checking |
||
852 | if ($context['show_spellchecking']) |
||
853 | { |
||
854 | require_once($sourcedir . '/Subs-Post.php'); |
||
855 | |||
856 | // Don't hardcode spellchecking functions! |
||
857 | $link = spell_init(); |
||
858 | |||
859 | $did_you_mean = array('search' => array(), 'display' => array()); |
||
860 | $found_misspelling = false; |
||
861 | foreach ($searchArray as $word) |
||
862 | { |
||
863 | if (empty($link)) |
||
864 | continue; |
||
865 | |||
866 | // Don't check phrases. |
||
867 | if (preg_match('~^\w+$~', $word) === 0) |
||
868 | { |
||
869 | $did_you_mean['search'][] = '"' . $word . '"'; |
||
870 | $did_you_mean['display'][] = '"' . $smcFunc['htmlspecialchars']($word) . '"'; |
||
871 | continue; |
||
872 | } |
||
873 | // For some strange reason spell check can crash PHP on decimals. |
||
874 | elseif (preg_match('~\d~', $word) === 1) |
||
875 | { |
||
876 | $did_you_mean['search'][] = $word; |
||
877 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
878 | continue; |
||
879 | } |
||
880 | elseif (spell_check($link, $word)) |
||
881 | { |
||
882 | $did_you_mean['search'][] = $word; |
||
883 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
884 | continue; |
||
885 | } |
||
886 | |||
887 | $suggestions = spell_suggest($link, $word); |
||
888 | foreach ($suggestions as $i => $s) |
||
889 | { |
||
890 | // Search is case insensitive. |
||
891 | if ($smcFunc['strtolower']($s) == $smcFunc['strtolower']($word)) |
||
892 | unset($suggestions[$i]); |
||
893 | |||
894 | // Plus, don't suggest something the user thinks is rude! |
||
895 | elseif ($suggestions[$i] != censorText($s)) |
||
896 | unset($suggestions[$i]); |
||
897 | } |
||
898 | |||
899 | // Anything found? If so, correct it! |
||
900 | if (!empty($suggestions)) |
||
901 | { |
||
902 | $suggestions = array_values($suggestions); |
||
903 | $did_you_mean['search'][] = $suggestions[0]; |
||
904 | $did_you_mean['display'][] = '<em><strong>' . $smcFunc['htmlspecialchars']($suggestions[0]) . '</strong></em>'; |
||
905 | $found_misspelling = true; |
||
906 | } |
||
907 | else |
||
908 | { |
||
909 | $did_you_mean['search'][] = $word; |
||
910 | $did_you_mean['display'][] = $smcFunc['htmlspecialchars']($word); |
||
911 | } |
||
912 | } |
||
913 | |||
914 | if ($found_misspelling) |
||
915 | { |
||
916 | // Don't spell check excluded words, but add them still... |
||
917 | $temp_excluded = array('search' => array(), 'display' => array()); |
||
918 | |||
919 | foreach ($excludedWords as $word) |
||
920 | { |
||
921 | if (preg_match('~^\w+$~', $word) == 0) |
||
922 | { |
||
923 | $temp_excluded['search'][] = '-"' . $word . '"'; |
||
924 | $temp_excluded['display'][] = '-"' . $smcFunc['htmlspecialchars']($word) . '"'; |
||
925 | } |
||
926 | else |
||
927 | { |
||
928 | $temp_excluded['search'][] = '-' . $word; |
||
929 | $temp_excluded['display'][] = '-' . $smcFunc['htmlspecialchars']($word); |
||
930 | } |
||
931 | } |
||
932 | |||
933 | $did_you_mean['search'] = array_merge($did_you_mean['search'], $temp_excluded['search']); |
||
934 | $did_you_mean['display'] = array_merge($did_you_mean['display'], $temp_excluded['display']); |
||
935 | |||
936 | $temp_params = $search_params; |
||
937 | $temp_params['search'] = implode(' ', $did_you_mean['search']); |
||
938 | |||
939 | if (isset($temp_params['brd'])) |
||
940 | $temp_params['brd'] = implode(',', $temp_params['brd']); |
||
941 | |||
942 | $context['params'] = array(); |
||
943 | |||
944 | foreach ($temp_params as $k => $v) |
||
945 | $context['did_you_mean_params'][] = $k . '|\'|' . $v; |
||
946 | |||
947 | $context['did_you_mean_params'] = base64_encode(implode('|"|', $context['did_you_mean_params'])); |
||
948 | $context['did_you_mean'] = implode(' ', $did_you_mean['display']); |
||
949 | } |
||
950 | } |
||
951 | |||
952 | // Let the user adjust the search query, should they wish? |
||
953 | $context['search_params'] = $search_params; |
||
954 | |||
955 | if (isset($context['search_params']['search'])) |
||
956 | $context['search_params']['search'] = $smcFunc['htmlspecialchars']($context['search_params']['search']); |
||
957 | |||
958 | if (isset($context['search_params']['userspec'])) |
||
959 | $context['search_params']['userspec'] = $smcFunc['htmlspecialchars']($context['search_params']['userspec']); |
||
960 | |||
961 | // Do we have captcha enabled? |
||
962 | 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'])) |
||
963 | { |
||
964 | require_once($sourcedir . '/Subs-Editor.php'); |
||
965 | |||
966 | $verificationOptions = array( |
||
967 | 'id' => 'search', |
||
968 | ); |
||
969 | |||
970 | $context['require_verification'] = create_control_verification($verificationOptions, true); |
||
971 | |||
972 | if (is_array($context['require_verification'])) |
||
973 | { |
||
974 | foreach ($context['require_verification'] as $error) |
||
975 | $context['search_errors'][$error] = true; |
||
976 | } |
||
977 | // Don't keep asking for it - they've proven themselves worthy. |
||
978 | else |
||
979 | $_SESSION['ss_vv_passed'] = true; |
||
980 | } |
||
981 | |||
982 | // *** Encode all search params |
||
983 | |||
984 | // All search params have been checked, let's compile them to a single string... made less simple by PHP 4.3.9 and below. |
||
985 | $temp_params = $search_params; |
||
986 | if (isset($temp_params['brd'])) |
||
987 | $temp_params['brd'] = implode(',', $temp_params['brd']); |
||
988 | |||
989 | $context['params'] = array(); |
||
990 | |||
991 | foreach ($temp_params as $k => $v) |
||
992 | $context['params'][] = $k . '|\'|' . $v; |
||
993 | |||
994 | if (!empty($context['params'])) |
||
995 | { |
||
996 | // Due to old IE's 2083 character limit, we have to compress long search strings |
||
997 | $params = @gzcompress(implode('|"|', $context['params'])); |
||
998 | |||
999 | // Gzcompress failed, use try non-gz |
||
1000 | if (empty($params)) |
||
1001 | $params = implode('|"|', $context['params']); |
||
1002 | |||
1003 | // Base64 encode, then replace +/= with uri safe ones that can be reverted |
||
1004 | $context['params'] = str_replace(array('+', '/', '='), array('-', '_', '.'), base64_encode($params)); |
||
1005 | } |
||
1006 | |||
1007 | // ... and add the links to the link tree. |
||
1008 | $context['linktree'][] = array( |
||
1009 | 'url' => $scripturl . '?action=search;params=' . $context['params'], |
||
1010 | 'name' => $txt['search'] |
||
1011 | ); |
||
1012 | $context['linktree'][] = array( |
||
1013 | 'url' => $scripturl . '?action=search2;params=' . $context['params'], |
||
1014 | 'name' => $txt['search_results'] |
||
1015 | ); |
||
1016 | |||
1017 | // *** A last error check |
||
1018 | call_integration_hook('integrate_search_errors'); |
||
1019 | |||
1020 | // One or more search errors? Go back to the first search screen. |
||
1021 | if (!empty($context['search_errors'])) |
||
1022 | { |
||
1023 | $_REQUEST['params'] = $context['params']; |
||
1024 | return PlushSearch1(); |
||
1025 | } |
||
1026 | |||
1027 | // Spam me not, Spam-a-lot? |
||
1028 | if (empty($_SESSION['last_ss']) || $_SESSION['last_ss'] != $search_params['search']) |
||
1029 | spamProtection('search'); |
||
1030 | |||
1031 | // Store the last search string to allow pages of results to be browsed. |
||
1032 | $_SESSION['last_ss'] = $search_params['search']; |
||
1033 | |||
1034 | // *** Reserve an ID for caching the search results. |
||
1035 | $query_params = array_merge($search_params, array( |
||
1036 | 'min_msg_id' => isset($minMsgID) ? (int) $minMsgID : 0, |
||
1037 | 'max_msg_id' => isset($maxMsgID) ? (int) $maxMsgID : 0, |
||
1038 | 'memberlist' => !empty($memberlist) ? $memberlist : array(), |
||
1039 | )); |
||
1040 | |||
1041 | // Can this search rely on the API given the parameters? |
||
1042 | if ($searchAPI->supportsMethod('searchQuery', $query_params)) |
||
1043 | { |
||
1044 | $participants = array(); |
||
1045 | $searchArray = array(); |
||
1046 | |||
1047 | $searchAPI->searchQuery($query_params, $searchWords, $excludedIndexWords, $participants, $searchArray); |
||
1048 | } |
||
1049 | |||
1050 | // Update the cache if the current search term is not yet cached. |
||
1051 | else |
||
1052 | { |
||
1053 | $update_cache = empty($_SESSION['search_cache']) || ($_SESSION['search_cache']['params'] != $context['params']); |
||
1054 | |||
1055 | // Are the result fresh? |
||
1056 | if (!$update_cache && !empty($_SESSION['search_cache']['id_search'])) |
||
1057 | { |
||
1058 | $request = $smcFunc['db_query']('', ' |
||
1059 | SELECT id_search |
||
1060 | FROM {db_prefix}log_search_results |
||
1061 | WHERE id_search = {int:search_id} |
||
1062 | LIMIT 1', |
||
1063 | array( |
||
1064 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
1065 | ) |
||
1066 | ); |
||
1067 | |||
1068 | if ($smcFunc['db_num_rows']($request) === 0) |
||
1069 | $update_cache = true; |
||
1070 | } |
||
1071 | |||
1072 | if ($update_cache) |
||
1073 | { |
||
1074 | // Increase the pointer... |
||
1075 | $modSettings['search_pointer'] = empty($modSettings['search_pointer']) ? 0 : (int) $modSettings['search_pointer']; |
||
1076 | |||
1077 | // ...and store it right off. |
||
1078 | updateSettings(array('search_pointer' => $modSettings['search_pointer'] >= 255 ? 0 : $modSettings['search_pointer'] + 1)); |
||
1079 | |||
1080 | // As long as you don't change the parameters, the cache result is yours. |
||
1081 | $_SESSION['search_cache'] = array( |
||
1082 | 'id_search' => $modSettings['search_pointer'], |
||
1083 | 'num_results' => -1, |
||
1084 | 'params' => $context['params'], |
||
1085 | ); |
||
1086 | |||
1087 | // Clear the previous cache of the final results cache. |
||
1088 | $smcFunc['db_search_query']('delete_log_search_results', ' |
||
1089 | DELETE FROM {db_prefix}log_search_results |
||
1090 | WHERE id_search = {int:search_id}', |
||
1091 | array( |
||
1092 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
1093 | ) |
||
1094 | ); |
||
1095 | |||
1096 | if ($search_params['subject_only']) |
||
1097 | { |
||
1098 | // We do this to try and avoid duplicate keys on databases not supporting INSERT IGNORE. |
||
1099 | $inserts = array(); |
||
1100 | |||
1101 | foreach ($searchWords as $orIndex => $words) |
||
1102 | { |
||
1103 | $subject_query_params = array( |
||
1104 | 'not_redirected' => 0, |
||
1105 | 'never_expires' => 0, |
||
1106 | ); |
||
1107 | $subject_query = array( |
||
1108 | 'from' => '{db_prefix}topics AS t', |
||
1109 | 'inner_join' => array(), |
||
1110 | 'left_join' => array(), |
||
1111 | 'where' => array( |
||
1112 | 't.id_redirect_topic = {int:not_redirected}', |
||
1113 | 't.redirect_expires = {int:never_expires}', |
||
1114 | ), |
||
1115 | ); |
||
1116 | |||
1117 | if ($modSettings['postmod_active']) |
||
1118 | $subject_query['where'][] = 't.approved = {int:is_approved}'; |
||
1119 | |||
1120 | $numTables = 0; |
||
1121 | $prev_join = 0; |
||
1122 | $numSubjectResults = 0; |
||
1123 | |||
1124 | foreach ($words['subject_words'] as $subjectWord) |
||
1125 | { |
||
1126 | $numTables++; |
||
1127 | |||
1128 | if (in_array($subjectWord, $excludedSubjectWords)) |
||
1129 | { |
||
1130 | $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)'; |
||
1131 | |||
1132 | $subject_query['where'][] = '(subj' . $numTables . '.word IS NULL)'; |
||
1133 | } |
||
1134 | else |
||
1135 | { |
||
1136 | $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)'; |
||
1137 | |||
1138 | $subject_query['where'][] = 'subj' . $numTables . '.word ' . (empty($modSettings['search_match_words']) ? 'LIKE {string:subject_words_' . $numTables . '_wild}' : '= {string:subject_words_' . $numTables . '}'); |
||
1139 | |||
1140 | $prev_join = $numTables; |
||
1141 | } |
||
1142 | |||
1143 | $subject_query_params['subject_words_' . $numTables] = $subjectWord; |
||
1144 | $subject_query_params['subject_words_' . $numTables . '_wild'] = '%' . $subjectWord . '%'; |
||
1145 | } |
||
1146 | |||
1147 | if (!empty($userQuery)) |
||
1148 | { |
||
1149 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
1150 | { |
||
1151 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_topic = t.id_topic)'; |
||
1152 | } |
||
1153 | |||
1154 | $subject_query['where'][] = $userQuery; |
||
1155 | } |
||
1156 | |||
1157 | if (!empty($search_params['topic'])) |
||
1158 | $subject_query['where'][] = 't.id_topic = ' . $search_params['topic']; |
||
1159 | |||
1160 | if (!empty($minMsgID)) |
||
1161 | $subject_query['where'][] = 't.id_first_msg >= ' . $minMsgID; |
||
1162 | |||
1163 | if (!empty($maxMsgID)) |
||
1164 | $subject_query['where'][] = 't.id_last_msg <= ' . $maxMsgID; |
||
1165 | |||
1166 | if (!empty($boardQuery)) |
||
1167 | $subject_query['where'][] = 't.id_board ' . $boardQuery; |
||
1168 | |||
1169 | if (!empty($excludedPhrases)) |
||
1170 | { |
||
1171 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
1172 | { |
||
1173 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
1174 | } |
||
1175 | |||
1176 | $count = 0; |
||
1177 | |||
1178 | foreach ($excludedPhrases as $phrase) |
||
1179 | { |
||
1180 | $subject_query['where'][] = 'm.subject NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:excluded_phrases_' . $count . '}'; |
||
1181 | |||
1182 | $subject_query_params['excluded_phrases_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]'; |
||
1183 | } |
||
1184 | } |
||
1185 | |||
1186 | call_integration_hook('integrate_subject_only_search_query', array(&$subject_query, &$subject_query_params)); |
||
1187 | |||
1188 | $relevance = '1000 * ('; |
||
1189 | |||
1190 | foreach ($weight_factors as $type => $value) |
||
1191 | { |
||
1192 | $relevance .= $weight[$type]; |
||
1193 | |||
1194 | if (!empty($value['results'])) |
||
1195 | $relevance .= ' * ' . $value['results']; |
||
1196 | |||
1197 | $relevance .= ' + '; |
||
1198 | } |
||
1199 | |||
1200 | $relevance = substr($relevance, 0, -3) . ') / ' . $weight_total . ' AS relevance'; |
||
1201 | |||
1202 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_subject', |
||
1203 | ($smcFunc['db_support_ignore'] ? ' |
||
1204 | INSERT IGNORE INTO {db_prefix}log_search_results |
||
1205 | (id_search, id_topic, relevance, id_msg, num_matches)' : '') . ' |
||
1206 | SELECT |
||
1207 | {int:id_search}, |
||
1208 | t.id_topic, |
||
1209 | ' . $relevance . ', |
||
1210 | ' . (empty($userQuery) ? 't.id_first_msg' : 'm.id_msg') . ', |
||
1211 | 1 |
||
1212 | FROM ' . $subject_query['from'] . (empty($subject_query['inner_join']) ? '' : ' |
||
1213 | INNER JOIN ' . implode(' |
||
1214 | INNER JOIN ', $subject_query['inner_join'])) . (empty($subject_query['left_join']) ? '' : ' |
||
1215 | LEFT JOIN ' . implode(' |
||
1216 | LEFT JOIN ', $subject_query['left_join'])) . ' |
||
1217 | WHERE ' . implode(' |
||
1218 | AND ', $subject_query['where']) . (empty($modSettings['search_max_results']) ? '' : ' |
||
1219 | LIMIT ' . ($modSettings['search_max_results'] - $numSubjectResults)), |
||
1220 | array_merge($subject_query_params, array( |
||
1221 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1222 | 'min_msg' => $minMsg, |
||
1223 | 'recent_message' => $recentMsg, |
||
1224 | 'huge_topic_posts' => $humungousTopicPosts, |
||
1225 | 'is_approved' => 1, |
||
1226 | )) |
||
1227 | ); |
||
1228 | |||
1229 | // If the database doesn't support IGNORE to make this fast we need to do some tracking. |
||
1230 | if (!$smcFunc['db_support_ignore']) |
||
1231 | { |
||
1232 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1233 | { |
||
1234 | // No duplicates! |
||
1235 | if (isset($inserts[$row[1]])) |
||
1236 | continue; |
||
1237 | |||
1238 | foreach ($row as $key => $value) |
||
1239 | $inserts[$row[1]][] = (int) $row[$key]; |
||
1240 | } |
||
1241 | $smcFunc['db_free_result']($ignoreRequest); |
||
1242 | $numSubjectResults = count($inserts); |
||
1243 | } |
||
1244 | else |
||
1245 | $numSubjectResults += $smcFunc['db_affected_rows'](); |
||
1246 | |||
1247 | if (!empty($modSettings['search_max_results']) && $numSubjectResults >= $modSettings['search_max_results']) |
||
1248 | break; |
||
1249 | } |
||
1250 | |||
1251 | // If there's data to be inserted for non-IGNORE databases do it here! |
||
1252 | if (!empty($inserts)) |
||
1253 | { |
||
1254 | $smcFunc['db_insert']('', |
||
1255 | '{db_prefix}log_search_results', |
||
1256 | array('id_search' => 'int', 'id_topic' => 'int', 'relevance' => 'int', 'id_msg' => 'int', 'num_matches' => 'int'), |
||
1257 | $inserts, |
||
1258 | array('id_search', 'id_topic') |
||
1259 | ); |
||
1260 | } |
||
1261 | |||
1262 | $_SESSION['search_cache']['num_results'] = $numSubjectResults; |
||
1263 | } |
||
1264 | else |
||
1265 | { |
||
1266 | $main_query = array( |
||
1267 | 'select' => array( |
||
1268 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1269 | 'relevance' => '0', |
||
1270 | ), |
||
1271 | 'weights' => array(), |
||
1272 | 'from' => '{db_prefix}topics AS t', |
||
1273 | 'inner_join' => array( |
||
1274 | '{db_prefix}messages AS m ON (m.id_topic = t.id_topic)' |
||
1275 | ), |
||
1276 | 'left_join' => array(), |
||
1277 | 'where' => array( |
||
1278 | 't.id_redirect_topic = {int:not_redirected}', |
||
1279 | 't.redirect_expires = {int:never_expires}', |
||
1280 | ), |
||
1281 | 'group_by' => array(), |
||
1282 | 'parameters' => array( |
||
1283 | 'min_msg' => $minMsg, |
||
1284 | 'recent_message' => $recentMsg, |
||
1285 | 'huge_topic_posts' => $humungousTopicPosts, |
||
1286 | 'is_approved' => 1, |
||
1287 | 'not_redirected' => 0, |
||
1288 | 'never_expires' => 0, |
||
1289 | ), |
||
1290 | ); |
||
1291 | |||
1292 | if (empty($search_params['topic']) && empty($search_params['show_complete'])) |
||
1293 | { |
||
1294 | $main_query['select']['id_topic'] = 't.id_topic'; |
||
1295 | $main_query['select']['id_msg'] = 'MAX(m.id_msg) AS id_msg'; |
||
1296 | $main_query['select']['num_matches'] = 'COUNT(*) AS num_matches'; |
||
1297 | |||
1298 | $main_query['weights'] = $weight_factors; |
||
1299 | |||
1300 | $main_query['group_by'][] = 't.id_topic'; |
||
1301 | } |
||
1302 | else |
||
1303 | { |
||
1304 | // This is outrageous! |
||
1305 | $main_query['select']['id_topic'] = 'm.id_msg AS id_topic'; |
||
1306 | $main_query['select']['id_msg'] = 'm.id_msg'; |
||
1307 | $main_query['select']['num_matches'] = '1 AS num_matches'; |
||
1308 | |||
1309 | $main_query['weights'] = array( |
||
1310 | 'age' => array( |
||
1311 | '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)', |
||
1312 | ), |
||
1313 | 'first_message' => array( |
||
1314 | 'search' => 'CASE WHEN m.id_msg = t.id_first_msg THEN 1 ELSE 0 END', |
||
1315 | ), |
||
1316 | ); |
||
1317 | |||
1318 | if (!empty($search_params['topic'])) |
||
1319 | { |
||
1320 | $main_query['where'][] = 't.id_topic = {int:topic}'; |
||
1321 | $main_query['parameters']['topic'] = $search_params['topic']; |
||
1322 | } |
||
1323 | if (!empty($search_params['show_complete'])) |
||
1324 | $main_query['group_by'][] = 'm.id_msg, t.id_first_msg, t.id_last_msg'; |
||
1325 | } |
||
1326 | |||
1327 | // *** Get the subject results. |
||
1328 | $numSubjectResults = 0; |
||
1329 | if (empty($search_params['topic'])) |
||
1330 | { |
||
1331 | $inserts = array(); |
||
1332 | // Create a temporary table to store some preliminary results in. |
||
1333 | $smcFunc['db_search_query']('drop_tmp_log_search_topics', ' |
||
1334 | DROP TABLE IF EXISTS {db_prefix}tmp_log_search_topics', |
||
1335 | array( |
||
1336 | ) |
||
1337 | ); |
||
1338 | $createTemporary = $smcFunc['db_search_query']('create_tmp_log_search_topics', ' |
||
1339 | CREATE TEMPORARY TABLE {db_prefix}tmp_log_search_topics ( |
||
1340 | id_topic int NOT NULL default {string:string_zero}, |
||
1341 | PRIMARY KEY (id_topic) |
||
1342 | ) ENGINE=MEMORY', |
||
1343 | array( |
||
1344 | 'string_zero' => '0', |
||
1345 | ) |
||
1346 | ) !== false; |
||
1347 | |||
1348 | // Clean up some previous cache. |
||
1349 | if (!$createTemporary) |
||
1350 | $smcFunc['db_search_query']('delete_log_search_topics', ' |
||
1351 | DELETE FROM {db_prefix}log_search_topics |
||
1352 | WHERE id_search = {int:search_id}', |
||
1353 | array( |
||
1354 | 'search_id' => $_SESSION['search_cache']['id_search'], |
||
1355 | ) |
||
1356 | ); |
||
1357 | |||
1358 | foreach ($searchWords as $orIndex => $words) |
||
1359 | { |
||
1360 | $subject_query = array( |
||
1361 | 'from' => '{db_prefix}topics AS t', |
||
1362 | 'inner_join' => array(), |
||
1363 | 'left_join' => array(), |
||
1364 | 'where' => array( |
||
1365 | 't.id_redirect_topic = {int:not_redirected}', |
||
1366 | 't.redirect_expires = {int:never_expires}', |
||
1367 | ), |
||
1368 | 'params' => array( |
||
1369 | 'not_redirected' => 0, |
||
1370 | 'never_expires' => 0, |
||
1371 | ), |
||
1372 | ); |
||
1373 | |||
1374 | $numTables = 0; |
||
1375 | $prev_join = 0; |
||
1376 | $count = 0; |
||
1377 | $excluded = false; |
||
1378 | foreach ($words['subject_words'] as $subjectWord) |
||
1379 | { |
||
1380 | $numTables++; |
||
1381 | if (in_array($subjectWord, $excludedSubjectWords)) |
||
1382 | { |
||
1383 | if (($subject_query['from'] != '{db_prefix}messages AS m') && !$excluded) |
||
1384 | { |
||
1385 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
1386 | $excluded = true; |
||
1387 | } |
||
1388 | $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)'; |
||
1389 | $subject_query['params']['subject_not_' . $count] = empty($modSettings['search_match_words']) ? '%' . $subjectWord . '%' : $subjectWord; |
||
1390 | |||
1391 | $subject_query['where'][] = '(subj' . $numTables . '.word IS NULL)'; |
||
1392 | $subject_query['where'][] = 'm.body NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:body_not_' . $count . '}'; |
||
1393 | $subject_query['params']['body_not_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($subjectWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $subjectWord), '\\\'') . '[[:>:]]'; |
||
1394 | } |
||
1395 | else |
||
1396 | { |
||
1397 | $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)'; |
||
1398 | $subject_query['where'][] = 'subj' . $numTables . '.word LIKE {string:subject_like_' . $count . '}'; |
||
1399 | $subject_query['params']['subject_like_' . $count++] = empty($modSettings['search_match_words']) ? '%' . $subjectWord . '%' : $subjectWord; |
||
1400 | $prev_join = $numTables; |
||
1401 | } |
||
1402 | } |
||
1403 | |||
1404 | if (!empty($userQuery)) |
||
1405 | { |
||
1406 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
1407 | { |
||
1408 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
1409 | } |
||
1410 | $subject_query['where'][] = '{raw:user_query}'; |
||
1411 | $subject_query['params']['user_query'] = $userQuery; |
||
1412 | } |
||
1413 | if (!empty($search_params['topic'])) |
||
1414 | { |
||
1415 | $subject_query['where'][] = 't.id_topic = {int:topic}'; |
||
1416 | $subject_query['params']['topic'] = $search_params['topic']; |
||
1417 | } |
||
1418 | if (!empty($minMsgID)) |
||
1419 | { |
||
1420 | $subject_query['where'][] = 't.id_first_msg >= {int:min_msg_id}'; |
||
1421 | $subject_query['params']['min_msg_id'] = $minMsgID; |
||
1422 | } |
||
1423 | if (!empty($maxMsgID)) |
||
1424 | { |
||
1425 | $subject_query['where'][] = 't.id_last_msg <= {int:max_msg_id}'; |
||
1426 | $subject_query['params']['max_msg_id'] = $maxMsgID; |
||
1427 | } |
||
1428 | if (!empty($boardQuery)) |
||
1429 | { |
||
1430 | $subject_query['where'][] = 't.id_board {raw:board_query}'; |
||
1431 | $subject_query['params']['board_query'] = $boardQuery; |
||
1432 | } |
||
1433 | if (!empty($excludedPhrases)) |
||
1434 | { |
||
1435 | if ($subject_query['from'] != '{db_prefix}messages AS m') |
||
1436 | { |
||
1437 | $subject_query['inner_join'][] = '{db_prefix}messages AS m ON (m.id_msg = t.id_first_msg)'; |
||
1438 | } |
||
1439 | $count = 0; |
||
1440 | foreach ($excludedPhrases as $phrase) |
||
1441 | { |
||
1442 | $subject_query['where'][] = 'm.subject NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:exclude_phrase_' . $count . '}'; |
||
1443 | $subject_query['where'][] = 'm.body NOT ' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:exclude_phrase_' . $count . '}'; |
||
1444 | $subject_query['params']['exclude_phrase_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]'; |
||
1445 | } |
||
1446 | } |
||
1447 | call_integration_hook('integrate_subject_search_query', array(&$subject_query)); |
||
1448 | |||
1449 | // Nothing to search for? |
||
1450 | if (empty($subject_query['where'])) |
||
1451 | continue; |
||
1452 | |||
1453 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_topics', ($smcFunc['db_support_ignore'] ? (' |
||
1454 | INSERT IGNORE INTO {db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics |
||
1455 | (' . ($createTemporary ? '' : 'id_search, ') . 'id_topic)') : '') . ' |
||
1456 | SELECT ' . ($createTemporary ? '' : $_SESSION['search_cache']['id_search'] . ', ') . 't.id_topic |
||
1457 | FROM ' . $subject_query['from'] . (empty($subject_query['inner_join']) ? '' : ' |
||
1458 | INNER JOIN ' . implode(' |
||
1459 | INNER JOIN ', $subject_query['inner_join'])) . (empty($subject_query['left_join']) ? '' : ' |
||
1460 | LEFT JOIN ' . implode(' |
||
1461 | LEFT JOIN ', $subject_query['left_join'])) . ' |
||
1462 | WHERE ' . implode(' |
||
1463 | AND ', $subject_query['where']) . (empty($modSettings['search_max_results']) ? '' : ' |
||
1464 | LIMIT ' . ($modSettings['search_max_results'] - $numSubjectResults)), |
||
1465 | $subject_query['params'] |
||
1466 | ); |
||
1467 | // Don't do INSERT IGNORE? Manually fix this up! |
||
1468 | if (!$smcFunc['db_support_ignore']) |
||
1469 | { |
||
1470 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1471 | { |
||
1472 | $ind = $createTemporary ? 0 : 1; |
||
1473 | // No duplicates! |
||
1474 | if (isset($inserts[$row[$ind]])) |
||
1475 | continue; |
||
1476 | |||
1477 | $inserts[$row[$ind]] = $row; |
||
1478 | } |
||
1479 | $smcFunc['db_free_result']($ignoreRequest); |
||
1480 | $numSubjectResults = count($inserts); |
||
1481 | } |
||
1482 | else |
||
1483 | $numSubjectResults += $smcFunc['db_affected_rows'](); |
||
1484 | |||
1485 | if (!empty($modSettings['search_max_results']) && $numSubjectResults >= $modSettings['search_max_results']) |
||
1486 | break; |
||
1487 | } |
||
1488 | |||
1489 | // Got some non-MySQL data to plonk in? |
||
1490 | if (!empty($inserts)) |
||
1491 | { |
||
1492 | $smcFunc['db_insert']('', |
||
1493 | ('{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics'), |
||
1494 | $createTemporary ? array('id_topic' => 'int') : array('id_search' => 'int', 'id_topic' => 'int'), |
||
1495 | $inserts, |
||
1496 | $createTemporary ? array('id_topic') : array('id_search', 'id_topic') |
||
1497 | ); |
||
1498 | } |
||
1499 | |||
1500 | if ($numSubjectResults !== 0) |
||
1501 | { |
||
1502 | $main_query['weights']['subject']['search'] = 'CASE WHEN MAX(lst.id_topic) IS NULL THEN 0 ELSE 1 END'; |
||
1503 | $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)'; |
||
1504 | if (!$createTemporary) |
||
1505 | $main_query['parameters']['id_search'] = $_SESSION['search_cache']['id_search']; |
||
1506 | } |
||
1507 | } |
||
1508 | |||
1509 | $indexedResults = 0; |
||
1510 | // We building an index? |
||
1511 | if ($searchAPI->supportsMethod('indexedWordQuery', $query_params)) |
||
1512 | { |
||
1513 | $inserts = array(); |
||
1514 | $smcFunc['db_search_query']('drop_tmp_log_search_messages', ' |
||
1515 | DROP TABLE IF EXISTS {db_prefix}tmp_log_search_messages', |
||
1516 | array( |
||
1517 | ) |
||
1518 | ); |
||
1519 | |||
1520 | $createTemporary = $smcFunc['db_search_query']('create_tmp_log_search_messages', ' |
||
1521 | CREATE TEMPORARY TABLE {db_prefix}tmp_log_search_messages ( |
||
1522 | id_msg int NOT NULL default {string:string_zero}, |
||
1523 | PRIMARY KEY (id_msg) |
||
1524 | ) ENGINE=MEMORY', |
||
1525 | array( |
||
1526 | 'string_zero' => '0', |
||
1527 | ) |
||
1528 | ) !== false; |
||
1529 | |||
1530 | // Clear, all clear! |
||
1531 | if (!$createTemporary) |
||
1532 | $smcFunc['db_search_query']('delete_log_search_messages', ' |
||
1533 | DELETE FROM {db_prefix}log_search_messages |
||
1534 | WHERE id_search = {int:id_search}', |
||
1535 | array( |
||
1536 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1537 | ) |
||
1538 | ); |
||
1539 | |||
1540 | foreach ($searchWords as $orIndex => $words) |
||
1541 | { |
||
1542 | // Search for this word, assuming we have some words! |
||
1543 | if (!empty($words['indexed_words'])) |
||
1544 | { |
||
1545 | // Variables required for the search. |
||
1546 | $search_data = array( |
||
1547 | 'insert_into' => ($createTemporary ? 'tmp_' : '') . 'log_search_messages', |
||
1548 | 'no_regexp' => $no_regexp, |
||
1549 | 'max_results' => $maxMessageResults, |
||
1550 | 'indexed_results' => $indexedResults, |
||
1551 | 'params' => array( |
||
1552 | 'id_search' => !$createTemporary ? $_SESSION['search_cache']['id_search'] : 0, |
||
1553 | 'excluded_words' => $excludedWords, |
||
1554 | 'user_query' => !empty($userQuery) ? $userQuery : '', |
||
1555 | 'board_query' => !empty($boardQuery) ? $boardQuery : '', |
||
1556 | 'topic' => !empty($search_params['topic']) ? $search_params['topic'] : 0, |
||
1557 | 'min_msg_id' => !empty($minMsgID) ? $minMsgID : 0, |
||
1558 | 'max_msg_id' => !empty($maxMsgID) ? $maxMsgID : 0, |
||
1559 | 'excluded_phrases' => !empty($excludedPhrases) ? $excludedPhrases : array(), |
||
1560 | 'excluded_index_words' => !empty($excludedIndexWords) ? $excludedIndexWords : array(), |
||
1561 | 'excluded_subject_words' => !empty($excludedSubjectWords) ? $excludedSubjectWords : array(), |
||
1562 | ), |
||
1563 | ); |
||
1564 | |||
1565 | $ignoreRequest = $searchAPI->indexedWordQuery($words, $search_data); |
||
1566 | |||
1567 | if (!$smcFunc['db_support_ignore']) |
||
1568 | { |
||
1569 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1570 | { |
||
1571 | // No duplicates! |
||
1572 | if (isset($inserts[$row[0]])) |
||
1573 | continue; |
||
1574 | |||
1575 | $inserts[$row[0]] = $row; |
||
1576 | } |
||
1577 | $smcFunc['db_free_result']($ignoreRequest); |
||
1578 | $indexedResults = count($inserts); |
||
1579 | } |
||
1580 | else |
||
1581 | $indexedResults += $smcFunc['db_affected_rows'](); |
||
1582 | |||
1583 | if (!empty($maxMessageResults) && $indexedResults >= $maxMessageResults) |
||
1584 | break; |
||
1585 | } |
||
1586 | } |
||
1587 | |||
1588 | // More non-MySQL stuff needed? |
||
1589 | if (!empty($inserts)) |
||
1590 | { |
||
1591 | $smcFunc['db_insert']('', |
||
1592 | '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_messages', |
||
1593 | $createTemporary ? array('id_msg' => 'int') : array('id_msg' => 'int', 'id_search' => 'int'), |
||
1594 | $inserts, |
||
1595 | $createTemporary ? array('id_msg') : array('id_msg', 'id_search') |
||
1596 | ); |
||
1597 | } |
||
1598 | |||
1599 | if (empty($indexedResults) && empty($numSubjectResults) && !empty($modSettings['search_force_index'])) |
||
1600 | { |
||
1601 | $context['search_errors']['query_not_specific_enough'] = true; |
||
1602 | $_REQUEST['params'] = $context['params']; |
||
1603 | return PlushSearch1(); |
||
1604 | } |
||
1605 | elseif (!empty($indexedResults)) |
||
1606 | { |
||
1607 | $main_query['inner_join'][] = '{db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_messages AS lsm ON (lsm.id_msg = m.id_msg)'; |
||
1608 | if (!$createTemporary) |
||
1609 | { |
||
1610 | $main_query['where'][] = 'lsm.id_search = {int:id_search}'; |
||
1611 | $main_query['parameters']['id_search'] = $_SESSION['search_cache']['id_search']; |
||
1612 | } |
||
1613 | } |
||
1614 | } |
||
1615 | |||
1616 | // Not using an index? All conditions have to be carried over. |
||
1617 | else |
||
1618 | { |
||
1619 | $orWhere = array(); |
||
1620 | $count = 0; |
||
1621 | foreach ($searchWords as $orIndex => $words) |
||
1622 | { |
||
1623 | $where = array(); |
||
1624 | foreach ($words['all_words'] as $regularWord) |
||
1625 | { |
||
1626 | $where[] = 'm.body' . (in_array($regularWord, $excludedWords) ? ' NOT' : '') . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:all_word_body_' . $count . '}'; |
||
1627 | if (in_array($regularWord, $excludedWords)) |
||
1628 | $where[] = 'm.subject NOT' . (empty($modSettings['search_match_words']) || $no_regexp ? ' LIKE ' : ' RLIKE ') . '{string:all_word_body_' . $count . '}'; |
||
1629 | $main_query['parameters']['all_word_body_' . $count++] = empty($modSettings['search_match_words']) || $no_regexp ? '%' . strtr($regularWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $regularWord), '\\\'') . '[[:>:]]'; |
||
1630 | } |
||
1631 | if (!empty($where)) |
||
1632 | $orWhere[] = count($where) > 1 ? '(' . implode(' AND ', $where) . ')' : $where[0]; |
||
1633 | } |
||
1634 | if (!empty($orWhere)) |
||
1635 | $main_query['where'][] = count($orWhere) > 1 ? '(' . implode(' OR ', $orWhere) . ')' : $orWhere[0]; |
||
1636 | |||
1637 | if (!empty($userQuery)) |
||
1638 | { |
||
1639 | $main_query['where'][] = '{raw:user_query}'; |
||
1640 | $main_query['parameters']['user_query'] = $userQuery; |
||
1641 | } |
||
1642 | if (!empty($search_params['topic'])) |
||
1643 | { |
||
1644 | $main_query['where'][] = 'm.id_topic = {int:topic}'; |
||
1645 | $main_query['parameters']['topic'] = $search_params['topic']; |
||
1646 | } |
||
1647 | if (!empty($minMsgID)) |
||
1648 | { |
||
1649 | $main_query['where'][] = 'm.id_msg >= {int:min_msg_id}'; |
||
1650 | $main_query['parameters']['min_msg_id'] = $minMsgID; |
||
1651 | } |
||
1652 | if (!empty($maxMsgID)) |
||
1653 | { |
||
1654 | $main_query['where'][] = 'm.id_msg <= {int:max_msg_id}'; |
||
1655 | $main_query['parameters']['max_msg_id'] = $maxMsgID; |
||
1656 | } |
||
1657 | if (!empty($boardQuery)) |
||
1658 | { |
||
1659 | $main_query['where'][] = 'm.id_board {raw:board_query}'; |
||
1660 | $main_query['parameters']['board_query'] = $boardQuery; |
||
1661 | } |
||
1662 | } |
||
1663 | call_integration_hook('integrate_main_search_query', array(&$main_query)); |
||
1664 | |||
1665 | // Did we either get some indexed results, or otherwise did not do an indexed query? |
||
1666 | if (!empty($indexedResults) || !$searchAPI->supportsMethod('indexedWordQuery', $query_params)) |
||
1667 | { |
||
1668 | $relevance = '1000 * ('; |
||
1669 | $new_weight_total = 0; |
||
1670 | foreach ($main_query['weights'] as $type => $value) |
||
1671 | { |
||
1672 | $relevance .= $weight[$type]; |
||
1673 | if (!empty($value['search'])) |
||
1674 | $relevance .= ' * ' . $value['search']; |
||
1675 | $relevance .= ' + '; |
||
1676 | $new_weight_total += $weight[$type]; |
||
1677 | } |
||
1678 | $main_query['select']['relevance'] = substr($relevance, 0, -3) . ') / ' . $new_weight_total . ' AS relevance'; |
||
1679 | |||
1680 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_no_index', ($smcFunc['db_support_ignore'] ? (' |
||
1681 | INSERT IGNORE INTO ' . '{db_prefix}log_search_results |
||
1682 | (' . implode(', ', array_keys($main_query['select'])) . ')') : '') . ' |
||
1683 | SELECT |
||
1684 | ' . implode(', |
||
1685 | ', $main_query['select']) . ' |
||
1686 | FROM ' . $main_query['from'] . (empty($main_query['inner_join']) ? '' : ' |
||
1687 | INNER JOIN ' . implode(' |
||
1688 | INNER JOIN ', $main_query['inner_join'])) . (empty($main_query['left_join']) ? '' : ' |
||
1689 | LEFT JOIN ' . implode(' |
||
1690 | LEFT JOIN ', $main_query['left_join'])) . (!empty($main_query['where']) ? ' |
||
1691 | WHERE ' : '') . implode(' |
||
1692 | AND ', $main_query['where']) . (empty($main_query['group_by']) ? '' : ' |
||
1693 | GROUP BY ' . implode(', ', $main_query['group_by'])) . (empty($modSettings['search_max_results']) ? '' : ' |
||
1694 | LIMIT ' . $modSettings['search_max_results']), |
||
1695 | $main_query['parameters'] |
||
1696 | ); |
||
1697 | |||
1698 | // We love to handle non-good databases that don't support our ignore! |
||
1699 | if (!$smcFunc['db_support_ignore']) |
||
1700 | { |
||
1701 | $inserts = array(); |
||
1702 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1703 | { |
||
1704 | // No duplicates! |
||
1705 | if (isset($inserts[$row[2]])) |
||
1706 | continue; |
||
1707 | |||
1708 | foreach ($row as $key => $value) |
||
1709 | $inserts[$row[2]][] = (int) $row[$key]; |
||
1710 | } |
||
1711 | $smcFunc['db_free_result']($ignoreRequest); |
||
1712 | |||
1713 | // Now put them in! |
||
1714 | if (!empty($inserts)) |
||
1715 | { |
||
1716 | $query_columns = array(); |
||
1717 | foreach ($main_query['select'] as $k => $v) |
||
1718 | $query_columns[$k] = 'int'; |
||
1719 | |||
1720 | $smcFunc['db_insert']('', |
||
1721 | '{db_prefix}log_search_results', |
||
1722 | $query_columns, |
||
1723 | $inserts, |
||
1724 | array('id_search', 'id_topic') |
||
1725 | ); |
||
1726 | } |
||
1727 | $_SESSION['search_cache']['num_results'] += count($inserts); |
||
1728 | } |
||
1729 | else |
||
1730 | $_SESSION['search_cache']['num_results'] = $smcFunc['db_affected_rows'](); |
||
1731 | } |
||
1732 | |||
1733 | // Insert subject-only matches. |
||
1734 | if ($_SESSION['search_cache']['num_results'] < $modSettings['search_max_results'] && $numSubjectResults !== 0) |
||
1735 | { |
||
1736 | $relevance = '1000 * ('; |
||
1737 | foreach ($weight_factors as $type => $value) |
||
1738 | if (isset($value['results'])) |
||
1739 | { |
||
1740 | $relevance .= $weight[$type]; |
||
1741 | if (!empty($value['results'])) |
||
1742 | $relevance .= ' * ' . $value['results']; |
||
1743 | $relevance .= ' + '; |
||
1744 | } |
||
1745 | $relevance = substr($relevance, 0, -3) . ') / ' . $weight_total . ' AS relevance'; |
||
1746 | |||
1747 | $usedIDs = array_flip(empty($inserts) ? array() : array_keys($inserts)); |
||
1748 | $ignoreRequest = $smcFunc['db_search_query']('insert_log_search_results_sub_only', ($smcFunc['db_support_ignore'] ? (' |
||
1749 | INSERT IGNORE INTO {db_prefix}log_search_results |
||
1750 | (id_search, id_topic, relevance, id_msg, num_matches)') : '') . ' |
||
1751 | SELECT |
||
1752 | {int:id_search}, |
||
1753 | t.id_topic, |
||
1754 | ' . $relevance . ', |
||
1755 | t.id_first_msg, |
||
1756 | 1 |
||
1757 | FROM {db_prefix}topics AS t |
||
1758 | INNER JOIN {db_prefix}' . ($createTemporary ? 'tmp_' : '') . 'log_search_topics AS lst ON (lst.id_topic = t.id_topic)' |
||
1759 | . ($createTemporary ? '' : ' WHERE lst.id_search = {int:id_search}') |
||
1760 | . (empty($modSettings['search_max_results']) ? '' : ' |
||
1761 | LIMIT ' . ($modSettings['search_max_results'] - $_SESSION['search_cache']['num_results'])), |
||
1762 | array( |
||
1763 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1764 | 'min_msg' => $minMsg, |
||
1765 | 'recent_message' => $recentMsg, |
||
1766 | 'huge_topic_posts' => $humungousTopicPosts, |
||
1767 | ) |
||
1768 | ); |
||
1769 | // Once again need to do the inserts if the database don't support ignore! |
||
1770 | if (!$smcFunc['db_support_ignore']) |
||
1771 | { |
||
1772 | $inserts = array(); |
||
1773 | while ($row = $smcFunc['db_fetch_row']($ignoreRequest)) |
||
1774 | { |
||
1775 | // No duplicates! |
||
1776 | if (isset($usedIDs[$row[1]])) |
||
1777 | continue; |
||
1778 | |||
1779 | $usedIDs[$row[1]] = true; |
||
1780 | $inserts[] = $row; |
||
1781 | } |
||
1782 | $smcFunc['db_free_result']($ignoreRequest); |
||
1783 | |||
1784 | // Now put them in! |
||
1785 | if (!empty($inserts)) |
||
1786 | { |
||
1787 | $smcFunc['db_insert']('', |
||
1788 | '{db_prefix}log_search_results', |
||
1789 | array('id_search' => 'int', 'id_topic' => 'int', 'relevance' => 'float', 'id_msg' => 'int', 'num_matches' => 'int'), |
||
1790 | $inserts, |
||
1791 | array('id_search', 'id_topic') |
||
1792 | ); |
||
1793 | } |
||
1794 | $_SESSION['search_cache']['num_results'] += count($inserts); |
||
1795 | } |
||
1796 | else |
||
1797 | $_SESSION['search_cache']['num_results'] += $smcFunc['db_affected_rows'](); |
||
1798 | } |
||
1799 | elseif ($_SESSION['search_cache']['num_results'] == -1) |
||
1800 | $_SESSION['search_cache']['num_results'] = 0; |
||
1801 | } |
||
1802 | } |
||
1803 | |||
1804 | $approve_query = ''; |
||
1805 | if (!empty($modSettings['postmod_active'])) |
||
1806 | { |
||
1807 | // Exclude unapproved topics, but show ones they started. |
||
1808 | if (empty($user_info['mod_cache']['ap'])) |
||
1809 | $approve_query = ' |
||
1810 | AND (t.approved = {int:is_approved} OR t.id_member_started = {int:current_member})'; |
||
1811 | |||
1812 | // Show unapproved topics in boards they have access to. |
||
1813 | elseif ($user_info['mod_cache']['ap'] !== array(0)) |
||
1814 | $approve_query = ' |
||
1815 | AND (t.approved = {int:is_approved} OR t.id_member_started = {int:current_member} OR t.id_board IN ({array_int:approve_boards}))'; |
||
1816 | } |
||
1817 | |||
1818 | // *** Retrieve the results to be shown on the page |
||
1819 | $participants = array(); |
||
1820 | $request = $smcFunc['db_search_query']('', ' |
||
1821 | SELECT ' . (empty($search_params['topic']) ? 'lsr.id_topic' : $search_params['topic'] . ' AS id_topic') . ', lsr.id_msg, lsr.relevance, lsr.num_matches |
||
1822 | FROM {db_prefix}log_search_results AS lsr' . ($search_params['sort'] == 'num_replies' || !empty($approve_query) ? ' |
||
1823 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = lsr.id_topic)' : '') . ' |
||
1824 | WHERE lsr.id_search = {int:id_search}' . $approve_query . ' |
||
1825 | ORDER BY {raw:sort} {raw:sort_dir} |
||
1826 | LIMIT {int:start}, {int:max}', |
||
1827 | array( |
||
1828 | 'id_search' => $_SESSION['search_cache']['id_search'], |
||
1829 | 'sort' => $search_params['sort'], |
||
1830 | 'sort_dir' => $search_params['sort_dir'], |
||
1831 | 'start' => $_REQUEST['start'], |
||
1832 | 'max' => $modSettings['search_results_per_page'], |
||
1833 | 'is_approved' => 1, |
||
1834 | 'current_member' => $user_info['id'], |
||
1835 | 'approve_boards' => !empty($modSettings['postmod_active']) ? $user_info['mod_cache']['ap'] : array(), |
||
1836 | ) |
||
1837 | ); |
||
1838 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1839 | { |
||
1840 | $context['topics'][$row['id_msg']] = array( |
||
1841 | 'relevance' => round($row['relevance'] / 10, 1) . '%', |
||
1842 | 'num_matches' => $row['num_matches'], |
||
1843 | 'matches' => array(), |
||
1844 | ); |
||
1845 | // By default they didn't participate in the topic! |
||
1846 | $participants[$row['id_topic']] = false; |
||
1847 | } |
||
1848 | $smcFunc['db_free_result']($request); |
||
1849 | } |
||
1850 | |||
1851 | $num_results = 0; |
||
1852 | if (!empty($context['topics'])) |
||
1853 | { |
||
1854 | // Create an array for the permissions. |
||
1855 | $perms = array('post_reply_own', 'post_reply_any'); |
||
1856 | |||
1857 | if (!empty($options['display_quick_mod'])) |
||
1858 | $perms = array_merge($perms, array('lock_any', 'lock_own', 'make_sticky', 'move_any', 'move_own', 'remove_any', 'remove_own', 'merge_any')); |
||
1859 | |||
1860 | $boards_can = boardsAllowedTo($perms, true, false); |
||
1861 | |||
1862 | // How's about some quick moderation? |
||
1863 | if (!empty($options['display_quick_mod'])) |
||
1864 | { |
||
1865 | $context['can_lock'] = in_array(0, $boards_can['lock_any']); |
||
1866 | $context['can_sticky'] = in_array(0, $boards_can['make_sticky']); |
||
1867 | $context['can_move'] = in_array(0, $boards_can['move_any']); |
||
1868 | $context['can_remove'] = in_array(0, $boards_can['remove_any']); |
||
1869 | $context['can_merge'] = in_array(0, $boards_can['merge_any']); |
||
1870 | } |
||
1871 | |||
1872 | $approve_query = ''; |
||
1873 | if (!empty($modSettings['postmod_active'])) |
||
1874 | { |
||
1875 | if (empty($user_info['mod_cache']['ap'])) |
||
1876 | $approve_query = ' |
||
1877 | AND (m.approved = {int:is_approved} OR m.id_member = {int:current_member})'; |
||
1878 | |||
1879 | elseif ($user_info['mod_cache']['ap'] !== array(0)) |
||
1880 | $approve_query = ' |
||
1881 | AND (m.approved = {int:is_approved} OR m.id_member = {int:current_member} OR m.id_board IN ({array_int:approve_boards}))'; |
||
1882 | } |
||
1883 | |||
1884 | // What messages are we using? |
||
1885 | $msg_list = array_keys($context['topics']); |
||
1886 | |||
1887 | // Load the posters... |
||
1888 | $request = $smcFunc['db_query']('', ' |
||
1889 | SELECT id_member |
||
1890 | FROM {db_prefix}messages |
||
1891 | WHERE id_member != {int:no_member} |
||
1892 | AND id_msg IN ({array_int:message_list}) |
||
1893 | LIMIT {int:limit}', |
||
1894 | array( |
||
1895 | 'message_list' => $msg_list, |
||
1896 | 'no_member' => 0, |
||
1897 | 'limit' => count($context['topics']), |
||
1898 | ) |
||
1899 | ); |
||
1900 | $posters = array(); |
||
1901 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
1902 | $posters[] = $row['id_member']; |
||
1903 | $smcFunc['db_free_result']($request); |
||
1904 | |||
1905 | call_integration_hook('integrate_search_message_list', array(&$msg_list, &$posters)); |
||
1906 | |||
1907 | if (!empty($posters)) |
||
1908 | loadMemberData(array_unique($posters)); |
||
1909 | |||
1910 | // Get the messages out for the callback - select enough that it can be made to look just like Display. |
||
1911 | $messages_request = $smcFunc['db_query']('', ' |
||
1912 | SELECT |
||
1913 | m.id_msg, m.subject, m.poster_name, m.poster_email, m.poster_time, m.id_member, |
||
1914 | m.icon, m.poster_ip, m.body, m.smileys_enabled, m.modified_time, m.modified_name, |
||
1915 | 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, |
||
1916 | first_mem.id_member AS first_member_id, COALESCE(first_mem.real_name, first_m.poster_name) AS first_member_name, |
||
1917 | last_m.id_msg AS last_msg, last_m.poster_time AS last_poster_time, last_mem.id_member AS last_member_id, |
||
1918 | 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, |
||
1919 | t.id_topic, t.is_sticky, t.locked, t.id_poll, t.num_replies, t.num_views, |
||
1920 | b.id_board, b.name AS board_name, c.id_cat, c.name AS cat_name |
||
1921 | FROM {db_prefix}messages AS m |
||
1922 | INNER JOIN {db_prefix}topics AS t ON (t.id_topic = m.id_topic) |
||
1923 | INNER JOIN {db_prefix}boards AS b ON (b.id_board = t.id_board) |
||
1924 | INNER JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat) |
||
1925 | INNER JOIN {db_prefix}messages AS first_m ON (first_m.id_msg = t.id_first_msg) |
||
1926 | INNER JOIN {db_prefix}messages AS last_m ON (last_m.id_msg = t.id_last_msg) |
||
1927 | LEFT JOIN {db_prefix}members AS first_mem ON (first_mem.id_member = first_m.id_member) |
||
1928 | LEFT JOIN {db_prefix}members AS last_mem ON (last_mem.id_member = first_m.id_member) |
||
1929 | WHERE m.id_msg IN ({array_int:message_list})' . $approve_query . ' |
||
1930 | ORDER BY ' . $smcFunc['db_custom_order']('m.id_msg', $msg_list) . ' |
||
1931 | LIMIT {int:limit}', |
||
1932 | array( |
||
1933 | 'message_list' => $msg_list, |
||
1934 | 'is_approved' => 1, |
||
1935 | 'current_member' => $user_info['id'], |
||
1936 | 'approve_boards' => !empty($modSettings['postmod_active']) ? $user_info['mod_cache']['ap'] : array(), |
||
1937 | 'limit' => count($context['topics']), |
||
1938 | ) |
||
1939 | ); |
||
1940 | |||
1941 | // How many results will the user be able to see? |
||
1942 | if (!empty($_SESSION['search_cache']['num_results'])) |
||
1943 | $num_results = $_SESSION['search_cache']['num_results']; |
||
1944 | else |
||
1945 | $num_results = $smcFunc['db_num_rows']($messages_request); |
||
1946 | |||
1947 | // If there are no results that means the things in the cache got deleted, so pretend we have no topics anymore. |
||
1948 | if ($num_results == 0) |
||
1949 | $context['topics'] = array(); |
||
1950 | |||
1951 | // If we want to know who participated in what then load this now. |
||
1952 | if (!empty($modSettings['enableParticipation']) && !$user_info['is_guest']) |
||
1953 | { |
||
1954 | $result = $smcFunc['db_query']('', ' |
||
1955 | SELECT id_topic |
||
1956 | FROM {db_prefix}messages |
||
1957 | WHERE id_topic IN ({array_int:topic_list}) |
||
1958 | AND id_member = {int:current_member} |
||
1959 | GROUP BY id_topic |
||
1960 | LIMIT {int:limit}', |
||
1961 | array( |
||
1962 | 'current_member' => $user_info['id'], |
||
1963 | 'topic_list' => array_keys($participants), |
||
1964 | 'limit' => count($participants), |
||
1965 | ) |
||
1966 | ); |
||
1967 | while ($row = $smcFunc['db_fetch_assoc']($result)) |
||
1968 | $participants[$row['id_topic']] = true; |
||
1969 | |||
1970 | $smcFunc['db_free_result']($result); |
||
1971 | } |
||
1972 | } |
||
1973 | |||
1974 | // Now that we know how many results to expect we can start calculating the page numbers. |
||
1975 | $context['page_index'] = constructPageIndex($scripturl . '?action=search2;params=' . $context['params'], $_REQUEST['start'], $num_results, $modSettings['search_results_per_page'], false); |
||
1976 | |||
1977 | // Consider the search complete! |
||
1978 | if (!empty($cache_enable) && $cache_enable >= 2) |
||
1979 | cache_put_data('search_start:' . ($user_info['is_guest'] ? $user_info['ip'] : $user_info['id']), null, 90); |
||
1980 | |||
1981 | $context['key_words'] = &$searchArray; |
||
1982 | |||
1983 | // Setup the default topic icons... for checking they exist and the like! |
||
1984 | $context['icon_sources'] = array(); |
||
1985 | foreach ($context['stable_icons'] as $icon) |
||
1986 | $context['icon_sources'][$icon] = 'images_url'; |
||
1987 | |||
1988 | $context['sub_template'] = 'results'; |
||
1989 | $context['page_title'] = $txt['search_results']; |
||
1990 | $context['get_topics'] = 'prepareSearchContext'; |
||
1991 | $context['can_restore_perm'] = allowedTo('move_any') && !empty($modSettings['recycle_enable']); |
||
1992 | $context['can_restore'] = false; // We won't know until we handle the context later whether we can actually restore... |
||
1993 | |||
1994 | $context['jump_to'] = array( |
||
1995 | 'label' => addslashes(un_htmlspecialchars($txt['jump_to'])), |
||
1996 | 'board_name' => addslashes(un_htmlspecialchars($txt['select_destination'])), |
||
1997 | ); |
||
2356 | ?> |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.