Add experimental support for a total hit count in the MySQL search engine.
authorBrion Vibber <brion@users.mediawiki.org>
Tue, 21 Jul 2009 23:20:00 +0000 (23:20 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Tue, 21 Jul 2009 23:20:00 +0000 (23:20 +0000)
Disabled by default as it's a bit flaky when you combine title and page content results... also might be slow on larger sites. :)
Good for local UI testing to confirm that the search count totals work in the search UI/API though!

/**
 * Set to true to have the default MySQL search engine count total
 * search matches to present in the Special:Search UI.
 *
 * This could however be slow on larger wikis, and is pretty flaky
 * with the current title vs content split. Recommend avoiding until
 * that's been worked out cleanly; but this may aid in testing the
 * search UI and API to confirm that the result count works.
 */
$wgSearchMySQLTotalHits = false;

includes/DefaultSettings.php
includes/SearchMySQL.php

index af406d9..290d698 100644 (file)
@@ -1908,6 +1908,17 @@ $wgAdvancedSearchHighlighting = false;
 $wgSearchHighlightBoundaries = version_compare("5.1", PHP_VERSION, "<")? '[\p{Z}\p{P}\p{C}]'
        : '[ ,.;:!?~!@#$%\^&*\(\)+=\-\\|\[\]"\'<>\n\r\/{}]'; // PHP 5.0 workaround
 
+/**
+ * Set to true to have the default MySQL search engine count total
+ * search matches to present in the Special:Search UI.
+ *
+ * This could however be slow on larger wikis, and is pretty flaky
+ * with the current title vs content split. Recommend avoiding until
+ * that's been worked out cleanly; but this may aid in testing the
+ * search UI and API to confirm that the result count works.
+ */
+$wgSearchMySQLTotalHits = false;
+
 /**
  * Template for OpenSearch suggestions, defaults to API action=opensearch
  *
index 6709033..3d04466 100644 (file)
@@ -148,8 +148,22 @@ class SearchMySQL extends SearchEngine {
         * @return MySQLSearchResultSet
         */
        function searchText( $term ) {
-               $resultSet = $this->db->resultObject( $this->db->query( $this->getQuery( $this->filter( $term ), true ) ) );
-               return new MySQLSearchResultSet( $resultSet, $this->searchTerms );
+               global $wgSearchMySQLTotalHits;
+               
+               $filteredTerm = $this->filter( $term );
+               $resultSet = $this->db->query( $this->getQuery( $filteredTerm, true ) );
+               
+               $total = null;
+               if( $wgSearchMySQLTotalHits ) {
+                       $totalResult = $this->db->query( $this->getCountQuery( $filteredTerm, true ) );
+                       $row = $totalResult->fetchObject();
+                       if( $row ) {
+                               $total = $row->c;
+                       }
+                       $totalResult->free();
+               }
+               
+               return new MySQLSearchResultSet( $resultSet, $this->searchTerms, $total );
        }
 
        /**
@@ -221,8 +235,7 @@ class SearchMySQL extends SearchEngine {
                        $this->queryRanking( $filteredTerm, $fulltext ) . ' ' .
                        $this->queryLimit();
        }
-
-
+       
        /**
         * Picks which field to index on, depending on what type of query.
         * @param $fulltext Boolean
@@ -251,6 +264,17 @@ class SearchMySQL extends SearchEngine {
                        'WHERE page_id=si_page AND ' . $match;
        }
 
+       function getCountQuery( $filteredTerm, $fulltext ) {
+               $match = $this->parseQuery( $filteredTerm, $fulltext );
+               $page        = $this->db->tableName( 'page' );
+               $searchindex = $this->db->tableName( 'searchindex' );
+               return "SELECT COUNT(*) AS c " .
+                       "FROM $page,$searchindex " .
+                       'WHERE page_id=si_page AND ' . $match .
+                       $this->queryRedirect() . ' ' .
+                       $this->queryNamespaces();
+       }
+
        /**
         * Create or update the search index record for the given page.
         * Title and text should be pre-processed.
@@ -292,9 +316,10 @@ class SearchMySQL extends SearchEngine {
  * @ingroup Search
  */
 class MySQLSearchResultSet extends SearchResultSet {
-       function MySQLSearchResultSet( $resultSet, $terms ) {
+       function MySQLSearchResultSet( $resultSet, $terms, $totalHits=null ) {
                $this->mResultSet = $resultSet;
                $this->mTerms = $terms;
+               $this->mTotalHits = $totalHits;
        }
 
        function termMatches() {
@@ -317,4 +342,9 @@ class MySQLSearchResultSet extends SearchResultSet {
        function free() {
                $this->mResultSet->free();
        }
+
+       
+       function getTotalHits() {
+               return $this->mTotalHits;
+       }
 }
\ No newline at end of file