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->addTables( 'tag_summary' );
136 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
138 $this->addFields( 'ts_tags' );
141 if ( !is_null( $params['tag'] ) ) {
142 $this->addTables( 'change_tag' );
144 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
146 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
148 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
149 } catch ( NameTableAccessException
$exception ) {
151 $this->addWhere( '1=0' );
155 if ( $fld_content ) {
156 $this->addTables( 'text' );
158 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
160 $this->addFields( [ 'ar_text_id', 'old_text', 'old_flags' ] );
162 // This also means stricter restrictions
163 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
166 $userMax = $fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
;
167 $botMax = $fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
;
169 $limit = $params['limit'];
171 if ( $limit == 'max' ) {
172 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
173 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
176 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
179 // Undelete tokens are identical for all pages, so we cache one here
180 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
183 $dir = $params['dir'];
185 // We need a custom WHERE clause that matches all titles.
186 if ( $mode == 'revs' ) {
187 $lb = new LinkBatch( $titles );
188 $where = $lb->constructSet( 'ar', $db );
189 $this->addWhere( $where );
190 } elseif ( $mode == 'all' ) {
191 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
193 $from = $params['from'] === null
195 : $this->titlePartToKey( $params['from'], $params['namespace'] );
196 $to = $params['to'] === null
198 : $this->titlePartToKey( $params['to'], $params['namespace'] );
199 $this->addWhereRange( 'ar_title', $dir, $from, $to );
201 if ( isset( $params['prefix'] ) ) {
202 $this->addWhere( 'ar_title' . $db->buildLike(
203 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
204 $db->anyString() ) );
208 if ( !is_null( $params['user'] ) ) {
209 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
210 $actorQuery = ActorMigration
::newMigration()
211 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
212 $this->addTables( $actorQuery['tables'] );
213 $this->addJoinConds( $actorQuery['joins'] );
214 $this->addWhere( $actorQuery['conds'] );
215 } elseif ( !is_null( $params['excludeuser'] ) ) {
216 // Here there's no chance of using ar_usertext_timestamp.
217 $actorQuery = ActorMigration
::newMigration()
218 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
219 $this->addTables( $actorQuery['tables'] );
220 $this->addJoinConds( $actorQuery['joins'] );
221 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
224 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
225 // Paranoia: avoid brute force searches (T19342)
226 // (shouldn't be able to get here without 'deletedhistory', but
227 // check it again just in case)
228 if ( !$user->isAllowed( 'deletedhistory' ) ) {
229 $bitmask = Revision
::DELETED_USER
;
230 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
231 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
236 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
240 if ( !is_null( $params['continue'] ) ) {
241 $cont = explode( '|', $params['continue'] );
242 $op = ( $dir == 'newer' ?
'>' : '<' );
243 if ( $mode == 'all' ||
$mode == 'revs' ) {
244 $this->dieContinueUsageIf( count( $cont ) != 4 );
245 $ns = intval( $cont[0] );
246 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
247 $title = $db->addQuotes( $cont[1] );
248 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
249 $ar_id = (int)$cont[3];
250 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
251 $this->addWhere( "ar_namespace $op $ns OR " .
252 "(ar_namespace = $ns AND " .
253 "(ar_title $op $title OR " .
254 "(ar_title = $title AND " .
255 "(ar_timestamp $op $ts OR " .
256 "(ar_timestamp = $ts AND " .
257 "ar_id $op= $ar_id)))))" );
259 $this->dieContinueUsageIf( count( $cont ) != 2 );
260 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
261 $ar_id = (int)$cont[1];
262 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
263 $this->addWhere( "ar_timestamp $op $ts OR " .
264 "(ar_timestamp = $ts AND " .
265 "ar_id $op= $ar_id)" );
269 $this->addOption( 'LIMIT', $limit +
1 );
270 if ( $mode == 'all' ) {
271 if ( $params['unique'] ) {
272 // @todo Does this work on non-MySQL?
273 $this->addOption( 'GROUP BY', 'ar_title' );
275 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
276 $this->addOption( 'ORDER BY', [
278 'ar_timestamp' . $sort,
283 if ( $mode == 'revs' ) {
284 // Sort by ns and title in the same order as timestamp for efficiency
285 $this->addWhereRange( 'ar_namespace', $dir, null, null );
286 $this->addWhereRange( 'ar_title', $dir, null, null );
288 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
289 // Include in ORDER BY for uniqueness
290 $this->addWhereRange( 'ar_id', $dir, null, null );
292 $res = $this->select( __METHOD__
);
293 $pageMap = []; // Maps ns&title to (fake) pageid
296 foreach ( $res as $row ) {
297 if ( ++
$count > $limit ) {
299 if ( $mode == 'all' ||
$mode == 'revs' ) {
300 $this->setContinueEnumParameter( 'continue',
301 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
304 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
312 $rev['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
);
314 $rev['revid'] = intval( $row->ar_rev_id
);
316 if ( $fld_parentid && !is_null( $row->ar_parent_id
) ) {
317 $rev['parentid'] = intval( $row->ar_parent_id
);
319 if ( $fld_user ||
$fld_userid ) {
320 if ( $row->ar_deleted
& Revision
::DELETED_USER
) {
321 $rev['userhidden'] = true;
324 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_USER
, $user ) ) {
326 $rev['user'] = $row->ar_user_text
;
329 $rev['userid'] = (int)$row->ar_user
;
334 if ( $fld_comment ||
$fld_parsedcomment ) {
335 if ( $row->ar_deleted
& Revision
::DELETED_COMMENT
) {
336 $rev['commenthidden'] = true;
339 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
340 $comment = $commentStore->getComment( 'ar_comment', $row )->text
;
341 if ( $fld_comment ) {
342 $rev['comment'] = $comment;
344 if ( $fld_parsedcomment ) {
345 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
346 $rev['parsedcomment'] = Linker
::formatComment( $comment, $title );
352 $rev['minor'] = $row->ar_minor_edit
== 1;
355 $rev['len'] = $row->ar_len
;
358 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
359 $rev['sha1hidden'] = true;
362 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
363 if ( $row->ar_sha1
!= '' ) {
364 $rev['sha1'] = Wikimedia\base_convert
( $row->ar_sha1
, 36, 16, 40 );
370 if ( $fld_content ) {
371 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
372 $rev['texthidden'] = true;
375 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
376 ApiResult
::setContentValue( $rev, 'text', Revision
::getRevisionText( $row ) );
381 if ( $row->ts_tags
) {
382 $tags = explode( ',', $row->ts_tags
);
383 ApiResult
::setIndexedTagName( $tags, 'tag' );
384 $rev['tags'] = $tags;
390 if ( $anyHidden && ( $row->ar_deleted
& Revision
::DELETED_RESTRICTED
) ) {
391 $rev['suppressed'] = true;
394 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
395 $pageID = $newPageID++
;
396 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
397 $a['revisions'] = [ $rev ];
398 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
399 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
400 ApiQueryBase
::addTitleInfo( $a, $title );
402 $a['token'] = $token;
404 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
406 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
407 $fit = $result->addValue(
408 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
412 if ( $mode == 'all' ||
$mode == 'revs' ) {
413 $this->setContinueEnumParameter( 'continue',
414 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
417 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
422 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
425 public function isDeprecated() {
429 public function getAllowedParams() {
432 ApiBase
::PARAM_TYPE
=> 'timestamp',
433 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
436 ApiBase
::PARAM_TYPE
=> 'timestamp',
437 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
440 ApiBase
::PARAM_TYPE
=> [
444 ApiBase
::PARAM_DFLT
=> 'older',
445 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
446 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 3 ] ],
449 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
452 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
455 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
458 ApiBase
::PARAM_DFLT
=> false,
459 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
462 ApiBase
::PARAM_TYPE
=> 'namespace',
463 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
464 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
468 ApiBase
::PARAM_TYPE
=> 'user'
471 ApiBase
::PARAM_TYPE
=> 'user'
474 ApiBase
::PARAM_DFLT
=> 'user|comment',
475 ApiBase
::PARAM_TYPE
=> [
489 ApiBase
::PARAM_ISMULTI
=> true
492 ApiBase
::PARAM_DFLT
=> 10,
493 ApiBase
::PARAM_TYPE
=> 'limit',
494 ApiBase
::PARAM_MIN
=> 1,
495 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
496 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
499 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
504 protected function getExamplesMessages() {
506 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
507 'drprop=user|comment|content'
508 => 'apihelp-query+deletedrevs-example-mode1',
509 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
510 => 'apihelp-query+deletedrevs-example-mode2',
511 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
512 => 'apihelp-query+deletedrevs-example-mode3-main',
513 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
514 => 'apihelp-query+deletedrevs-example-mode3-talk',
518 public function getHelpUrls() {
519 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';