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