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