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