Drop $wgChangeTagsSchemaMigrationStage
[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->addTables( 'tag_summary' );
135 $this->addJoinConds(
136 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
137 );
138 $this->addFields( 'ts_tags' );
139 }
140
141 if ( !is_null( $params['tag'] ) ) {
142 $this->addTables( 'change_tag' );
143 $this->addJoinConds(
144 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
145 );
146 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
147 try {
148 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
149 } catch ( NameTableAccessException $exception ) {
150 // Return nothing.
151 $this->addWhere( '1=0' );
152 }
153 }
154
155 if ( $fld_content ) {
156 $this->addTables( 'text' );
157 $this->addJoinConds(
158 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
159 );
160 $this->addFields( [ 'ar_text_id', 'old_text', 'old_flags' ] );
161
162 // This also means stricter restrictions
163 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
164 }
165 // Check limits
166 $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
167 $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
168
169 $limit = $params['limit'];
170
171 if ( $limit == 'max' ) {
172 $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
173 $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
174 }
175
176 $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
177
178 if ( $fld_token ) {
179 // Undelete tokens are identical for all pages, so we cache one here
180 $token = $user->getEditToken( '', $this->getMain()->getRequest() );
181 }
182
183 $dir = $params['dir'];
184
185 // We need a custom WHERE clause that matches all titles.
186 if ( $mode == 'revs' ) {
187 $lb = new LinkBatch( $titles );
188 $where = $lb->constructSet( 'ar', $db );
189 $this->addWhere( $where );
190 } elseif ( $mode == 'all' ) {
191 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
192
193 $from = $params['from'] === null
194 ? null
195 : $this->titlePartToKey( $params['from'], $params['namespace'] );
196 $to = $params['to'] === null
197 ? null
198 : $this->titlePartToKey( $params['to'], $params['namespace'] );
199 $this->addWhereRange( 'ar_title', $dir, $from, $to );
200
201 if ( isset( $params['prefix'] ) ) {
202 $this->addWhere( 'ar_title' . $db->buildLike(
203 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
204 $db->anyString() ) );
205 }
206 }
207
208 if ( !is_null( $params['user'] ) ) {
209 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
210 $actorQuery = ActorMigration::newMigration()
211 ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
212 $this->addTables( $actorQuery['tables'] );
213 $this->addJoinConds( $actorQuery['joins'] );
214 $this->addWhere( $actorQuery['conds'] );
215 } elseif ( !is_null( $params['excludeuser'] ) ) {
216 // Here there's no chance of using ar_usertext_timestamp.
217 $actorQuery = ActorMigration::newMigration()
218 ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
219 $this->addTables( $actorQuery['tables'] );
220 $this->addJoinConds( $actorQuery['joins'] );
221 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
222 }
223
224 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
225 // Paranoia: avoid brute force searches (T19342)
226 // (shouldn't be able to get here without 'deletedhistory', but
227 // check it again just in case)
228 if ( !$user->isAllowed( 'deletedhistory' ) ) {
229 $bitmask = Revision::DELETED_USER;
230 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
231 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
232 } else {
233 $bitmask = 0;
234 }
235 if ( $bitmask ) {
236 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
237 }
238 }
239
240 if ( !is_null( $params['continue'] ) ) {
241 $cont = explode( '|', $params['continue'] );
242 $op = ( $dir == 'newer' ? '>' : '<' );
243 if ( $mode == 'all' || $mode == 'revs' ) {
244 $this->dieContinueUsageIf( count( $cont ) != 4 );
245 $ns = intval( $cont[0] );
246 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
247 $title = $db->addQuotes( $cont[1] );
248 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
249 $ar_id = (int)$cont[3];
250 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
251 $this->addWhere( "ar_namespace $op $ns OR " .
252 "(ar_namespace = $ns AND " .
253 "(ar_title $op $title OR " .
254 "(ar_title = $title AND " .
255 "(ar_timestamp $op $ts OR " .
256 "(ar_timestamp = $ts AND " .
257 "ar_id $op= $ar_id)))))" );
258 } else {
259 $this->dieContinueUsageIf( count( $cont ) != 2 );
260 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
261 $ar_id = (int)$cont[1];
262 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
263 $this->addWhere( "ar_timestamp $op $ts OR " .
264 "(ar_timestamp = $ts AND " .
265 "ar_id $op= $ar_id)" );
266 }
267 }
268
269 $this->addOption( 'LIMIT', $limit + 1 );
270 if ( $mode == 'all' ) {
271 if ( $params['unique'] ) {
272 // @todo Does this work on non-MySQL?
273 $this->addOption( 'GROUP BY', 'ar_title' );
274 } else {
275 $sort = ( $dir == 'newer' ? '' : ' DESC' );
276 $this->addOption( 'ORDER BY', [
277 'ar_title' . $sort,
278 'ar_timestamp' . $sort,
279 'ar_id' . $sort,
280 ] );
281 }
282 } else {
283 if ( $mode == 'revs' ) {
284 // Sort by ns and title in the same order as timestamp for efficiency
285 $this->addWhereRange( 'ar_namespace', $dir, null, null );
286 $this->addWhereRange( 'ar_title', $dir, null, null );
287 }
288 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
289 // Include in ORDER BY for uniqueness
290 $this->addWhereRange( 'ar_id', $dir, null, null );
291 }
292 $res = $this->select( __METHOD__ );
293 $pageMap = []; // Maps ns&title to (fake) pageid
294 $count = 0;
295 $newPageID = 0;
296 foreach ( $res as $row ) {
297 if ( ++$count > $limit ) {
298 // We've had enough
299 if ( $mode == 'all' || $mode == 'revs' ) {
300 $this->setContinueEnumParameter( 'continue',
301 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
302 );
303 } else {
304 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
305 }
306 break;
307 }
308
309 $rev = [];
310 $anyHidden = false;
311
312 $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
313 if ( $fld_revid ) {
314 $rev['revid'] = intval( $row->ar_rev_id );
315 }
316 if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
317 $rev['parentid'] = intval( $row->ar_parent_id );
318 }
319 if ( $fld_user || $fld_userid ) {
320 if ( $row->ar_deleted & Revision::DELETED_USER ) {
321 $rev['userhidden'] = true;
322 $anyHidden = true;
323 }
324 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_USER, $user ) ) {
325 if ( $fld_user ) {
326 $rev['user'] = $row->ar_user_text;
327 }
328 if ( $fld_userid ) {
329 $rev['userid'] = (int)$row->ar_user;
330 }
331 }
332 }
333
334 if ( $fld_comment || $fld_parsedcomment ) {
335 if ( $row->ar_deleted & Revision::DELETED_COMMENT ) {
336 $rev['commenthidden'] = true;
337 $anyHidden = true;
338 }
339 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_COMMENT, $user ) ) {
340 $comment = $commentStore->getComment( 'ar_comment', $row )->text;
341 if ( $fld_comment ) {
342 $rev['comment'] = $comment;
343 }
344 if ( $fld_parsedcomment ) {
345 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
346 $rev['parsedcomment'] = Linker::formatComment( $comment, $title );
347 }
348 }
349 }
350
351 if ( $fld_minor ) {
352 $rev['minor'] = $row->ar_minor_edit == 1;
353 }
354 if ( $fld_len ) {
355 $rev['len'] = $row->ar_len;
356 }
357 if ( $fld_sha1 ) {
358 if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
359 $rev['sha1hidden'] = true;
360 $anyHidden = true;
361 }
362 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
363 if ( $row->ar_sha1 != '' ) {
364 $rev['sha1'] = Wikimedia\base_convert( $row->ar_sha1, 36, 16, 40 );
365 } else {
366 $rev['sha1'] = '';
367 }
368 }
369 }
370 if ( $fld_content ) {
371 if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
372 $rev['texthidden'] = true;
373 $anyHidden = true;
374 }
375 if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
376 ApiResult::setContentValue( $rev, 'text', Revision::getRevisionText( $row ) );
377 }
378 }
379
380 if ( $fld_tags ) {
381 if ( $row->ts_tags ) {
382 $tags = explode( ',', $row->ts_tags );
383 ApiResult::setIndexedTagName( $tags, 'tag' );
384 $rev['tags'] = $tags;
385 } else {
386 $rev['tags'] = [];
387 }
388 }
389
390 if ( $anyHidden && ( $row->ar_deleted & Revision::DELETED_RESTRICTED ) ) {
391 $rev['suppressed'] = true;
392 }
393
394 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
395 $pageID = $newPageID++;
396 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
397 $a['revisions'] = [ $rev ];
398 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
399 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
400 ApiQueryBase::addTitleInfo( $a, $title );
401 if ( $fld_token ) {
402 $a['token'] = $token;
403 }
404 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
405 } else {
406 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
407 $fit = $result->addValue(
408 [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
409 null, $rev );
410 }
411 if ( !$fit ) {
412 if ( $mode == 'all' || $mode == 'revs' ) {
413 $this->setContinueEnumParameter( 'continue',
414 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
415 );
416 } else {
417 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
418 }
419 break;
420 }
421 }
422 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
423 }
424
425 public function isDeprecated() {
426 return true;
427 }
428
429 public function getAllowedParams() {
430 return [
431 'start' => [
432 ApiBase::PARAM_TYPE => 'timestamp',
433 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
434 ],
435 'end' => [
436 ApiBase::PARAM_TYPE => 'timestamp',
437 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
438 ],
439 'dir' => [
440 ApiBase::PARAM_TYPE => [
441 'newer',
442 'older'
443 ],
444 ApiBase::PARAM_DFLT => 'older',
445 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
446 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 3 ] ],
447 ],
448 'from' => [
449 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
450 ],
451 'to' => [
452 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
453 ],
454 'prefix' => [
455 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
456 ],
457 'unique' => [
458 ApiBase::PARAM_DFLT => false,
459 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
460 ],
461 'namespace' => [
462 ApiBase::PARAM_TYPE => 'namespace',
463 ApiBase::PARAM_DFLT => NS_MAIN,
464 ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
465 ],
466 'tag' => null,
467 'user' => [
468 ApiBase::PARAM_TYPE => 'user'
469 ],
470 'excludeuser' => [
471 ApiBase::PARAM_TYPE => 'user'
472 ],
473 'prop' => [
474 ApiBase::PARAM_DFLT => 'user|comment',
475 ApiBase::PARAM_TYPE => [
476 'revid',
477 'parentid',
478 'user',
479 'userid',
480 'comment',
481 'parsedcomment',
482 'minor',
483 'len',
484 'sha1',
485 'content',
486 'token',
487 'tags'
488 ],
489 ApiBase::PARAM_ISMULTI => true
490 ],
491 'limit' => [
492 ApiBase::PARAM_DFLT => 10,
493 ApiBase::PARAM_TYPE => 'limit',
494 ApiBase::PARAM_MIN => 1,
495 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
496 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
497 ],
498 'continue' => [
499 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
500 ],
501 ];
502 }
503
504 protected function getExamplesMessages() {
505 return [
506 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
507 'drprop=user|comment|content'
508 => 'apihelp-query+deletedrevs-example-mode1',
509 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
510 => 'apihelp-query+deletedrevs-example-mode2',
511 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
512 => 'apihelp-query+deletedrevs-example-mode3-main',
513 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
514 => 'apihelp-query+deletedrevs-example-mode3-talk',
515 ];
516 }
517
518 public function getHelpUrls() {
519 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';
520 }
521 }