includes/search.php
changeset 945 c19242d13a49
parent 832 7152ca0a0ce9
child 953 323c4cd1aa37
equal deleted inserted replaced
944:073a42e4177f 945:c19242d13a49
   185         $term = strtolower($term);
   185         $term = strtolower($term);
   186       $where_any[] = $term;
   186       $where_any[] = $term;
   187     }
   187     }
   188 
   188 
   189     $col_word = ( $case_sensitive ) ? 'word' : 'word_lcase';
   189     $col_word = ( $case_sensitive ) ? 'word' : 'word_lcase';
   190     $where_any = ( count($where_any) > 0 ) ? '( ' . $col_word . ' = \'' . implode('\' OR ' . $col_word . ' = \'', $where_any) . '\' )' : '';
   190     $where_any = ( count($where_any) > 0 ) ? '( ' . $col_word . ' LIKE \'%' . implode('%\' OR ' . $col_word . ' LIKE \'%', $where_any) . '%\' )' : '';
   191 
   191 
   192     // generate query
   192     // generate query
   193     // using a GROUP BY here ensures that the same word with a different case isn't counted as 2 words - it's all melted back
       
   194     // into one later in the processing stages
       
   195     // $group_by = ( $case_sensitive ) ? '' : ' GROUP BY lcase(word);';
       
   196     $sql = "SELECT word, page_names FROM " . table_prefix . "search_index WHERE {$where_any}";
   193     $sql = "SELECT word, page_names FROM " . table_prefix . "search_index WHERE {$where_any}";
   197     if ( !($q = $db->sql_unbuffered_query($sql)) )
   194     if ( !($q = $db->sql_query($sql)) )
   198       $db->_die('Error is in perform_search(), includes/search.php, query 1');
   195       $db->_die('Error is in perform_search(), includes/search.php, query 1');
   199 
   196 
   200     $word_tracking = array();
   197     $word_tracking = array();
   201     if ( $row = $db->fetchrow() )
   198     if ( $row = $db->fetchrow($q) )
   202     {
   199     {
   203       do
   200       do
   204       {
   201       {
   205         // get page list
   202         // get page list
   206         $pages =& $row['page_names'];
   203         $pages =& $row['page_names'];
   276         }
   273         }
   277         else
   274         else
   278         {
   275         {
   279           // the term only occurs in one page
   276           // the term only occurs in one page
   280           $word_cs = (( $case_sensitive ) ? $row['word'] : strtolower($row['word']));
   277           $word_cs = (( $case_sensitive ) ? $row['word'] : strtolower($row['word']));
       
   278           
   281           if ( isset($word_tracking[$pages]) && in_array($word_cs, $word_tracking[$pages]) )
   279           if ( isset($word_tracking[$pages]) && in_array($word_cs, $word_tracking[$pages]) )
   282           {
   280           {
   283             continue;
   281             continue;
   284           }
   282           }
   285           if ( isset($word_tracking[$pages]) )
   283           if ( isset($word_tracking[$pages]) )
   295           }
   293           }
   296           $inc = 1;
   294           $inc = 1;
   297 
   295 
   298           // Is this search term present in the page's title? If so, give extra points
   296           // Is this search term present in the page's title? If so, give extra points
   299           preg_match("/^ns=$ns_list;pid=(.+)$/", $pages, $piecesparts);
   297           preg_match("/^ns=$ns_list;pid=(.+)$/", $pages, $piecesparts);
   300           $pathskey = $paths->nslist[ $piecesparts[1] ] . sanitize_page_id($piecesparts[2]);
   298           $title = get_page_title_ns($piecesparts[2], $piecesparts[1]);
   301           if ( isPage($pathskey) )
   299           
   302           {
   300           $test_func = ( $case_sensitive ) ? 'strstr' : 'stristr';
   303             $test_func = ( $case_sensitive ) ? 'strstr' : 'stristr';
   301           if ( $test_func($title, $row['word']) || $test_func($piecesparts[2], $row['word']) )
   304             if ( $test_func($paths->pages[$pathskey]['name'], $row['word']) || $test_func($paths->pages[$pathskey]['urlname_nons'], $row['word']) )
   302           {
   305             {
   303             $inc = 1.5;
   306               $inc = 1.5;
   304           }
   307             }
   305           
   308           }
       
   309           if ( isset($scores[$pages]) )
   306           if ( isset($scores[$pages]) )
   310           {
   307           {
   311             $scores[$pages] = $scores[$pages] + $inc;
   308             $scores[$pages] = $scores[$pages] + $inc;
   312           }
   309           }
   313           else
   310           else
   314           {
   311           {
   315             $scores[$pages] = $inc;
   312             $scores[$pages] = $inc;
   316           }
   313           }
   317         }
   314         }
   318       }
   315       }
   319       while ( $row = $db->fetchrow() );
   316       while ( $row = $db->fetchrow($q) );
   320     }
   317     }
   321     $db->free_result();
   318     $db->free_result($q);
   322 
   319 
   323     //
   320     //
   324     // STAGE 2: FIRST ELIMINATION ROUND
   321     // STAGE 2: FIRST ELIMINATION ROUND
   325     // Iterate through the list of required terms. If a given page is not found to have the required term, eliminate it
   322     // Iterate through the list of required terms. If a given page is not found to have the required term, eliminate it
   326     //
   323     //
   334           unset($word_tracking[$i], $scores[$i]);
   331           unset($word_tracking[$i], $scores[$i]);
   335         }
   332         }
   336       }
   333       }
   337     }
   334     }
   338   }
   335   }
   339 
   336   
   340   //
   337   //
   341   // STAGE 3: PHRASE SEARCHING
   338   // STAGE 3: PHRASE SEARCHING
   342   // Use LIKE to find pages with specified phrases. We can do a super-picky single query without another elimination round because
   339   // Use LIKE to find pages with specified phrases. We can do a super-picky single query without another elimination round because
   343   // at this stage we can search the full page_text column instead of relying on a word list.
   340   // at this stage we can search the full page_text column instead of relying on a word list.
   344   //
   341   //
  1053         }
  1050         }
  1054         else if ( isset($options['formatcallback']) )
  1051         else if ( isset($options['formatcallback']) )
  1055         {
  1052         {
  1056           if ( is_callable($options['formatcallback']) )
  1053           if ( is_callable($options['formatcallback']) )
  1057           {
  1054           {
  1058             $text = @call_user_func($options['formatcallback'], $row, $word_list);
  1055             $text = call_user_func($options['formatcallback'], $row, $word_list);
  1059           }
  1056           }
  1060           else
  1057           else
  1061           {
  1058           {
  1062             $parser = $template->makeParserText($options['formatcallback']);
  1059             $parser = $template->makeParserText($options['formatcallback']);
  1063             $parser->assign_vars($row);
  1060             $parser->assign_vars($row);