Merge "ResourceLoader::makeLoaderImplementScript: Bind args as '$' and 'jQuery'"
[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(
43 'You don\'t have permission to view deleted revision information',
44 'permissiondenied'
45 );
46 }
47
48 $db = $this->getDB();
49 $params = $this->extractRequestParams( false );
50 $prop = array_flip( $params['prop'] );
51 $fld_parentid = isset( $prop['parentid'] );
52 $fld_revid = isset( $prop['revid'] );
53 $fld_user = isset( $prop['user'] );
54 $fld_userid = isset( $prop['userid'] );
55 $fld_comment = isset( $prop['comment'] );
56 $fld_parsedcomment = isset( $prop['parsedcomment'] );
57 $fld_minor = isset( $prop['minor'] );
58 $fld_len = isset( $prop['len'] );
59 $fld_sha1 = isset( $prop['sha1'] );
60 $fld_content = isset( $prop['content'] );
61 $fld_token = isset( $prop['token'] );
62 $fld_tags = isset( $prop['tags'] );
63
64 // If we're in JSON callback mode, no tokens can be obtained
65 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
66 $fld_token = false;
67 }
68
69 // If user can't undelete, no tokens
70 if ( !$user->isAllowed( 'undelete' ) ) {
71 $fld_token = false;
72 }
73
74 $result = $this->getResult();
75 $pageSet = $this->getPageSet();
76 $titles = $pageSet->getTitles();
77
78 // This module operates in three modes:
79 // 'revs': List deleted revs for certain titles (1)
80 // 'user': List deleted revs by a certain user (2)
81 // 'all': List all deleted revs in NS (3)
82 $mode = 'all';
83 if ( count( $titles ) > 0 ) {
84 $mode = 'revs';
85 } elseif ( !is_null( $params['user'] ) ) {
86 $mode = 'user';
87 }
88
89 if ( $mode == 'revs' || $mode == 'user' ) {
90 // Ignore namespace and unique due to inability to know whether they were purposely set
91 foreach ( array( 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ) as $p ) {
92 if ( !is_null( $params[$p] ) ) {
93 $this->dieUsage( "The '{$p}' parameter cannot be used in modes 1 or 2", 'badparams' );
94 }
95 }
96 } else {
97 foreach ( array( 'start', 'end' ) as $p ) {
98 if ( !is_null( $params[$p] ) ) {
99 $this->dieUsage( "The {$p} parameter cannot be used in mode 3", 'badparams' );
100 }
101 }
102 }
103
104 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
105 $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
106 }
107
108 $this->addTables( 'archive' );
109 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_deleted', 'ar_id' ) );
110
111 $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
112 $this->addFieldsIf( 'ar_rev_id', $fld_revid );
113 $this->addFieldsIf( 'ar_user_text', $fld_user );
114 $this->addFieldsIf( 'ar_user', $fld_userid );
115 $this->addFieldsIf( 'ar_comment', $fld_comment || $fld_parsedcomment );
116 $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
117 $this->addFieldsIf( 'ar_len', $fld_len );
118 $this->addFieldsIf( 'ar_sha1', $fld_sha1 );
119
120 if ( $fld_tags ) {
121 $this->addTables( 'tag_summary' );
122 $this->addJoinConds(
123 array( 'tag_summary' => array( 'LEFT JOIN', array( 'ar_rev_id=ts_rev_id' ) ) )
124 );
125 $this->addFields( 'ts_tags' );
126 }
127
128 if ( !is_null( $params['tag'] ) ) {
129 $this->addTables( 'change_tag' );
130 $this->addJoinConds(
131 array( 'change_tag' => array( 'INNER JOIN', array( 'ar_rev_id=ct_rev_id' ) ) )
132 );
133 $this->addWhereFld( 'ct_tag', $params['tag'] );
134 }
135
136 if ( $fld_content ) {
137 $this->addTables( 'text' );
138 $this->addJoinConds(
139 array( 'text' => array( 'INNER JOIN', array( 'ar_text_id=old_id' ) ) )
140 );
141 $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
142
143 // This also means stricter restrictions
144 if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) {
145 $this->dieUsage(
146 'You don\'t have permission to view deleted revision content',
147 'permissiondenied'
148 );
149 }
150 }
151 // Check limits
152 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
153 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
154
155 $limit = $params['limit'];
156
157 if ( $limit == 'max' ) {
158 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
159 $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
160 }
161
162 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
163
164 if ( $fld_token ) {
165 // Undelete tokens are identical for all pages, so we cache one here
166 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
167 }
168
169 $dir = $params['dir'];
170
171 // We need a custom WHERE clause that matches all titles.
172 if ( $mode == 'revs' ) {
173 $lb = new LinkBatch( $titles );
174 $where = $lb->constructSet( 'ar', $db );
175 $this->addWhere( $where );
176 } elseif ( $mode == 'all' ) {
177 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
178
179 $from = $params['from'] === null
180 ? null
181 : $this->titlePartToKey( $params['from'], $params['namespace'] );
182 $to = $params['to'] === null
183 ? null
184 : $this->titlePartToKey( $params['to'], $params['namespace'] );
185 $this->addWhereRange( 'ar_title', $dir, $from, $to );
186
187 if ( isset( $params['prefix'] ) ) {
188 $this->addWhere( 'ar_title' . $db->buildLike(
189 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
190 $db->anyString() ) );
191 }
192 }
193
194 if ( !is_null( $params['user'] ) ) {
195 $this->addWhereFld( 'ar_user_text', $params['user'] );
196 } elseif ( !is_null( $params['excludeuser'] ) ) {
197 $this->addWhere( 'ar_user_text != ' .
198 $db->addQuotes( $params['excludeuser'] ) );
199 }
200
201 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
202 // Paranoia: avoid brute force searches (bug 17342)
203 // (shouldn't be able to get here without 'deletedhistory', but
204 // check it again just in case)
205 if ( !$user->isAllowed( 'deletedhistory' ) ) {
206 $bitmask = Revision::DELETED_USER;
207 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
208 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
209 } else {
210 $bitmask = 0;
211 }
212 if ( $bitmask ) {
213 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
214 }
215 }
216
217 if ( !is_null( $params['continue'] ) ) {
218 $cont = explode( '|', $params['continue'] );
219 $op = ( $dir == 'newer' ? '>' : '<' );
220 if ( $mode == 'all' || $mode == 'revs' ) {
221 $this->dieContinueUsageIf( count( $cont ) != 4 );
222 $ns = intval( $cont[0] );
223 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
224 $title = $db->addQuotes( $cont[1] );
225 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
226 $ar_id = (int)$cont[3];
227 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
228 $this->addWhere( "ar_namespace $op $ns OR " .
229 "(ar_namespace = $ns AND " .
230 "(ar_title $op $title OR " .
231 "(ar_title = $title AND " .
232 "(ar_timestamp $op $ts OR " .
233 "(ar_timestamp = $ts AND " .
234 "ar_id $op= $ar_id)))))" );
235 } else {
236 $this->dieContinueUsageIf( count( $cont ) != 2 );
237 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
238 $ar_id = (int)$cont[1];
239 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
240 $this->addWhere( "ar_timestamp $op $ts OR " .
241 "(ar_timestamp = $ts AND " .
242 "ar_id $op= $ar_id)" );
243 }
244 }
245
246 $this->addOption( 'LIMIT', $limit + 1 );
247 $this->addOption(
248 'USE INDEX',
249 array( 'archive' => ( $mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp' ) )
250 );
251 if ( $mode == 'all' ) {
252 if ( $params['unique'] ) {
253 // @todo Does this work on non-MySQL?
254 $this->addOption( 'GROUP BY', 'ar_title' );
255 } else {
256 $sort = ( $dir == 'newer' ? '' : ' DESC' );
257 $this->addOption( 'ORDER BY', array(
258 'ar_title' . $sort,
259 'ar_timestamp' . $sort,
260 'ar_id' . $sort,
261 ) );
262 }
263 } else {
264 if ( $mode == 'revs' ) {
265 // Sort by ns and title in the same order as timestamp for efficiency
266 $this->addWhereRange( 'ar_namespace', $dir, null, null );
267 $this->addWhereRange( 'ar_title', $dir, null, null );
268 }
269 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
270 // Include in ORDER BY for uniqueness
271 $this->addWhereRange( 'ar_id', $dir, null, null );
272 }
273 $res = $this->select( __METHOD__ );
274 $pageMap = array(); // Maps ns&title to (fake) pageid
275 $count = 0;
276 $newPageID = 0;
277 foreach ( $res as $row ) {
278 if ( ++$count > $limit ) {
279 // We've had enough
280 if ( $mode == 'all' || $mode == 'revs' ) {
281 $this->setContinueEnumParameter( 'continue',
282 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
283 );
284 } else {
285 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
286 }
287 break;
288 }
289
290 $rev = array();
291 $anyHidden = false;
292
293 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
294 if ( $fld_revid ) {
295 $rev['revid'] = intval( $row->ar_rev_id );
296 }
297 if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
298 $rev['parentid'] = intval( $row->ar_parent_id );
299 }
300 if ( $fld_user || $fld_userid ) {
301 if ( $row->ar_deleted & Revision::DELETED_USER ) {
302 $rev['userhidden'] = '';
303 $anyHidden = true;
304 }
305 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_USER, $user ) ) {
306 if ( $fld_user ) {
307 $rev['user'] = $row->ar_user_text;
308 }
309 if ( $fld_userid ) {
310 $rev['userid'] = $row->ar_user;
311 }
312 }
313 }
314
315 if ( $fld_comment || $fld_parsedcomment ) {
316 if ( $row->ar_deleted & Revision::DELETED_COMMENT ) {
317 $rev['commenthidden'] = '';
318 $anyHidden = true;
319 }
320 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_COMMENT, $user ) ) {
321 if ( $fld_comment ) {
322 $rev['comment'] = $row->ar_comment;
323 }
324 if ( $fld_parsedcomment ) {
325 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
326 $rev['parsedcomment'] = Linker::formatComment( $row->ar_comment, $title );
327 }
328 }
329 }
330
331 if ( $fld_minor && $row->ar_minor_edit == 1 ) {
332 $rev['minor'] = '';
333 }
334 if ( $fld_len ) {
335 $rev['len'] = $row->ar_len;
336 }
337 if ( $fld_sha1 ) {
338 if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
339 $rev['sha1hidden'] = '';
340 $anyHidden = true;
341 }
342 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
343 if ( $row->ar_sha1 != '' ) {
344 $rev['sha1'] = wfBaseConvert( $row->ar_sha1, 36, 16, 40 );
345 } else {
346 $rev['sha1'] = '';
347 }
348 }
349 }
350 if ( $fld_content ) {
351 if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
352 $rev['texthidden'] = '';
353 $anyHidden = true;
354 }
355 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
356 ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
357 }
358 }
359
360 if ( $fld_tags ) {
361 if ( $row->ts_tags ) {
362 $tags = explode( ',', $row->ts_tags );
363 $this->getResult()->setIndexedTagName( $tags, 'tag' );
364 $rev['tags'] = $tags;
365 } else {
366 $rev['tags'] = array();
367 }
368 }
369
370 if ( $anyHidden && ( $row->ar_deleted & Revision::DELETED_RESTRICTED ) ) {
371 $rev['suppressed'] = '';
372 }
373
374 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
375 $pageID = $newPageID++;
376 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
377 $a['revisions'] = array( $rev );
378 $result->setIndexedTagName( $a['revisions'], 'rev' );
379 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
380 ApiQueryBase::addTitleInfo( $a, $title );
381 if ( $fld_token ) {
382 $a['token'] = $token;
383 }
384 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
385 } else {
386 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
387 $fit = $result->addValue(
388 array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
389 null, $rev );
390 }
391 if ( !$fit ) {
392 if ( $mode == 'all' || $mode == 'revs' ) {
393 $this->setContinueEnumParameter( 'continue',
394 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
395 );
396 } else {
397 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
398 }
399 break;
400 }
401 }
402 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
403 }
404
405 public function getAllowedParams() {
406 return array(
407 'start' => array(
408 ApiBase::PARAM_TYPE => 'timestamp'
409 ),
410 'end' => array(
411 ApiBase::PARAM_TYPE => 'timestamp',
412 ),
413 'dir' => array(
414 ApiBase::PARAM_TYPE => array(
415 'newer',
416 'older'
417 ),
418 ApiBase::PARAM_DFLT => 'older'
419 ),
420 'from' => null,
421 'to' => null,
422 'prefix' => null,
423 'continue' => null,
424 'unique' => false,
425 'tag' => null,
426 'user' => array(
427 ApiBase::PARAM_TYPE => 'user'
428 ),
429 'excludeuser' => array(
430 ApiBase::PARAM_TYPE => 'user'
431 ),
432 'namespace' => array(
433 ApiBase::PARAM_TYPE => 'namespace',
434 ApiBase::PARAM_DFLT => NS_MAIN,
435 ),
436 'limit' => array(
437 ApiBase::PARAM_DFLT => 10,
438 ApiBase::PARAM_TYPE => 'limit',
439 ApiBase::PARAM_MIN => 1,
440 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
441 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
442 ),
443 'prop' => array(
444 ApiBase::PARAM_DFLT => 'user|comment',
445 ApiBase::PARAM_TYPE => array(
446 'revid',
447 'parentid',
448 'user',
449 'userid',
450 'comment',
451 'parsedcomment',
452 'minor',
453 'len',
454 'sha1',
455 'content',
456 'token',
457 'tags'
458 ),
459 ApiBase::PARAM_ISMULTI => true
460 ),
461 );
462 }
463
464 public function getParamDescription() {
465 return array(
466 'start' => 'The timestamp to start enumerating from (1, 2)',
467 'end' => 'The timestamp to stop enumerating at (1, 2)',
468 'dir' => $this->getDirectionDescription( $this->getModulePrefix(), ' (1, 3)' ),
469 'from' => 'Start listing at this title (3)',
470 'to' => 'Stop listing at this title (3)',
471 'prefix' => 'Search for all page titles that begin with this value (3)',
472 'limit' => 'The maximum amount of revisions to list',
473 'prop' => array(
474 'Which properties to get',
475 ' revid - Adds the revision ID of the deleted revision',
476 ' parentid - Adds the revision ID of the previous revision to the page',
477 ' user - Adds the user who made the revision',
478 ' userid - Adds the user ID whom made the revision',
479 ' comment - Adds the comment of the revision',
480 ' parsedcomment - Adds the parsed comment of the revision',
481 ' minor - Tags if the revision is minor',
482 ' len - Adds the length (bytes) of the revision',
483 ' sha1 - Adds the SHA-1 (base 16) of the revision',
484 ' content - Adds the content of the revision',
485 ' token - Gives the edit token',
486 ' tags - Tags for the revision',
487 ),
488 'namespace' => 'Only list pages in this namespace (3)',
489 'user' => 'Only list revisions by this user',
490 'excludeuser' => 'Don\'t list revisions by this user',
491 'continue' => 'When more results are available, use this to continue',
492 'unique' => 'List only one revision for each page (3)',
493 'tag' => 'Only list revisions tagged with this tag',
494 );
495 }
496
497 public function getResultProperties() {
498 return array(
499 '' => array(
500 'ns' => 'namespace',
501 'title' => 'string'
502 ),
503 'token' => array(
504 'token' => 'string'
505 )
506 );
507 }
508
509 public function getDescription() {
510 $p = $this->getModulePrefix();
511
512 return array(
513 'List deleted revisions.',
514 'Operates in three modes:',
515 ' 1) List deleted revisions for the given title(s), sorted by timestamp.',
516 ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified).',
517 ' 3) List all deleted revisions in the given namespace, sorted by title and timestamp',
518 " (no titles specified, {$p}user not set).",
519 'Certain parameters only apply to some modes and are ignored in others.',
520 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
521 );
522 }
523
524 public function getPossibleErrors() {
525 return array_merge( parent::getPossibleErrors(), array(
526 array(
527 'code' => 'permissiondenied',
528 'info' => 'You don\'t have permission to view deleted revision information'
529 ),
530 array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together'
531 ),
532 array(
533 'code' => 'permissiondenied',
534 'info' => 'You don\'t have permission to view deleted revision content'
535 ),
536 array( 'code' => 'badparams', 'info' => "The 'from' parameter cannot be used in modes 1 or 2" ),
537 array( 'code' => 'badparams', 'info' => "The 'to' parameter cannot be used in modes 1 or 2" ),
538 array(
539 'code' => 'badparams',
540 'info' => "The 'prefix' parameter cannot be used in modes 1 or 2"
541 ),
542 array( 'code' => 'badparams', 'info' => "The 'start' parameter cannot be used in mode 3" ),
543 array( 'code' => 'badparams', 'info' => "The 'end' parameter cannot be used in mode 3" ),
544 ) );
545 }
546
547 public function getExamples() {
548 return array(
549 'api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
550 'drprop=user|comment|content'
551 => 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1)',
552 'api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50'
553 => 'List the last 50 deleted contributions by Bob (mode 2)',
554 'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50'
555 => 'List the first 50 deleted revisions in the main namespace (mode 3)',
556 'api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
557 => 'List the first 50 deleted pages in the Talk namespace (mode 3):',
558 );
559 }
560
561 public function getHelpUrls() {
562 return 'https://www.mediawiki.org/wiki/API:Deletedrevs';
563 }
564 }