Allow extensions to customize the search forms. This required some cleanup and refact...
[lhc/web/wiklou.git] / includes / search / SearchEngine.php
index deba774..4ef728a 100644 (file)
@@ -22,6 +22,22 @@ class SearchEngine {
        var $namespaces = array( NS_MAIN );
        var $showRedirects = false;
 
+       /// Feature values
+       protected $features = array();
+
+       /**
+        * @var DatabaseBase
+        */
+       protected $db;
+
+       function __construct($db = null) {
+               if ( $db ) {
+                       $this->db = $db;
+               } else {
+                       $this->db = wfGetDB( DB_SLAVE );
+               }
+       }
+
        /**
         * Perform a full text search query and return a result set.
         * If title searches are not supported or disabled, return null.
@@ -46,9 +62,38 @@ class SearchEngine {
                return null;
        }
 
-       /** If this search backend can list/unlist redirects */
+       /**
+        * If this search backend can list/unlist redirects
+        * @deprecated Call supports( 'list-redirects' );
+        */
        function acceptListRedirects() {
-               return true;
+               return $this->supports( 'list-redirects' );
+       }
+
+       /**
+        * @since 1.18
+        * @param $feature String
+        * @return Boolean
+        */
+       public function supports( $feature ) {
+               switch( $feature ) {
+               case 'list-redirects':
+                       return true;
+               case 'title-suffix-filter':
+               default:
+                       return false;
+               }
+       }
+
+       /**
+        * Way to pass custom data for engines
+        * @since 1.18
+        * @param $feature String
+        * @param $data Mixed
+        * @return Noolean
+        */
+       public function setFeatureData( $feature, $data ) {
+               $this->features[$feature] = $data;
        }
 
        /**
@@ -116,19 +161,23 @@ class SearchEngine {
                        return $titleResult;
                }
 
+               $context = new RequestContext;
+
                foreach ( $allSearchTerms as $term ) {
 
                        # Exact match? No need to look further.
                        $title = Title::newFromText( $term );
-                       if ( is_null( $title ) )
+                       if ( is_null( $title ) ){
                                return null;
+                       }
 
                        if ( $title->getNamespace() == NS_SPECIAL || $title->isExternal() || $title->exists() ) {
                                return $title;
                        }
 
                        # See if it still otherwise has content is some sane sense
-                       $article = MediaWiki::articleFromTitle( $title );
+                       $context->setTitle( $title );
+                       $article = MediaWiki::articleFromTitle( $title, $context );
                        if ( $article->hasViewableContent() ) {
                                return $title;
                        }
@@ -383,10 +432,11 @@ class SearchEngine {
         */
        public static function create() {
                global $wgSearchType;
-               $dbr = wfGetDB( DB_SLAVE );
+               $dbr = null;
                if ( $wgSearchType ) {
                        $class = $wgSearchType;
                } else {
+                       $dbr = wfGetDB( DB_SLAVE );
                        $class = $dbr->getSearchEngine();
                }
                $search = new $class( $dbr );
@@ -425,13 +475,13 @@ class SearchEngine {
         * @return String
         */
        public static function getOpenSearchTemplate() {
-               global $wgOpenSearchTemplate, $wgServer, $wgScriptPath;
+               global $wgOpenSearchTemplate, $wgServer;
                if ( $wgOpenSearchTemplate )    {
                        return $wgOpenSearchTemplate;
                } else {
                        $ns = implode( '|', SearchEngine::defaultNamespaces() );
                        if ( !$ns ) $ns = "0";
-                       return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace=' . $ns;
+                       return $wgServer . wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
                }
        }
 
@@ -441,11 +491,11 @@ class SearchEngine {
         * @return String
         */
        public static function getMWSuggestTemplate() {
-               global $wgMWSuggestTemplate, $wgServer, $wgScriptPath;
+               global $wgMWSuggestTemplate, $wgServer;
                if ( $wgMWSuggestTemplate )
                        return $wgMWSuggestTemplate;
                else
-                       return $wgServer . $wgScriptPath . '/api.php?action=opensearch&search={searchTerms}&namespace={namespaces}&suggest';
+                       return $wgServer . wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace={namespaces}&suggest';
        }
 }
 
@@ -616,9 +666,23 @@ class SearchResultTooMany {
  * @ingroup Search
  */
 class SearchResult {
+
+       /**
+        * @var Revision
+        */
        var $mRevision = null;
        var $mImage = null;
 
+       /**
+        * @var Title
+        */
+       var $mTitle;
+
+       /**
+        * @var String
+        */
+       var $mText;
+
        /**
         * Return a new SearchResult and initializes it with a title.
         * 
@@ -1309,12 +1373,13 @@ class SearchHighlighter {
                 continue;
             }
             --$contextlines;
-            $pre = $wgContLang->truncate( $m[1], - $contextchars );
+            // truncate function changes ... to relevant i18n message.
+            $pre = $wgContLang->truncate( $m[1], - $contextchars, '...', false );
 
             if ( count( $m ) < 3 ) {
                 $post = '';
             } else {
-                $post = $wgContLang->truncate( $m[3], $contextchars );
+                $post = $wgContLang->truncate( $m[3], $contextchars, '...', false );
             }
 
             $found = $m[2];