Merge "Revision: Assert that $mRecord is never null in Revision"
[lhc/web/wiklou.git] / includes / api / ApiQueryDeletedrevs.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Storage\NameTableAccessException;
25
26 /**
27 * Query module to enumerate all deleted revisions.
28 *
29 * @ingroup API
30 * @deprecated since 1.25
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 // Before doing anything at all, let's check permissions
40 $this->checkUserRightsAny( 'deletedhistory' );
41
42 $this->addDeprecation( 'apiwarn-deprecation-deletedrevs', 'action=query&list=deletedrevs' );
43
44 $user = $this->getUser();
45 $db = $this->getDB();
46 $commentStore = CommentStore::getStore();
47 $params = $this->extractRequestParams( false );
48 $prop = array_flip( $params['prop'] );
49 $fld_parentid = isset( $prop['parentid'] );
50 $fld_revid = isset( $prop['revid'] );
51 $fld_user = isset( $prop['user'] );
52 $fld_userid = isset( $prop['userid'] );
53 $fld_comment = isset( $prop['comment'] );
54 $fld_parsedcomment = isset( $prop['parsedcomment'] );
55 $fld_minor = isset( $prop['minor'] );
56 $fld_len = isset( $prop['len'] );
57 $fld_sha1 = isset( $prop['sha1'] );
58 $fld_content = isset( $prop['content'] );
59 $fld_token = isset( $prop['token'] );
60 $fld_tags = isset( $prop['tags'] );
61
62 if ( isset( $prop['token'] ) ) {
63 $p = $this->getModulePrefix();
64 }
65
66 // If we're in a mode that breaks the same-origin policy, no tokens can
67 // be obtained
68 if ( $this->lacksSameOriginSecurity() ) {
69 $fld_token = false;
70 }
71
72 // If user can't undelete, no tokens
73 if ( !$user->isAllowed( 'undelete' ) ) {
74 $fld_token = false;
75 }
76
77 $result = $this->getResult();
78 $pageSet = $this->getPageSet();
79 $titles = $pageSet->getTitles();
80
81 // This module operates in three modes:
82 // 'revs': List deleted revs for certain titles (1)
83 // 'user': List deleted revs by a certain user (2)
84 // 'all': List all deleted revs in NS (3)
85 $mode = 'all';
86 if ( count( $titles ) > 0 ) {
87 $mode = 'revs';
88 } elseif ( !is_null( $params['user'] ) ) {
89 $mode = 'user';
90 }
91
92 if ( $mode == 'revs' || $mode == 'user' ) {
93 // Ignore namespace and unique due to inability to know whether they were purposely set
94 foreach ( [ 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ] as $p ) {
95 if ( !is_null( $params[$p] ) ) {
96 $this->dieWithError( [ 'apierror-deletedrevs-param-not-1-2', $p ], 'badparams' );
97 }
98 }
99 } else {
100 foreach ( [ 'start', 'end' ] as $p ) {
101 if ( !is_null( $params[$p] ) ) {
102 $this->dieWithError( [ 'apierror-deletedrevs-param-not-3', $p ], 'badparams' );
103 }
104 }
105 }
106
107 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
108 $this->dieWithError( 'user and excludeuser cannot be used together', 'badparams' );
109 }
110
111 $this->addTables( 'archive' );
112 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_deleted', 'ar_id' ] );
113
114 $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
115 $this->addFieldsIf( 'ar_rev_id', $fld_revid );
116 if ( $fld_user || $fld_userid ) {
117 $actorQuery = ActorMigration::newMigration()->getJoin( 'ar_user' );
118 $this->addTables( $actorQuery['tables'] );
119 $this->addFields( $actorQuery['fields'] );
120 $this->addJoinConds( $actorQuery['joins'] );
121 }
122 $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
123 $this->addFieldsIf( 'ar_len', $fld_len );
124 $this->addFieldsIf( 'ar_sha1', $fld_sha1 );
125
126 if ( $fld_comment || $fld_parsedcomment ) {
127 $commentQuery = $commentStore->getJoin( 'ar_comment' );
128 $this->addTables( $commentQuery['tables'] );
129 $this->addFields( $commentQuery['fields'] );
130 $this->addJoinConds( $commentQuery['joins'] );
131 }
132
133 if ( $fld_tags ) {
134 $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
135 }
136
137 if ( !is_null( $params['tag'] ) ) {
138 $this->addTables( 'change_tag' );
139 $this->addJoinConds(
140 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
141 );
142 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
143 try {
144 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
145 } catch ( NameTableAccessException $exception ) {
146 // Return nothing.
147 $this->addWhere( '1=0' );
148 }
149 }
150
151 if ( $fld_content ) {
152 $this->addTables( 'text' );
153 $this->addJoinConds(
154 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
155 );
156 $this->addFields( [ 'ar_text_id', 'old_text', 'old_flags' ] );
157
158 // This also means stricter restrictions
159 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
160 }
161 // Check limits
162 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
163 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
164
165 $limit = $params['limit'];
166
167 if ( $limit == 'max' ) {
168 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
169 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
170 }
171
172 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
173
174 if ( $fld_token ) {
175 // Undelete tokens are identical for all pages, so we cache one here
176 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
177 }
178
179 $dir = $params['dir'];
180
181 // We need a custom WHERE clause that matches all titles.
182 if ( $mode == 'revs' ) {
183 $lb = new LinkBatch( $titles );
184 $where = $lb->constructSet( 'ar', $db );
185 $this->addWhere( $where );
186 } elseif ( $mode == 'all' ) {
187 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
188
189 $from = $params['from'] === null
190 ? null
191 : $this->titlePartToKey( $params['from'], $params['namespace'] );
192 $to = $params['to'] === null
193 ? null
194 : $this->titlePartToKey( $params['to'], $params['namespace'] );
195 $this->addWhereRange( 'ar_title', $dir, $from, $to );
196
197 if ( isset( $params['prefix'] ) ) {
198 $this->addWhere( 'ar_title' . $db->buildLike(
199 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
200 $db->anyString() ) );
201 }
202 }
203
204 if ( !is_null( $params['user'] ) ) {
205 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
206 $actorQuery = ActorMigration::newMigration()
207 ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
208 $this->addTables( $actorQuery['tables'] );
209 $this->addJoinConds( $actorQuery['joins'] );
210 $this->addWhere( $actorQuery['conds'] );
211 } elseif ( !is_null( $params['excludeuser'] ) ) {
212 // Here there's no chance of using ar_usertext_timestamp.
213 $actorQuery = ActorMigration::newMigration()
214 ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
215 $this->addTables( $actorQuery['tables'] );
216 $this->addJoinConds( $actorQuery['joins'] );
217 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
218 }
219
220 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
221 // Paranoia: avoid brute force searches (T19342)
222 // (shouldn't be able to get here without 'deletedhistory', but
223 // check it again just in case)
224 if ( !$user->isAllowed( 'deletedhistory' ) ) {
225 $bitmask = Revision::DELETED_USER;
226 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
227 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
228 } else {
229 $bitmask = 0;
230 }
231 if ( $bitmask ) {
232 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
233 }
234 }
235
236 if ( !is_null( $params['continue'] ) ) {
237 $cont = explode( '|', $params['continue'] );
238 $op = ( $dir == 'newer' ? '>' : '<' );
239 if ( $mode == 'all' || $mode == 'revs' ) {
240 $this->dieContinueUsageIf( count( $cont ) != 4 );
241 $ns = intval( $cont[0] );
242 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
243 $title = $db->addQuotes( $cont[1] );
244 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
245 $ar_id = (int)$cont[3];
246 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
247 $this->addWhere( "ar_namespace $op $ns OR " .
248 "(ar_namespace = $ns AND " .
249 "(ar_title $op $title OR " .
250 "(ar_title = $title AND " .
251 "(ar_timestamp $op $ts OR " .
252 "(ar_timestamp = $ts AND " .
253 "ar_id $op= $ar_id)))))" );
254 } else {
255 $this->dieContinueUsageIf( count( $cont ) != 2 );
256 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
257 $ar_id = (int)$cont[1];
258 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
259 $this->addWhere( "ar_timestamp $op $ts OR " .
260 "(ar_timestamp = $ts AND " .
261 "ar_id $op= $ar_id)" );
262 }
263 }
264
265 $this->addOption( 'LIMIT', $limit + 1 );
266 if ( $mode == 'all' ) {
267 if ( $params['unique'] ) {
268 // @todo Does this work on non-MySQL?
269 $this->addOption( 'GROUP BY', 'ar_title' );
270 } else {
271 $sort = ( $dir == 'newer' ? '' : ' DESC' );
272 $this->addOption( 'ORDER BY', [
273 'ar_title' . $sort,
274 'ar_timestamp' . $sort,
275 'ar_id' . $sort,
276 ] );
277 }
278 } else {
279 if ( $mode == 'revs' ) {
280 // Sort by ns and title in the same order as timestamp for efficiency
281 $this->addWhereRange( 'ar_namespace', $dir, null, null );
282 $this->addWhereRange( 'ar_title', $dir, null, null );
283 }
284 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
285 // Include in ORDER BY for uniqueness
286 $this->addWhereRange( 'ar_id', $dir, null, null );
287 }
288 $res = $this->select( __METHOD__ );
289 $pageMap = []; // Maps ns&title to (fake) pageid
290 $count = 0;
291 $newPageID = 0;
292 foreach ( $res as $row ) {
293 if ( ++$count > $limit ) {
294 // We've had enough
295 if ( $mode == 'all' || $mode == 'revs' ) {
296 $this->setContinueEnumParameter( 'continue',
297 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
298 );
299 } else {
300 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
301 }
302 break;
303 }
304
305 $rev = [];
306 $anyHidden = false;
307
308 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
309 if ( $fld_revid ) {
310 $rev['revid'] = intval( $row->ar_rev_id );
311 }
312 if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
313 $rev['parentid'] = intval( $row->ar_parent_id );
314 }
315 if ( $fld_user || $fld_userid ) {
316 if ( $row->ar_deleted & Revision::DELETED_USER ) {
317 $rev['userhidden'] = true;
318 $anyHidden = true;
319 }
320 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_USER, $user ) ) {
321 if ( $fld_user ) {
322 $rev['user'] = $row->ar_user_text;
323 }
324 if ( $fld_userid ) {
325 $rev['userid'] = (int)$row->ar_user;
326 }
327 }
328 }
329
330 if ( $fld_comment || $fld_parsedcomment ) {
331 if ( $row->ar_deleted & Revision::DELETED_COMMENT ) {
332 $rev['commenthidden'] = true;
333 $anyHidden = true;
334 }
335 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_COMMENT, $user ) ) {
336 $comment = $commentStore->getComment( 'ar_comment', $row )->text;
337 if ( $fld_comment ) {
338 $rev['comment'] = $comment;
339 }
340 if ( $fld_parsedcomment ) {
341 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
342 $rev['parsedcomment'] = Linker::formatComment( $comment, $title );
343 }
344 }
345 }
346
347 if ( $fld_minor ) {
348 $rev['minor'] = $row->ar_minor_edit == 1;
349 }
350 if ( $fld_len ) {
351 $rev['len'] = $row->ar_len;
352 }
353 if ( $fld_sha1 ) {
354 if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
355 $rev['sha1hidden'] = true;
356 $anyHidden = true;
357 }
358 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
359 if ( $row->ar_sha1 != '' ) {
360 $rev['sha1'] = Wikimedia\base_convert( $row->ar_sha1, 36, 16, 40 );
361 } else {
362 $rev['sha1'] = '';
363 }
364 }
365 }
366 if ( $fld_content ) {
367 if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
368 $rev['texthidden'] = true;
369 $anyHidden = true;
370 }
371 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
372 ApiResult::setContentValue( $rev, 'text', Revision::getRevisionText( $row ) );
373 }
374 }
375
376 if ( $fld_tags ) {
377 if ( $row->ts_tags ) {
378 $tags = explode( ',', $row->ts_tags );
379 ApiResult::setIndexedTagName( $tags, 'tag' );
380 $rev['tags'] = $tags;
381 } else {
382 $rev['tags'] = [];
383 }
384 }
385
386 if ( $anyHidden && ( $row->ar_deleted & Revision::DELETED_RESTRICTED ) ) {
387 $rev['suppressed'] = true;
388 }
389
390 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
391 $pageID = $newPageID++;
392 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
393 $a['revisions'] = [ $rev ];
394 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
395 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
396 ApiQueryBase::addTitleInfo( $a, $title );
397 if ( $fld_token ) {
398 $a['token'] = $token;
399 }
400 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
401 } else {
402 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
403 $fit = $result->addValue(
404 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
405 null, $rev );
406 }
407 if ( !$fit ) {
408 if ( $mode == 'all' || $mode == 'revs' ) {
409 $this->setContinueEnumParameter( 'continue',
410 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
411 );
412 } else {
413 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
414 }
415 break;
416 }
417 }
418 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
419 }
420
421 public function isDeprecated() {
422 return true;
423 }
424
425 public function getAllowedParams() {
426 return [
427 'start' => [
428 ApiBase::PARAM_TYPE => 'timestamp',
429 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
430 ],
431 'end' => [
432 ApiBase::PARAM_TYPE => 'timestamp',
433 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
434 ],
435 'dir' => [
436 ApiBase::PARAM_TYPE => [
437 'newer',
438 'older'
439 ],
440 ApiBase::PARAM_DFLT => 'older',
441 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
442 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 3 ] ],
443 ],
444 'from' => [
445 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
446 ],
447 'to' => [
448 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
449 ],
450 'prefix' => [
451 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
452 ],
453 'unique' => [
454 ApiBase::PARAM_DFLT => false,
455 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
456 ],
457 'namespace' => [
458 ApiBase::PARAM_TYPE => 'namespace',
459 ApiBase::PARAM_DFLT => NS_MAIN,
460 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
461 ],
462 'tag' => null,
463 'user' => [
464 ApiBase::PARAM_TYPE => 'user'
465 ],
466 'excludeuser' => [
467 ApiBase::PARAM_TYPE => 'user'
468 ],
469 'prop' => [
470 ApiBase::PARAM_DFLT => 'user|comment',
471 ApiBase::PARAM_TYPE => [
472 'revid',
473 'parentid',
474 'user',
475 'userid',
476 'comment',
477 'parsedcomment',
478 'minor',
479 'len',
480 'sha1',
481 'content',
482 'token',
483 'tags'
484 ],
485 ApiBase::PARAM_ISMULTI => true
486 ],
487 'limit' => [
488 ApiBase::PARAM_DFLT => 10,
489 ApiBase::PARAM_TYPE => 'limit',
490 ApiBase::PARAM_MIN => 1,
491 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
492 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
493 ],
494 'continue' => [
495 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
496 ],
497 ];
498 }
499
500 protected function getExamplesMessages() {
501 return [
502 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
503 'drprop=user|comment|content'
504 => 'apihelp-query+deletedrevs-example-mode1',
505 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
506 => 'apihelp-query+deletedrevs-example-mode2',
507 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
508 => 'apihelp-query+deletedrevs-example-mode3-main',
509 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
510 => 'apihelp-query+deletedrevs-example-mode3-talk',
511 ];
512 }
513
514 public function getHelpUrls() {
515 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';
516 }
517 }