Merge "Allow minor edits to be filtered out of Special:Contributions"
[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 use SearchApi;
29
30 /** @var array list of api allowed params */
31 private $allowedParams;
32
33 public function __construct( $query, $moduleName ) {
34 parent::__construct( $query, $moduleName, 'ps' );
35 }
36
37 public function execute() {
38 $this->run();
39 }
40
41 public function executeGenerator( $resultPageSet ) {
42 $this->run( $resultPageSet );
43 }
44
45 /**
46 * @param ApiPageSet $resultPageSet
47 */
48 private function run( $resultPageSet = null ) {
49 $params = $this->extractRequestParams();
50 $search = $params['search'];
51 $limit = $params['limit'];
52 $offset = $params['offset'];
53
54 $searchEngine = $this->buildSearchEngine( $params );
55 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
56
57 if ( $resultPageSet ) {
58 $resultPageSet->setRedirectMergePolicy( function( array $current, array $new ) {
59 if ( !isset( $current['index'] ) || $new['index'] < $current['index'] ) {
60 $current['index'] = $new['index'];
61 }
62 return $current;
63 } );
64 if ( count( $titles ) > $limit ) {
65 $this->setContinueEnumParameter( 'offset', $offset + $limit );
66 array_pop( $titles );
67 }
68 $resultPageSet->populateFromTitles( $titles );
69 foreach ( $titles as $index => $title ) {
70 $resultPageSet->setGeneratorData( $title, [ 'index' => $index + $offset + 1 ] );
71 }
72 } else {
73 $result = $this->getResult();
74 $count = 0;
75 foreach ( $titles as $title ) {
76 if ( ++$count > $limit ) {
77 $this->setContinueEnumParameter( 'offset', $offset + $limit );
78 break;
79 }
80 $vals = [
81 'ns' => intval( $title->getNamespace() ),
82 'title' => $title->getPrefixedText(),
83 ];
84 if ( $title->isSpecialPage() ) {
85 $vals['special'] = true;
86 } else {
87 $vals['pageid'] = intval( $title->getArticleID() );
88 }
89 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
90 if ( !$fit ) {
91 $this->setContinueEnumParameter( 'offset', $offset + $count - 1 );
92 break;
93 }
94 }
95 $result->addIndexedTagName(
96 [ 'query', $this->getModuleName() ], $this->getModulePrefix()
97 );
98 }
99 }
100
101 public function getCacheMode( $params ) {
102 return 'public';
103 }
104
105 public function getAllowedParams() {
106 if ( $this->allowedParams !== null ) {
107 return $this->allowedParams;
108 }
109 $this->allowedParams = [
110 'search' => [
111 ApiBase::PARAM_TYPE => 'string',
112 ApiBase::PARAM_REQUIRED => true,
113 ],
114 'namespace' => [
115 ApiBase::PARAM_DFLT => NS_MAIN,
116 ApiBase::PARAM_TYPE => 'namespace',
117 ApiBase::PARAM_ISMULTI => true,
118 ],
119 'limit' => [
120 ApiBase::PARAM_DFLT => 10,
121 ApiBase::PARAM_TYPE => 'limit',
122 ApiBase::PARAM_MIN => 1,
123 // Non-standard value for compatibility with action=opensearch
124 ApiBase::PARAM_MAX => 100,
125 ApiBase::PARAM_MAX2 => 200,
126 ],
127 'offset' => [
128 ApiBase::PARAM_DFLT => 0,
129 ApiBase::PARAM_TYPE => 'integer',
130 ],
131 ];
132 $profileParam = $this->buildProfileApiParam( SearchEngine::COMPLETION_PROFILE_TYPE,
133 'apihelp-query+prefixsearch-param-profile' );
134 if ( $profileParam ) {
135 $this->allowedParams['profile'] = $profileParam;
136 }
137 return $this->allowedParams;
138 }
139
140 public function getSearchProfileParams() {
141 if ( isset( $this->getAllowedParams()['profile'] ) ) {
142 return [ SearchEngine::COMPLETION_PROFILE_TYPE => 'profile' ];
143 }
144 return [];
145 }
146
147 protected function getExamplesMessages() {
148 return [
149 'action=query&list=prefixsearch&pssearch=meaning'
150 => 'apihelp-query+prefixsearch-example-simple',
151 ];
152 }
153
154 public function getHelpUrls() {
155 return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
156 }
157 }