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 we're in a mode that breaks the same-origin policy, no tokens can
64 if ( $this->lacksSameOriginSecurity() ) {
68 // If user can't undelete, no tokens
69 if ( !$user->isAllowed( 'undelete' ) ) {
73 $result = $this->getResult();
74 $pageSet = $this->getPageSet();
75 $titles = $pageSet->getTitles();
77 // This module operates in three modes:
78 // 'revs': List deleted revs for certain titles (1)
79 // 'user': List deleted revs by a certain user (2)
80 // 'all': List all deleted revs in NS (3)
82 if ( count( $titles ) > 0 ) {
84 } elseif ( !is_null( $params['user'] ) ) {
88 if ( $mode == 'revs' ||
$mode == 'user' ) {
89 // Ignore namespace and unique due to inability to know whether they were purposely set
90 foreach ( [ 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ] as $p ) {
91 if ( !is_null( $params[$p] ) ) {
92 $this->dieWithError( [ 'apierror-deletedrevs-param-not-1-2', $p ], 'badparams' );
96 foreach ( [ 'start', 'end' ] as $p ) {
97 if ( !is_null( $params[$p] ) ) {
98 $this->dieWithError( [ 'apierror-deletedrevs-param-not-3', $p ], 'badparams' );
103 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
104 $this->dieWithError( 'user and excludeuser cannot be used together', 'badparams' );
107 $this->addTables( 'archive' );
108 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_deleted', 'ar_id' ] );
110 $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
111 $this->addFieldsIf( 'ar_rev_id', $fld_revid );
112 if ( $fld_user ||
$fld_userid ) {
113 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ar_user' );
114 $this->addTables( $actorQuery['tables'] );
115 $this->addFields( $actorQuery['fields'] );
116 $this->addJoinConds( $actorQuery['joins'] );
118 $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
119 $this->addFieldsIf( 'ar_len', $fld_len );
120 $this->addFieldsIf( 'ar_sha1', $fld_sha1 );
122 if ( $fld_comment ||
$fld_parsedcomment ) {
123 $commentQuery = $commentStore->getJoin( 'ar_comment' );
124 $this->addTables( $commentQuery['tables'] );
125 $this->addFields( $commentQuery['fields'] );
126 $this->addJoinConds( $commentQuery['joins'] );
130 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
133 if ( !is_null( $params['tag'] ) ) {
134 $this->addTables( 'change_tag' );
136 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
138 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
140 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
141 } catch ( NameTableAccessException
$exception ) {
143 $this->addWhere( '1=0' );
147 if ( $fld_content ) {
148 $this->addTables( 'text' );
150 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
152 $this->addFields( [ 'ar_text_id', 'old_text', 'old_flags' ] );
154 // This also means stricter restrictions
155 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
158 $userMax = $fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
;
159 $botMax = $fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
;
161 $limit = $params['limit'];
163 if ( $limit == 'max' ) {
164 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
165 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
168 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
171 // Undelete tokens are identical for all pages, so we cache one here
172 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
175 $dir = $params['dir'];
177 // We need a custom WHERE clause that matches all titles.
178 if ( $mode == 'revs' ) {
179 $lb = new LinkBatch( $titles );
180 $where = $lb->constructSet( 'ar', $db );
181 $this->addWhere( $where );
182 } elseif ( $mode == 'all' ) {
183 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
185 $from = $params['from'] === null
187 : $this->titlePartToKey( $params['from'], $params['namespace'] );
188 $to = $params['to'] === null
190 : $this->titlePartToKey( $params['to'], $params['namespace'] );
191 $this->addWhereRange( 'ar_title', $dir, $from, $to );
193 if ( isset( $params['prefix'] ) ) {
194 $this->addWhere( 'ar_title' . $db->buildLike(
195 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
196 $db->anyString() ) );
200 if ( !is_null( $params['user'] ) ) {
201 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
202 $actorQuery = ActorMigration
::newMigration()
203 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
204 $this->addTables( $actorQuery['tables'] );
205 $this->addJoinConds( $actorQuery['joins'] );
206 $this->addWhere( $actorQuery['conds'] );
207 } elseif ( !is_null( $params['excludeuser'] ) ) {
208 // Here there's no chance of using ar_usertext_timestamp.
209 $actorQuery = ActorMigration
::newMigration()
210 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
211 $this->addTables( $actorQuery['tables'] );
212 $this->addJoinConds( $actorQuery['joins'] );
213 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
216 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
217 // Paranoia: avoid brute force searches (T19342)
218 // (shouldn't be able to get here without 'deletedhistory', but
219 // check it again just in case)
220 if ( !$user->isAllowed( 'deletedhistory' ) ) {
221 $bitmask = Revision
::DELETED_USER
;
222 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
223 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
228 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
232 if ( !is_null( $params['continue'] ) ) {
233 $cont = explode( '|', $params['continue'] );
234 $op = ( $dir == 'newer' ?
'>' : '<' );
235 if ( $mode == 'all' ||
$mode == 'revs' ) {
236 $this->dieContinueUsageIf( count( $cont ) != 4 );
238 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
239 $title = $db->addQuotes( $cont[1] );
240 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
241 $ar_id = (int)$cont[3];
242 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
243 $this->addWhere( "ar_namespace $op $ns OR " .
244 "(ar_namespace = $ns AND " .
245 "(ar_title $op $title OR " .
246 "(ar_title = $title AND " .
247 "(ar_timestamp $op $ts OR " .
248 "(ar_timestamp = $ts AND " .
249 "ar_id $op= $ar_id)))))" );
251 $this->dieContinueUsageIf( count( $cont ) != 2 );
252 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
253 $ar_id = (int)$cont[1];
254 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
255 $this->addWhere( "ar_timestamp $op $ts OR " .
256 "(ar_timestamp = $ts AND " .
257 "ar_id $op= $ar_id)" );
261 $this->addOption( 'LIMIT', $limit +
1 );
262 if ( $mode == 'all' ) {
263 if ( $params['unique'] ) {
264 // @todo Does this work on non-MySQL?
265 $this->addOption( 'GROUP BY', 'ar_title' );
267 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
268 $this->addOption( 'ORDER BY', [
270 'ar_timestamp' . $sort,
275 if ( $mode == 'revs' ) {
276 // Sort by ns and title in the same order as timestamp for efficiency
277 $this->addWhereRange( 'ar_namespace', $dir, null, null );
278 $this->addWhereRange( 'ar_title', $dir, null, null );
280 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
281 // Include in ORDER BY for uniqueness
282 $this->addWhereRange( 'ar_id', $dir, null, null );
284 $res = $this->select( __METHOD__
);
285 $pageMap = []; // Maps ns&title to (fake) pageid
288 foreach ( $res as $row ) {
289 if ( ++
$count > $limit ) {
291 if ( $mode == 'all' ||
$mode == 'revs' ) {
292 $this->setContinueEnumParameter( 'continue',
293 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
296 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
304 $rev['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
);
306 $rev['revid'] = (int)$row->ar_rev_id
;
308 if ( $fld_parentid && !is_null( $row->ar_parent_id
) ) {
309 $rev['parentid'] = (int)$row->ar_parent_id
;
311 if ( $fld_user ||
$fld_userid ) {
312 if ( $row->ar_deleted
& Revision
::DELETED_USER
) {
313 $rev['userhidden'] = true;
316 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_USER
, $user ) ) {
318 $rev['user'] = $row->ar_user_text
;
321 $rev['userid'] = (int)$row->ar_user
;
326 if ( $fld_comment ||
$fld_parsedcomment ) {
327 if ( $row->ar_deleted
& Revision
::DELETED_COMMENT
) {
328 $rev['commenthidden'] = true;
331 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
332 $comment = $commentStore->getComment( 'ar_comment', $row )->text
;
333 if ( $fld_comment ) {
334 $rev['comment'] = $comment;
336 if ( $fld_parsedcomment ) {
337 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
338 $rev['parsedcomment'] = Linker
::formatComment( $comment, $title );
344 $rev['minor'] = $row->ar_minor_edit
== 1;
347 $rev['len'] = $row->ar_len
;
350 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
351 $rev['sha1hidden'] = true;
354 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
355 if ( $row->ar_sha1
!= '' ) {
356 $rev['sha1'] = Wikimedia\base_convert
( $row->ar_sha1
, 36, 16, 40 );
362 if ( $fld_content ) {
363 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
364 $rev['texthidden'] = true;
367 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
368 ApiResult
::setContentValue( $rev, 'text', Revision
::getRevisionText( $row ) );
373 if ( $row->ts_tags
) {
374 $tags = explode( ',', $row->ts_tags
);
375 ApiResult
::setIndexedTagName( $tags, 'tag' );
376 $rev['tags'] = $tags;
382 if ( $anyHidden && ( $row->ar_deleted
& Revision
::DELETED_RESTRICTED
) ) {
383 $rev['suppressed'] = true;
386 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
387 $pageID = $newPageID++
;
388 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
389 $a['revisions'] = [ $rev ];
390 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
391 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
392 ApiQueryBase
::addTitleInfo( $a, $title );
394 $a['token'] = $token;
396 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
398 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
399 $fit = $result->addValue(
400 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
404 if ( $mode == 'all' ||
$mode == 'revs' ) {
405 $this->setContinueEnumParameter( 'continue',
406 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
409 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
414 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
417 public function isDeprecated() {
421 public function getAllowedParams() {
424 ApiBase
::PARAM_TYPE
=> 'timestamp',
425 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
428 ApiBase
::PARAM_TYPE
=> 'timestamp',
429 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
432 ApiBase
::PARAM_TYPE
=> [
436 ApiBase
::PARAM_DFLT
=> 'older',
437 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
438 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 3 ] ],
441 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
444 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
447 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
450 ApiBase
::PARAM_DFLT
=> false,
451 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
454 ApiBase
::PARAM_TYPE
=> 'namespace',
455 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
456 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
460 ApiBase
::PARAM_TYPE
=> 'user'
463 ApiBase
::PARAM_TYPE
=> 'user'
466 ApiBase
::PARAM_DFLT
=> 'user|comment',
467 ApiBase
::PARAM_TYPE
=> [
481 ApiBase
::PARAM_ISMULTI
=> true
484 ApiBase
::PARAM_DFLT
=> 10,
485 ApiBase
::PARAM_TYPE
=> 'limit',
486 ApiBase
::PARAM_MIN
=> 1,
487 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
488 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
491 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
496 protected function getExamplesMessages() {
498 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
499 'drprop=user|comment|content'
500 => 'apihelp-query+deletedrevs-example-mode1',
501 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
502 => 'apihelp-query+deletedrevs-example-mode2',
503 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
504 => 'apihelp-query+deletedrevs-example-mode3-main',
505 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
506 => 'apihelp-query+deletedrevs-example-mode3-talk',
510 public function getHelpUrls() {
511 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';