3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Storage\NameTableAccessException
;
25 use MediaWiki\Storage\RevisionRecord
;
28 * Query module to enumerate all deleted revisions.
31 * @deprecated since 1.25
33 class ApiQueryDeletedrevs
extends ApiQueryBase
{
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'dr' );
39 public function execute() {
40 // Before doing anything at all, let's check permissions
41 $this->checkUserRightsAny( 'deletedhistory' );
43 $this->addDeprecation( 'apiwarn-deprecation-deletedrevs', 'action=query&list=deletedrevs' );
45 $user = $this->getUser();
47 $commentStore = CommentStore
::getStore();
48 $params = $this->extractRequestParams( false );
49 $prop = array_flip( $params['prop'] );
50 $fld_parentid = isset( $prop['parentid'] );
51 $fld_revid = isset( $prop['revid'] );
52 $fld_user = isset( $prop['user'] );
53 $fld_userid = isset( $prop['userid'] );
54 $fld_comment = isset( $prop['comment'] );
55 $fld_parsedcomment = isset( $prop['parsedcomment'] );
56 $fld_minor = isset( $prop['minor'] );
57 $fld_len = isset( $prop['len'] );
58 $fld_sha1 = isset( $prop['sha1'] );
59 $fld_content = isset( $prop['content'] );
60 $fld_token = isset( $prop['token'] );
61 $fld_tags = isset( $prop['tags'] );
63 // If we're in a mode that breaks the same-origin policy, no tokens can
65 if ( $this->lacksSameOriginSecurity() ) {
69 // If user can't undelete, no tokens
70 if ( !$this->getPermissionManager()->userHasRight( $user, 'undelete' ) ) {
74 $result = $this->getResult();
75 $pageSet = $this->getPageSet();
76 $titles = $pageSet->getTitles();
78 // This module operates in three modes:
79 // 'revs': List deleted revs for certain titles (1)
80 // 'user': List deleted revs by a certain user (2)
81 // 'all': List all deleted revs in NS (3)
83 if ( count( $titles ) > 0 ) {
85 } elseif ( !is_null( $params['user'] ) ) {
89 if ( $mode == 'revs' ||
$mode == 'user' ) {
90 // Ignore namespace and unique due to inability to know whether they were purposely set
91 foreach ( [ 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ] as $p ) {
92 if ( !is_null( $params[$p] ) ) {
93 $this->dieWithError( [ 'apierror-deletedrevs-param-not-1-2', $p ], 'badparams' );
97 foreach ( [ 'start', 'end' ] as $p ) {
98 if ( !is_null( $params[$p] ) ) {
99 $this->dieWithError( [ 'apierror-deletedrevs-param-not-3', $p ], 'badparams' );
104 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
105 $this->dieWithError( 'user and excludeuser cannot be used together', 'badparams' );
108 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
109 $arQuery = $revisionStore->getArchiveQueryInfo();
110 $this->addTables( $arQuery['tables'] );
111 $this->addFields( $arQuery['fields'] );
112 $this->addJoinConds( $arQuery['joins'] );
113 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
116 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
119 if ( !is_null( $params['tag'] ) ) {
120 $this->addTables( 'change_tag' );
122 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
124 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
126 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
127 } catch ( NameTableAccessException
$exception ) {
129 $this->addWhere( '1=0' );
133 // This means stricter restrictions
134 if ( $fld_content ) {
135 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
138 $userMax = $fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
;
139 $botMax = $fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
;
141 $limit = $params['limit'];
143 if ( $limit == 'max' ) {
144 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
145 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
148 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
151 // Undelete tokens are identical for all pages, so we cache one here
152 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
155 $dir = $params['dir'];
157 // We need a custom WHERE clause that matches all titles.
158 if ( $mode == 'revs' ) {
159 $lb = new LinkBatch( $titles );
160 $where = $lb->constructSet( 'ar', $db );
161 $this->addWhere( $where );
162 } elseif ( $mode == 'all' ) {
163 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
165 $from = $params['from'] === null
167 : $this->titlePartToKey( $params['from'], $params['namespace'] );
168 $to = $params['to'] === null
170 : $this->titlePartToKey( $params['to'], $params['namespace'] );
171 $this->addWhereRange( 'ar_title', $dir, $from, $to );
173 if ( isset( $params['prefix'] ) ) {
174 $this->addWhere( 'ar_title' . $db->buildLike(
175 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
176 $db->anyString() ) );
180 if ( !is_null( $params['user'] ) ) {
181 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
182 $actorQuery = ActorMigration
::newMigration()
183 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
184 $this->addTables( $actorQuery['tables'] );
185 $this->addJoinConds( $actorQuery['joins'] );
186 $this->addWhere( $actorQuery['conds'] );
187 } elseif ( !is_null( $params['excludeuser'] ) ) {
188 // Here there's no chance of using ar_usertext_timestamp.
189 $actorQuery = ActorMigration
::newMigration()
190 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
191 $this->addTables( $actorQuery['tables'] );
192 $this->addJoinConds( $actorQuery['joins'] );
193 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
196 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
197 // Paranoia: avoid brute force searches (T19342)
198 // (shouldn't be able to get here without 'deletedhistory', but
199 // check it again just in case)
200 if ( !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' ) ) {
201 $bitmask = RevisionRecord
::DELETED_USER
;
202 } elseif ( !$this->getPermissionManager()
203 ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
205 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
210 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
214 if ( !is_null( $params['continue'] ) ) {
215 $cont = explode( '|', $params['continue'] );
216 $op = ( $dir == 'newer' ?
'>' : '<' );
217 if ( $mode == 'all' ||
$mode == 'revs' ) {
218 $this->dieContinueUsageIf( count( $cont ) != 4 );
220 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
221 $title = $db->addQuotes( $cont[1] );
222 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
223 $ar_id = (int)$cont[3];
224 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
225 $this->addWhere( "ar_namespace $op $ns OR " .
226 "(ar_namespace = $ns AND " .
227 "(ar_title $op $title OR " .
228 "(ar_title = $title AND " .
229 "(ar_timestamp $op $ts OR " .
230 "(ar_timestamp = $ts AND " .
231 "ar_id $op= $ar_id)))))" );
233 $this->dieContinueUsageIf( count( $cont ) != 2 );
234 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
235 $ar_id = (int)$cont[1];
236 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
237 $this->addWhere( "ar_timestamp $op $ts OR " .
238 "(ar_timestamp = $ts AND " .
239 "ar_id $op= $ar_id)" );
243 $this->addOption( 'LIMIT', $limit +
1 );
244 if ( $mode == 'all' ) {
245 if ( $params['unique'] ) {
246 // @todo Does this work on non-MySQL?
247 $this->addOption( 'GROUP BY', 'ar_title' );
249 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
250 $this->addOption( 'ORDER BY', [
252 'ar_timestamp' . $sort,
257 if ( $mode == 'revs' ) {
258 // Sort by ns and title in the same order as timestamp for efficiency
259 $this->addWhereRange( 'ar_namespace', $dir, null, null );
260 $this->addWhereRange( 'ar_title', $dir, null, null );
262 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
263 // Include in ORDER BY for uniqueness
264 $this->addWhereRange( 'ar_id', $dir, null, null );
266 $res = $this->select( __METHOD__
);
267 $pageMap = []; // Maps ns&title to (fake) pageid
270 foreach ( $res as $row ) {
271 if ( ++
$count > $limit ) {
273 if ( $mode == 'all' ||
$mode == 'revs' ) {
274 $this->setContinueEnumParameter( 'continue',
275 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
278 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
286 $rev['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
);
288 $rev['revid'] = (int)$row->ar_rev_id
;
290 if ( $fld_parentid && !is_null( $row->ar_parent_id
) ) {
291 $rev['parentid'] = (int)$row->ar_parent_id
;
293 if ( $fld_user ||
$fld_userid ) {
294 if ( $row->ar_deleted
& RevisionRecord
::DELETED_USER
) {
295 $rev['userhidden'] = true;
298 if ( Revision
::userCanBitfield( $row->ar_deleted
, RevisionRecord
::DELETED_USER
, $user ) ) {
300 $rev['user'] = $row->ar_user_text
;
303 $rev['userid'] = (int)$row->ar_user
;
308 if ( $fld_comment ||
$fld_parsedcomment ) {
309 if ( $row->ar_deleted
& RevisionRecord
::DELETED_COMMENT
) {
310 $rev['commenthidden'] = true;
313 if ( Revision
::userCanBitfield( $row->ar_deleted
, RevisionRecord
::DELETED_COMMENT
, $user ) ) {
314 $comment = $commentStore->getComment( 'ar_comment', $row )->text
;
315 if ( $fld_comment ) {
316 $rev['comment'] = $comment;
318 if ( $fld_parsedcomment ) {
319 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
320 $rev['parsedcomment'] = Linker
::formatComment( $comment, $title );
326 $rev['minor'] = $row->ar_minor_edit
== 1;
329 $rev['len'] = $row->ar_len
;
332 if ( $row->ar_deleted
& RevisionRecord
::DELETED_TEXT
) {
333 $rev['sha1hidden'] = true;
336 if ( Revision
::userCanBitfield( $row->ar_deleted
, RevisionRecord
::DELETED_TEXT
, $user ) ) {
337 if ( $row->ar_sha1
!= '' ) {
338 $rev['sha1'] = Wikimedia\base_convert
( $row->ar_sha1
, 36, 16, 40 );
344 if ( $fld_content ) {
345 if ( $row->ar_deleted
& RevisionRecord
::DELETED_TEXT
) {
346 $rev['texthidden'] = true;
349 if ( Revision
::userCanBitfield( $row->ar_deleted
, RevisionRecord
::DELETED_TEXT
, $user ) ) {
350 ApiResult
::setContentValue( $rev, 'text', Revision
::getRevisionText( $row, 'ar_' ) );
355 if ( $row->ts_tags
) {
356 $tags = explode( ',', $row->ts_tags
);
357 ApiResult
::setIndexedTagName( $tags, 'tag' );
358 $rev['tags'] = $tags;
364 if ( $anyHidden && ( $row->ar_deleted
& RevisionRecord
::DELETED_RESTRICTED
) ) {
365 $rev['suppressed'] = true;
368 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
369 $pageID = $newPageID++
;
370 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
371 $a['revisions'] = [ $rev ];
372 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
373 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
374 ApiQueryBase
::addTitleInfo( $a, $title );
376 $a['token'] = $token;
378 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
380 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
381 $fit = $result->addValue(
382 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
386 if ( $mode == 'all' ||
$mode == 'revs' ) {
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 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
399 public function isDeprecated() {
403 public function getAllowedParams() {
406 ApiBase
::PARAM_TYPE
=> 'timestamp',
407 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
410 ApiBase
::PARAM_TYPE
=> 'timestamp',
411 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
414 ApiBase
::PARAM_TYPE
=> [
418 ApiBase
::PARAM_DFLT
=> 'older',
419 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
420 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 3 ] ],
423 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
426 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
429 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
432 ApiBase
::PARAM_DFLT
=> false,
433 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
436 ApiBase
::PARAM_TYPE
=> 'namespace',
437 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
438 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
442 ApiBase
::PARAM_TYPE
=> 'user'
445 ApiBase
::PARAM_TYPE
=> 'user'
448 ApiBase
::PARAM_DFLT
=> 'user|comment',
449 ApiBase
::PARAM_TYPE
=> [
463 ApiBase
::PARAM_ISMULTI
=> true
466 ApiBase
::PARAM_DFLT
=> 10,
467 ApiBase
::PARAM_TYPE
=> 'limit',
468 ApiBase
::PARAM_MIN
=> 1,
469 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
470 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
473 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
478 protected function getExamplesMessages() {
480 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
481 'drprop=user|comment|content'
482 => 'apihelp-query+deletedrevs-example-mode1',
483 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
484 => 'apihelp-query+deletedrevs-example-mode2',
485 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
486 => 'apihelp-query+deletedrevs-example-mode3-main',
487 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
488 => 'apihelp-query+deletedrevs-example-mode3-talk',
492 public function getHelpUrls() {
493 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';