Select search backend by query parameter
[lhc/web/wiklou.git] / includes / search / SearchEngine.php
index 13e8a00..bfe7dda 100644 (file)
@@ -59,7 +59,7 @@ class SearchEngine {
         * STUB
         *
         * @param string $term raw search term
-        * @return SearchResultSet
+        * @return SearchResultSet|Status|null
         */
        function searchText( $term ) {
                return null;
@@ -71,7 +71,7 @@ class SearchEngine {
         * STUB
         *
         * @param string $term raw search term
-        * @return SearchResultSet
+        * @return SearchResultSet|null
         */
        function searchTitle( $term ) {
                return null;
@@ -453,13 +453,17 @@ class SearchEngine {
         * Load up the appropriate search engine class for the currently
         * active database backend, and return a configured instance.
         *
+        * @param String $type Type of search backend, if not the default
         * @return SearchEngine
         */
-       public static function create() {
+       public static function create( $type = null ) {
                global $wgSearchType;
                $dbr = null;
-               if ( $wgSearchType ) {
-                       $class = $wgSearchType;
+
+               $alternatives = self::getSearchTypes();
+               $type = $type !== null ? $type : $wgSearchType;
+               if ( $type && in_array( $type, $alternatives ) ) {
+                       $class = $type;
                } else {
                        $dbr = wfGetDB( DB_SLAVE );
                        $class = $dbr->getSearchEngine();
@@ -469,6 +473,22 @@ class SearchEngine {
                return $search;
        }
 
+       /**
+        * Return the search engines we support. If only $wgSearchType
+        * is set, it'll be an array of just that one item.
+        *
+        * @return array
+        */
+       public static function getSearchTypes() {
+               global $wgSearchType, $wgSearchTypeAlternatives;
+               static $alternatives = null;
+               if ( $alternatives === null ) {
+                       $alternatives = $wgSearchTypeAlternatives ?: array();
+                       array_unshift( $alternatives, $wgSearchType );
+               }
+               return $alternatives;
+       }
+
        /**
         * Create or update the search index record for the given page.
         * Title and text should be pre-processed.
@@ -494,6 +514,18 @@ class SearchEngine {
                // no-op
        }
 
+       /**
+        * Delete an indexed page
+        * Title should be pre-processed.
+        * STUB
+        *
+        * @param Integer $id Page id that was deleted
+        * @param String $title Title of page that was deleted
+        */
+       function delete( $id, $title ) {
+               // no-op
+       }
+
        /**
         * Get OpenSearch suggestion template
         *
@@ -511,6 +543,20 @@ class SearchEngine {
                        return $wgCanonicalServer . wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
                }
        }
+
+       /**
+        * Get the raw text for updating the index from a content object
+        * Nicer search backends could possibly do something cooler than
+        * just returning raw text
+        *
+        * @todo This isn't ideal, we'd really like to have content-specific handling here
+        * @param Title $t Title we're indexing
+        * @param Content $c Content of the page to index
+        * @return string
+        */
+       public function getTextFromContent( Title $t, Content $c = null ) {
+               return $c ? $c->getTextForSearchIndex() : '';
+       }
 }
 
 /**
@@ -803,10 +849,8 @@ class SearchResult {
        protected function initText() {
                if ( !isset( $this->mText ) ) {
                        if ( $this->mRevision != null ) {
-                               //TODO: if we could plug in some code that knows about special content models *and* about
-                               //      special features of the search engine, the search could benefit.
-                               $content = $this->mRevision->getContent();
-                               $this->mText = $content ? $content->getTextForSearchIndex() : '';
+                               $this->mText = SearchEngine::create()
+                                       ->getTextFromContent( $this->mTitle, $this->mRevision->getContent() );
                        } else { // TODO: can we fetch raw wikitext for commons images?
                                $this->mText = '';
                        }