6c32a206d75ee38c5aa38772148ca3fd9febb140
4 * Created on Jul 2, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
32 * Query module to enumerate all available pages.
36 class ApiQueryDeletedrevs
extends ApiQueryBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
:: __construct( $query, $moduleName, 'dr' );
42 public function execute() {
45 // Before doing anything at all, let's check permissions
46 if ( !$wgUser->isAllowed( 'deletedhistory' ) )
47 $this->dieUsage( 'You don\'t have permission to view deleted revision information', 'permissiondenied' );
50 $params = $this->extractRequestParams( false );
51 $prop = array_flip( $params['prop'] );
52 $fld_revid = isset( $prop['revid'] );
53 $fld_user = isset( $prop['user'] );
54 $fld_comment = isset( $prop['comment'] );
55 $fld_minor = isset( $prop['minor'] );
56 $fld_len = isset( $prop['len'] );
57 $fld_content = isset( $prop['content'] );
58 $fld_token = isset( $prop['token'] );
60 $result = $this->getResult();
61 $pageSet = $this->getPageSet();
62 $titles = $pageSet->getTitles();
65 // This module operates in three modes:
66 // 'revs': List deleted revs for certain titles
67 // 'user': List deleted revs by a certain user
68 // 'all': List all deleted revs
70 if ( count( $titles ) > 0 )
72 else if ( !is_null( $params['user'] ) )
75 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) )
76 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
78 $this->addTables( 'archive' );
79 $this->addWhere( 'ar_deleted = 0' );
80 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) );
82 $this->addFields( 'ar_rev_id' );
84 $this->addFields( 'ar_user_text' );
86 $this->addFields( 'ar_comment' );
88 $this->addFields( 'ar_minor_edit' );
90 $this->addFields( 'ar_len' );
92 $this->addTables( 'text' );
93 $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
94 $this->addWhere( 'ar_text_id = old_id' );
96 // This also means stricter restrictions
97 if ( !$wgUser->isAllowed( 'undelete' ) )
98 $this->dieUsage( 'You don\'t have permission to view deleted revision content', 'permissiondenied' );
101 $userMax = $fld_content ? ApiBase
:: LIMIT_SML1
: ApiBase
:: LIMIT_BIG1
;
102 $botMax = $fld_content ? ApiBase
:: LIMIT_SML2
: ApiBase
:: LIMIT_BIG2
;
104 $limit = $params['limit'];
106 if ( $limit == 'max' ) {
107 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
108 $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
111 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
114 // Undelete tokens are identical for all pages, so we cache one here
115 $token = $wgUser->editToken();
117 // We need a custom WHERE clause that matches all titles.
118 if ( $mode == 'revs' ) {
119 $lb = new LinkBatch( $titles );
120 $where = $lb->constructSet( 'ar', $db );
121 $this->addWhere( $where );
122 } elseif ( $mode == 'all' ) {
123 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
124 if ( !is_null( $params['from'] ) )
126 $from = $this->getDB()->strencode( $this->titleToKey( $params['from'] ) );
127 $this->addWhere( "ar_title >= '$from'" );
131 if ( !is_null( $params['user'] ) ) {
132 $this->addWhereFld( 'ar_user_text', $params['user'] );
133 } elseif ( !is_null( $params['excludeuser'] ) ) {
134 $this->addWhere( 'ar_user_text != ' .
135 $this->getDB()->addQuotes( $params['excludeuser'] ) );
138 if ( !is_null( $params['continue'] ) && ( $mode == 'all' ||
$mode == 'revs' ) )
140 $cont = explode( '|', $params['continue'] );
141 if ( count( $cont ) != 3 )
142 $this->dieUsage( "Invalid continue param. You should pass the original value returned by the previous query", "badcontinue" );
143 $ns = intval( $cont[0] );
144 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
145 $ts = $this->getDB()->strencode( $cont[2] );
146 $op = ( $params['dir'] == 'newer' ?
'>' : '<' );
147 $this->addWhere( "ar_namespace $op $ns OR " .
148 "(ar_namespace = $ns AND " .
149 "(ar_title $op '$title' OR " .
150 "(ar_title = '$title' AND " .
151 "ar_timestamp $op= '$ts')))" );
154 $this->addOption( 'LIMIT', $limit +
1 );
155 $this->addOption( 'USE INDEX', array( 'archive' => ( $mode == 'user' ?
'usertext_timestamp' : 'name_title_timestamp' ) ) );
156 if ( $mode == 'all' ) {
157 if ( $params['unique'] )
159 $this->addOption( 'GROUP BY', 'ar_title' );
160 $this->addOption( 'ORDER BY', 'ar_title' );
162 $this->addOption( 'ORDER BY', 'ar_title, ar_timestamp' );
164 if ( $mode == 'revs' )
166 // Sort by ns and title in the same order as timestamp for efficiency
167 $this->addWhereRange( 'ar_namespace', $params['dir'], null, null );
168 $this->addWhereRange( 'ar_title', $params['dir'], null, null );
170 $this->addWhereRange( 'ar_timestamp', $params['dir'], $params['start'], $params['end'] );
172 $res = $this->select( __METHOD__
);
173 $pageMap = array(); // Maps ns&title to (fake) pageid
176 while ( $row = $db->fetchObject( $res ) )
178 if ( ++
$count > $limit ) {
180 if ( $mode == 'all' ||
$mode == 'revs' )
181 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace
) . '|' .
182 $this->keyToTitle( $row->ar_title
) . '|' . $row->ar_timestamp
);
184 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
) );
189 $rev['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
);
191 $rev['revid'] = intval( $row->ar_rev_id
);
193 $rev['user'] = $row->ar_user_text
;
195 $rev['comment'] = $row->ar_comment
;
196 if ( $fld_minor && $row->ar_minor_edit
== 1 )
199 $rev['len'] = $row->ar_len
;
201 ApiResult
::setContent( $rev, Revision
::getRevisionText( $row ) );
203 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
204 $pageID = $newPageID++
;
205 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
206 $t = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
207 $a['revisions'] = array( $rev );
208 $result->setIndexedTagName( $a['revisions'], 'rev' );
209 ApiQueryBase
::addTitleInfo( $a, $t );
211 $a['token'] = $token;
212 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
214 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
215 $fit = $result->addValue(
216 array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
220 if ( $mode == 'all' ||
$mode == 'revs' )
221 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace
) . '|' .
222 $this->keyToTitle( $row->ar_title
) . '|' . $row->ar_timestamp
);
224 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->ar_timestamp
) );
228 $db->freeResult( $res );
229 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
232 public function getAllowedParams() {
235 ApiBase
:: PARAM_TYPE
=> 'timestamp'
238 ApiBase
:: PARAM_TYPE
=> 'timestamp',
241 ApiBase
:: PARAM_TYPE
=> array(
245 ApiBase
:: PARAM_DFLT
=> 'older'
251 ApiBase
:: PARAM_TYPE
=> 'user'
253 'excludeuser' => array(
254 ApiBase
:: PARAM_TYPE
=> 'user'
256 'namespace' => array(
257 ApiBase
:: PARAM_TYPE
=> 'namespace',
258 ApiBase
:: PARAM_DFLT
=> 0,
261 ApiBase
:: PARAM_DFLT
=> 10,
262 ApiBase
:: PARAM_TYPE
=> 'limit',
263 ApiBase
:: PARAM_MIN
=> 1,
264 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
265 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
268 ApiBase
:: PARAM_DFLT
=> 'user|comment',
269 ApiBase
:: PARAM_TYPE
=> array(
278 ApiBase
:: PARAM_ISMULTI
=> true
283 public function getParamDescription() {
285 'start' => 'The timestamp to start enumerating from. (1,2)',
286 'end' => 'The timestamp to stop enumerating at. (1,2)',
287 'dir' => 'The direction in which to enumerate. (1,2)',
288 'limit' => 'The maximum amount of revisions to list',
289 'prop' => 'Which properties to get',
290 'namespace' => 'Only list pages in this namespace (3)',
291 'user' => 'Only list revisions by this user',
292 'excludeuser' => 'Don\'t list revisions by this user',
293 'from' => 'Start listing at this title (3)',
294 'continue' => 'When more results are available, use this to continue (3)',
295 'unique' => 'List only one revision for each page (3)',
299 public function getDescription() {
300 return array( 'List deleted revisions.',
301 'This module operates in three modes:',
302 '1) List deleted revisions for the given title(s), sorted by timestamp',
303 '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
304 '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
305 'Certain parameters only apply to some modes and are ignored in others.',
306 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
310 protected function getExamples() {
312 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
313 ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
314 'List the last 50 deleted contributions by Bob (mode 2):',
315 ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
316 'List the first 50 deleted revisions in the main namespace (mode 3):',
317 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
318 'List the first 50 deleted pages in the Talk namespace (mode 3):',
319 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique',
323 public function getVersion() {
324 return __CLASS__
. ': $Id$';