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