Merge "Convert Special:DeletedContributions to use OOUI."
[lhc/web/wiklou.git] / includes / api / ApiQueryAllPages.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26 use MediaWiki\MediaWikiServices;
27
28 /**
29 * Query module to enumerate all available pages.
30 *
31 * @ingroup API
32 */
33 class ApiQueryAllPages extends ApiQueryGeneratorBase {
34
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'ap' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function getCacheMode( $params ) {
44 return 'public';
45 }
46
47 /**
48 * @param ApiPageSet $resultPageSet
49 * @return void
50 */
51 public function executeGenerator( $resultPageSet ) {
52 if ( $resultPageSet->isResolvingRedirects() ) {
53 $this->dieUsage(
54 'Use "gapfilterredir=nonredirects" option instead of "redirects" ' .
55 'when using allpages as a generator',
56 'params'
57 );
58 }
59
60 $this->run( $resultPageSet );
61 }
62
63 /**
64 * @param ApiPageSet $resultPageSet
65 * @return void
66 */
67 private function run( $resultPageSet = null ) {
68 $db = $this->getDB();
69
70 $params = $this->extractRequestParams();
71
72 // Page filters
73 $this->addTables( 'page' );
74
75 if ( !is_null( $params['continue'] ) ) {
76 $cont = explode( '|', $params['continue'] );
77 $this->dieContinueUsageIf( count( $cont ) != 1 );
78 $op = $params['dir'] == 'descending' ? '<' : '>';
79 $cont_from = $db->addQuotes( $cont[0] );
80 $this->addWhere( "page_title $op= $cont_from" );
81 }
82
83 if ( $params['filterredir'] == 'redirects' ) {
84 $this->addWhereFld( 'page_is_redirect', 1 );
85 } elseif ( $params['filterredir'] == 'nonredirects' ) {
86 $this->addWhereFld( 'page_is_redirect', 0 );
87 }
88
89 $this->addWhereFld( 'page_namespace', $params['namespace'] );
90 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
91 $from = ( $params['from'] === null
92 ? null
93 : $this->titlePartToKey( $params['from'], $params['namespace'] ) );
94 $to = ( $params['to'] === null
95 ? null
96 : $this->titlePartToKey( $params['to'], $params['namespace'] ) );
97 $this->addWhereRange( 'page_title', $dir, $from, $to );
98
99 if ( isset( $params['prefix'] ) ) {
100 $this->addWhere( 'page_title' . $db->buildLike(
101 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
102 $db->anyString() ) );
103 }
104
105 if ( is_null( $resultPageSet ) ) {
106 $selectFields = [
107 'page_namespace',
108 'page_title',
109 'page_id'
110 ];
111 } else {
112 $selectFields = $resultPageSet->getPageTableFields();
113 }
114
115 $this->addFields( $selectFields );
116 $forceNameTitleIndex = true;
117 if ( isset( $params['minsize'] ) ) {
118 $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
119 $forceNameTitleIndex = false;
120 }
121
122 if ( isset( $params['maxsize'] ) ) {
123 $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
124 $forceNameTitleIndex = false;
125 }
126
127 // Page protection filtering
128 if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) {
129 $this->addTables( 'page_restrictions' );
130 $this->addWhere( 'page_id=pr_page' );
131 $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
132
133 if ( count( $params['prtype'] ) ) {
134 $this->addWhereFld( 'pr_type', $params['prtype'] );
135
136 if ( isset( $params['prlevel'] ) ) {
137 // Remove the empty string and '*' from the prlevel array
138 $prlevel = array_diff( $params['prlevel'], [ '', '*' ] );
139
140 if ( count( $prlevel ) ) {
141 $this->addWhereFld( 'pr_level', $prlevel );
142 }
143 }
144 if ( $params['prfiltercascade'] == 'cascading' ) {
145 $this->addWhereFld( 'pr_cascade', 1 );
146 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
147 $this->addWhereFld( 'pr_cascade', 0 );
148 }
149 }
150 $forceNameTitleIndex = false;
151
152 if ( $params['prexpiry'] == 'indefinite' ) {
153 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
154 } elseif ( $params['prexpiry'] == 'definite' ) {
155 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
156 }
157
158 $this->addOption( 'DISTINCT' );
159 } elseif ( isset( $params['prlevel'] ) ) {
160 $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
161 }
162
163 if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
164 $this->addTables( 'langlinks' );
165 $this->addJoinConds( [ 'langlinks' => [ 'LEFT JOIN', 'page_id=ll_from' ] ] );
166 $this->addWhere( 'll_from IS NULL' );
167 $forceNameTitleIndex = false;
168 } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
169 $this->addTables( 'langlinks' );
170 $this->addWhere( 'page_id=ll_from' );
171 $this->addOption( 'STRAIGHT_JOIN' );
172
173 // MySQL filesorts if we use a GROUP BY that works with the rules
174 // in the 1992 SQL standard (it doesn't like having the
175 // constant-in-WHERE page_namespace column in there). Using the
176 // 1999 rules works fine, but that breaks other DBs. Sigh.
177 /// @todo Once we drop support for 1992-rule DBs, we can simplify this.
178 $dbType = $db->getType();
179 if ( $dbType === 'mysql' || $dbType === 'sqlite' ) {
180 // Ignore the rules, or 1999 rules if you count unique keys
181 // over non-NULL columns as satisfying the requirement for
182 // "functional dependency" and don't require including
183 // constant-in-WHERE columns in the GROUP BY.
184 $this->addOption( 'GROUP BY', [ 'page_title' ] );
185 } elseif ( $dbType === 'postgres' && $db->getServerVersion() >= 9.1 ) {
186 // 1999 rules only counting primary keys
187 $this->addOption( 'GROUP BY', [ 'page_title', 'page_id' ] );
188 } else {
189 // 1992 rules
190 $this->addOption( 'GROUP BY', $selectFields );
191 }
192
193 $forceNameTitleIndex = false;
194 }
195
196 if ( $forceNameTitleIndex ) {
197 $this->addOption( 'USE INDEX', 'name_title' );
198 }
199
200 $limit = $params['limit'];
201 $this->addOption( 'LIMIT', $limit + 1 );
202 $res = $this->select( __METHOD__ );
203
204 // Get gender information
205 if ( MWNamespace::hasGenderDistinction( $params['namespace'] ) ) {
206 $users = [];
207 foreach ( $res as $row ) {
208 $users[] = $row->page_title;
209 }
210 MediaWikiServices::getInstance()->getGenderCache()->doQuery( $users, __METHOD__ );
211 $res->rewind(); // reset
212 }
213
214 $count = 0;
215 $result = $this->getResult();
216 foreach ( $res as $row ) {
217 if ( ++$count > $limit ) {
218 // We've reached the one extra which shows that there are
219 // additional pages to be had. Stop here...
220 $this->setContinueEnumParameter( 'continue', $row->page_title );
221 break;
222 }
223
224 if ( is_null( $resultPageSet ) ) {
225 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
226 $vals = [
227 'pageid' => intval( $row->page_id ),
228 'ns' => intval( $title->getNamespace() ),
229 'title' => $title->getPrefixedText()
230 ];
231 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
232 if ( !$fit ) {
233 $this->setContinueEnumParameter( 'continue', $row->page_title );
234 break;
235 }
236 } else {
237 $resultPageSet->processDbRow( $row );
238 }
239 }
240
241 if ( is_null( $resultPageSet ) ) {
242 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'p' );
243 }
244 }
245
246 public function getAllowedParams() {
247 return [
248 'from' => null,
249 'continue' => [
250 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
251 ],
252 'to' => null,
253 'prefix' => null,
254 'namespace' => [
255 ApiBase::PARAM_DFLT => NS_MAIN,
256 ApiBase::PARAM_TYPE => 'namespace',
257 ],
258 'filterredir' => [
259 ApiBase::PARAM_DFLT => 'all',
260 ApiBase::PARAM_TYPE => [
261 'all',
262 'redirects',
263 'nonredirects'
264 ]
265 ],
266 'minsize' => [
267 ApiBase::PARAM_TYPE => 'integer',
268 ],
269 'maxsize' => [
270 ApiBase::PARAM_TYPE => 'integer',
271 ],
272 'prtype' => [
273 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
274 ApiBase::PARAM_ISMULTI => true
275 ],
276 'prlevel' => [
277 ApiBase::PARAM_TYPE => $this->getConfig()->get( 'RestrictionLevels' ),
278 ApiBase::PARAM_ISMULTI => true
279 ],
280 'prfiltercascade' => [
281 ApiBase::PARAM_DFLT => 'all',
282 ApiBase::PARAM_TYPE => [
283 'cascading',
284 'noncascading',
285 'all'
286 ],
287 ],
288 'limit' => [
289 ApiBase::PARAM_DFLT => 10,
290 ApiBase::PARAM_TYPE => 'limit',
291 ApiBase::PARAM_MIN => 1,
292 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
293 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
294 ],
295 'dir' => [
296 ApiBase::PARAM_DFLT => 'ascending',
297 ApiBase::PARAM_TYPE => [
298 'ascending',
299 'descending'
300 ]
301 ],
302 'filterlanglinks' => [
303 ApiBase::PARAM_TYPE => [
304 'withlanglinks',
305 'withoutlanglinks',
306 'all'
307 ],
308 ApiBase::PARAM_DFLT => 'all'
309 ],
310 'prexpiry' => [
311 ApiBase::PARAM_TYPE => [
312 'indefinite',
313 'definite',
314 'all'
315 ],
316 ApiBase::PARAM_DFLT => 'all'
317 ],
318 ];
319 }
320
321 protected function getExamplesMessages() {
322 return [
323 'action=query&list=allpages&apfrom=B'
324 => 'apihelp-query+allpages-example-B',
325 'action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info'
326 => 'apihelp-query+allpages-example-generator',
327 'action=query&generator=allpages&gaplimit=2&' .
328 'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
329 => 'apihelp-query+allpages-example-generator-revisions',
330 ];
331 }
332
333 public function getHelpUrls() {
334 return 'https://www.mediawiki.org/wiki/API:Allpages';
335 }
336 }