Merge "Override momentjs's digit transform logic with MW's"
[lhc/web/wiklou.git] / includes / api / ApiQueryPrefixSearch.php
1 <?php
2 use MediaWiki\MediaWikiServices;
3
4 /**
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @since 1.23
22 */
23
24 /**
25 * @ingroup API
26 */
27 class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
28 public function __construct( $query, $moduleName ) {
29 parent::__construct( $query, $moduleName, 'ps' );
30 }
31
32 public function execute() {
33 $this->run();
34 }
35
36 public function executeGenerator( $resultPageSet ) {
37 $this->run( $resultPageSet );
38 }
39
40 /**
41 * @param ApiPageSet $resultPageSet
42 */
43 private function run( $resultPageSet = null ) {
44 $params = $this->extractRequestParams();
45 $search = $params['search'];
46 $limit = $params['limit'];
47 $namespaces = $params['namespace'];
48 $offset = $params['offset'];
49
50 $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
51 $searchEngine->setLimitOffset( $limit + 1, $offset );
52 $searchEngine->setNamespaces( $namespaces );
53 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
54
55 if ( $resultPageSet ) {
56 $resultPageSet->setRedirectMergePolicy( function( array $current, array $new ) {
57 if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
58 $current['index'] = $new['index'];
59 }
60 return $current;
61 } );
62 if ( count( $titles ) > $limit ) {
63 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
64 array_pop( $titles );
65 }
66 $resultPageSet->populateFromTitles( $titles );
67 foreach ( $titles as $index => $title ) {
68 $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
69 }
70 } else {
71 $result = $this->getResult();
72 $count = 0;
73 foreach ( $titles as $title ) {
74 if ( ++$count > $limit ) {
75 $this->setContinueEnumParameter( 'offset', $offset + $params['limit'] );
76 break;
77 }
78 $vals = [
79 'ns' => intval( $title->getNamespace() ),
80 'title' => $title->getPrefixedText(),
81 ];
82 if ( $title->isSpecialPage() ) {
83 $vals['special'] = true;
84 } else {
85 $vals['pageid'] = intval( $title->getArticleID() );
86 }
87 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
88 if ( !$fit ) {
89 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
90 break;
91 }
92 }
93 $result->addIndexedTagName(
94 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
95 );
96 }
97 }
98
99 public function getCacheMode( $params ) {
100 return 'public';
101 }
102
103 public function getAllowedParams() {
104 return [
105 'search' => [
106 ApiBase::PARAM_TYPE => 'string',
107 ApiBase::PARAM_REQUIRED => true,
108 ],
109 'namespace' => [
110 ApiBase::PARAM_DFLT => NS_MAIN,
111 ApiBase::PARAM_TYPE => 'namespace',
112 ApiBase::PARAM_ISMULTI => true,
113 ],
114 'limit' => [
115 ApiBase::PARAM_DFLT => 10,
116 ApiBase::PARAM_TYPE => 'limit',
117 ApiBase::PARAM_MIN => 1,
118 // Non-standard value for compatibility with action=opensearch
119 ApiBase::PARAM_MAX => 100,
120 ApiBase::PARAM_MAX2 => 200,
121 ],
122 'offset' => [
123 ApiBase::PARAM_DFLT => 0,
124 ApiBase::PARAM_TYPE => 'integer',
125 ],
126 ];
127 }
128
129 protected function getExamplesMessages() {
130 return [
131 'action=query&list=prefixsearch&pssearch=meaning'
132 => 'apihelp-query+prefixsearch-example-simple',
133 ];
134 }
135
136 public function getHelpUrls() {
137 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
138 }
139 }