Note incorrect docs in DefaultSettings.php
[lhc/web/wiklou.git] / includes / SearchEngine.php
index 85071d5..cd8f804 100644 (file)
@@ -13,6 +13,7 @@
 class SearchEngine {
        var $limit = 10;
        var $offset = 0;
+       var $prefix = '';
        var $searchTerms = array();
        var $namespaces = array( NS_MAIN );
        var $showRedirects = false;
@@ -43,6 +44,19 @@ class SearchEngine {
                return null;
        }
        
+       /** If this search backend can list/unlist redirects */
+       function acceptListRedirects() {
+               return true;
+       }
+       
+       /**
+        * Transform search term in cases when parts of the query came as different GET params (when supported)
+        * e.g. for prefix queries: search=test&prefix=Main_Page/Archive -> test prefix:Main Page/Archive
+        */
+       function transformSearchTerm( $term ) {
+               return $term;
+       }
+       
        /**
         * If an exact title match can be find, or a very slightly close match,
         * return the title. If no match, returns NULL.
@@ -122,7 +136,7 @@ class SearchEngine {
                # 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 ) {
+               if( $title->getNamespace() == NS_FILE ) {
                        $image = wfFindFile( $title );
                        if( $image ) {
                                return $title;
@@ -145,7 +159,7 @@ class SearchEngine {
        }
 
        public static function legalSearchChars() {
-               return "A-Za-z_'0-9\\x80-\\xFF\\-";
+               return "A-Za-z_'.0-9\\x80-\\xFF\\-";
        }
 
        /**
@@ -262,7 +276,51 @@ class SearchEngine {
                
                return array_keys($wgNamespacesToBeSearchedDefault, true);
        }
-
+       
+       /**
+        * Get a list of namespace names useful for showing in tooltips
+        * and preferences
+        *
+        * @param unknown_type $namespaces
+        */
+       public static function namespacesAsText( $namespaces ){
+               global $wgContLang;
+               
+               $formatted = array_map( array($wgContLang,'getFormattedNsText'), $namespaces );
+               foreach( $formatted as $key => $ns ){
+                       if ( empty($ns) )
+                               $formatted[$key] = wfMsg( 'blanknamespace' );
+               }
+               return $formatted;
+       }
+       
+       /**
+        * An array of "project" namespaces indexes typically searched
+        * by logged-in users
+        * 
+        * @return array 
+        * @static
+        */
+       public static function projectNamespaces() {
+               global $wgNamespacesToBeSearchedDefault, $wgNamespacesToBeSearchedProject;
+               
+               return array_keys( $wgNamespacesToBeSearchedProject, true );
+       }
+       
+       /**
+        * An array of "project" namespaces indexes typically searched
+        * by logged-in users in addition to the default namespaces
+        * 
+        * @return array 
+        * @static
+        */
+       public static function defaultAndProjectNamespaces() {
+               global $wgNamespacesToBeSearchedDefault, $wgNamespacesToBeSearchedProject;
+               
+               return array_keys( $wgNamespacesToBeSearchedDefault + 
+                       $wgNamespacesToBeSearchedProject, true);
+       }
+       
        /**
         * Return a 'cleaned up' search string
         *
@@ -325,11 +383,11 @@ class SearchEngine {
         */
        public static function getOpenSearchTemplate() {
                global $wgOpenSearchTemplate, $wgServer, $wgScriptPath;
-               if($wgOpenSearchTemplate)               
+               if( $wgOpenSearchTemplate )     {       
                        return $wgOpenSearchTemplate;
-               else
-                       $ns = implode(',',SearchEngine::defaultNamespaces());
-                       if(!$ns) $ns = "0";
+               } else 
+                       $ns = implode( '|', SearchEngine::defaultNamespaces() );
+                       if( !$ns ) $ns = "0";
                        return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace='.$ns;
                }
        }
@@ -345,7 +403,7 @@ class SearchEngine {
                if($wgMWSuggestTemplate)                
                        return $wgMWSuggestTemplate;
                else 
-                       return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace={namespaces}';
+                       return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace={namespaces}&suggest';
        }
 }
 
@@ -412,7 +470,7 @@ class SearchResultSet {
        }
 
        /**
-        * @return string highlighted suggested query, '' if none
+        * @return string HTML highlighted suggested query, '' if none
         */
        function getSuggestionSnippet(){
                return '';
@@ -485,11 +543,11 @@ class SearchResult {
        var $mRevision = null;
        var $mImage = null;
 
-       function SearchResult( $row ) {
+       function __construct( $row ) {
                $this->mTitle = Title::makeTitle( $row->page_namespace, $row->page_title );
                if( !is_null($this->mTitle) ){
                        $this->mRevision = Revision::newFromTitle( $this->mTitle );
-                       if($this->mTitle->getNamespace() == NS_IMAGE)
+                       if( $this->mTitle->getNamespace() === NS_FILE )
                                $this->mImage = wfFindFile( $this->mTitle );
                }
        }
@@ -696,7 +754,7 @@ class SearchHighlighter {
                                                if($key == 2){
                                                        // see if this is an image link
                                                        $ns = substr($val[0],2,-1);
-                                                       if( $wgContLang->getNsIndex($ns) != NS_IMAGE )
+                                                       if( $wgContLang->getNsIndex($ns) != NS_FILE )
                                                                break;
                                                        
                                                }
@@ -1066,7 +1124,7 @@ class SearchHighlighter {
                global $wgContLang;
                $ns = substr( $matches[1], 0, $colon );
                $index = $wgContLang->getNsIndex($ns);
-               if( $index !== false && ($index == NS_IMAGE || $index == NS_CATEGORY) )
+               if( $index !== false && ($index == NS_FILE || $index == NS_CATEGORY) )
                        return $matches[0]; // return the whole thing 
                else
                        return $matches[2];
@@ -1107,12 +1165,12 @@ class SearchHighlighter {
                 continue;
             }
             --$contextlines;
-            $pre = $wgContLang->truncate( $m[1], -$contextchars, ' ... ' );
+            $pre = $wgContLang->truncate( $m[1], -$contextchars );
 
             if ( count( $m ) < 3 ) {
                 $post = '';
             } else {
-                $post = $wgContLang->truncate( $m[3], $contextchars, ' ... ' );
+                $post = $wgContLang->truncate( $m[3], $contextchars );
             }
 
             $found = $m[2];