Expose SearchEngine specific profiles
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
index 3955cc5..80798a1 100644 (file)
  * @file
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Query module to perform full text search within wiki titles and content
  *
  * @ingroup API
  */
 class ApiQuerySearch extends ApiQueryGeneratorBase {
+       use SearchApi;
+
+       /** @var array list of api allowed params */
+       private $allowedParams;
 
        /**
         * When $wgSearchType is null, $wgSearchAlternatives[0] is null. Null isn't
@@ -59,8 +65,11 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                global $wgContLang;
                $params = $this->extractRequestParams();
 
+               if ( isset( $params['backend'] ) && $params['backend'] == self::BACKEND_NULL_PARAM ) {
+                       unset( $params['backend'] );
+               }
+
                // Extract parameters
-               $limit = $params['limit'];
                $query = $params['search'];
                $what = $params['what'];
                $interwiki = $params['interwiki'];
@@ -78,10 +87,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                }
 
                // Create search engine instance and set options
-               $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 = $this->buildSearchEngine( $params );
                $search->setFeatureData( 'rewrite', (bool)$params['enablerewrites'] );
 
                $query = $search->transformSearchTerm( $query );
@@ -95,7 +101,8 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                } elseif ( $what == 'nearmatch' ) {
                        // near matches must receive the user input as provided, otherwise
                        // the near matches within namespaces are lost.
-                       $matches = SearchEngine::getNearMatchResultSet( $params['search'] );
+                       $matches = $search->getNearMatcher( $this->getConfig() )
+                               ->getNearMatchResultSet( $params['search'] );
                } else {
                        // We default to title searches; this is a terrible legacy
                        // of the way we initially set up the MySQL fulltext-based
@@ -148,6 +155,7 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                $titles = [];
                $count = 0;
                $result = $matches->next();
+               $limit = $params['limit'];
 
                while ( $result ) {
                        if ( ++$count > $limit ) {
@@ -297,7 +305,11 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
        }
 
        public function getAllowedParams() {
-               $params = [
+               if ( $this->allowedParams !== null ) {
+                       return $this->allowedParams;
+               }
+
+               $this->allowedParams = [
                        'search' => [
                                ApiBase::PARAM_TYPE => 'string',
                                ApiBase::PARAM_REQUIRED => true
@@ -358,18 +370,37 @@ class ApiQuerySearch extends ApiQueryGeneratorBase {
                        'enablerewrites' => false,
                ];
 
-               $alternatives = SearchEngine::getSearchTypes();
+               $searchConfig = MediaWikiServices::getInstance()->getSearchEngineConfig();
+               $alternatives = $searchConfig->getSearchTypes();
                if ( count( $alternatives ) > 1 ) {
                        if ( $alternatives[0] === null ) {
                                $alternatives[0] = self::BACKEND_NULL_PARAM;
                        }
-                       $params['backend'] = [
-                               ApiBase::PARAM_DFLT => $this->getConfig()->get( 'SearchType' ),
+                       $this->allowedParams['backend'] = [
+                               ApiBase::PARAM_DFLT => $searchConfig->getSearchType(),
                                ApiBase::PARAM_TYPE => $alternatives,
                        ];
+                       // @todo: support profile selection when multiple
+                       // backends are available. The solution could be to
+                       // merge all possible profiles and let ApiBase
+                       // subclasses do the check. Making ApiHelp and ApiSandbox
+                       // comprehensive might be more difficult.
+               } else {
+                       $profileParam = $this->buildProfileApiParam( SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE,
+                               'apihelp-query+search-param-qiprofile' );
+                       if ( $profileParam ) {
+                               $this->allowedParams['qiprofile'] = $profileParam;
+                       }
                }
 
-               return $params;
+               return $this->allowedParams;
+       }
+
+       public function getSearchProfileParams() {
+               if ( isset( $this->getAllowedParams()['qiprofile'] ) ) {
+                       return [ SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE => 'qiprofile' ];
+               }
+               return [];
        }
 
        protected function getExamplesMessages() {