Search backend:
authorRobert Stojnić <rainman@users.mediawiki.org>
Sun, 23 Mar 2008 17:29:43 +0000 (17:29 +0000)
committerRobert Stojnić <rainman@users.mediawiki.org>
Sun, 23 Mar 2008 17:29:43 +0000 (17:29 +0000)
* add "all:" prefix that searches all namespaces (port from LuceneSearch)
* added a simplistic replacePrefixes so that now image:something will
  always search the image namespace

includes/SearchEngine.php
includes/SearchMySQL.php
includes/SearchPostgres.php
includes/SpecialSearch.php
languages/messages/MessagesEn.php
maintenance/language/messages.inc

index 739e8a1..a2c92a4 100644 (file)
@@ -176,6 +176,37 @@ class SearchEngine {
        function setNamespaces( $namespaces ) {
                $this->namespaces = $namespaces;
        }
+       
+       /**
+        * Parse some common prefixes: all (search everything)
+        * or namespace names
+        *
+        * @param string $query
+        */
+       function replacePrefixes( $query ){
+               global $wgContLang;
+                               
+               if( strpos($query,':') === false )
+                       return $query; // nothing to do
+                       
+               $parsed = $query;
+               $allkeyword = wfMsg('searchall').":";
+               if( strncmp($query, $allkeyword, strlen($allkeyword)) == 0 ){
+                       $this->namespaces = null;
+                       $parsed = substr($query,strlen($allkeyword));
+               } else if( strpos($query,':') !== false ) {
+                       $prefix = substr($query,0,strpos($query,':'));
+                       $index = $wgContLang->getNsIndex($prefix);
+                       if($index !== false){
+                               $this->namespaces = array($index);
+                               $parsed = substr($query,strlen($prefix)+1);
+                       }
+               }
+               if(trim($parsed) == '')
+                       return $query; // prefix was the whole query
+                       
+               return $parsed;
+       }
 
        /**
         * Make a list of searchable namespaces and their canonical names.
index e6e312c..ecbb528 100644 (file)
@@ -115,6 +115,8 @@ class SearchMySQL extends SearchEngine {
         * @private
         */
        function queryNamespaces() {
+               if( is_null($this->namespaces) )
+                       return '';  # search all
                $namespaces = implode( ',', $this->namespaces );
                if ($namespaces == '') {
                        $namespaces = '0';
index 59110a5..18a02a8 100644 (file)
@@ -174,11 +174,13 @@ class SearchPostgres extends SearchEngine {
                        $query .= ' AND page_is_redirect = 0';
 
                ## Namespaces - defaults to 0
-               if ( count($this->namespaces) < 1)
-                       $query .= ' AND page_namespace = 0';
-               else {
-                       $namespaces = implode( ',', $this->namespaces );
-                       $query .= " AND page_namespace IN ($namespaces)";
+               if( !is_null($this->namespaces) ){ // null -> search all
+                       if ( count($this->namespaces) < 1)
+                               $query .= ' AND page_namespace = 0';
+                       else {
+                               $namespaces = implode( ',', $this->namespaces );
+                               $query .= " AND page_namespace IN ($namespaces)";
+                       }
                }
 
                $query .= " ORDER BY score DESC, page_id DESC";
index 434ea33..84d1568 100644 (file)
@@ -159,8 +159,10 @@ class SpecialSearch {
                $search = SearchEngine::create();
                $search->setLimitOffset( $this->limit, $this->offset );
                $search->setNamespaces( $this->namespaces );
-               $search->showRedirects = $this->searchRedirects;
-               $titleMatches = $search->searchTitle( $term );
+               $search->showRedirects = $this->searchRedirects;                
+               $rewritten = $search->replacePrefixes($term);
+               
+               $titleMatches = $search->searchTitle( $rewritten );
 
                // Sometimes the search engine knows there are too many hits
                if ($titleMatches instanceof SearchResultTooMany) {
@@ -170,7 +172,7 @@ class SpecialSearch {
                        wfProfileOut( $fname );
                        return;
                }
-               $textMatches = $search->searchText( $term );
+               $textMatches = $search->searchText( $rewritten );
                
                // did you mean...
                if($textMatches && $textMatches->hasSuggestion()){
index 29bdb41..01482e0 100644 (file)
@@ -1236,6 +1236,7 @@ Make sure that this change will maintain historical page continuity.
 'search-redirect'       => '(redirect $1)',
 'search-section'        => '(section $1)',
 'search-suggest'        => 'Did you mean: $1',
+'searchall'             => 'all',
 'showingresults'        => "Showing below up to {{PLURAL:$1|'''1''' result|'''$1''' results}} starting with #'''$2'''.",
 'showingresultsnum'     => "Showing below {{PLURAL:$3|'''1''' result|'''$3''' results}} starting with #'''$2'''.",
 'showingresultstotal'   => "Showing below results '''$1 - $2''' of '''$3'''",
index 07bc8d3..73b66ae 100644 (file)
@@ -685,6 +685,7 @@ $wgMessageStructure = array(
                'search-redirect',
                'search-section',
                'search-suggest',
+               'searchall',
                'showingresults',
                'showingresultsnum',
                'showingresultstotal',