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