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