Fix for bug 13004, in which the Postgres full-text search has too many results,
[lhc/web/wiklou.git] / includes / SearchEngine.php
index cec40c9..c22e58d 100644 (file)
@@ -1,12 +1,7 @@
 <?php
 /**
  * Contain a class for special pages
- * @package MediaWiki
- * @subpackage Search
- */
-
-/**
- * @package MediaWiki
+ * @addtogroup Search
  */
 class SearchEngine {
        var $limit = 10;
@@ -45,12 +40,10 @@ class SearchEngine {
         * If an exact title match can be find, or a very slightly close match,
         * return the title. If no match, returns NULL.
         *
-        * @static
         * @param string $term
         * @return Title
-        * @private
         */
-       function getNearMatch( $searchterm ) {
+       public static function getNearMatch( $searchterm ) {
                global $wgContLang;
 
                $allSearchTerms = array($searchterm);
@@ -58,7 +51,7 @@ class SearchEngine {
                if($wgContLang->hasVariants()){
                        $allSearchTerms = array_merge($allSearchTerms,$wgContLang->convertLinkToAllVariants($searchterm));
                }
-
+               
                foreach($allSearchTerms as $term){
 
                        # Exact match? No need to look further.
@@ -109,6 +102,12 @@ class SearchEngine {
                                        return $title;
                                }
                        }
+
+                       // Give hooks a chance at better match variants
+                       $title = null;
+                       if( !wfRunHooks( 'SearchGetNearMatch', array( $term, &$title ) ) ) {
+                               return $title;
+                       }
                }
 
                $title = Title::newFromText( $searchterm );
@@ -116,7 +115,7 @@ class SearchEngine {
                # Entering an IP address goes to the contributions page
                if ( ( $title->getNamespace() == NS_USER && User::isIP($title->getText() ) )
                        || User::isIP( trim( $searchterm ) ) ) {
-                       return SpecialPage::getTitleFor( 'Contributions', $title->getDbkey() );
+                       return SpecialPage::getTitleFor( 'Contributions', $title->getDBkey() );
                }
 
 
@@ -124,17 +123,33 @@ class SearchEngine {
                if ( $title->getNamespace() == NS_USER ) {
                        return $title;
                }
+               
+               # Go to images that exist even if there's no local page.
+               # There may have been a funny upload, or it may be on a shared
+               # file repository such as Wikimedia Commons.
+               if( $title->getNamespace() == NS_IMAGE ) {
+                       $image = wfFindFile( $title );
+                       if( $image ) {
+                               return $title;
+                       }
+               }
+
+               # MediaWiki namespace? Page may be "implied" if not customized.
+               # Just return it, with caps forced as the message system likes it.
+               if( $title->getNamespace() == NS_MEDIAWIKI ) {
+                       return Title::makeTitle( NS_MEDIAWIKI, $wgContLang->ucfirst( $title->getText() ) );
+               }
 
                # Quoted term? Try without the quotes...
                $matches = array();
                if( preg_match( '/^"([^"]+)"$/', $searchterm, $matches ) ) {
                        return SearchEngine::getNearMatch( $matches[1] );
                }
-
+               
                return NULL;
        }
 
-       function legalSearchChars() {
+       public static function legalSearchChars() {
                return "A-Za-z_'0-9\\x80-\\xFF\\-";
        }
 
@@ -165,9 +180,8 @@ class SearchEngine {
        /**
         * Make a list of searchable namespaces and their canonical names.
         * @return array
-        * @access public
         */
-       function searchableNamespaces() {
+       public static function searchableNamespaces() {
                global $wgContLang;
                $arr = array();
                foreach( $wgContLang->getNamespaces() as $ns => $name ) {
@@ -193,9 +207,8 @@ class SearchEngine {
         * active database backend, and return a configured instance.
         *
         * @return SearchEngine
-        * @private
         */
-       function create() {
+       public static function create() {
                global $wgDBtype, $wgSearchType;
                if( $wgSearchType ) {
                        $class = $wgSearchType;
@@ -203,6 +216,8 @@ class SearchEngine {
                        $class = 'SearchMySQL4';
                } else if ( $wgDBtype == 'postgres' ) {
                        $class = 'SearchPostgres';
+               } else if ( $wgDBtype == 'oracle' ) {
+                       $class = 'SearchOracle';
                } else {
                        $class = 'SearchEngineDummy';
                }
@@ -232,12 +247,15 @@ class SearchEngine {
         * @param string $title
         * @abstract
         */
-    function updateTitle( $id, $title ) {
+       function updateTitle( $id, $title ) {
                // no-op
-    }
+       }
 }
 
-/** @package MediaWiki */
+
+/**
+ * @addtogroup Search
+ */
 class SearchResultSet {
        /**
         * Fetch an array of regular expression fragments for matching
@@ -310,10 +328,30 @@ class SearchResultSet {
        function next() {
                return false;
        }
+       
+       /**
+        * Frees the result set, if applicable.
+        * @ access public
+        */
+       function free() {
+               // ...
+       }
 }
 
-/** @package MediaWiki */
+
+/**
+ * @addtogroup Search
+ */
+class SearchResultTooMany {
+       ## Some search engines may bail out if too many matches are found
+}
+
+
+/**
+ * @addtogroup Search
+ */
 class SearchResult {
+
        function SearchResult( $row ) {
                $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
        }
@@ -335,7 +373,7 @@ class SearchResult {
 }
 
 /**
- * @package MediaWiki
+ * @addtogroup Search
  */
 class SearchEngineDummy {
        function search( $term ) {
@@ -348,4 +386,4 @@ class SearchEngineDummy {
        function searchtitle() {}
        function searchtext() {}
 }
-?>
+