diff options
Diffstat (limited to 'includes/api/ApiQuerySearch.php')
-rw-r--r-- | includes/api/ApiQuerySearch.php | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/includes/api/ApiQuerySearch.php b/includes/api/ApiQuerySearch.php index 364433d5..36b55979 100644 --- a/includes/api/ApiQuerySearch.php +++ b/includes/api/ApiQuerySearch.php @@ -31,6 +31,14 @@ */ 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,6 +102,8 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { } if ( is_null( $matches ) ) { $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" ); + } elseif ( $matches instanceof Status && !$matches->isGood() ) { + $this->dieUsage( $matches->getWikiText(), 'search-error' ); } $apiResult = $this->getResult(); @@ -168,7 +179,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { } } if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) { - $vals['hasrelated'] = ""; + $vals['hasrelated'] = ''; } // Add item to results and see whether it fits @@ -199,13 +210,15 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { } public function getAllowedParams() { - return array( + global $wgSearchType; + + $params = array( 'search' => array( ApiBase::PARAM_TYPE => 'string', ApiBase::PARAM_REQUIRED => true ), 'namespace' => array( - ApiBase::PARAM_DFLT => 0, + ApiBase::PARAM_DFLT => NS_MAIN, ApiBase::PARAM_TYPE => 'namespace', ApiBase::PARAM_ISMULTI => true, ), @@ -252,10 +265,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', @@ -278,6 +304,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() { @@ -345,6 +377,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { return array_merge( parent::getPossibleErrors(), array( array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ), array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ), + array( 'code' => 'search-error', 'info' => 'search error has occurred' ), ) ); } @@ -359,8 +392,4 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { public function getHelpUrls() { return 'https://www.mediawiki.org/wiki/API:Search'; } - - public function getVersion() { - return __CLASS__ . ': $Id$'; - } } |