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 // Before doing anything at all, let's check permissions
47 $this->checkUserRightsAny( 'deletedhistory' );
49 $user = $this->getUser();
51 $params = $this->extractRequestParams( false );
52 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
54 $result = $this->getResult();
56 // If the user wants no namespaces, they get no pages.
57 if ( $params['namespace'] === [] ) {
58 if ( $resultPageSet === null ) {
59 $result->addValue( 'query', $this->getModuleName(), [] );
64 // This module operates in two modes:
65 // 'user': List deleted revs by a certain user
66 // 'all': List all deleted revs in NS
68 if ( !is_null( $params['user'] ) ) {
72 if ( $mode == 'user' ) {
73 foreach ( [ 'from', 'to', 'prefix', 'excludeuser' ] as $param ) {
74 if ( !is_null( $params[$param] ) ) {
75 $p = $this->getModulePrefix();
77 [ 'apierror-invalidparammix-cannotusewith', $p . $param, "{$p}user" ],
83 foreach ( [ 'start', 'end' ] as $param ) {
84 if ( !is_null( $params[$param] ) ) {
85 $p = $this->getModulePrefix();
87 [ 'apierror-invalidparammix-mustusewith', $p . $param, "{$p}user" ],
94 // If we're generating titles only, we can use DISTINCT for a better
95 // query. But we can't do that in 'user' mode (wrong index), and we can
96 // only do it when sorting ASC (because MySQL apparently can't use an
97 // index backwards for grouping even though it can for ORDER BY, WTF?)
98 $dir = $params['dir'];
99 $optimizeGenerateTitles = false;
100 if ( $mode === 'all' && $params['generatetitles'] && $resultPageSet !== null ) {
101 if ( $dir === 'newer' ) {
102 $optimizeGenerateTitles = true;
104 $p = $this->getModulePrefix();
105 $this->addWarning( [ 'apiwarn-alldeletedrevisions-performance', $p ], 'performance' );
109 if ( $resultPageSet === null ) {
110 $this->parseParameters( $params );
111 $arQuery = $revisionStore->getArchiveQueryInfo();
112 $this->addTables( $arQuery['tables'] );
113 $this->addJoinConds( $arQuery['joins'] );
114 $this->addFields( $arQuery['fields'] );
115 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
117 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
118 $this->addTables( 'archive' );
119 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
120 if ( $optimizeGenerateTitles ) {
121 $this->addOption( 'DISTINCT' );
123 $this->addFields( [ 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
127 if ( $this->fld_tags
) {
128 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
131 if ( !is_null( $params['tag'] ) ) {
132 $this->addTables( 'change_tag' );
134 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
136 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
138 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
139 } catch ( NameTableAccessException
$exception ) {
141 $this->addWhere( '1=0' );
145 if ( $this->fetchContent
) {
146 $this->addTables( 'text' );
148 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
150 $this->addFields( [ 'old_text', 'old_flags' ] );
152 // This also means stricter restrictions
153 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
158 if ( $mode == 'all' ) {
159 $namespaces = $params['namespace'] ?? MWNamespace
::getValidNamespaces();
160 $this->addWhereFld( 'ar_namespace', $namespaces );
162 // For from/to/prefix, we have to consider the potential
163 // transformations of the title in all specified namespaces.
164 // Generally there will be only one transformation, but wikis with
165 // some namespaces case-sensitive could have two.
166 if ( $params['from'] !== null ||
$params['to'] !== null ) {
167 $isDirNewer = ( $dir === 'newer' );
168 $after = ( $isDirNewer ?
'>=' : '<=' );
169 $before = ( $isDirNewer ?
'<=' : '>=' );
171 foreach ( $namespaces as $ns ) {
173 if ( $params['from'] !== null ) {
174 $w[] = 'ar_title' . $after .
175 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
177 if ( $params['to'] !== null ) {
178 $w[] = 'ar_title' . $before .
179 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
181 $w = $db->makeList( $w, LIST_AND
);
184 if ( count( $where ) == 1 ) {
185 $where = key( $where );
186 $this->addWhere( $where );
189 foreach ( $where as $w => $ns ) {
190 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND
);
192 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
196 if ( isset( $params['prefix'] ) ) {
198 foreach ( $namespaces as $ns ) {
199 $w = 'ar_title' . $db->buildLike(
200 $this->titlePartToKey( $params['prefix'], $ns ),
204 if ( count( $where ) == 1 ) {
205 $where = key( $where );
206 $this->addWhere( $where );
209 foreach ( $where as $w => $ns ) {
210 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND
);
212 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
216 if ( $this->getConfig()->get( 'MiserMode' ) ) {
217 $miser_ns = $params['namespace'];
219 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
221 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
224 if ( !is_null( $params['user'] ) ) {
225 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
226 $actorQuery = ActorMigration
::newMigration()
227 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
228 $this->addTables( $actorQuery['tables'] );
229 $this->addJoinConds( $actorQuery['joins'] );
230 $this->addWhere( $actorQuery['conds'] );
231 } elseif ( !is_null( $params['excludeuser'] ) ) {
232 // Here there's no chance of using ar_usertext_timestamp.
233 $actorQuery = ActorMigration
::newMigration()
234 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
235 $this->addTables( $actorQuery['tables'] );
236 $this->addJoinConds( $actorQuery['joins'] );
237 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
240 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
241 // Paranoia: avoid brute force searches (T19342)
242 // (shouldn't be able to get here without 'deletedhistory', but
243 // check it again just in case)
244 if ( !$user->isAllowed( 'deletedhistory' ) ) {
245 $bitmask = RevisionRecord
::DELETED_USER
;
246 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
247 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
252 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
256 if ( !is_null( $params['continue'] ) ) {
257 $cont = explode( '|', $params['continue'] );
258 $op = ( $dir == 'newer' ?
'>' : '<' );
259 if ( $optimizeGenerateTitles ) {
260 $this->dieContinueUsageIf( count( $cont ) != 2 );
262 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
263 $title = $db->addQuotes( $cont[1] );
264 $this->addWhere( "ar_namespace $op $ns OR " .
265 "(ar_namespace = $ns AND ar_title $op= $title)" );
266 } elseif ( $mode == 'all' ) {
267 $this->dieContinueUsageIf( count( $cont ) != 4 );
269 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
270 $title = $db->addQuotes( $cont[1] );
271 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
272 $ar_id = (int)$cont[3];
273 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
274 $this->addWhere( "ar_namespace $op $ns OR " .
275 "(ar_namespace = $ns AND " .
276 "(ar_title $op $title OR " .
277 "(ar_title = $title AND " .
278 "(ar_timestamp $op $ts OR " .
279 "(ar_timestamp = $ts AND " .
280 "ar_id $op= $ar_id)))))" );
282 $this->dieContinueUsageIf( count( $cont ) != 2 );
283 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
284 $ar_id = (int)$cont[1];
285 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
286 $this->addWhere( "ar_timestamp $op $ts OR " .
287 "(ar_timestamp = $ts AND " .
288 "ar_id $op= $ar_id)" );
292 $this->addOption( 'LIMIT', $this->limit +
1 );
294 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
296 if ( $optimizeGenerateTitles ) {
297 // Targeting index name_title_timestamp
298 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
299 $orderby[] = "ar_namespace $sort";
301 $orderby[] = "ar_title $sort";
302 } elseif ( $mode == 'all' ) {
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 $orderby[] = "ar_timestamp $sort";
309 $orderby[] = "ar_id $sort";
311 // Targeting index usertext_timestamp
312 // 'user' is always constant.
313 $orderby[] = "ar_timestamp $sort";
314 $orderby[] = "ar_id $sort";
316 $this->addOption( 'ORDER BY', $orderby );
318 $res = $this->select( __METHOD__
);
319 $pageMap = []; // Maps ns&title to array index
323 foreach ( $res as $row ) {
324 if ( ++
$count > $this->limit
) {
326 if ( $optimizeGenerateTitles ) {
327 $this->setContinueEnumParameter( 'continue', "$row->ar_namespace|$row->ar_title" );
328 } elseif ( $mode == 'all' ) {
329 $this->setContinueEnumParameter( 'continue',
330 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
333 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
338 // Miser mode namespace check
339 if ( $miser_ns !== null && !in_array( $row->ar_namespace
, $miser_ns ) ) {
343 if ( $resultPageSet !== null ) {
344 if ( $params['generatetitles'] ) {
345 $key = "{$row->ar_namespace}:{$row->ar_title}";
346 if ( !isset( $generated[$key] ) ) {
347 $generated[$key] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
350 $generated[] = $row->ar_rev_id
;
353 $revision = $revisionStore->newRevisionFromArchiveRow( $row );
354 $rev = $this->extractRevisionInfo( $revision, $row );
356 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
357 $index = $nextIndex++
;
358 $pageMap[$row->ar_namespace
][$row->ar_title
] = $index;
359 $title = Title
::newFromLinkTarget( $revision->getPageAsLinkTarget() );
361 'pageid' => $title->getArticleID(),
362 'revisions' => [ $rev ],
364 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
365 ApiQueryBase
::addTitleInfo( $a, $title );
366 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
368 $index = $pageMap[$row->ar_namespace
][$row->ar_title
];
369 $fit = $result->addValue(
370 [ 'query', $this->getModuleName(), $index, 'revisions' ],
374 if ( $mode == 'all' ) {
375 $this->setContinueEnumParameter( 'continue',
376 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
379 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
386 if ( $resultPageSet !== null ) {
387 if ( $params['generatetitles'] ) {
388 $resultPageSet->populateFromTitles( $generated );
390 $resultPageSet->populateFromRevisionIDs( $generated );
393 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
397 public function getAllowedParams() {
398 $ret = parent
::getAllowedParams() +
[
400 ApiBase
::PARAM_TYPE
=> 'user'
403 ApiBase
::PARAM_ISMULTI
=> true,
404 ApiBase
::PARAM_TYPE
=> 'namespace',
407 ApiBase
::PARAM_TYPE
=> 'timestamp',
408 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
411 ApiBase
::PARAM_TYPE
=> 'timestamp',
412 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
415 ApiBase
::PARAM_TYPE
=> [
419 ApiBase
::PARAM_DFLT
=> 'older',
420 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
423 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
426 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
429 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
432 ApiBase
::PARAM_TYPE
=> 'user',
433 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
437 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
439 'generatetitles' => [
440 ApiBase
::PARAM_DFLT
=> false
444 if ( $this->getConfig()->get( 'MiserMode' ) ) {
445 $ret['user'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
446 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
448 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
449 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
456 protected function getExamplesMessages() {
458 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
459 => 'apihelp-query+alldeletedrevisions-example-user',
460 'action=query&list=alldeletedrevisions&adrdir=newer&adrnamespace=0&adrlimit=50'
461 => 'apihelp-query+alldeletedrevisions-example-ns-main',
465 public function getHelpUrls() {
466 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Alldeletedrevisions';