3 * Copyright © 2014 Wikimedia Foundation and contributors
5 * Heavily based on ApiQueryDeletedrevs,
6 * Copyright © 2007 Roan Kattouw <roan.kattouw@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 namespace MediaWiki\Api
;
28 use MediaWiki\ChangeTags\ChangeTagsStore
;
29 use MediaWiki\CommentFormatter\CommentFormatter
;
30 use MediaWiki\Content\IContentHandlerFactory
;
31 use MediaWiki\Content\Renderer\ContentRenderer
;
32 use MediaWiki\Content\Transform\ContentTransformer
;
33 use MediaWiki\MainConfigNames
;
34 use MediaWiki\ParamValidator\TypeDef\UserDef
;
35 use MediaWiki\Parser\ParserFactory
;
36 use MediaWiki\Revision\RevisionRecord
;
37 use MediaWiki\Revision\RevisionStore
;
38 use MediaWiki\Revision\SlotRoleRegistry
;
39 use MediaWiki\Storage\NameTableAccessException
;
40 use MediaWiki\Storage\NameTableStore
;
41 use MediaWiki\Title\NamespaceInfo
;
42 use MediaWiki\Title\Title
;
43 use MediaWiki\User\TempUser\TempUserCreator
;
44 use MediaWiki\User\UserFactory
;
45 use Wikimedia\ParamValidator\ParamValidator
;
46 use Wikimedia\Rdbms\IExpression
;
47 use Wikimedia\Rdbms\LikeValue
;
50 * Query module to enumerate all deleted revisions.
54 class ApiQueryAllDeletedRevisions
extends ApiQueryRevisionsBase
{
56 private RevisionStore
$revisionStore;
57 private NameTableStore
$changeTagDefStore;
58 private ChangeTagsStore
$changeTagsStore;
59 private NamespaceInfo
$namespaceInfo;
61 public function __construct(
64 RevisionStore
$revisionStore,
65 IContentHandlerFactory
$contentHandlerFactory,
66 ParserFactory
$parserFactory,
67 SlotRoleRegistry
$slotRoleRegistry,
68 NameTableStore
$changeTagDefStore,
69 ChangeTagsStore
$changeTagsStore,
70 NamespaceInfo
$namespaceInfo,
71 ContentRenderer
$contentRenderer,
72 ContentTransformer
$contentTransformer,
73 CommentFormatter
$commentFormatter,
74 TempUserCreator
$tempUserCreator,
75 UserFactory
$userFactory
82 $contentHandlerFactory,
91 $this->revisionStore
= $revisionStore;
92 $this->changeTagDefStore
= $changeTagDefStore;
93 $this->changeTagsStore
= $changeTagsStore;
94 $this->namespaceInfo
= $namespaceInfo;
98 * @param ApiPageSet|null $resultPageSet
101 protected function run( ?ApiPageSet
$resultPageSet = null ) {
102 $db = $this->getDB();
103 $params = $this->extractRequestParams( false );
105 $result = $this->getResult();
107 // If the user wants no namespaces, they get no pages.
108 if ( $params['namespace'] === [] ) {
109 if ( $resultPageSet === null ) {
110 $result->addValue( 'query', $this->getModuleName(), [] );
115 // This module operates in two modes:
116 // 'user': List deleted revs by a certain user
117 // 'all': List all deleted revs in NS
119 if ( $params['user'] !== null ) {
123 if ( $mode == 'user' ) {
124 foreach ( [ 'from', 'to', 'prefix', 'excludeuser' ] as $param ) {
125 if ( $params[$param] !== null ) {
126 $p = $this->getModulePrefix();
128 [ 'apierror-invalidparammix-cannotusewith', $p . $param, "{$p}user" ],
134 foreach ( [ 'start', 'end' ] as $param ) {
135 if ( $params[$param] !== null ) {
136 $p = $this->getModulePrefix();
138 [ 'apierror-invalidparammix-mustusewith', $p . $param, "{$p}user" ],
145 // If we're generating titles only, we can use DISTINCT for a better
146 // query. But we can't do that in 'user' mode (wrong index), and we can
147 // only do it when sorting ASC (because MySQL apparently can't use an
148 // index backwards for grouping even though it can for ORDER BY, WTF?)
149 $dir = $params['dir'];
150 $optimizeGenerateTitles = false;
151 if ( $mode === 'all' && $params['generatetitles'] && $resultPageSet !== null ) {
152 if ( $dir === 'newer' ) {
153 $optimizeGenerateTitles = true;
155 $p = $this->getModulePrefix();
156 $this->addWarning( [ 'apiwarn-alldeletedrevisions-performance', $p ], 'performance' );
160 if ( $resultPageSet === null ) {
161 $this->parseParameters( $params );
162 $arQuery = $this->revisionStore
->getArchiveQueryInfo();
163 $this->addTables( $arQuery['tables'] );
164 $this->addJoinConds( $arQuery['joins'] );
165 $this->addFields( $arQuery['fields'] );
166 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
168 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
169 $this->addTables( 'archive' );
170 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
171 if ( $optimizeGenerateTitles ) {
172 $this->addOption( 'DISTINCT' );
174 $this->addFields( [ 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
176 if ( $params['user'] !== null ||
$params['excludeuser'] !== null ) {
177 $this->addTables( 'actor' );
178 $this->addJoinConds( [ 'actor' => 'actor_id=ar_actor' ] );
182 if ( $this->fld_tags
) {
184 'ts_tags' => $this->changeTagsStore
->makeTagSummarySubquery( 'archive' )
188 if ( $params['tag'] !== null ) {
189 $this->addTables( 'change_tag' );
191 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
194 $this->addWhereFld( 'ct_tag_id', $this->changeTagDefStore
->getId( $params['tag'] ) );
195 } catch ( NameTableAccessException
$exception ) {
197 $this->addWhere( '1=0' );
201 // This means stricter restrictions
202 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) &&
203 !$this->getAuthority()->isAllowed( 'deletedhistory' )
205 $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
207 if ( $this->fetchContent
&&
208 !$this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' )
210 $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
215 if ( $mode == 'all' ) {
216 $namespaces = $params['namespace'] ??
$this->namespaceInfo
->getValidNamespaces();
217 $this->addWhereFld( 'ar_namespace', $namespaces );
219 // For from/to/prefix, we have to consider the potential
220 // transformations of the title in all specified namespaces.
221 // Generally there will be only one transformation, but wikis with
222 // some namespaces case-sensitive could have two.
223 if ( $params['from'] !== null ||
$params['to'] !== null ) {
224 $isDirNewer = ( $dir === 'newer' );
225 $after = ( $isDirNewer ?
'>=' : '<=' );
226 $before = ( $isDirNewer ?
'<=' : '>=' );
228 foreach ( $namespaces as $ns ) {
229 if ( $params['from'] !== null ) {
230 $fromTitlePart = $this->titlePartToKey( $params['from'], $ns );
234 if ( $params['to'] !== null ) {
235 $toTitlePart = $this->titlePartToKey( $params['to'], $ns );
239 $titleParts[$fromTitlePart . '|' . $toTitlePart][] = $ns;
241 if ( count( $titleParts ) === 1 ) {
242 [ $fromTitlePart, $toTitlePart, ] = explode( '|', key( $titleParts ), 2 );
243 if ( $fromTitlePart !== '' ) {
244 $this->addWhere( $db->expr( 'ar_title', $after, $fromTitlePart ) );
246 if ( $toTitlePart !== '' ) {
247 $this->addWhere( $db->expr( 'ar_title', $before, $toTitlePart ) );
251 foreach ( $titleParts as $titlePart => $ns ) {
252 [ $fromTitlePart, $toTitlePart, ] = explode( '|', $titlePart, 2 );
253 $expr = $db->expr( 'ar_namespace', '=', $ns );
254 if ( $fromTitlePart !== '' ) {
255 $expr = $expr->and( 'ar_title', $after, $fromTitlePart );
257 if ( $toTitlePart !== '' ) {
258 $expr = $expr->and( 'ar_title', $before, $toTitlePart );
262 $this->addWhere( $db->orExpr( $where ) );
266 if ( isset( $params['prefix'] ) ) {
268 foreach ( $namespaces as $ns ) {
269 $prefixTitlePart = $this->titlePartToKey( $params['prefix'], $ns );
270 $titleParts[$prefixTitlePart][] = $ns;
272 if ( count( $titleParts ) === 1 ) {
273 $prefixTitlePart = key( $titleParts );
274 $this->addWhere( $db->expr( 'ar_title', IExpression
::LIKE
,
275 new LikeValue( $prefixTitlePart, $db->anyString() )
279 foreach ( $titleParts as $prefixTitlePart => $ns ) {
280 $where[] = $db->expr( 'ar_namespace', '=', $ns )
281 ->and( 'ar_title', IExpression
::LIKE
,
282 new LikeValue( $prefixTitlePart, $db->anyString() ) );
284 $this->addWhere( $db->orExpr( $where ) );
288 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
289 $miser_ns = $params['namespace'];
291 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
293 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
296 if ( $params['user'] !== null ) {
297 // We could get the actor ID from the ActorStore, but it's probably
298 // uncached at this point, and the non-generator case needs an actor
299 // join anyway so adding this join here is normally free. This should
300 // use the ar_actor_timestamp index.
301 $this->addWhereFld( 'actor_name', $params['user'] );
302 } elseif ( $params['excludeuser'] !== null ) {
303 $this->addWhere( $db->expr( 'actor_name', '!=', $params['excludeuser'] ) );
306 if ( $params['user'] !== null ||
$params['excludeuser'] !== null ) {
307 // Paranoia: avoid brute force searches (T19342)
308 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
309 $bitmask = RevisionRecord
::DELETED_USER
;
310 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
311 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
316 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
320 if ( $params['continue'] !== null ) {
321 $op = ( $dir == 'newer' ?
'>=' : '<=' );
322 if ( $optimizeGenerateTitles ) {
323 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
324 $this->addWhere( $db->buildComparison( $op, [
325 'ar_namespace' => $cont[0],
326 'ar_title' => $cont[1],
328 } elseif ( $mode == 'all' ) {
329 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'timestamp', 'int' ] );
330 $this->addWhere( $db->buildComparison( $op, [
331 'ar_namespace' => $cont[0],
332 'ar_title' => $cont[1],
333 'ar_timestamp' => $db->timestamp( $cont[2] ),
337 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] );
338 $this->addWhere( $db->buildComparison( $op, [
339 'ar_timestamp' => $db->timestamp( $cont[0] ),
345 $this->addOption( 'LIMIT', $this->limit +
1 );
347 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
349 if ( $optimizeGenerateTitles ) {
350 // Targeting index ar_name_title_timestamp
351 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
352 $orderby[] = "ar_namespace $sort";
354 $orderby[] = "ar_title $sort";
355 } elseif ( $mode == 'all' ) {
356 // Targeting index ar_name_title_timestamp
357 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
358 $orderby[] = "ar_namespace $sort";
360 $orderby[] = "ar_title $sort";
361 $orderby[] = "ar_timestamp $sort";
362 $orderby[] = "ar_id $sort";
364 // Targeting index usertext_timestamp
365 // 'user' is always constant.
366 $orderby[] = "ar_timestamp $sort";
367 $orderby[] = "ar_id $sort";
369 $this->addOption( 'ORDER BY', $orderby );
371 $res = $this->select( __METHOD__
);
373 if ( $resultPageSet === null ) {
374 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__
, 'ar' );
377 $pageMap = []; // Maps ns&title to array index
381 foreach ( $res as $row ) {
382 if ( ++
$count > $this->limit
) {
384 if ( $optimizeGenerateTitles ) {
385 $this->setContinueEnumParameter( 'continue', "$row->ar_namespace|$row->ar_title" );
386 } elseif ( $mode == 'all' ) {
387 $this->setContinueEnumParameter( 'continue',
388 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
391 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
396 // Miser mode namespace check
397 if ( $miser_ns !== null && !in_array( $row->ar_namespace
, $miser_ns ) ) {
401 if ( $resultPageSet !== null ) {
402 if ( $params['generatetitles'] ) {
403 $key = "{$row->ar_namespace}:{$row->ar_title}";
404 if ( !isset( $generated[$key] ) ) {
405 $generated[$key] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
408 $generated[] = $row->ar_rev_id
;
411 $revision = $this->revisionStore
->newRevisionFromArchiveRow( $row );
412 $rev = $this->extractRevisionInfo( $revision, $row );
414 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
415 $index = $nextIndex++
;
416 $pageMap[$row->ar_namespace
][$row->ar_title
] = $index;
417 $title = Title
::newFromLinkTarget( $revision->getPageAsLinkTarget() );
419 'pageid' => $title->getArticleID(),
420 'revisions' => [ $rev ],
422 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
423 ApiQueryBase
::addTitleInfo( $a, $title );
424 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
426 $index = $pageMap[$row->ar_namespace
][$row->ar_title
];
427 $fit = $result->addValue(
428 [ 'query', $this->getModuleName(), $index, 'revisions' ],
432 if ( $mode == 'all' ) {
433 $this->setContinueEnumParameter( 'continue',
434 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
437 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
444 if ( $resultPageSet !== null ) {
445 if ( $params['generatetitles'] ) {
446 $resultPageSet->populateFromTitles( $generated );
448 $resultPageSet->populateFromRevisionIDs( $generated );
451 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
455 public function getAllowedParams() {
456 $ret = parent
::getAllowedParams() +
[
458 ParamValidator
::PARAM_TYPE
=> 'user',
459 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
462 ParamValidator
::PARAM_ISMULTI
=> true,
463 ParamValidator
::PARAM_TYPE
=> 'namespace',
466 ParamValidator
::PARAM_TYPE
=> 'timestamp',
467 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
470 ParamValidator
::PARAM_TYPE
=> 'timestamp',
471 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
474 ParamValidator
::PARAM_TYPE
=> [
478 ParamValidator
::PARAM_DEFAULT
=> 'older',
479 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
480 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [
481 'newer' => 'api-help-paramvalue-direction-newer',
482 'older' => 'api-help-paramvalue-direction-older',
486 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
489 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
492 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
495 ParamValidator
::PARAM_TYPE
=> 'user',
496 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
497 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
501 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
503 'generatetitles' => [
504 ParamValidator
::PARAM_DEFAULT
=> false
508 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
509 $ret['user'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
510 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
512 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
513 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
520 protected function getExamplesMessages() {
522 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
523 => 'apihelp-query+alldeletedrevisions-example-user',
524 'action=query&list=alldeletedrevisions&adrdir=newer&adrnamespace=0&adrlimit=50'
525 => 'apihelp-query+alldeletedrevisions-example-ns-main',
529 public function getHelpUrls() {
530 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Alldeletedrevisions';
534 /** @deprecated class alias since 1.43 */
535 class_alias( ApiQueryAllDeletedRevisions
::class, 'ApiQueryAllDeletedRevisions' );