Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / includes / api / ApiQueryPrefixSearch.php
index 5c50273..3bf6d3f 100644 (file)
@@ -1,5 +1,4 @@
 <?php
-use MediaWiki\MediaWikiServices;
 
 /**
  * This program is free software; you can redistribute it and/or modify
@@ -25,6 +24,11 @@ use MediaWiki\MediaWikiServices;
  * @ingroup API
  */
 class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
+       use SearchApi;
+
+       /** @var array list of api allowed params */
+       private $allowedParams;
+
        public function __construct( $query, $moduleName ) {
                parent::__construct( $query, $moduleName, 'ps' );
        }
@@ -44,12 +48,9 @@ class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
                $params = $this->extractRequestParams();
                $search = $params['search'];
                $limit = $params['limit'];
-               $namespaces = $params['namespace'];
                $offset = $params['offset'];
 
-               $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
-               $searchEngine->setLimitOffset( $limit + 1, $offset );
-               $searchEngine->setNamespaces( $namespaces );
+               $searchEngine = $this->buildSearchEngine( $params );
                $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
 
                if ( $resultPageSet ) {
@@ -60,7 +61,7 @@ class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
                                return $current;
                        } );
                        if ( count( $titles ) > $limit ) {
-                               $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
+                               $this->setContinueEnumParameter( 'offset', $offset + $limit );
                                array_pop( $titles );
                        }
                        $resultPageSet->populateFromTitles( $titles );
@@ -72,7 +73,7 @@ class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
                        $count = 0;
                        foreach ( $titles as $title ) {
                                if ( ++$count > $limit ) {
-                                       $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
+                                       $this->setContinueEnumParameter( 'offset', $offset + $limit );
                                        break;
                                }
                                $vals = [
@@ -101,29 +102,21 @@ class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
        }
 
        public function getAllowedParams() {
-                       return [
-                               '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',
-                               ],
-                       ];
+               if ( $this->allowedParams !== null ) {
+                       return $this->allowedParams;
+               }
+               $this->allowedParams = $this->buildCommonApiParams();
+
+               return $this->allowedParams;
+       }
+
+       public function getSearchProfileParams() {
+               return [
+                       'profile' => [
+                               'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
+                               'help-message' => 'apihelp-query+prefixsearch-param-profile',
+                       ],
+               ];
        }
 
        protected function getExamplesMessages() {