5 * Created on Jul 2, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * Query module to enumerate all deleted revisions.
32 class ApiQueryDeletedrevs
extends ApiQueryBase
{
34 public function __construct( ApiQuery
$query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'dr' );
38 public function execute() {
39 $user = $this->getUser();
40 // Before doing anything at all, let's check permissions
41 if ( !$user->isAllowed( 'deletedhistory' ) ) {
43 'You don\'t have permission to view deleted revision information',
49 $params = $this->extractRequestParams( false );
50 $prop = array_flip( $params['prop'] );
51 $fld_parentid = isset( $prop['parentid'] );
52 $fld_revid = isset( $prop['revid'] );
53 $fld_user = isset( $prop['user'] );
54 $fld_userid = isset( $prop['userid'] );
55 $fld_comment = isset( $prop['comment'] );
56 $fld_parsedcomment = isset( $prop['parsedcomment'] );
57 $fld_minor = isset( $prop['minor'] );
58 $fld_len = isset( $prop['len'] );
59 $fld_sha1 = isset( $prop['sha1'] );
60 $fld_content = isset( $prop['content'] );
61 $fld_token = isset( $prop['token'] );
62 $fld_tags = isset( $prop['tags'] );
64 if ( isset( $prop['token'] ) ) {
65 $p = $this->getModulePrefix();
67 "{$p}prop=token has been deprecated. Please use action=query&meta=tokens instead."
71 // If we're in JSON callback mode, no tokens can be obtained
72 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
76 // If user can't undelete, no tokens
77 if ( !$user->isAllowed( 'undelete' ) ) {
81 $result = $this->getResult();
82 $pageSet = $this->getPageSet();
83 $titles = $pageSet->getTitles();
85 // This module operates in three modes:
86 // 'revs': List deleted revs for certain titles (1)
87 // 'user': List deleted revs by a certain user (2)
88 // 'all': List all deleted revs in NS (3)
90 if ( count( $titles ) > 0 ) {
92 } elseif ( !is_null( $params['user'] ) ) {
96 if ( $mode == 'revs' ||
$mode == 'user' ) {
97 // Ignore namespace and unique due to inability to know whether they were purposely set
98 foreach ( array( 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ) as $p ) {
99 if ( !is_null( $params[$p] ) ) {
100 $this->dieUsage( "The '{$p}' parameter cannot be used in modes 1 or 2", 'badparams' );
104 foreach ( array( 'start', 'end' ) as $p ) {
105 if ( !is_null( $params[$p] ) ) {
106 $this->dieUsage( "The {$p} parameter cannot be used in mode 3", 'badparams' );
111 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
112 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
115 $this->addTables( 'archive' );
116 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_deleted', 'ar_id' ) );
118 $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
119 $this->addFieldsIf( 'ar_rev_id', $fld_revid );
120 $this->addFieldsIf( 'ar_user_text', $fld_user );
121 $this->addFieldsIf( 'ar_user', $fld_userid );
122 $this->addFieldsIf( 'ar_comment', $fld_comment ||
$fld_parsedcomment );
123 $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
124 $this->addFieldsIf( 'ar_len', $fld_len );
125 $this->addFieldsIf( 'ar_sha1', $fld_sha1 );
128 $this->addTables( 'tag_summary' );
130 array( 'tag_summary' => array( 'LEFT JOIN', array( 'ar_rev_id=ts_rev_id' ) ) )
132 $this->addFields( 'ts_tags' );
135 if ( !is_null( $params['tag'] ) ) {
136 $this->addTables( 'change_tag' );
138 array( 'change_tag' => array( 'INNER JOIN', array( 'ar_rev_id=ct_rev_id' ) ) )
140 $this->addWhereFld( 'ct_tag', $params['tag'] );
143 if ( $fld_content ) {
144 // Modern MediaWiki has the content for deleted revs in the 'text'
145 // table using fields old_text and old_flags. But revisions deleted
146 // pre-1.5 store the content in the 'archive' table directly using
147 // fields ar_text and ar_flags, and no corresponding 'text' row. So
148 // we have to LEFT JOIN and fetch all four fields, plus ar_text_id
149 // to be able to tell the difference.
150 $this->addTables( 'text' );
152 array( 'text' => array( 'LEFT JOIN', array( 'ar_text_id=old_id' ) ) )
154 $this->addFields( array( 'ar_text', 'ar_flags', 'ar_text_id', 'old_text', 'old_flags' ) );
156 // This also means stricter restrictions
157 if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) {
159 'You don\'t have permission to view deleted revision content',
165 $userMax = $fld_content ? ApiBase
::LIMIT_SML1
: ApiBase
::LIMIT_BIG1
;
166 $botMax = $fld_content ? ApiBase
::LIMIT_SML2
: ApiBase
::LIMIT_BIG2
;
168 $limit = $params['limit'];
170 if ( $limit == 'max' ) {
171 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
172 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
175 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
178 // Undelete tokens are identical for all pages, so we cache one here
179 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
182 $dir = $params['dir'];
184 // We need a custom WHERE clause that matches all titles.
185 if ( $mode == 'revs' ) {
186 $lb = new LinkBatch( $titles );
187 $where = $lb->constructSet( 'ar', $db );
188 $this->addWhere( $where );
189 } elseif ( $mode == 'all' ) {
190 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
192 $from = $params['from'] === null
194 : $this->titlePartToKey( $params['from'], $params['namespace'] );
195 $to = $params['to'] === null
197 : $this->titlePartToKey( $params['to'], $params['namespace'] );
198 $this->addWhereRange( 'ar_title', $dir, $from, $to );
200 if ( isset( $params['prefix'] ) ) {
201 $this->addWhere( 'ar_title' . $db->buildLike(
202 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
203 $db->anyString() ) );
207 if ( !is_null( $params['user'] ) ) {
208 $this->addWhereFld( 'ar_user_text', $params['user'] );
209 } elseif ( !is_null( $params['excludeuser'] ) ) {
210 $this->addWhere( 'ar_user_text != ' .
211 $db->addQuotes( $params['excludeuser'] ) );
214 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
215 // Paranoia: avoid brute force searches (bug 17342)
216 // (shouldn't be able to get here without 'deletedhistory', but
217 // check it again just in case)
218 if ( !$user->isAllowed( 'deletedhistory' ) ) {
219 $bitmask = Revision
::DELETED_USER
;
220 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
221 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
226 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
230 if ( !is_null( $params['continue'] ) ) {
231 $cont = explode( '|', $params['continue'] );
232 $op = ( $dir == 'newer' ?
'>' : '<' );
233 if ( $mode == 'all' ||
$mode == 'revs' ) {
234 $this->dieContinueUsageIf( count( $cont ) != 4 );
235 $ns = intval( $cont[0] );
236 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
237 $title = $db->addQuotes( $cont[1] );
238 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
239 $ar_id = (int)$cont[3];
240 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
241 $this->addWhere( "ar_namespace $op $ns OR " .
242 "(ar_namespace = $ns AND " .
243 "(ar_title $op $title OR " .
244 "(ar_title = $title AND " .
245 "(ar_timestamp $op $ts OR " .
246 "(ar_timestamp = $ts AND " .
247 "ar_id $op= $ar_id)))))" );
249 $this->dieContinueUsageIf( count( $cont ) != 2 );
250 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
251 $ar_id = (int)$cont[1];
252 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
253 $this->addWhere( "ar_timestamp $op $ts OR " .
254 "(ar_timestamp = $ts AND " .
255 "ar_id $op= $ar_id)" );
259 $this->addOption( 'LIMIT', $limit +
1 );
262 array( 'archive' => ( $mode == 'user' ?
'usertext_timestamp' : 'name_title_timestamp' ) )
264 if ( $mode == 'all' ) {
265 if ( $params['unique'] ) {
266 // @todo Does this work on non-MySQL?
267 $this->addOption( 'GROUP BY', 'ar_title' );
269 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
270 $this->addOption( 'ORDER BY', array(
272 'ar_timestamp' . $sort,
277 if ( $mode == 'revs' ) {
278 // Sort by ns and title in the same order as timestamp for efficiency
279 $this->addWhereRange( 'ar_namespace', $dir, null, null );
280 $this->addWhereRange( 'ar_title', $dir, null, null );
282 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
283 // Include in ORDER BY for uniqueness
284 $this->addWhereRange( 'ar_id', $dir, null, null );
286 $res = $this->select( __METHOD__
);
287 $pageMap = array(); // Maps ns&title to (fake) pageid
290 foreach ( $res as $row ) {
291 if ( ++
$count > $limit ) {
293 if ( $mode == 'all' ||
$mode == 'revs' ) {
294 $this->setContinueEnumParameter( 'continue',
295 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
298 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
306 $rev['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
);
308 $rev['revid'] = intval( $row->ar_rev_id
);
310 if ( $fld_parentid && !is_null( $row->ar_parent_id
) ) {
311 $rev['parentid'] = intval( $row->ar_parent_id
);
313 if ( $fld_user ||
$fld_userid ) {
314 if ( $row->ar_deleted
& Revision
::DELETED_USER
) {
315 $rev['userhidden'] = '';
318 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_USER
, $user ) ) {
320 $rev['user'] = $row->ar_user_text
;
323 $rev['userid'] = $row->ar_user
;
328 if ( $fld_comment ||
$fld_parsedcomment ) {
329 if ( $row->ar_deleted
& Revision
::DELETED_COMMENT
) {
330 $rev['commenthidden'] = '';
333 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
334 if ( $fld_comment ) {
335 $rev['comment'] = $row->ar_comment
;
337 if ( $fld_parsedcomment ) {
338 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
339 $rev['parsedcomment'] = Linker
::formatComment( $row->ar_comment
, $title );
344 if ( $fld_minor && $row->ar_minor_edit
== 1 ) {
348 $rev['len'] = $row->ar_len
;
351 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
352 $rev['sha1hidden'] = '';
355 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
356 if ( $row->ar_sha1
!= '' ) {
357 $rev['sha1'] = wfBaseConvert( $row->ar_sha1
, 36, 16, 40 );
363 if ( $fld_content ) {
364 if ( $row->ar_deleted
& Revision
::DELETED_TEXT
) {
365 $rev['texthidden'] = '';
368 if ( Revision
::userCanBitfield( $row->ar_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
369 if ( isset( $row->ar_text
) && !$row->ar_text_id
) {
370 // Pre-1.5 ar_text row (if condition from Revision::newFromArchiveRow)
371 ApiResult
::setContent( $rev, Revision
::getRevisionText( $row, 'ar_' ) );
373 ApiResult
::setContent( $rev, Revision
::getRevisionText( $row ) );
379 if ( $row->ts_tags
) {
380 $tags = explode( ',', $row->ts_tags
);
381 $this->getResult()->setIndexedTagName( $tags, 'tag' );
382 $rev['tags'] = $tags;
384 $rev['tags'] = array();
388 if ( $anyHidden && ( $row->ar_deleted
& Revision
::DELETED_RESTRICTED
) ) {
389 $rev['suppressed'] = '';
392 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
393 $pageID = $newPageID++
;
394 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
395 $a['revisions'] = array( $rev );
396 $result->setIndexedTagName( $a['revisions'], 'rev' );
397 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
398 ApiQueryBase
::addTitleInfo( $a, $title );
400 $a['token'] = $token;
402 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
404 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
405 $fit = $result->addValue(
406 array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
410 if ( $mode == 'all' ||
$mode == 'revs' ) {
411 $this->setContinueEnumParameter( 'continue',
412 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
415 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
420 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
423 public function getAllowedParams() {
426 ApiBase
::PARAM_TYPE
=> 'timestamp',
427 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'modes', 1, 2 ) ),
430 ApiBase
::PARAM_TYPE
=> 'timestamp',
431 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'modes', 1, 2 ) ),
434 ApiBase
::PARAM_TYPE
=> array(
438 ApiBase
::PARAM_DFLT
=> 'older',
439 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
440 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'modes', 1, 3 ) ),
443 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'modes', 3 ) ),
446 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'modes', 3 ) ),
449 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'modes', 3 ) ),
452 ApiBase
::PARAM_DFLT
=> false,
453 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'modes', 3 ) ),
455 'namespace' => array(
456 ApiBase
::PARAM_TYPE
=> 'namespace',
457 ApiBase
::PARAM_DFLT
=> NS_MAIN
,
458 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'modes', 3 ) ),
462 ApiBase
::PARAM_TYPE
=> 'user'
464 'excludeuser' => array(
465 ApiBase
::PARAM_TYPE
=> 'user'
468 ApiBase
::PARAM_DFLT
=> 'user|comment',
469 ApiBase
::PARAM_TYPE
=> array(
483 ApiBase
::PARAM_ISMULTI
=> true
486 ApiBase
::PARAM_DFLT
=> 10,
487 ApiBase
::PARAM_TYPE
=> 'limit',
488 ApiBase
::PARAM_MIN
=> 1,
489 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
490 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
493 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
498 public function getExamplesMessages() {
500 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
501 'drprop=user|comment|content'
502 => 'apihelp-query+deletedrevs-example-mode1',
503 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
504 => 'apihelp-query+deletedrevs-example-mode2',
505 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
506 => 'apihelp-query+deletedrevs-example-mode3-main',
507 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
508 => 'apihelp-query+deletedrevs-example-mode3-talk',
512 public function getHelpUrls() {
513 return 'https://www.mediawiki.org/wiki/API:Deletedrevs';