Exclude redirects from Special:Fewestrevisions
[lhc/web/wiklou.git] / includes / api / ApiQueryAllDeletedRevisions.php
1 <?php
2 /**
3 * Copyright © 2014 Wikimedia Foundation and contributors
4 *
5 * Heavily based on ApiQueryDeletedrevs,
6 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Revision\RevisionRecord;
28 use MediaWiki\Storage\NameTableAccessException;
29
30 /**
31 * Query module to enumerate all deleted revisions.
32 *
33 * @ingroup API
34 */
35 class ApiQueryAllDeletedRevisions extends ApiQueryRevisionsBase {
36
37 public function __construct( ApiQuery $query, $moduleName ) {
38 parent::__construct( $query, $moduleName, 'adr' );
39 }
40
41 /**
42 * @param ApiPageSet|null $resultPageSet
43 * @return void
44 */
45 protected function run( ApiPageSet $resultPageSet = null ) {
46 // Before doing anything at all, let's check permissions
47 $this->checkUserRightsAny( 'deletedhistory' );
48
49 $user = $this->getUser();
50 $db = $this->getDB();
51 $params = $this->extractRequestParams( false );
52 $services = MediaWikiServices::getInstance();
53 $revisionStore = $services->getRevisionStore();
54
55 $result = $this->getResult();
56
57 // If the user wants no namespaces, they get no pages.
58 if ( $params['namespace'] === [] ) {
59 if ( $resultPageSet === null ) {
60 $result->addValue( 'query', $this->getModuleName(), [] );
61 }
62 return;
63 }
64
65 // This module operates in two modes:
66 // 'user': List deleted revs by a certain user
67 // 'all': List all deleted revs in NS
68 $mode = 'all';
69 if ( !is_null( $params['user'] ) ) {
70 $mode = 'user';
71 }
72
73 if ( $mode == 'user' ) {
74 foreach ( [ 'from', 'to', 'prefix', 'excludeuser' ] as $param ) {
75 if ( !is_null( $params[$param] ) ) {
76 $p = $this->getModulePrefix();
77 $this->dieWithError(
78 [ 'apierror-invalidparammix-cannotusewith', $p . $param, "{$p}user" ],
79 'invalidparammix'
80 );
81 }
82 }
83 } else {
84 foreach ( [ 'start', 'end' ] as $param ) {
85 if ( !is_null( $params[$param] ) ) {
86 $p = $this->getModulePrefix();
87 $this->dieWithError(
88 [ 'apierror-invalidparammix-mustusewith', $p . $param, "{$p}user" ],
89 'invalidparammix'
90 );
91 }
92 }
93 }
94
95 // If we're generating titles only, we can use DISTINCT for a better
96 // query. But we can't do that in 'user' mode (wrong index), and we can
97 // only do it when sorting ASC (because MySQL apparently can't use an
98 // index backwards for grouping even though it can for ORDER BY, WTF?)
99 $dir = $params['dir'];
100 $optimizeGenerateTitles = false;
101 if ( $mode === 'all' && $params['generatetitles'] && $resultPageSet !== null ) {
102 if ( $dir === 'newer' ) {
103 $optimizeGenerateTitles = true;
104 } else {
105 $p = $this->getModulePrefix();
106 $this->addWarning( [ 'apiwarn-alldeletedrevisions-performance', $p ], 'performance' );
107 }
108 }
109
110 if ( $resultPageSet === null ) {
111 $this->parseParameters( $params );
112 $arQuery = $revisionStore->getArchiveQueryInfo();
113 $this->addTables( $arQuery['tables'] );
114 $this->addJoinConds( $arQuery['joins'] );
115 $this->addFields( $arQuery['fields'] );
116 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
117 } else {
118 $this->limit = $this->getParameter( 'limit' ) ?: 10;
119 $this->addTables( 'archive' );
120 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
121 if ( $optimizeGenerateTitles ) {
122 $this->addOption( 'DISTINCT' );
123 } else {
124 $this->addFields( [ 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
125 }
126 }
127
128 if ( $this->fld_tags ) {
129 $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
130 }
131
132 if ( !is_null( $params['tag'] ) ) {
133 $this->addTables( 'change_tag' );
134 $this->addJoinConds(
135 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
136 );
137 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
138 try {
139 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
140 } catch ( NameTableAccessException $exception ) {
141 // Return nothing.
142 $this->addWhere( '1=0' );
143 }
144 }
145
146 if ( $this->fetchContent ) {
147 $this->addTables( 'text' );
148 $this->addJoinConds(
149 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
150 );
151 $this->addFields( [ 'old_text', 'old_flags' ] );
152
153 // This also means stricter restrictions
154 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
155 }
156
157 $miser_ns = null;
158
159 if ( $mode == 'all' ) {
160 $namespaces = $params['namespace'] ??
161 $services->getNamespaceInfo()->getValidNamespaces();
162 $this->addWhereFld( 'ar_namespace', $namespaces );
163
164 // For from/to/prefix, we have to consider the potential
165 // transformations of the title in all specified namespaces.
166 // Generally there will be only one transformation, but wikis with
167 // some namespaces case-sensitive could have two.
168 if ( $params['from'] !== null || $params['to'] !== null ) {
169 $isDirNewer = ( $dir === 'newer' );
170 $after = ( $isDirNewer ? '>=' : '<=' );
171 $before = ( $isDirNewer ? '<=' : '>=' );
172 $where = [];
173 foreach ( $namespaces as $ns ) {
174 $w = [];
175 if ( $params['from'] !== null ) {
176 $w[] = 'ar_title' . $after .
177 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
178 }
179 if ( $params['to'] !== null ) {
180 $w[] = 'ar_title' . $before .
181 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
182 }
183 $w = $db->makeList( $w, LIST_AND );
184 $where[$w][] = $ns;
185 }
186 if ( count( $where ) == 1 ) {
187 $where = key( $where );
188 $this->addWhere( $where );
189 } else {
190 $where2 = [];
191 foreach ( $where as $w => $ns ) {
192 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND );
193 }
194 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
195 }
196 }
197
198 if ( isset( $params['prefix'] ) ) {
199 $where = [];
200 foreach ( $namespaces as $ns ) {
201 $w = 'ar_title' . $db->buildLike(
202 $this->titlePartToKey( $params['prefix'], $ns ),
203 $db->anyString() );
204 $where[$w][] = $ns;
205 }
206 if ( count( $where ) == 1 ) {
207 $where = key( $where );
208 $this->addWhere( $where );
209 } else {
210 $where2 = [];
211 foreach ( $where as $w => $ns ) {
212 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND );
213 }
214 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
215 }
216 }
217 } else {
218 if ( $this->getConfig()->get( 'MiserMode' ) ) {
219 $miser_ns = $params['namespace'];
220 } else {
221 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
222 }
223 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
224 }
225
226 if ( !is_null( $params['user'] ) ) {
227 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
228 $actorQuery = ActorMigration::newMigration()
229 ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
230 $this->addTables( $actorQuery['tables'] );
231 $this->addJoinConds( $actorQuery['joins'] );
232 $this->addWhere( $actorQuery['conds'] );
233 } elseif ( !is_null( $params['excludeuser'] ) ) {
234 // Here there's no chance of using ar_usertext_timestamp.
235 $actorQuery = ActorMigration::newMigration()
236 ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
237 $this->addTables( $actorQuery['tables'] );
238 $this->addJoinConds( $actorQuery['joins'] );
239 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
240 }
241
242 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
243 // Paranoia: avoid brute force searches (T19342)
244 // (shouldn't be able to get here without 'deletedhistory', but
245 // check it again just in case)
246 if ( !$user->isAllowed( 'deletedhistory' ) ) {
247 $bitmask = RevisionRecord::DELETED_USER;
248 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
249 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
250 } else {
251 $bitmask = 0;
252 }
253 if ( $bitmask ) {
254 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
255 }
256 }
257
258 if ( !is_null( $params['continue'] ) ) {
259 $cont = explode( '|', $params['continue'] );
260 $op = ( $dir == 'newer' ? '>' : '<' );
261 if ( $optimizeGenerateTitles ) {
262 $this->dieContinueUsageIf( count( $cont ) != 2 );
263 $ns = (int)$cont[0];
264 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
265 $title = $db->addQuotes( $cont[1] );
266 $this->addWhere( "ar_namespace $op $ns OR " .
267 "(ar_namespace = $ns AND ar_title $op= $title)" );
268 } elseif ( $mode == 'all' ) {
269 $this->dieContinueUsageIf( count( $cont ) != 4 );
270 $ns = (int)$cont[0];
271 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
272 $title = $db->addQuotes( $cont[1] );
273 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
274 $ar_id = (int)$cont[3];
275 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
276 $this->addWhere( "ar_namespace $op $ns OR " .
277 "(ar_namespace = $ns AND " .
278 "(ar_title $op $title OR " .
279 "(ar_title = $title AND " .
280 "(ar_timestamp $op $ts OR " .
281 "(ar_timestamp = $ts AND " .
282 "ar_id $op= $ar_id)))))" );
283 } else {
284 $this->dieContinueUsageIf( count( $cont ) != 2 );
285 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
286 $ar_id = (int)$cont[1];
287 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
288 $this->addWhere( "ar_timestamp $op $ts OR " .
289 "(ar_timestamp = $ts AND " .
290 "ar_id $op= $ar_id)" );
291 }
292 }
293
294 $this->addOption( 'LIMIT', $this->limit + 1 );
295
296 $sort = ( $dir == 'newer' ? '' : ' DESC' );
297 $orderby = [];
298 if ( $optimizeGenerateTitles ) {
299 // Targeting index name_title_timestamp
300 if ( $params['namespace'] === null || count( array_unique( $params['namespace'] ) ) > 1 ) {
301 $orderby[] = "ar_namespace $sort";
302 }
303 $orderby[] = "ar_title $sort";
304 } elseif ( $mode == 'all' ) {
305 // Targeting index name_title_timestamp
306 if ( $params['namespace'] === null || count( array_unique( $params['namespace'] ) ) > 1 ) {
307 $orderby[] = "ar_namespace $sort";
308 }
309 $orderby[] = "ar_title $sort";
310 $orderby[] = "ar_timestamp $sort";
311 $orderby[] = "ar_id $sort";
312 } else {
313 // Targeting index usertext_timestamp
314 // 'user' is always constant.
315 $orderby[] = "ar_timestamp $sort";
316 $orderby[] = "ar_id $sort";
317 }
318 $this->addOption( 'ORDER BY', $orderby );
319
320 $res = $this->select( __METHOD__ );
321 $pageMap = []; // Maps ns&title to array index
322 $count = 0;
323 $nextIndex = 0;
324 $generated = [];
325 foreach ( $res as $row ) {
326 if ( ++$count > $this->limit ) {
327 // We've had enough
328 if ( $optimizeGenerateTitles ) {
329 $this->setContinueEnumParameter( 'continue', "$row->ar_namespace|$row->ar_title" );
330 } elseif ( $mode == 'all' ) {
331 $this->setContinueEnumParameter( 'continue',
332 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
333 );
334 } else {
335 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
336 }
337 break;
338 }
339
340 // Miser mode namespace check
341 if ( $miser_ns !== null && !in_array( $row->ar_namespace, $miser_ns ) ) {
342 continue;
343 }
344
345 if ( $resultPageSet !== null ) {
346 if ( $params['generatetitles'] ) {
347 $key = "{$row->ar_namespace}:{$row->ar_title}";
348 if ( !isset( $generated[$key] ) ) {
349 $generated[$key] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
350 }
351 } else {
352 $generated[] = $row->ar_rev_id;
353 }
354 } else {
355 $revision = $revisionStore->newRevisionFromArchiveRow( $row );
356 $rev = $this->extractRevisionInfo( $revision, $row );
357
358 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
359 $index = $nextIndex++;
360 $pageMap[$row->ar_namespace][$row->ar_title] = $index;
361 $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
362 $a = [
363 'pageid' => $title->getArticleID(),
364 'revisions' => [ $rev ],
365 ];
366 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
367 ApiQueryBase::addTitleInfo( $a, $title );
368 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
369 } else {
370 $index = $pageMap[$row->ar_namespace][$row->ar_title];
371 $fit = $result->addValue(
372 [ 'query', $this->getModuleName(), $index, 'revisions' ],
373 null, $rev );
374 }
375 if ( !$fit ) {
376 if ( $mode == 'all' ) {
377 $this->setContinueEnumParameter( 'continue',
378 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
379 );
380 } else {
381 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
382 }
383 break;
384 }
385 }
386 }
387
388 if ( $resultPageSet !== null ) {
389 if ( $params['generatetitles'] ) {
390 $resultPageSet->populateFromTitles( $generated );
391 } else {
392 $resultPageSet->populateFromRevisionIDs( $generated );
393 }
394 } else {
395 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
396 }
397 }
398
399 public function getAllowedParams() {
400 $ret = parent::getAllowedParams() + [
401 'user' => [
402 ApiBase::PARAM_TYPE => 'user'
403 ],
404 'namespace' => [
405 ApiBase::PARAM_ISMULTI => true,
406 ApiBase::PARAM_TYPE => 'namespace',
407 ],
408 'start' => [
409 ApiBase::PARAM_TYPE => 'timestamp',
410 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'useronly' ] ],
411 ],
412 'end' => [
413 ApiBase::PARAM_TYPE => 'timestamp',
414 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'useronly' ] ],
415 ],
416 'dir' => [
417 ApiBase::PARAM_TYPE => [
418 'newer',
419 'older'
420 ],
421 ApiBase::PARAM_DFLT => 'older',
422 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
423 ],
424 'from' => [
425 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
426 ],
427 'to' => [
428 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
429 ],
430 'prefix' => [
431 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
432 ],
433 'excludeuser' => [
434 ApiBase::PARAM_TYPE => 'user',
435 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
436 ],
437 'tag' => null,
438 'continue' => [
439 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
440 ],
441 'generatetitles' => [
442 ApiBase::PARAM_DFLT => false
443 ],
444 ];
445
446 if ( $this->getConfig()->get( 'MiserMode' ) ) {
447 $ret['user'][ApiBase::PARAM_HELP_MSG_APPEND] = [
448 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
449 ];
450 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
451 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
452 ];
453 }
454
455 return $ret;
456 }
457
458 protected function getExamplesMessages() {
459 return [
460 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
461 => 'apihelp-query+alldeletedrevisions-example-user',
462 'action=query&list=alldeletedrevisions&adrdir=newer&adrnamespace=0&adrlimit=50'
463 => 'apihelp-query+alldeletedrevisions-example-ns-main',
464 ];
465 }
466
467 public function getHelpUrls() {
468 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Alldeletedrevisions';
469 }
470 }