* (bug 28560) list=deletedrevs should die, if combination of param is invalid
[lhc/web/wiklou.git] / includes / api / ApiQueryDeletedrevs.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jul 2, 2007
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to enumerate all deleted revisions.
34 *
35 * @ingroup API
36 */
37 class ApiQueryDeletedrevs extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'dr' );
41 }
42
43 public function execute() {
44 global $wgUser;
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' );
48 }
49
50 $db = $this->getDB();
51 $params = $this->extractRequestParams( false );
52 $prop = array_flip( $params['prop'] );
53 $fld_parentid = isset( $prop['parentid'] );
54 $fld_revid = isset( $prop['revid'] );
55 $fld_user = isset( $prop['user'] );
56 $fld_userid = isset( $prop['userid'] );
57 $fld_comment = isset( $prop['comment'] );
58 $fld_parsedcomment = isset ( $prop['parsedcomment'] );
59 $fld_minor = isset( $prop['minor'] );
60 $fld_len = isset( $prop['len'] );
61 $fld_content = isset( $prop['content'] );
62 $fld_token = isset( $prop['token'] );
63
64 $result = $this->getResult();
65 $pageSet = $this->getPageSet();
66 $titles = $pageSet->getTitles();
67
68 // This module operates in three modes:
69 // 'revs': List deleted revs for certain titles (1)
70 // 'user': List deleted revs by a certain user (2)
71 // 'all': List all deleted revs in NS (3)
72 $mode = 'all';
73 if ( count( $titles ) > 0 ) {
74 $mode = 'revs';
75 } elseif ( !is_null( $params['user'] ) ) {
76 $mode = 'user';
77 }
78
79 if ( $mode == 'revs' || $mode == 'user' ) {
80 // Ignore namespace and unique due to inability to know whether they were purposely set
81 foreach( array( 'from', 'to', 'prefix', /*'namespace',*/ 'continue', /*'unique'*/ ) as $p ) {
82 if ( !is_null( $params[$p] ) ) {
83 $this->dieUsage( "The '{$p}' parameter cannot be used in modes 1 or 2", 'badparams');
84 }
85 }
86 } else {
87 foreach( array( 'start', 'end' ) as $p ) {
88 if ( !is_null( $params[$p] ) ) {
89 $this->dieUsage( "The {$p} parameter cannot be used in mode 3", 'badparams');
90 }
91 }
92 }
93
94 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
95 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
96 }
97
98 $this->addTables( 'archive' );
99 $this->addWhere( 'ar_deleted = 0' );
100 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) );
101
102 if ( $fld_parentid ) {
103 $this->addFields( 'ar_parent_id' );
104 }
105 if ( $fld_revid ) {
106 $this->addFields( 'ar_rev_id' );
107 }
108 if ( $fld_user ) {
109 $this->addFields( 'ar_user_text' );
110 }
111 if ( $fld_userid ) {
112 $this->addFields( 'ar_user' );
113 }
114 if ( $fld_comment || $fld_parsedcomment ) {
115 $this->addFields( 'ar_comment' );
116 }
117 if ( $fld_minor ) {
118 $this->addFields( 'ar_minor_edit' );
119 }
120 if ( $fld_len ) {
121 $this->addFields( 'ar_len' );
122 }
123 if ( $fld_content ) {
124 $this->addTables( 'text' );
125 $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
126 $this->addWhere( 'ar_text_id = old_id' );
127
128 // This also means stricter restrictions
129 if ( !$wgUser->isAllowed( 'undelete' ) ) {
130 $this->dieUsage( 'You don\'t have permission to view deleted revision content', 'permissiondenied' );
131 }
132 }
133 // Check limits
134 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
135 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
136
137 $limit = $params['limit'];
138
139 if ( $limit == 'max' ) {
140 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
141 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
142 }
143
144 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
145
146 if ( $fld_token ) {
147 // Undelete tokens are identical for all pages, so we cache one here
148 $token = $wgUser->editToken( '', $this->getMain()->getRequest() );
149 }
150
151 $dir = $params['dir'];
152
153 // We need a custom WHERE clause that matches all titles.
154 if ( $mode == 'revs' ) {
155 $lb = new LinkBatch( $titles );
156 $where = $lb->constructSet( 'ar', $db );
157 $this->addWhere( $where );
158 } elseif ( $mode == 'all' ) {
159 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
160
161 $from = is_null( $params['from'] ) ? null : $this->titleToKey( $params['from'] );
162 $to = is_null( $params['to'] ) ? null : $this->titleToKey( $params['to'] );
163 $this->addWhereRange( 'ar_title', $dir, $from, $to );
164
165 if ( isset( $params['prefix'] ) ) {
166 $this->addWhere( 'ar_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
167 }
168 }
169
170 if ( !is_null( $params['user'] ) ) {
171 $this->addWhereFld( 'ar_user_text', $params['user'] );
172 } elseif ( !is_null( $params['excludeuser'] ) ) {
173 $this->addWhere( 'ar_user_text != ' .
174 $this->getDB()->addQuotes( $params['excludeuser'] ) );
175 }
176
177 if ( !is_null( $params['continue'] ) && ( $mode == 'all' || $mode == 'revs' ) ) {
178 $cont = explode( '|', $params['continue'] );
179 if ( count( $cont ) != 3 ) {
180 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
181 }
182 $ns = intval( $cont[0] );
183 $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
184 $ts = $this->getDB()->strencode( $cont[2] );
185 $op = ( $dir == 'newer' ? '>' : '<' );
186 $this->addWhere( "ar_namespace $op $ns OR " .
187 "(ar_namespace = $ns AND " .
188 "(ar_title $op '$title' OR " .
189 "(ar_title = '$title' AND " .
190 "ar_timestamp $op= '$ts')))" );
191 }
192
193 $this->addOption( 'LIMIT', $limit + 1 );
194 $this->addOption( 'USE INDEX', array( 'archive' => ( $mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp' ) ) );
195 if ( $mode == 'all' ) {
196 if ( $params['unique'] ) {
197 $this->addOption( 'GROUP BY', 'ar_title' );
198 } else {
199 $this->addOption( 'ORDER BY', 'ar_title, ar_timestamp' );
200 }
201 } else {
202 if ( $mode == 'revs' ) {
203 // Sort by ns and title in the same order as timestamp for efficiency
204 $this->addWhereRange( 'ar_namespace', $dir, null, null );
205 $this->addWhereRange( 'ar_title', $dir, null, null );
206 }
207 $this->addWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
208 }
209 $res = $this->select( __METHOD__ );
210 $pageMap = array(); // Maps ns&title to (fake) pageid
211 $count = 0;
212 $newPageID = 0;
213 foreach ( $res as $row ) {
214 if ( ++$count > $limit ) {
215 // We've had enough
216 if ( $mode == 'all' || $mode == 'revs' ) {
217 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
218 $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
219 } else {
220 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
221 }
222 break;
223 }
224
225 $rev = array();
226 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
227 if ( $fld_revid ) {
228 $rev['revid'] = intval( $row->ar_rev_id );
229 }
230 if ( $fld_parentid ) {
231 $rev['parentid'] = intval( $row->ar_parent_id );
232 }
233 if ( $fld_user ) {
234 $rev['user'] = $row->ar_user_text;
235 }
236 if ( $fld_userid ) {
237 $rev['userid'] = $row->ar_user;
238 }
239 if ( $fld_comment ) {
240 $rev['comment'] = $row->ar_comment;
241 }
242
243 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
244
245 if ( $fld_parsedcomment ) {
246 $rev['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->ar_comment, $title );
247 }
248 if ( $fld_minor && $row->ar_minor_edit == 1 ) {
249 $rev['minor'] = '';
250 }
251 if ( $fld_len ) {
252 $rev['len'] = $row->ar_len;
253 }
254 if ( $fld_content ) {
255 ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
256 }
257
258 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
259 $pageID = $newPageID++;
260 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
261 $a['revisions'] = array( $rev );
262 $result->setIndexedTagName( $a['revisions'], 'rev' );
263 ApiQueryBase::addTitleInfo( $a, $title );
264 if ( $fld_token ) {
265 $a['token'] = $token;
266 }
267 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
268 } else {
269 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
270 $fit = $result->addValue(
271 array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
272 null, $rev );
273 }
274 if ( !$fit ) {
275 if ( $mode == 'all' || $mode == 'revs' ) {
276 $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
277 $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
278 } else {
279 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
280 }
281 break;
282 }
283 }
284 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
285 }
286
287 public function getAllowedParams() {
288 return array(
289 'start' => array(
290 ApiBase::PARAM_TYPE => 'timestamp'
291 ),
292 'end' => array(
293 ApiBase::PARAM_TYPE => 'timestamp',
294 ),
295 'dir' => array(
296 ApiBase::PARAM_TYPE => array(
297 'newer',
298 'older'
299 ),
300 ApiBase::PARAM_DFLT => 'older'
301 ),
302 'from' => null,
303 'to' => null,
304 'prefix' => null,
305 'continue' => null,
306 'unique' => false,
307 'user' => array(
308 ApiBase::PARAM_TYPE => 'user'
309 ),
310 'excludeuser' => array(
311 ApiBase::PARAM_TYPE => 'user'
312 ),
313 'namespace' => array(
314 ApiBase::PARAM_TYPE => 'namespace',
315 ApiBase::PARAM_DFLT => 0,
316 ),
317 'limit' => array(
318 ApiBase::PARAM_DFLT => 10,
319 ApiBase::PARAM_TYPE => 'limit',
320 ApiBase::PARAM_MIN => 1,
321 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
322 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
323 ),
324 'prop' => array(
325 ApiBase::PARAM_DFLT => 'user|comment',
326 ApiBase::PARAM_TYPE => array(
327 'revid',
328 'parentid',
329 'user',
330 'userid',
331 'comment',
332 'parsedcomment',
333 'minor',
334 'len',
335 'content',
336 'token'
337 ),
338 ApiBase::PARAM_ISMULTI => true
339 ),
340 );
341 }
342
343 public function getParamDescription() {
344 return array(
345 'start' => 'The timestamp to start enumerating from (1,2)',
346 'end' => 'The timestamp to stop enumerating at (1,2)',
347 'dir' => $this->getDirectionDescription( $this->getModulePrefix(), ' (1, 3)' ),
348 'from' => 'Start listing at this title (3)',
349 'to' => 'Stop listing at this title (3)',
350 'prefix' => 'Search for all page titles that begin with this value (3)',
351 'limit' => 'The maximum amount of revisions to list',
352 'prop' => array(
353 'Which properties to get',
354 ' revid - Adds the revision ID of the deleted revision',
355 ' parentid - Adds the revision ID of the previous revision to the page',
356 ' user - Adds the user who made the revision',
357 ' userid - Adds the user ID whom made the revision',
358 ' comment - Adds the comment of the revision',
359 ' parsedcomment - Adds the parsed comment of the revision',
360 ' minor - Tags if the revision is minor',
361 ' len - Adds the length of the revision',
362 ' content - Adds the content of the revision',
363 ' token - Gives the edit token',
364 ),
365 'namespace' => 'Only list pages in this namespace (3)',
366 'user' => 'Only list revisions by this user',
367 'excludeuser' => 'Don\'t list revisions by this user',
368 'continue' => 'When more results are available, use this to continue (3)',
369 'unique' => 'List only one revision for each page (3)',
370 );
371 }
372
373 public function getDescription() {
374 $p = $this->getModulePrefix();
375 return array(
376 'List deleted revisions.',
377 'This module operates in three modes:',
378 ' 1) List deleted revisions for the given title(s), sorted by timestamp',
379 ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
380 " 3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, {$p}user not set)",
381 'Certain parameters only apply to some modes and are ignored in others.',
382 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3',
383 );
384 }
385
386 public function getPossibleErrors() {
387 return array_merge( parent::getPossibleErrors(), array(
388 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision information' ),
389 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
390 array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision content' ),
391 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
392 array( 'code' => 'badparams', 'info' => "The 'from' parameter cannot be used in modes 1 or 2" ),
393 array( 'code' => 'badparams', 'info' => "The 'to' parameter cannot be used in modes 1 or 2" ),
394 array( 'code' => 'badparams', 'info' => "The 'prefix' parameter cannot be used in modes 1 or 2" ),
395 array( 'code' => 'badparams', 'info' => "The 'continue' parameter cannot be used in modes 1 or 2" ),
396 array( 'code' => 'badparams', 'info' => "The 'start' parameter cannot be used in mode 3" ),
397 array( 'code' => 'badparams', 'info' => "The 'end' parameter cannot be used in mode 3" ),
398 ) );
399 }
400
401 protected function getExamples() {
402 return array(
403 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
404 ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
405 'List the last 50 deleted contributions by Bob (mode 2):',
406 ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
407 'List the first 50 deleted revisions in the main namespace (mode 3):',
408 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
409 'List the first 50 deleted pages in the Talk namespace (mode 3):',
410 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique=',
411 );
412 }
413
414 public function getVersion() {
415 return __CLASS__ . ': $Id$';
416 }
417 }