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->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
133 if ( !is_null( $params['tag'] ) ) {
134 $this->addTables( 'change_tag' );
136 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
138 if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH
) {
139 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
141 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
142 } catch ( NameTableAccessException
$exception ) {
144 $this->addWhere( '1=0' );
147 $this->addWhereFld( 'ct_tag', $params['tag'] );
151 if ( $this->fetchContent
) {
152 $this->addTables( 'text' );
154 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
156 $this->addFields( [ 'old_text', 'old_flags' ] );
158 // This also means stricter restrictions
159 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
164 if ( $mode == 'all' ) {
165 $namespaces = $params['namespace'] ?? MWNamespace
::getValidNamespaces();
166 $this->addWhereFld( 'ar_namespace', $namespaces );
168 // For from/to/prefix, we have to consider the potential
169 // transformations of the title in all specified namespaces.
170 // Generally there will be only one transformation, but wikis with
171 // some namespaces case-sensitive could have two.
172 if ( $params['from'] !== null ||
$params['to'] !== null ) {
173 $isDirNewer = ( $dir === 'newer' );
174 $after = ( $isDirNewer ?
'>=' : '<=' );
175 $before = ( $isDirNewer ?
'<=' : '>=' );
177 foreach ( $namespaces as $ns ) {
179 if ( $params['from'] !== null ) {
180 $w[] = 'ar_title' . $after .
181 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
183 if ( $params['to'] !== null ) {
184 $w[] = 'ar_title' . $before .
185 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
187 $w = $db->makeList( $w, LIST_AND
);
190 if ( count( $where ) == 1 ) {
191 $where = key( $where );
192 $this->addWhere( $where );
195 foreach ( $where as $w => $ns ) {
196 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND
);
198 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
202 if ( isset( $params['prefix'] ) ) {
204 foreach ( $namespaces as $ns ) {
205 $w = 'ar_title' . $db->buildLike(
206 $this->titlePartToKey( $params['prefix'], $ns ),
210 if ( count( $where ) == 1 ) {
211 $where = key( $where );
212 $this->addWhere( $where );
215 foreach ( $where as $w => $ns ) {
216 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND
);
218 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
222 if ( $this->getConfig()->get( 'MiserMode' ) ) {
223 $miser_ns = $params['namespace'];
225 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
227 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
230 if ( !is_null( $params['user'] ) ) {
231 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
232 $actorQuery = ActorMigration
::newMigration()
233 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
234 $this->addTables( $actorQuery['tables'] );
235 $this->addJoinConds( $actorQuery['joins'] );
236 $this->addWhere( $actorQuery['conds'] );
237 } elseif ( !is_null( $params['excludeuser'] ) ) {
238 // Here there's no chance of using ar_usertext_timestamp.
239 $actorQuery = ActorMigration
::newMigration()
240 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
241 $this->addTables( $actorQuery['tables'] );
242 $this->addJoinConds( $actorQuery['joins'] );
243 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
246 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
247 // Paranoia: avoid brute force searches (T19342)
248 // (shouldn't be able to get here without 'deletedhistory', but
249 // check it again just in case)
250 if ( !$user->isAllowed( 'deletedhistory' ) ) {
251 $bitmask = RevisionRecord
::DELETED_USER
;
252 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
253 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
258 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
262 if ( !is_null( $params['continue'] ) ) {
263 $cont = explode( '|', $params['continue'] );
264 $op = ( $dir == 'newer' ?
'>' : '<' );
265 if ( $optimizeGenerateTitles ) {
266 $this->dieContinueUsageIf( count( $cont ) != 2 );
267 $ns = intval( $cont[0] );
268 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
269 $title = $db->addQuotes( $cont[1] );
270 $this->addWhere( "ar_namespace $op $ns OR " .
271 "(ar_namespace = $ns AND ar_title $op= $title)" );
272 } elseif ( $mode == 'all' ) {
273 $this->dieContinueUsageIf( count( $cont ) != 4 );
274 $ns = intval( $cont[0] );
275 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
276 $title = $db->addQuotes( $cont[1] );
277 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
278 $ar_id = (int)$cont[3];
279 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
280 $this->addWhere( "ar_namespace $op $ns OR " .
281 "(ar_namespace = $ns AND " .
282 "(ar_title $op $title OR " .
283 "(ar_title = $title AND " .
284 "(ar_timestamp $op $ts OR " .
285 "(ar_timestamp = $ts AND " .
286 "ar_id $op= $ar_id)))))" );
288 $this->dieContinueUsageIf( count( $cont ) != 2 );
289 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
290 $ar_id = (int)$cont[1];
291 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
292 $this->addWhere( "ar_timestamp $op $ts OR " .
293 "(ar_timestamp = $ts AND " .
294 "ar_id $op= $ar_id)" );
298 $this->addOption( 'LIMIT', $this->limit +
1 );
300 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
302 if ( $optimizeGenerateTitles ) {
303 // Targeting index name_title_timestamp
304 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
305 $orderby[] = "ar_namespace $sort";
307 $orderby[] = "ar_title $sort";
308 } elseif ( $mode == 'all' ) {
309 // Targeting index name_title_timestamp
310 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
311 $orderby[] = "ar_namespace $sort";
313 $orderby[] = "ar_title $sort";
314 $orderby[] = "ar_timestamp $sort";
315 $orderby[] = "ar_id $sort";
317 // Targeting index usertext_timestamp
318 // 'user' is always constant.
319 $orderby[] = "ar_timestamp $sort";
320 $orderby[] = "ar_id $sort";
322 $this->addOption( 'ORDER BY', $orderby );
324 $res = $this->select( __METHOD__
);
325 $pageMap = []; // Maps ns&title to array index
329 foreach ( $res as $row ) {
330 if ( ++
$count > $this->limit
) {
332 if ( $optimizeGenerateTitles ) {
333 $this->setContinueEnumParameter( 'continue', "$row->ar_namespace|$row->ar_title" );
334 } elseif ( $mode == 'all' ) {
335 $this->setContinueEnumParameter( 'continue',
336 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
339 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
344 // Miser mode namespace check
345 if ( $miser_ns !== null && !in_array( $row->ar_namespace
, $miser_ns ) ) {
349 if ( $resultPageSet !== null ) {
350 if ( $params['generatetitles'] ) {
351 $key = "{$row->ar_namespace}:{$row->ar_title}";
352 if ( !isset( $generated[$key] ) ) {
353 $generated[$key] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
356 $generated[] = $row->ar_rev_id
;
359 $revision = $revisionStore->newRevisionFromArchiveRow( $row );
360 $rev = $this->extractRevisionInfo( $revision, $row );
362 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
363 $index = $nextIndex++
;
364 $pageMap[$row->ar_namespace
][$row->ar_title
] = $index;
365 $title = Title
::newFromLinkTarget( $revision->getPageAsLinkTarget() );
367 'pageid' => $title->getArticleID(),
368 'revisions' => [ $rev ],
370 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
371 ApiQueryBase
::addTitleInfo( $a, $title );
372 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
374 $index = $pageMap[$row->ar_namespace
][$row->ar_title
];
375 $fit = $result->addValue(
376 [ 'query', $this->getModuleName(), $index, 'revisions' ],
380 if ( $mode == 'all' ) {
381 $this->setContinueEnumParameter( 'continue',
382 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
385 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
392 if ( $resultPageSet !== null ) {
393 if ( $params['generatetitles'] ) {
394 $resultPageSet->populateFromTitles( $generated );
396 $resultPageSet->populateFromRevisionIDs( $generated );
399 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
403 public function getAllowedParams() {
404 $ret = parent
::getAllowedParams() +
[
406 ApiBase
::PARAM_TYPE
=> 'user'
409 ApiBase
::PARAM_ISMULTI
=> true,
410 ApiBase
::PARAM_TYPE
=> 'namespace',
413 ApiBase
::PARAM_TYPE
=> 'timestamp',
414 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
417 ApiBase
::PARAM_TYPE
=> 'timestamp',
418 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
421 ApiBase
::PARAM_TYPE
=> [
425 ApiBase
::PARAM_DFLT
=> 'older',
426 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
429 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
432 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
435 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
438 ApiBase
::PARAM_TYPE
=> 'user',
439 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
443 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
445 'generatetitles' => [
446 ApiBase
::PARAM_DFLT
=> false
450 if ( $this->getConfig()->get( 'MiserMode' ) ) {
451 $ret['user'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
452 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
454 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
455 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
462 protected function getExamplesMessages() {
464 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
465 => 'apihelp-query+alldeletedrevisions-example-user',
466 'action=query&list=alldeletedrevisions&adrdir=newer&adrnamespace=0&adrlimit=50'
467 => 'apihelp-query+alldeletedrevisions-example-ns-main',
471 public function getHelpUrls() {
472 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Alldeletedrevisions';