Call method with the same name it's defined with
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
index ab78ba6..4fedebc 100644 (file)
  */
 class ApiQuerySearch extends ApiQueryGeneratorBase {
 
+       /**
+        * When $wgSearchType is null, $wgSearchAlternatives[0] is null. Null isn't
+        * a valid option for an array for PARAM_TYPE, so we'll use a fake name
+        * that can't possibly be a class name and describes what the null behavior
+        * does
+        */
+       const BACKEND_NULL_PARAM = 'database-backed';
+
        public function __construct( $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'sr' );
        }
@@ -59,7 +67,8 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                $prop = array_flip( $params['prop'] );
 
                // Create search engine instance and set options
-               $search = SearchEngine::create();
+               $search = isset( $params['backend'] ) && $params['backend'] != self::BACKEND_NULL_PARAM ?
+                       SearchEngine::create( $params['backend'] ) : SearchEngine::create();
                $search->setLimitOffset( $limit + 1, $params['offset'] );
                $search->setNamespaces( $params['namespace'] );
                $search->showRedirects = $params['redirects'];
@@ -93,7 +102,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                }
                if ( is_null( $matches ) ) {
                        $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
-               } elseif( $matches instanceof Status && !$matches->isGood() ) {
+               } elseif ( $matches instanceof Status && !$matches->isGood() ) {
                        $this->dieUsage( $matches->getWikiText(), 'search-error' );
                }
 
@@ -103,12 +112,12 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        $totalhits = $matches->getTotalHits();
                        if ( $totalhits !== null ) {
                                $apiResult->addValue( array( 'query', 'searchinfo' ),
-                                               'totalhits', $totalhits );
+                                       'totalhits', $totalhits );
                        }
                }
                if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
                        $apiResult->addValue( array( 'query', 'searchinfo' ),
-                                               'suggestion', $matches->getSuggestionQuery() );
+                               'suggestion', $matches->getSuggestionQuery() );
                }
 
                // Add the search results to the result
@@ -118,8 +127,9 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                $result = $matches->next();
 
                while ( $result ) {
-                       if ( ++ $count > $limit ) {
-                               // We've reached the one extra which shows that there are additional items to be had. Stop here...
+                       if ( ++$count > $limit ) {
+                               // We've reached the one extra which shows that there are
+                               // additional items to be had. Stop here...
                                $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
                                break;
                        }
@@ -175,7 +185,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
 
                                // Add item to results and see whether it fits
                                $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
-                                               null, $vals );
+                                       null, $vals );
                                if ( !$fit ) {
                                        $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
                                        break;
@@ -189,8 +199,8 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
 
                if ( is_null( $resultPageSet ) ) {
                        $apiResult->setIndexedTagName_internal( array(
-                                               'query', $this->getModuleName()
-                                       ), 'p' );
+                               'query', $this->getModuleName()
+                       ), 'p' );
                } else {
                        $resultPageSet->populateFromTitles( $titles );
                }
@@ -201,7 +211,9 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
        }
 
        public function getAllowedParams() {
-               return array(
+               global $wgSearchType;
+
+               $params = array(
                        'search' => array(
                                ApiBase::PARAM_TYPE => 'string',
                                ApiBase::PARAM_REQUIRED => true
@@ -254,10 +266,23 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
                        )
                );
+
+               $alternatives = SearchEngine::getSearchTypes();
+               if ( count( $alternatives ) > 1 ) {
+                       if ( $alternatives[0] === null ) {
+                               $alternatives[0] = self::BACKEND_NULL_PARAM;
+                       }
+                       $params['backend'] = array(
+                               ApiBase::PARAM_DFLT => $wgSearchType,
+                               ApiBase::PARAM_TYPE => $alternatives,
+                       );
+               }
+
+               return $params;
        }
 
        public function getParamDescription() {
-               return array(
+               $descriptions = array(
                        'search' => 'Search for all page titles (or content) that has this value',
                        'namespace' => 'The namespace(s) to enumerate',
                        'what' => 'Search inside the text or titles',
@@ -280,6 +305,12 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        'offset' => 'Use this value to continue paging (return by query)',
                        'limit' => 'How many total pages to return'
                );
+
+               if ( count( SearchEngine::getSearchTypes() ) > 1 ) {
+                       $descriptions['backend'] = 'Which search backend to use, if not the default';
+               }
+
+               return $descriptions;
        }
 
        public function getResultProperties() {