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
;
27 * Query module to enumerate all deleted revisions.
30 * @deprecated since 1.25
32 class ApiQueryDeletedrevs
extends ApiQueryBase
{
34 public function __construct( ApiQuery
$query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'dr' );
38 public function execute() {
39 // Before doing anything at all, let's check permissions
40 $this->checkUserRightsAny( 'deletedhistory' );
42 $this->addDeprecation( 'apiwarn-deprecation-deletedrevs', 'action=query&list=deletedrevs' );
44 $user = $this->getUser();
46 $commentStore = CommentStore
::getStore();
47 $params = $this->extractRequestParams( false );
48 $prop = array_flip( $params['prop'] );
49 $fld_parentid = isset( $prop['parentid'] );
50 $fld_revid = isset( $prop['revid'] );
51 $fld_user = isset( $prop['user'] );
52 $fld_userid = isset( $prop['userid'] );
53 $fld_comment = isset( $prop['comment'] );
54 $fld_parsedcomment = isset( $prop['parsedcomment'] );
55 $fld_minor = isset( $prop['minor'] );
56 $fld_len = isset( $prop['len'] );
57 $fld_sha1 = isset( $prop['sha1'] );
58 $fld_content = isset( $prop['content'] );
59 $fld_token = isset( $prop['token'] );
60 $fld_tags = isset( $prop['tags'] );
62 if ( isset( $prop['token'] ) ) {
63 $p = $this->getModulePrefix();
66 // If we're in a mode that breaks the same-origin policy, no tokens can
68 if ( $this->lacksSameOriginSecurity() ) {
72 // If user can't undelete, no tokens
73 if ( !$user->isAllowed( 'undelete' ) ) {
77 $result = $this->getResult();
78 $pageSet = $this->getPageSet();
79 $titles = $pageSet->getTitles();
81 // This module operates in three modes:
82 // 'revs': List deleted revs for certain titles (1)
83 // 'user': List deleted revs by a certain user (2)
84 // 'all': List all deleted revs in NS (3)
86 if ( count( $titles ) > 0 ) {
88 } elseif ( !is_null( $params['user'] ) ) {
92 if ( $mode == 'revs' ||
$mode == 'user' ) {
93 // Ignore namespace and unique due to inability to know whether they were purposely set
94 foreach ( [ 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ] as $p ) {
95 if ( !is_null( $params[$p] ) ) {
96 $this->dieWithError( [ 'apierror-deletedrevs-param-not-1-2', $p ], 'badparams' );
100 foreach ( [ 'start', 'end' ] as $p ) {
101 if ( !is_null( $params[$p] ) ) {
102 $this->dieWithError( [ 'apierror-deletedrevs-param-not-3', $p ], 'badparams' );
107 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
108 $this->dieWithError( 'user and excludeuser cannot be used together', 'badparams' );
111 $this->addTables( 'archive' );
112 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_deleted', 'ar_id' ] );
114 $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
115 $this->addFieldsIf( 'ar_rev_id', $fld_revid );
116 if ( $fld_user ||
$fld_userid ) {
117 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ar_user' );
118 $this->addTables( $actorQuery['tables'] );
119 $this->addFields( $actorQuery['fields'] );
120 $this->addJoinConds( $actorQuery['joins'] );
122 $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
123 $this->addFieldsIf( 'ar_len', $fld_len );
124 $this->addFieldsIf( 'ar_sha1', $fld_sha1 );
126 if ( $fld_comment ||
$fld_parsedcomment ) {
127 $commentQuery = $commentStore->getJoin( 'ar_comment' );
128 $this->addTables( $commentQuery['tables'] );
129 $this->addFields( $commentQuery['fields'] );
130 $this->addJoinConds( $commentQuery['joins'] );
134 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
137 if ( !is_null( $params['tag'] ) ) {
138 $this->addTables( 'change_tag' );
140 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
142 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
144 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
145 } catch ( NameTableAccessException
$exception ) {
147 $this->addWhere( '1=0' );
151 if ( $fld_content ) {
152 $this->addTables( 'text' );
154 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
156 $this->addFields( [ 'ar_text_id', 'old_text', 'old_flags' ] );
158 // This also means stricter restrictions
159 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
162 $userMax = $fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
;
163 $botMax = $fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
;
165 $limit = $params['limit'];
167 if ( $limit == 'max' ) {
168 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
169 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
172 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
175 // Undelete tokens are identical for all pages, so we cache one here
176 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
179 $dir = $params['dir'];
181 // We need a custom WHERE clause that matches all titles.
182 if ( $mode == 'revs' ) {
183 $lb = new LinkBatch( $titles );
184 $where = $lb->constructSet( 'ar', $db );
185 $this->addWhere( $where );
186 } elseif ( $mode == 'all' ) {
187 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
189 $from = $params['from'] === null
191 : $this->titlePartToKey( $params['from'], $params['namespace'] );
192 $to = $params['to'] === null
194 : $this->titlePartToKey( $params['to'], $params['namespace'] );
195 $this->addWhereRange( 'ar_title', $dir, $from, $to );
197 if ( isset( $params['prefix'] ) ) {
198 $this->addWhere( 'ar_title' . $db->buildLike(
199 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
200 $db->anyString() ) );
204 if ( !is_null( $params['user'] ) ) {
205 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
206 $actorQuery = ActorMigration
::newMigration()
207 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
208 $this->addTables( $actorQuery['tables'] );
209 $this->addJoinConds( $actorQuery['joins'] );
210 $this->addWhere( $actorQuery['conds'] );
211 } elseif ( !is_null( $params['excludeuser'] ) ) {
212 // Here there's no chance of using ar_usertext_timestamp.
213 $actorQuery = ActorMigration
::newMigration()
214 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
215 $this->addTables( $actorQuery['tables'] );
216 $this->addJoinConds( $actorQuery['joins'] );
217 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
220 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
221 // Paranoia: avoid brute force searches (T19342)
222 // (shouldn't be able to get here without 'deletedhistory', but
223 // check it again just in case)
224 if ( !$user->isAllowed( 'deletedhistory' ) ) {
225 $bitmask = Revision
::DELETED_USER
;
226 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
227 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
232 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
236 if ( !is_null( $params['continue'] ) ) {
237 $cont = explode( '|', $params['continue'] );
238 $op = ( $dir == 'newer' ?
'>' : '<' );
239 if ( $mode == 'all' ||
$mode == 'revs' ) {
240 $this->dieContinueUsageIf( count( $cont ) != 4 );
241 $ns = intval( $cont[0] );
242 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
243 $title = $db->addQuotes( $cont[1] );
244 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
245 $ar_id = (int)$cont[3];
246 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
247 $this->addWhere( "ar_namespace $op $ns OR " .
248 "(ar_namespace = $ns AND " .
249 "(ar_title $op $title OR " .
250 "(ar_title = $title AND " .
251 "(ar_timestamp $op $ts OR " .
252 "(ar_timestamp = $ts AND " .
253 "ar_id $op= $ar_id)))))" );
255 $this->dieContinueUsageIf( count( $cont ) != 2 );
256 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
257 $ar_id = (int)$cont[1];
258 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
259 $this->addWhere( "ar_timestamp $op $ts OR " .
260 "(ar_timestamp = $ts AND " .
261 "ar_id $op= $ar_id)" );
265 $this->addOption( 'LIMIT', $limit +
1 );
266 if ( $mode == 'all' ) {
267 if ( $params['unique'] ) {
268 // @todo Does this work on non-MySQL?
269 $this->addOption( 'GROUP BY', 'ar_title' );
271 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
272 $this->addOption( 'ORDER BY', [
274 'ar_timestamp' . $sort,
279 if ( $mode == 'revs' ) {
280 // Sort by ns and title in the same order as timestamp for efficiency
281 $this->addWhereRange( 'ar_namespace', $dir, null, null );
282 $this->addWhereRange( 'ar_title', $dir, null, null );
284 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
285 // Include in ORDER BY for uniqueness
286 $this->addWhereRange( 'ar_id', $dir, null, null );
288 $res = $this->select( __METHOD__
);
289 $pageMap = []; // Maps ns&title to (fake) pageid
292 foreach ( $res as $row ) {
293 if ( ++
$count > $limit ) {
295 if ( $mode == 'all' ||
$mode == 'revs' ) {
296 $this->setContinueEnumParameter( 'continue',
297 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
300 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
308 $rev['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
);
310 $rev['revid'] = intval( $row->ar_rev_id
);
312 if ( $fld_parentid && !is_null( $row->ar_parent_id
) ) {
313 $rev['parentid'] = intval( $row->ar_parent_id
);
315 if ( $fld_user ||
$fld_userid ) {
316 if ( $row->ar_deleted
& Revision
::DELETED_USER
) {
317 $rev['userhidden'] = true;
320 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_USER
, $user ) ) {
322 $rev['user'] = $row->ar_user_text
;
325 $rev['userid'] = (int)$row->ar_user
;
330 if ( $fld_comment ||
$fld_parsedcomment ) {
331 if ( $row->ar_deleted
& Revision
::DELETED_COMMENT
) {
332 $rev['commenthidden'] = true;
335 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
336 $comment = $commentStore->getComment( 'ar_comment', $row )->text
;
337 if ( $fld_comment ) {
338 $rev['comment'] = $comment;
340 if ( $fld_parsedcomment ) {
341 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
342 $rev['parsedcomment'] = Linker
::formatComment( $comment, $title );
348 $rev['minor'] = $row->ar_minor_edit
== 1;
351 $rev['len'] = $row->ar_len
;
354 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
355 $rev['sha1hidden'] = true;
358 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
359 if ( $row->ar_sha1
!= '' ) {
360 $rev['sha1'] = Wikimedia\base_convert
( $row->ar_sha1
, 36, 16, 40 );
366 if ( $fld_content ) {
367 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
368 $rev['texthidden'] = true;
371 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
372 ApiResult
::setContentValue( $rev, 'text', Revision
::getRevisionText( $row ) );
377 if ( $row->ts_tags
) {
378 $tags = explode( ',', $row->ts_tags
);
379 ApiResult
::setIndexedTagName( $tags, 'tag' );
380 $rev['tags'] = $tags;
386 if ( $anyHidden && ( $row->ar_deleted
& Revision
::DELETED_RESTRICTED
) ) {
387 $rev['suppressed'] = true;
390 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
391 $pageID = $newPageID++
;
392 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
393 $a['revisions'] = [ $rev ];
394 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
395 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
396 ApiQueryBase
::addTitleInfo( $a, $title );
398 $a['token'] = $token;
400 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
402 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
403 $fit = $result->addValue(
404 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
408 if ( $mode == 'all' ||
$mode == 'revs' ) {
409 $this->setContinueEnumParameter( 'continue',
410 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
413 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
418 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
421 public function isDeprecated() {
425 public function getAllowedParams() {
428 ApiBase
::PARAM_TYPE
=> 'timestamp',
429 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
432 ApiBase
::PARAM_TYPE
=> 'timestamp',
433 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
436 ApiBase
::PARAM_TYPE
=> [
440 ApiBase
::PARAM_DFLT
=> 'older',
441 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
442 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 3 ] ],
445 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
448 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
451 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
454 ApiBase
::PARAM_DFLT
=> false,
455 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
458 ApiBase
::PARAM_TYPE
=> 'namespace',
459 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
460 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
464 ApiBase
::PARAM_TYPE
=> 'user'
467 ApiBase
::PARAM_TYPE
=> 'user'
470 ApiBase
::PARAM_DFLT
=> 'user|comment',
471 ApiBase
::PARAM_TYPE
=> [
485 ApiBase
::PARAM_ISMULTI
=> true
488 ApiBase
::PARAM_DFLT
=> 10,
489 ApiBase
::PARAM_TYPE
=> 'limit',
490 ApiBase
::PARAM_MIN
=> 1,
491 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
492 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
495 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
500 protected function getExamplesMessages() {
502 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
503 'drprop=user|comment|content'
504 => 'apihelp-query+deletedrevs-example-mode1',
505 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
506 => 'apihelp-query+deletedrevs-example-mode2',
507 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
508 => 'apihelp-query+deletedrevs-example-mode3-main',
509 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
510 => 'apihelp-query+deletedrevs-example-mode3-talk',
514 public function getHelpUrls() {
515 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';