Update git submodules
[mediawiki.git] / includes / api / ApiQueryAllDeletedRevisions.php
blob663ea17ac0e82e6679e76eb8e474e0ffd907ff65
1 <?php
2 /**
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
23 * @file
26 use MediaWiki\CommentFormatter\CommentFormatter;
27 use MediaWiki\Content\IContentHandlerFactory;
28 use MediaWiki\Content\Renderer\ContentRenderer;
29 use MediaWiki\Content\Transform\ContentTransformer;
30 use MediaWiki\MainConfigNames;
31 use MediaWiki\ParamValidator\TypeDef\UserDef;
32 use MediaWiki\Revision\RevisionRecord;
33 use MediaWiki\Revision\RevisionStore;
34 use MediaWiki\Revision\SlotRoleRegistry;
35 use MediaWiki\Storage\NameTableAccessException;
36 use MediaWiki\Storage\NameTableStore;
37 use MediaWiki\Title\NamespaceInfo;
38 use MediaWiki\Title\Title;
39 use MediaWiki\User\TempUser\TempUserCreator;
40 use MediaWiki\User\UserFactory;
41 use Wikimedia\ParamValidator\ParamValidator;
43 /**
44 * Query module to enumerate all deleted revisions.
46 * @ingroup API
48 class ApiQueryAllDeletedRevisions extends ApiQueryRevisionsBase {
50 private RevisionStore $revisionStore;
51 private NameTableStore $changeTagDefStore;
52 private NamespaceInfo $namespaceInfo;
54 /**
55 * @param ApiQuery $query
56 * @param string $moduleName
57 * @param RevisionStore $revisionStore
58 * @param IContentHandlerFactory $contentHandlerFactory
59 * @param ParserFactory $parserFactory
60 * @param SlotRoleRegistry $slotRoleRegistry
61 * @param NameTableStore $changeTagDefStore
62 * @param NamespaceInfo $namespaceInfo
63 * @param ContentRenderer $contentRenderer
64 * @param ContentTransformer $contentTransformer
65 * @param CommentFormatter $commentFormatter
66 * @param TempUserCreator $tempUserCreator
67 * @param UserFactory $userFactory
69 public function __construct(
70 ApiQuery $query,
71 $moduleName,
72 RevisionStore $revisionStore,
73 IContentHandlerFactory $contentHandlerFactory,
74 ParserFactory $parserFactory,
75 SlotRoleRegistry $slotRoleRegistry,
76 NameTableStore $changeTagDefStore,
77 NamespaceInfo $namespaceInfo,
78 ContentRenderer $contentRenderer,
79 ContentTransformer $contentTransformer,
80 CommentFormatter $commentFormatter,
81 TempUserCreator $tempUserCreator,
82 UserFactory $userFactory
83 ) {
84 parent::__construct(
85 $query,
86 $moduleName,
87 'adr',
88 $revisionStore,
89 $contentHandlerFactory,
90 $parserFactory,
91 $slotRoleRegistry,
92 $contentRenderer,
93 $contentTransformer,
94 $commentFormatter,
95 $tempUserCreator,
96 $userFactory
98 $this->revisionStore = $revisionStore;
99 $this->changeTagDefStore = $changeTagDefStore;
100 $this->namespaceInfo = $namespaceInfo;
104 * @param ApiPageSet|null $resultPageSet
105 * @return void
107 protected function run( ApiPageSet $resultPageSet = null ) {
108 $db = $this->getDB();
109 $params = $this->extractRequestParams( false );
111 $result = $this->getResult();
113 // If the user wants no namespaces, they get no pages.
114 if ( $params['namespace'] === [] ) {
115 if ( $resultPageSet === null ) {
116 $result->addValue( 'query', $this->getModuleName(), [] );
118 return;
121 // This module operates in two modes:
122 // 'user': List deleted revs by a certain user
123 // 'all': List all deleted revs in NS
124 $mode = 'all';
125 if ( $params['user'] !== null ) {
126 $mode = 'user';
129 if ( $mode == 'user' ) {
130 foreach ( [ 'from', 'to', 'prefix', 'excludeuser' ] as $param ) {
131 if ( $params[$param] !== null ) {
132 $p = $this->getModulePrefix();
133 $this->dieWithError(
134 [ 'apierror-invalidparammix-cannotusewith', $p . $param, "{$p}user" ],
135 'invalidparammix'
139 } else {
140 foreach ( [ 'start', 'end' ] as $param ) {
141 if ( $params[$param] !== null ) {
142 $p = $this->getModulePrefix();
143 $this->dieWithError(
144 [ 'apierror-invalidparammix-mustusewith', $p . $param, "{$p}user" ],
145 'invalidparammix'
151 // If we're generating titles only, we can use DISTINCT for a better
152 // query. But we can't do that in 'user' mode (wrong index), and we can
153 // only do it when sorting ASC (because MySQL apparently can't use an
154 // index backwards for grouping even though it can for ORDER BY, WTF?)
155 $dir = $params['dir'];
156 $optimizeGenerateTitles = false;
157 if ( $mode === 'all' && $params['generatetitles'] && $resultPageSet !== null ) {
158 if ( $dir === 'newer' ) {
159 $optimizeGenerateTitles = true;
160 } else {
161 $p = $this->getModulePrefix();
162 $this->addWarning( [ 'apiwarn-alldeletedrevisions-performance', $p ], 'performance' );
166 if ( $resultPageSet === null ) {
167 $this->parseParameters( $params );
168 $arQuery = $this->revisionStore->getArchiveQueryInfo();
169 $this->addTables( $arQuery['tables'] );
170 $this->addJoinConds( $arQuery['joins'] );
171 $this->addFields( $arQuery['fields'] );
172 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
173 } else {
174 $this->limit = $this->getParameter( 'limit' ) ?: 10;
175 $this->addTables( 'archive' );
176 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
177 if ( $optimizeGenerateTitles ) {
178 $this->addOption( 'DISTINCT' );
179 } else {
180 $this->addFields( [ 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
182 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
183 $this->addTables( 'actor' );
184 $this->addJoinConds( [ 'actor' => 'actor_id=ar_actor' ] );
188 if ( $this->fld_tags ) {
189 $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
192 if ( $params['tag'] !== null ) {
193 $this->addTables( 'change_tag' );
194 $this->addJoinConds(
195 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
197 try {
198 $this->addWhereFld( 'ct_tag_id', $this->changeTagDefStore->getId( $params['tag'] ) );
199 } catch ( NameTableAccessException $exception ) {
200 // Return nothing.
201 $this->addWhere( '1=0' );
205 // This means stricter restrictions
206 if ( ( $this->fld_comment || $this->fld_parsedcomment ) &&
207 !$this->getAuthority()->isAllowed( 'deletedhistory' )
209 $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
211 if ( $this->fetchContent &&
212 !$this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' )
214 $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
217 $miser_ns = null;
219 if ( $mode == 'all' ) {
220 $namespaces = $params['namespace'] ?? $this->namespaceInfo->getValidNamespaces();
221 $this->addWhereFld( 'ar_namespace', $namespaces );
223 // For from/to/prefix, we have to consider the potential
224 // transformations of the title in all specified namespaces.
225 // Generally there will be only one transformation, but wikis with
226 // some namespaces case-sensitive could have two.
227 if ( $params['from'] !== null || $params['to'] !== null ) {
228 $isDirNewer = ( $dir === 'newer' );
229 $after = ( $isDirNewer ? '>=' : '<=' );
230 $before = ( $isDirNewer ? '<=' : '>=' );
231 $where = [];
232 foreach ( $namespaces as $ns ) {
233 $w = [];
234 if ( $params['from'] !== null ) {
235 $w[] = 'ar_title' . $after .
236 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
238 if ( $params['to'] !== null ) {
239 $w[] = 'ar_title' . $before .
240 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
242 $w = $db->makeList( $w, LIST_AND );
243 $where[$w][] = $ns;
245 if ( count( $where ) == 1 ) {
246 $where = key( $where );
247 $this->addWhere( $where );
248 } else {
249 $where2 = [];
250 foreach ( $where as $w => $ns ) {
251 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND );
253 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
257 if ( isset( $params['prefix'] ) ) {
258 $where = [];
259 foreach ( $namespaces as $ns ) {
260 $w = 'ar_title' . $db->buildLike(
261 $this->titlePartToKey( $params['prefix'], $ns ),
262 $db->anyString() );
263 $where[$w][] = $ns;
265 if ( count( $where ) == 1 ) {
266 $where = key( $where );
267 $this->addWhere( $where );
268 } else {
269 $where2 = [];
270 foreach ( $where as $w => $ns ) {
271 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND );
273 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
276 } else {
277 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
278 $miser_ns = $params['namespace'];
279 } else {
280 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
282 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
285 if ( $params['user'] !== null ) {
286 // We could get the actor ID from the ActorStore, but it's probably
287 // uncached at this point, and the non-generator case needs an actor
288 // join anyway so adding this join here is normally free. This should
289 // use the ar_actor_timestamp index.
290 $this->addWhereFld( 'actor_name', $params['user'] );
291 } elseif ( $params['excludeuser'] !== null ) {
292 $this->addWhere( 'actor_name<>' . $db->addQuotes( $params['excludeuser'] ) );
295 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
296 // Paranoia: avoid brute force searches (T19342)
297 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
298 $bitmask = RevisionRecord::DELETED_USER;
299 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
300 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
301 } else {
302 $bitmask = 0;
304 if ( $bitmask ) {
305 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
309 if ( $params['continue'] !== null ) {
310 $op = ( $dir == 'newer' ? '>=' : '<=' );
311 if ( $optimizeGenerateTitles ) {
312 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string' ] );
313 $this->addWhere( $db->buildComparison( $op, [
314 'ar_namespace' => $cont[0],
315 'ar_title' => $cont[1],
316 ] ) );
317 } elseif ( $mode == 'all' ) {
318 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'timestamp', 'int' ] );
319 $this->addWhere( $db->buildComparison( $op, [
320 'ar_namespace' => $cont[0],
321 'ar_title' => $cont[1],
322 'ar_timestamp' => $db->timestamp( $cont[2] ),
323 'ar_id' => $cont[3],
324 ] ) );
325 } else {
326 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] );
327 $this->addWhere( $db->buildComparison( $op, [
328 'ar_timestamp' => $db->timestamp( $cont[0] ),
329 'ar_id' => $cont[1],
330 ] ) );
334 $this->addOption( 'LIMIT', $this->limit + 1 );
336 $sort = ( $dir == 'newer' ? '' : ' DESC' );
337 $orderby = [];
338 if ( $optimizeGenerateTitles ) {
339 // Targeting index ar_name_title_timestamp
340 if ( $params['namespace'] === null || count( array_unique( $params['namespace'] ) ) > 1 ) {
341 $orderby[] = "ar_namespace $sort";
343 $orderby[] = "ar_title $sort";
344 } elseif ( $mode == 'all' ) {
345 // Targeting index ar_name_title_timestamp
346 if ( $params['namespace'] === null || count( array_unique( $params['namespace'] ) ) > 1 ) {
347 $orderby[] = "ar_namespace $sort";
349 $orderby[] = "ar_title $sort";
350 $orderby[] = "ar_timestamp $sort";
351 $orderby[] = "ar_id $sort";
352 } else {
353 // Targeting index usertext_timestamp
354 // 'user' is always constant.
355 $orderby[] = "ar_timestamp $sort";
356 $orderby[] = "ar_id $sort";
358 $this->addOption( 'ORDER BY', $orderby );
360 $res = $this->select( __METHOD__ );
362 if ( $resultPageSet === null ) {
363 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__, 'ar' );
366 $pageMap = []; // Maps ns&title to array index
367 $count = 0;
368 $nextIndex = 0;
369 $generated = [];
370 foreach ( $res as $row ) {
371 if ( ++$count > $this->limit ) {
372 // We've had enough
373 if ( $optimizeGenerateTitles ) {
374 $this->setContinueEnumParameter( 'continue', "$row->ar_namespace|$row->ar_title" );
375 } elseif ( $mode == 'all' ) {
376 $this->setContinueEnumParameter( 'continue',
377 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
379 } else {
380 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
382 break;
385 // Miser mode namespace check
386 if ( $miser_ns !== null && !in_array( $row->ar_namespace, $miser_ns ) ) {
387 continue;
390 if ( $resultPageSet !== null ) {
391 if ( $params['generatetitles'] ) {
392 $key = "{$row->ar_namespace}:{$row->ar_title}";
393 if ( !isset( $generated[$key] ) ) {
394 $generated[$key] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
396 } else {
397 $generated[] = $row->ar_rev_id;
399 } else {
400 $revision = $this->revisionStore->newRevisionFromArchiveRow( $row );
401 $rev = $this->extractRevisionInfo( $revision, $row );
403 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
404 $index = $nextIndex++;
405 $pageMap[$row->ar_namespace][$row->ar_title] = $index;
406 $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
407 $a = [
408 'pageid' => $title->getArticleID(),
409 'revisions' => [ $rev ],
411 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
412 ApiQueryBase::addTitleInfo( $a, $title );
413 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
414 } else {
415 $index = $pageMap[$row->ar_namespace][$row->ar_title];
416 $fit = $result->addValue(
417 [ 'query', $this->getModuleName(), $index, 'revisions' ],
418 null, $rev );
420 if ( !$fit ) {
421 if ( $mode == 'all' ) {
422 $this->setContinueEnumParameter( 'continue',
423 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
425 } else {
426 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
428 break;
433 if ( $resultPageSet !== null ) {
434 if ( $params['generatetitles'] ) {
435 $resultPageSet->populateFromTitles( $generated );
436 } else {
437 $resultPageSet->populateFromRevisionIDs( $generated );
439 } else {
440 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
444 public function getAllowedParams() {
445 $ret = parent::getAllowedParams() + [
446 'user' => [
447 ParamValidator::PARAM_TYPE => 'user',
448 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
450 'namespace' => [
451 ParamValidator::PARAM_ISMULTI => true,
452 ParamValidator::PARAM_TYPE => 'namespace',
454 'start' => [
455 ParamValidator::PARAM_TYPE => 'timestamp',
456 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'useronly' ] ],
458 'end' => [
459 ParamValidator::PARAM_TYPE => 'timestamp',
460 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'useronly' ] ],
462 'dir' => [
463 ParamValidator::PARAM_TYPE => [
464 'newer',
465 'older'
467 ParamValidator::PARAM_DEFAULT => 'older',
468 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
469 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
470 'newer' => 'api-help-paramvalue-direction-newer',
471 'older' => 'api-help-paramvalue-direction-older',
474 'from' => [
475 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
477 'to' => [
478 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
480 'prefix' => [
481 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
483 'excludeuser' => [
484 ParamValidator::PARAM_TYPE => 'user',
485 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
486 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'nonuseronly' ] ],
488 'tag' => null,
489 'continue' => [
490 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
492 'generatetitles' => [
493 ParamValidator::PARAM_DEFAULT => false
497 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
498 $ret['user'][ApiBase::PARAM_HELP_MSG_APPEND] = [
499 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
501 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
502 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
506 return $ret;
509 protected function getExamplesMessages() {
510 return [
511 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
512 => 'apihelp-query+alldeletedrevisions-example-user',
513 'action=query&list=alldeletedrevisions&adrdir=newer&adrnamespace=0&adrlimit=50'
514 => 'apihelp-query+alldeletedrevisions-example-ns-main',
518 public function getHelpUrls() {
519 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Alldeletedrevisions';