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 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
108 $arQuery = $revisionStore->getArchiveQueryInfo();
109 $this->addTables( $arQuery['tables'] );
110 $this->addFields( $arQuery['fields'] );
111 $this->addJoinConds( $arQuery['joins'] );
112 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
115 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
118 if ( !is_null( $params['tag'] ) ) {
119 $this->addTables( 'change_tag' );
121 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
123 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
125 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
126 } catch ( NameTableAccessException
$exception ) {
128 $this->addWhere( '1=0' );
132 // This means stricter restrictions
133 if ( $fld_content ) {
134 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
137 $userMax = $fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
;
138 $botMax = $fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
;
140 $limit = $params['limit'];
142 if ( $limit == 'max' ) {
143 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
144 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
147 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
150 // Undelete tokens are identical for all pages, so we cache one here
151 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
154 $dir = $params['dir'];
156 // We need a custom WHERE clause that matches all titles.
157 if ( $mode == 'revs' ) {
158 $lb = new LinkBatch( $titles );
159 $where = $lb->constructSet( 'ar', $db );
160 $this->addWhere( $where );
161 } elseif ( $mode == 'all' ) {
162 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
164 $from = $params['from'] === null
166 : $this->titlePartToKey( $params['from'], $params['namespace'] );
167 $to = $params['to'] === null
169 : $this->titlePartToKey( $params['to'], $params['namespace'] );
170 $this->addWhereRange( 'ar_title', $dir, $from, $to );
172 if ( isset( $params['prefix'] ) ) {
173 $this->addWhere( 'ar_title' . $db->buildLike(
174 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
175 $db->anyString() ) );
179 if ( !is_null( $params['user'] ) ) {
180 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
181 $actorQuery = ActorMigration
::newMigration()
182 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
183 $this->addTables( $actorQuery['tables'] );
184 $this->addJoinConds( $actorQuery['joins'] );
185 $this->addWhere( $actorQuery['conds'] );
186 } elseif ( !is_null( $params['excludeuser'] ) ) {
187 // Here there's no chance of using ar_usertext_timestamp.
188 $actorQuery = ActorMigration
::newMigration()
189 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
190 $this->addTables( $actorQuery['tables'] );
191 $this->addJoinConds( $actorQuery['joins'] );
192 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
195 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
196 // Paranoia: avoid brute force searches (T19342)
197 // (shouldn't be able to get here without 'deletedhistory', but
198 // check it again just in case)
199 if ( !$user->isAllowed( 'deletedhistory' ) ) {
200 $bitmask = Revision
::DELETED_USER
;
201 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
202 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
207 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
211 if ( !is_null( $params['continue'] ) ) {
212 $cont = explode( '|', $params['continue'] );
213 $op = ( $dir == 'newer' ?
'>' : '<' );
214 if ( $mode == 'all' ||
$mode == 'revs' ) {
215 $this->dieContinueUsageIf( count( $cont ) != 4 );
217 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
218 $title = $db->addQuotes( $cont[1] );
219 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
220 $ar_id = (int)$cont[3];
221 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
222 $this->addWhere( "ar_namespace $op $ns OR " .
223 "(ar_namespace = $ns AND " .
224 "(ar_title $op $title OR " .
225 "(ar_title = $title AND " .
226 "(ar_timestamp $op $ts OR " .
227 "(ar_timestamp = $ts AND " .
228 "ar_id $op= $ar_id)))))" );
230 $this->dieContinueUsageIf( count( $cont ) != 2 );
231 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
232 $ar_id = (int)$cont[1];
233 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
234 $this->addWhere( "ar_timestamp $op $ts OR " .
235 "(ar_timestamp = $ts AND " .
236 "ar_id $op= $ar_id)" );
240 $this->addOption( 'LIMIT', $limit +
1 );
241 if ( $mode == 'all' ) {
242 if ( $params['unique'] ) {
243 // @todo Does this work on non-MySQL?
244 $this->addOption( 'GROUP BY', 'ar_title' );
246 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
247 $this->addOption( 'ORDER BY', [
249 'ar_timestamp' . $sort,
254 if ( $mode == 'revs' ) {
255 // Sort by ns and title in the same order as timestamp for efficiency
256 $this->addWhereRange( 'ar_namespace', $dir, null, null );
257 $this->addWhereRange( 'ar_title', $dir, null, null );
259 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
260 // Include in ORDER BY for uniqueness
261 $this->addWhereRange( 'ar_id', $dir, null, null );
263 $res = $this->select( __METHOD__
);
264 $pageMap = []; // Maps ns&title to (fake) pageid
267 foreach ( $res as $row ) {
268 if ( ++
$count > $limit ) {
270 if ( $mode == 'all' ||
$mode == 'revs' ) {
271 $this->setContinueEnumParameter( 'continue',
272 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
275 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
283 $rev['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
);
285 $rev['revid'] = (int)$row->ar_rev_id
;
287 if ( $fld_parentid && !is_null( $row->ar_parent_id
) ) {
288 $rev['parentid'] = (int)$row->ar_parent_id
;
290 if ( $fld_user ||
$fld_userid ) {
291 if ( $row->ar_deleted
& Revision
::DELETED_USER
) {
292 $rev['userhidden'] = true;
295 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_USER
, $user ) ) {
297 $rev['user'] = $row->ar_user_text
;
300 $rev['userid'] = (int)$row->ar_user
;
305 if ( $fld_comment ||
$fld_parsedcomment ) {
306 if ( $row->ar_deleted
& Revision
::DELETED_COMMENT
) {
307 $rev['commenthidden'] = true;
310 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
311 $comment = $commentStore->getComment( 'ar_comment', $row )->text
;
312 if ( $fld_comment ) {
313 $rev['comment'] = $comment;
315 if ( $fld_parsedcomment ) {
316 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
317 $rev['parsedcomment'] = Linker
::formatComment( $comment, $title );
323 $rev['minor'] = $row->ar_minor_edit
== 1;
326 $rev['len'] = $row->ar_len
;
329 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
330 $rev['sha1hidden'] = true;
333 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
334 if ( $row->ar_sha1
!= '' ) {
335 $rev['sha1'] = Wikimedia\base_convert
( $row->ar_sha1
, 36, 16, 40 );
341 if ( $fld_content ) {
342 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
343 $rev['texthidden'] = true;
346 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
347 ApiResult
::setContentValue( $rev, 'text', Revision
::getRevisionText( $row, 'ar_' ) );
352 if ( $row->ts_tags
) {
353 $tags = explode( ',', $row->ts_tags
);
354 ApiResult
::setIndexedTagName( $tags, 'tag' );
355 $rev['tags'] = $tags;
361 if ( $anyHidden && ( $row->ar_deleted
& Revision
::DELETED_RESTRICTED
) ) {
362 $rev['suppressed'] = true;
365 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
366 $pageID = $newPageID++
;
367 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
368 $a['revisions'] = [ $rev ];
369 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
370 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
371 ApiQueryBase
::addTitleInfo( $a, $title );
373 $a['token'] = $token;
375 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
377 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
378 $fit = $result->addValue(
379 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
383 if ( $mode == 'all' ||
$mode == 'revs' ) {
384 $this->setContinueEnumParameter( 'continue',
385 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
388 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
393 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
396 public function isDeprecated() {
400 public function getAllowedParams() {
403 ApiBase
::PARAM_TYPE
=> 'timestamp',
404 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
407 ApiBase
::PARAM_TYPE
=> 'timestamp',
408 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 2 ] ],
411 ApiBase
::PARAM_TYPE
=> [
415 ApiBase
::PARAM_DFLT
=> 'older',
416 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
417 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 1, 3 ] ],
420 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
423 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
426 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
429 ApiBase
::PARAM_DFLT
=> false,
430 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
433 ApiBase
::PARAM_TYPE
=> 'namespace',
434 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
435 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'modes', 3 ] ],
439 ApiBase
::PARAM_TYPE
=> 'user'
442 ApiBase
::PARAM_TYPE
=> 'user'
445 ApiBase
::PARAM_DFLT
=> 'user|comment',
446 ApiBase
::PARAM_TYPE
=> [
460 ApiBase
::PARAM_ISMULTI
=> true
463 ApiBase
::PARAM_DFLT
=> 10,
464 ApiBase
::PARAM_TYPE
=> 'limit',
465 ApiBase
::PARAM_MIN
=> 1,
466 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
467 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
470 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
475 protected function getExamplesMessages() {
477 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
478 'drprop=user|comment|content'
479 => 'apihelp-query+deletedrevs-example-mode1',
480 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
481 => 'apihelp-query+deletedrevs-example-mode2',
482 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
483 => 'apihelp-query+deletedrevs-example-mode3-main',
484 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
485 => 'apihelp-query+deletedrevs-example-mode3-talk',
489 public function getHelpUrls() {
490 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';