Merge "Push common search api parameters into SearchApi class"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Wed, 27 Jul 2016 09:18:33 +0000 (09:18 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Wed, 27 Jul 2016 09:18:33 +0000 (09:18 +0000)
1  2 
includes/api/ApiQueryPrefixSearch.php
includes/api/ApiQuerySearch.php

@@@ -1,4 -1,5 +1,4 @@@
  <?php
 -use MediaWiki\MediaWikiServices;
  
  /**
   * This program is free software; you can redistribute it and/or modify
@@@ -105,42 -106,18 +105,18 @@@ class ApiQueryPrefixSearch extends ApiQ
                if ( $this->allowedParams !== null ) {
                        return $this->allowedParams;
                }
-               $this->allowedParams = [
-                       'search' => [
-                               ApiBase::PARAM_TYPE => 'string',
-                               ApiBase::PARAM_REQUIRED => true,
-                       ],
-                       'namespace' => [
-                               ApiBase::PARAM_DFLT => NS_MAIN,
-                               ApiBase::PARAM_TYPE => 'namespace',
-                               ApiBase::PARAM_ISMULTI => true,
-                       ],
-                       'limit' => [
-                               ApiBase::PARAM_DFLT => 10,
-                               ApiBase::PARAM_TYPE => 'limit',
-                               ApiBase::PARAM_MIN => 1,
-                               // Non-standard value for compatibility with action=opensearch
-                               ApiBase::PARAM_MAX => 100,
-                               ApiBase::PARAM_MAX2 => 200,
-                       ],
-                       'offset' => [
-                               ApiBase::PARAM_DFLT => 0,
-                               ApiBase::PARAM_TYPE => 'integer',
-                       ],
-               ];
-               $profileParam = $this->buildProfileApiParam( SearchEngine::COMPLETION_PROFILE_TYPE,
-                       'apihelp-query+prefixsearch-param-profile' );
-               if ( $profileParam ) {
-                       $this->allowedParams['profile'] = $profileParam;
-               }
+               $this->allowedParams = $this->buildCommonApiParams();
                return $this->allowedParams;
        }
  
        public function getSearchProfileParams() {
-               if ( isset( $this->getAllowedParams()['profile'] ) ) {
-                       return [ SearchEngine::COMPLETION_PROFILE_TYPE => 'profile' ];
-               }
-               return [];
+               return [
+                       'profile' => [
+                               'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
+                               'help-message' => 'apihelp-query+prefixsearch-param-profile',
+                       ],
+               ];
        }
  
        protected function getExamplesMessages() {
@@@ -37,14 -37,6 +37,6 @@@ class ApiQuerySearch extends ApiQueryGe
        /** @var array list of api allowed params */
        private $allowedParams;
  
-       /**
-        * 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( ApiQuery $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'sr' );
        }
                global $wgContLang;
                $params = $this->extractRequestParams();
  
-               if ( isset( $params['backend'] ) && $params['backend'] == self::BACKEND_NULL_PARAM ) {
-                       unset( $params['backend'] );
-               }
                // Extract parameters
                $query = $params['search'];
                $what = $params['what'];
                $hasInterwikiResults = false;
                $totalhits = null;
                if ( $interwiki && $resultPageSet === null && $matches->hasInterwikiResults() ) {
 -                      foreach ( $matches->getInterwikiResults() as $matches ) {
 -                              $matches = $matches->getInterwikiResults();
 +                      foreach ( $matches->getInterwikiResults() as $interwikiMatches ) {
                                $hasInterwikiResults = true;
  
                                // Include number of results if requested
                                if ( $resultPageSet === null && isset( $searchInfo['totalhits'] ) ) {
 -                                      $totalhits += $matches->getTotalHits();
 +                                      $totalhits += $interwikiMatches->getTotalHits();
                                }
  
 -                              $result = $matches->next();
 +                              $result = $interwikiMatches->next();
                                while ( $result ) {
                                        $title = $result->getTitle();
  
                                                $titles[] = $title;
                                        }
  
 -                                      $result = $matches->next();
 +                                      $result = $interwikiMatches->next();
                                }
                        }
                        if ( $totalhits !== null ) {
                        return $this->allowedParams;
                }
  
-               $this->allowedParams = [
-                       'search' => [
-                               ApiBase::PARAM_TYPE => 'string',
-                               ApiBase::PARAM_REQUIRED => true
-                       ],
-                       'namespace' => [
-                               ApiBase::PARAM_DFLT => NS_MAIN,
-                               ApiBase::PARAM_TYPE => 'namespace',
-                               ApiBase::PARAM_ISMULTI => true,
-                       ],
+               $this->allowedParams = $this->buildCommonApiParams() + [
                        'what' => [
                                ApiBase::PARAM_TYPE => [
                                        'title',
                                ApiBase::PARAM_ISMULTI => true,
                                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
                        ],
-                       'offset' => [
-                               ApiBase::PARAM_DFLT => 0,
-                               ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
-                       ],
-                       'limit' => [
-                               ApiBase::PARAM_DFLT => 10,
-                               ApiBase::PARAM_TYPE => 'limit',
-                               ApiBase::PARAM_MIN => 1,
-                               ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
-                               ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
-                       ],
                        'interwiki' => false,
                        'enablerewrites' => false,
                ];
  
-               $searchConfig = MediaWikiServices::getInstance()->getSearchEngineConfig();
-               $alternatives = $searchConfig->getSearchTypes();
-               if ( count( $alternatives ) > 1 ) {
-                       if ( $alternatives[0] === null ) {
-                               $alternatives[0] = self::BACKEND_NULL_PARAM;
-                       }
-                       $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 $this->allowedParams;
        }
  
        public function getSearchProfileParams() {
-               if ( isset( $this->getAllowedParams()['qiprofile'] ) ) {
-                       return [ SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE => 'qiprofile' ];
-               }
-               return [];
+               return [
+                       'qiprofile' => [
+                               'profile-type' => SearchEngine::FT_QUERY_INDEP_PROFILE_TYPE,
+                               'help-message' => 'apihelp-query+search-param-qiprofile',
+                       ],
+               ];
        }
  
        protected function getExamplesMessages() {