3 * Copyright © 2014 Wikimedia Foundation and contributors
5 * Heavily based on ApiQueryDeletedrevs,
6 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
26 use MediaWiki\MediaWikiServices
;
27 use MediaWiki\Revision\RevisionRecord
;
28 use MediaWiki\Storage\NameTableAccessException
;
31 * Query module to enumerate all deleted revisions.
35 class ApiQueryAllDeletedRevisions
extends ApiQueryRevisionsBase
{
37 public function __construct( ApiQuery
$query, $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'adr' );
42 * @param ApiPageSet|null $resultPageSet
45 protected function run( ApiPageSet
$resultPageSet = null ) {
46 global $wgChangeTagsSchemaMigrationStage;
48 // Before doing anything at all, let's check permissions
49 $this->checkUserRightsAny( 'deletedhistory' );
51 $user = $this->getUser();
53 $params = $this->extractRequestParams( false );
54 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
56 $result = $this->getResult();
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(), [] );
66 // This module operates in two modes:
67 // 'user': List deleted revs by a certain user
68 // 'all': List all deleted revs in NS
70 if ( !is_null( $params['user'] ) ) {
74 if ( $mode == 'user' ) {
75 foreach ( [ 'from', 'to', 'prefix', 'excludeuser' ] as $param ) {
76 if ( !is_null( $params[$param] ) ) {
77 $p = $this->getModulePrefix();
79 [ 'apierror-invalidparammix-cannotusewith', $p . $param, "{$p}user" ],
85 foreach ( [ 'start', 'end' ] as $param ) {
86 if ( !is_null( $params[$param] ) ) {
87 $p = $this->getModulePrefix();
89 [ 'apierror-invalidparammix-mustusewith', $p . $param, "{$p}user" ],
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;
106 $p = $this->getModulePrefix();
107 $this->addWarning( [ 'apiwarn-alldeletedrevisions-performance', $p ], 'performance' );
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' ] );
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' );
125 $this->addFields( [ 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
129 if ( $this->fld_tags
) {
130 $this->addTables( 'tag_summary' );
132 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
134 $this->addFields( 'ts_tags' );
137 if ( !is_null( $params['tag'] ) ) {
138 $this->addTables( 'change_tag' );
140 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
142 if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH
) {
143 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
145 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
146 } catch ( NameTableAccessException
$exception ) {
148 $this->addWhere( '1=0' );
151 $this->addWhereFld( 'ct_tag', $params['tag'] );
155 if ( $this->fetchContent
) {
156 $this->addTables( 'text' );
158 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
160 $this->addFields( [ 'old_text', 'old_flags' ] );
162 // This also means stricter restrictions
163 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
168 if ( $mode == 'all' ) {
169 $namespaces = $params['namespace'] ?? MWNamespace
::getValidNamespaces();
170 $this->addWhereFld( 'ar_namespace', $namespaces );
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 ?
'<=' : '>=' );
181 foreach ( $namespaces as $ns ) {
183 if ( $params['from'] !== null ) {
184 $w[] = 'ar_title' . $after .
185 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
187 if ( $params['to'] !== null ) {
188 $w[] = 'ar_title' . $before .
189 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
191 $w = $db->makeList( $w, LIST_AND
);
194 if ( count( $where ) == 1 ) {
195 $where = key( $where );
196 $this->addWhere( $where );
199 foreach ( $where as $w => $ns ) {
200 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND
);
202 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
206 if ( isset( $params['prefix'] ) ) {
208 foreach ( $namespaces as $ns ) {
209 $w = 'ar_title' . $db->buildLike(
210 $this->titlePartToKey( $params['prefix'], $ns ),
214 if ( count( $where ) == 1 ) {
215 $where = key( $where );
216 $this->addWhere( $where );
219 foreach ( $where as $w => $ns ) {
220 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND
);
222 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
226 if ( $this->getConfig()->get( 'MiserMode' ) ) {
227 $miser_ns = $params['namespace'];
229 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
231 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
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'] . ')' );
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
;
262 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
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)))))" );
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)" );
302 $this->addOption( 'LIMIT', $this->limit +
1 );
304 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
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";
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";
317 $orderby[] = "ar_title $sort";
318 $orderby[] = "ar_timestamp $sort";
319 $orderby[] = "ar_id $sort";
321 // Targeting index usertext_timestamp
322 // 'user' is always constant.
323 $orderby[] = "ar_timestamp $sort";
324 $orderby[] = "ar_id $sort";
326 $this->addOption( 'ORDER BY', $orderby );
328 $res = $this->select( __METHOD__
);
329 $pageMap = []; // Maps ns&title to array index
333 foreach ( $res as $row ) {
334 if ( ++
$count > $this->limit
) {
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"
343 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
348 // Miser mode namespace check
349 if ( $miser_ns !== null && !in_array( $row->ar_namespace
, $miser_ns ) ) {
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
);
360 $generated[] = $row->ar_rev_id
;
363 $revision = $revisionStore->newRevisionFromArchiveRow( $row );
364 $rev = $this->extractRevisionInfo( $revision, $row );
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() );
371 'pageid' => $title->getArticleID(),
372 'revisions' => [ $rev ],
374 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
375 ApiQueryBase
::addTitleInfo( $a, $title );
376 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
378 $index = $pageMap[$row->ar_namespace
][$row->ar_title
];
379 $fit = $result->addValue(
380 [ 'query', $this->getModuleName(), $index, 'revisions' ],
384 if ( $mode == 'all' ) {
385 $this->setContinueEnumParameter( 'continue',
386 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
389 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
396 if ( $resultPageSet !== null ) {
397 if ( $params['generatetitles'] ) {
398 $resultPageSet->populateFromTitles( $generated );
400 $resultPageSet->populateFromRevisionIDs( $generated );
403 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
407 public function getAllowedParams() {
408 $ret = parent
::getAllowedParams() +
[
410 ApiBase
::PARAM_TYPE
=> 'user'
413 ApiBase
::PARAM_ISMULTI
=> true,
414 ApiBase
::PARAM_TYPE
=> 'namespace',
417 ApiBase
::PARAM_TYPE
=> 'timestamp',
418 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
421 ApiBase
::PARAM_TYPE
=> 'timestamp',
422 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
425 ApiBase
::PARAM_TYPE
=> [
429 ApiBase
::PARAM_DFLT
=> 'older',
430 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
433 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
436 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
439 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
442 ApiBase
::PARAM_TYPE
=> 'user',
443 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
447 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
449 'generatetitles' => [
450 ApiBase
::PARAM_DFLT
=> false
454 if ( $this->getConfig()->get( 'MiserMode' ) ) {
455 $ret['user'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
456 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
458 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
459 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
466 protected function getExamplesMessages() {
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',
475 public function getHelpUrls() {
476 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Alldeletedrevisions';