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