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