Merge "Set namespaces for dtp"
[mediawiki.git] / includes / api / ApiQueryPrefixSearch.php
bloba06ed04251d77e76e1ffd0873c3005244b6bdec0
1 <?php
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
19 * @file
20 * @since 1.23
23 namespace MediaWiki\Api;
25 use SearchEngine;
26 use SearchEngineConfig;
27 use SearchEngineFactory;
29 /**
30 * @ingroup API
32 class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
33 use \MediaWiki\Api\SearchApi;
35 public function __construct(
36 ApiQuery $query,
37 string $moduleName,
38 SearchEngineConfig $searchEngineConfig,
39 SearchEngineFactory $searchEngineFactory
40 ) {
41 parent::__construct( $query, $moduleName, 'ps' );
42 // Services needed in SearchApi trait
43 $this->searchEngineConfig = $searchEngineConfig;
44 $this->searchEngineFactory = $searchEngineFactory;
47 public function execute() {
48 $this->run();
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
55 /**
56 * @param ApiPageSet|null $resultPageSet
58 private function run( $resultPageSet = null ) {
59 $params = $this->extractRequestParams();
60 $search = $params['search'];
61 $limit = $params['limit'];
62 $offset = $params['offset'];
64 $searchEngine = $this->buildSearchEngine( $params );
65 $suggestions = $searchEngine->completionSearchWithVariants( $search );
66 $titles = $searchEngine->extractTitles( $suggestions );
68 if ( $suggestions->hasMoreResults() ) {
69 $this->setContinueEnumParameter( 'offset', $offset + $limit );
72 if ( $resultPageSet ) {
73 $resultPageSet->setRedirectMergePolicy( static function ( array $current, array $new ) {
74 if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
75 $current['index'] = $new['index'];
77 return $current;
78 } );
79 $resultPageSet->populateFromTitles( $titles );
80 foreach ( $titles as $index => $title ) {
81 $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
83 } else {
84 $result = $this->getResult();
85 $count = 0;
86 foreach ( $titles as $title ) {
87 $vals = [
88 'ns' => $title->getNamespace(),
89 'title' => $title->getPrefixedText(),
91 if ( $title->isSpecialPage() ) {
92 $vals['special'] = true;
93 } else {
94 $vals['pageid'] = (int)$title->getArticleID();
96 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
97 ++$count;
98 if ( !$fit ) {
99 $this->setContinueEnumParameter( 'offset', $offset + $count );
100 break;
103 $result->addIndexedTagName(
104 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
109 public function getCacheMode( $params ) {
110 return 'public';
113 public function getAllowedParams() {
114 return $this->buildCommonApiParams();
117 public function getSearchProfileParams() {
118 return [
119 'profile' => [
120 'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
121 'help-message' => 'apihelp-query+prefixsearch-param-profile',
126 protected function getExamplesMessages() {
127 return [
128 'action=query&list=prefixsearch&pssearch=meaning'
129 => 'apihelp-query+prefixsearch-example-simple',
133 public function getHelpUrls() {
134 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Prefixsearch';
138 /** @deprecated class alias since 1.43 */
139 class_alias( ApiQueryPrefixSearch::class, 'ApiQueryPrefixSearch' );