Drop $wgChangeTagsSchemaMigrationStage
[lhc/web/wiklou.git] / includes / api / ApiQueryRecentChanges.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<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\Revision\RevisionRecord;
25 use MediaWiki\Storage\NameTableAccessException;
26
27 /**
28 * A query action to enumerate the recent changes that were done to the wiki.
29 * Various filters are supported.
30 *
31 * @ingroup API
32 */
33 class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
34
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'rc' );
37 }
38
39 private $commentStore;
40
41 private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
42 $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false,
43 $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false,
44 $fld_tags = false, $fld_sha1 = false, $token = [];
45
46 private $tokenFunctions;
47
48 /**
49 * Get an array mapping token names to their handler functions.
50 * The prototype for a token function is func($pageid, $title, $rc)
51 * it should return a token or false (permission denied)
52 * @deprecated since 1.24
53 * @return array [ tokenname => function ]
54 */
55 protected function getTokenFunctions() {
56 // Don't call the hooks twice
57 if ( isset( $this->tokenFunctions ) ) {
58 return $this->tokenFunctions;
59 }
60
61 // If we're in a mode that breaks the same-origin policy, no tokens can
62 // be obtained
63 if ( $this->lacksSameOriginSecurity() ) {
64 return [];
65 }
66
67 $this->tokenFunctions = [
68 'patrol' => [ self::class, 'getPatrolToken' ]
69 ];
70 Hooks::run( 'APIQueryRecentChangesTokens', [ &$this->tokenFunctions ] );
71
72 return $this->tokenFunctions;
73 }
74
75 /**
76 * @deprecated since 1.24
77 * @param int $pageid
78 * @param Title $title
79 * @param RecentChange|null $rc
80 * @return bool|string
81 */
82 public static function getPatrolToken( $pageid, $title, $rc = null ) {
83 global $wgUser;
84
85 $validTokenUser = false;
86
87 if ( $rc ) {
88 if ( ( $wgUser->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT ) ||
89 ( $wgUser->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW )
90 ) {
91 $validTokenUser = true;
92 }
93 } elseif ( $wgUser->useRCPatrol() || $wgUser->useNPPatrol() ) {
94 $validTokenUser = true;
95 }
96
97 if ( $validTokenUser ) {
98 // The patrol token is always the same, let's exploit that
99 static $cachedPatrolToken = null;
100
101 if ( is_null( $cachedPatrolToken ) ) {
102 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
103 }
104
105 return $cachedPatrolToken;
106 }
107
108 return false;
109 }
110
111 /**
112 * Sets internal state to include the desired properties in the output.
113 * @param array $prop Associative array of properties, only keys are used here
114 */
115 public function initProperties( $prop ) {
116 $this->fld_comment = isset( $prop['comment'] );
117 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
118 $this->fld_user = isset( $prop['user'] );
119 $this->fld_userid = isset( $prop['userid'] );
120 $this->fld_flags = isset( $prop['flags'] );
121 $this->fld_timestamp = isset( $prop['timestamp'] );
122 $this->fld_title = isset( $prop['title'] );
123 $this->fld_ids = isset( $prop['ids'] );
124 $this->fld_sizes = isset( $prop['sizes'] );
125 $this->fld_redirect = isset( $prop['redirect'] );
126 $this->fld_patrolled = isset( $prop['patrolled'] );
127 $this->fld_loginfo = isset( $prop['loginfo'] );
128 $this->fld_tags = isset( $prop['tags'] );
129 $this->fld_sha1 = isset( $prop['sha1'] );
130 }
131
132 public function execute() {
133 $this->run();
134 }
135
136 public function executeGenerator( $resultPageSet ) {
137 $this->run( $resultPageSet );
138 }
139
140 /**
141 * Generates and outputs the result of this query based upon the provided parameters.
142 *
143 * @param ApiPageSet|null $resultPageSet
144 */
145 public function run( $resultPageSet = null ) {
146 $user = $this->getUser();
147 /* Get the parameters of the request. */
148 $params = $this->extractRequestParams();
149
150 /* Build our basic query. Namely, something along the lines of:
151 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
152 * AND rc_timestamp < $end AND rc_namespace = $namespace
153 */
154 $this->addTables( 'recentchanges' );
155 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
156
157 if ( !is_null( $params['continue'] ) ) {
158 $cont = explode( '|', $params['continue'] );
159 $this->dieContinueUsageIf( count( $cont ) != 2 );
160 $db = $this->getDB();
161 $timestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
162 $id = intval( $cont[1] );
163 $this->dieContinueUsageIf( $id != $cont[1] );
164 $op = $params['dir'] === 'older' ? '<' : '>';
165 $this->addWhere(
166 "rc_timestamp $op $timestamp OR " .
167 "(rc_timestamp = $timestamp AND " .
168 "rc_id $op= $id)"
169 );
170 }
171
172 $order = $params['dir'] === 'older' ? 'DESC' : 'ASC';
173 $this->addOption( 'ORDER BY', [
174 "rc_timestamp $order",
175 "rc_id $order",
176 ] );
177
178 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
179
180 if ( !is_null( $params['type'] ) ) {
181 try {
182 $this->addWhereFld( 'rc_type', RecentChange::parseToRCType( $params['type'] ) );
183 } catch ( Exception $e ) {
184 ApiBase::dieDebug( __METHOD__, $e->getMessage() );
185 }
186 }
187
188 $title = $params['title'];
189 if ( !is_null( $title ) ) {
190 $titleObj = Title::newFromText( $title );
191 if ( is_null( $titleObj ) ) {
192 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
193 }
194 $this->addWhereFld( 'rc_namespace', $titleObj->getNamespace() );
195 $this->addWhereFld( 'rc_title', $titleObj->getDBkey() );
196 }
197
198 if ( !is_null( $params['show'] ) ) {
199 $show = array_flip( $params['show'] );
200
201 /* Check for conflicting parameters. */
202 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
203 || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
204 || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
205 || ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
206 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
207 || ( isset( $show['patrolled'] ) && isset( $show['unpatrolled'] ) )
208 || ( isset( $show['!patrolled'] ) && isset( $show['unpatrolled'] ) )
209 || ( isset( $show['autopatrolled'] ) && isset( $show['!autopatrolled'] ) )
210 || ( isset( $show['autopatrolled'] ) && isset( $show['unpatrolled'] ) )
211 || ( isset( $show['autopatrolled'] ) && isset( $show['!patrolled'] ) )
212 ) {
213 $this->dieWithError( 'apierror-show' );
214 }
215
216 // Check permissions
217 if ( isset( $show['patrolled'] )
218 || isset( $show['!patrolled'] )
219 || isset( $show['unpatrolled'] )
220 || isset( $show['autopatrolled'] )
221 || isset( $show['!autopatrolled'] )
222 ) {
223 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
224 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
225 }
226 }
227
228 /* Add additional conditions to query depending upon parameters. */
229 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
230 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
231 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
232 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
233 if ( isset( $show['anon'] ) || isset( $show['!anon'] ) ) {
234 $actorMigration = ActorMigration::newMigration();
235 $actorQuery = $actorMigration->getJoin( 'rc_user' );
236 $this->addTables( $actorQuery['tables'] );
237 $this->addJoinConds( $actorQuery['joins'] );
238 $this->addWhereIf(
239 $actorMigration->isAnon( $actorQuery['fields']['rc_user'] ), isset( $show['anon'] )
240 );
241 $this->addWhereIf(
242 $actorMigration->isNotAnon( $actorQuery['fields']['rc_user'] ), isset( $show['!anon'] )
243 );
244 }
245 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
246 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
247 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
248
249 if ( isset( $show['unpatrolled'] ) ) {
250 // See ChangesList::isUnpatrolled
251 if ( $user->useRCPatrol() ) {
252 $this->addWhere( 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED );
253 } elseif ( $user->useNPPatrol() ) {
254 $this->addWhere( 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED );
255 $this->addWhereFld( 'rc_type', RC_NEW );
256 }
257 }
258
259 $this->addWhereIf(
260 'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED,
261 isset( $show['!autopatrolled'] )
262 );
263 $this->addWhereIf(
264 'rc_patrolled = ' . RecentChange::PRC_AUTOPATROLLED,
265 isset( $show['autopatrolled'] )
266 );
267
268 // Don't throw log entries out the window here
269 $this->addWhereIf(
270 'page_is_redirect = 0 OR page_is_redirect IS NULL',
271 isset( $show['!redirect'] )
272 );
273 }
274
275 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
276
277 if ( !is_null( $params['user'] ) ) {
278 // Don't query by user ID here, it might be able to use the rc_user_text index.
279 $actorQuery = ActorMigration::newMigration()
280 ->getWhere( $this->getDB(), 'rc_user', User::newFromName( $params['user'], false ), false );
281 $this->addTables( $actorQuery['tables'] );
282 $this->addJoinConds( $actorQuery['joins'] );
283 $this->addWhere( $actorQuery['conds'] );
284 }
285
286 if ( !is_null( $params['excludeuser'] ) ) {
287 // Here there's no chance to use the rc_user_text index, so allow ID to be used.
288 $actorQuery = ActorMigration::newMigration()
289 ->getWhere( $this->getDB(), 'rc_user', User::newFromName( $params['excludeuser'], false ) );
290 $this->addTables( $actorQuery['tables'] );
291 $this->addJoinConds( $actorQuery['joins'] );
292 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
293 }
294
295 /* Add the fields we're concerned with to our query. */
296 $this->addFields( [
297 'rc_id',
298 'rc_timestamp',
299 'rc_namespace',
300 'rc_title',
301 'rc_cur_id',
302 'rc_type',
303 'rc_deleted'
304 ] );
305
306 $showRedirects = false;
307 /* Determine what properties we need to display. */
308 if ( !is_null( $params['prop'] ) ) {
309 $prop = array_flip( $params['prop'] );
310
311 /* Set up internal members based upon params. */
312 $this->initProperties( $prop );
313
314 if ( $this->fld_patrolled && !$user->useRCPatrol() && !$user->useNPPatrol() ) {
315 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
316 }
317
318 /* Add fields to our query if they are specified as a needed parameter. */
319 $this->addFieldsIf( [ 'rc_this_oldid', 'rc_last_oldid' ], $this->fld_ids );
320 if ( $this->fld_user || $this->fld_userid ) {
321 $actorQuery = ActorMigration::newMigration()->getJoin( 'rc_user' );
322 $this->addTables( $actorQuery['tables'] );
323 $this->addFields( $actorQuery['fields'] );
324 $this->addJoinConds( $actorQuery['joins'] );
325 }
326 $this->addFieldsIf( [ 'rc_minor', 'rc_type', 'rc_bot' ], $this->fld_flags );
327 $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes );
328 $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrolled );
329 $this->addFieldsIf(
330 [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ],
331 $this->fld_loginfo
332 );
333 $showRedirects = $this->fld_redirect || isset( $show['redirect'] )
334 || isset( $show['!redirect'] );
335 }
336 $this->addFieldsIf( [ 'rc_this_oldid' ],
337 $resultPageSet && $params['generaterevisions'] );
338
339 if ( $this->fld_tags ) {
340 $this->addTables( 'tag_summary' );
341 $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', [ 'rc_id=ts_rc_id' ] ] ] );
342 $this->addFields( 'ts_tags' );
343 }
344
345 if ( $this->fld_sha1 ) {
346 $this->addTables( 'revision' );
347 $this->addJoinConds( [ 'revision' => [ 'LEFT JOIN',
348 [ 'rc_this_oldid=rev_id' ] ] ] );
349 $this->addFields( [ 'rev_sha1', 'rev_deleted' ] );
350 }
351
352 if ( $params['toponly'] || $showRedirects ) {
353 $this->addTables( 'page' );
354 $this->addJoinConds( [ 'page' => [ 'LEFT JOIN',
355 [ 'rc_namespace=page_namespace', 'rc_title=page_title' ] ] ] );
356 $this->addFields( 'page_is_redirect' );
357
358 if ( $params['toponly'] ) {
359 $this->addWhere( 'rc_this_oldid = page_latest' );
360 }
361 }
362
363 if ( !is_null( $params['tag'] ) ) {
364 $this->addTables( 'change_tag' );
365 $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN', [ 'rc_id=ct_rc_id' ] ] ] );
366 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
367 try {
368 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
369 } catch ( NameTableAccessException $exception ) {
370 // Return nothing.
371 $this->addWhere( '1=0' );
372 }
373 }
374
375 // Paranoia: avoid brute force searches (T19342)
376 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
377 if ( !$user->isAllowed( 'deletedhistory' ) ) {
378 $bitmask = RevisionRecord::DELETED_USER;
379 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
380 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
381 } else {
382 $bitmask = 0;
383 }
384 if ( $bitmask ) {
385 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
386 }
387 }
388 if ( $this->getRequest()->getCheck( 'namespace' ) ) {
389 // LogPage::DELETED_ACTION hides the affected page, too.
390 if ( !$user->isAllowed( 'deletedhistory' ) ) {
391 $bitmask = LogPage::DELETED_ACTION;
392 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
393 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
394 } else {
395 $bitmask = 0;
396 }
397 if ( $bitmask ) {
398 $this->addWhere( $this->getDB()->makeList( [
399 'rc_type != ' . RC_LOG,
400 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
401 ], LIST_OR ) );
402 }
403 }
404
405 $this->token = $params['token'];
406
407 if ( $this->fld_comment || $this->fld_parsedcomment || $this->token ) {
408 $this->commentStore = CommentStore::getStore();
409 $commentQuery = $this->commentStore->getJoin( 'rc_comment' );
410 $this->addTables( $commentQuery['tables'] );
411 $this->addFields( $commentQuery['fields'] );
412 $this->addJoinConds( $commentQuery['joins'] );
413 }
414
415 $this->addOption( 'LIMIT', $params['limit'] + 1 );
416
417 $hookData = [];
418 $count = 0;
419 /* Perform the actual query. */
420 $res = $this->select( __METHOD__, [], $hookData );
421
422 $revids = [];
423 $titles = [];
424
425 $result = $this->getResult();
426
427 /* Iterate through the rows, adding data extracted from them to our query result. */
428 foreach ( $res as $row ) {
429 if ( $count === 0 && $resultPageSet !== null ) {
430 // Set the non-continue since the list of recentchanges is
431 // prone to having entries added at the start frequently.
432 $this->getContinuationManager()->addGeneratorNonContinueParam(
433 $this, 'continue', "$row->rc_timestamp|$row->rc_id"
434 );
435 }
436 if ( ++$count > $params['limit'] ) {
437 // We've reached the one extra which shows that there are
438 // additional pages to be had. Stop here...
439 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
440 break;
441 }
442
443 if ( is_null( $resultPageSet ) ) {
444 /* Extract the data from a single row. */
445 $vals = $this->extractRowInfo( $row );
446
447 /* Add that row's data to our final output. */
448 $fit = $this->processRow( $row, $vals, $hookData ) &&
449 $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
450 if ( !$fit ) {
451 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
452 break;
453 }
454 } elseif ( $params['generaterevisions'] ) {
455 $revid = (int)$row->rc_this_oldid;
456 if ( $revid > 0 ) {
457 $revids[] = $revid;
458 }
459 } else {
460 $titles[] = Title::makeTitle( $row->rc_namespace, $row->rc_title );
461 }
462 }
463
464 if ( is_null( $resultPageSet ) ) {
465 /* Format the result */
466 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'rc' );
467 } elseif ( $params['generaterevisions'] ) {
468 $resultPageSet->populateFromRevisionIDs( $revids );
469 } else {
470 $resultPageSet->populateFromTitles( $titles );
471 }
472 }
473
474 /**
475 * Extracts from a single sql row the data needed to describe one recent change.
476 *
477 * @param stdClass $row The row from which to extract the data.
478 * @return array An array mapping strings (descriptors) to their respective string values.
479 * @access public
480 */
481 public function extractRowInfo( $row ) {
482 /* Determine the title of the page that has been changed. */
483 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
484 $user = $this->getUser();
485
486 /* Our output data. */
487 $vals = [];
488
489 $type = intval( $row->rc_type );
490 $vals['type'] = RecentChange::parseFromRCType( $type );
491
492 $anyHidden = false;
493
494 /* Create a new entry in the result for the title. */
495 if ( $this->fld_title || $this->fld_ids ) {
496 if ( $type === RC_LOG && ( $row->rc_deleted & LogPage::DELETED_ACTION ) ) {
497 $vals['actionhidden'] = true;
498 $anyHidden = true;
499 }
500 if ( $type !== RC_LOG ||
501 LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user )
502 ) {
503 if ( $this->fld_title ) {
504 ApiQueryBase::addTitleInfo( $vals, $title );
505 }
506 if ( $this->fld_ids ) {
507 $vals['pageid'] = intval( $row->rc_cur_id );
508 $vals['revid'] = intval( $row->rc_this_oldid );
509 $vals['old_revid'] = intval( $row->rc_last_oldid );
510 }
511 }
512 }
513
514 if ( $this->fld_ids ) {
515 $vals['rcid'] = intval( $row->rc_id );
516 }
517
518 /* Add user data and 'anon' flag, if user is anonymous. */
519 if ( $this->fld_user || $this->fld_userid ) {
520 if ( $row->rc_deleted & RevisionRecord::DELETED_USER ) {
521 $vals['userhidden'] = true;
522 $anyHidden = true;
523 }
524 if ( RevisionRecord::userCanBitfield( $row->rc_deleted, RevisionRecord::DELETED_USER, $user ) ) {
525 if ( $this->fld_user ) {
526 $vals['user'] = $row->rc_user_text;
527 }
528
529 if ( $this->fld_userid ) {
530 $vals['userid'] = (int)$row->rc_user;
531 }
532
533 if ( !$row->rc_user ) {
534 $vals['anon'] = true;
535 }
536 }
537 }
538
539 /* Add flags, such as new, minor, bot. */
540 if ( $this->fld_flags ) {
541 $vals['bot'] = (bool)$row->rc_bot;
542 $vals['new'] = $row->rc_type == RC_NEW;
543 $vals['minor'] = (bool)$row->rc_minor;
544 }
545
546 /* Add sizes of each revision. (Only available on 1.10+) */
547 if ( $this->fld_sizes ) {
548 $vals['oldlen'] = intval( $row->rc_old_len );
549 $vals['newlen'] = intval( $row->rc_new_len );
550 }
551
552 /* Add the timestamp. */
553 if ( $this->fld_timestamp ) {
554 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
555 }
556
557 /* Add edit summary / log summary. */
558 if ( $this->fld_comment || $this->fld_parsedcomment ) {
559 if ( $row->rc_deleted & RevisionRecord::DELETED_COMMENT ) {
560 $vals['commenthidden'] = true;
561 $anyHidden = true;
562 }
563 if ( RevisionRecord::userCanBitfield(
564 $row->rc_deleted, RevisionRecord::DELETED_COMMENT, $user
565 ) ) {
566 $comment = $this->commentStore->getComment( 'rc_comment', $row )->text;
567 if ( $this->fld_comment ) {
568 $vals['comment'] = $comment;
569 }
570
571 if ( $this->fld_parsedcomment ) {
572 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
573 }
574 }
575 }
576
577 if ( $this->fld_redirect ) {
578 $vals['redirect'] = (bool)$row->page_is_redirect;
579 }
580
581 /* Add the patrolled flag */
582 if ( $this->fld_patrolled ) {
583 $vals['patrolled'] = $row->rc_patrolled != RecentChange::PRC_UNPATROLLED;
584 $vals['unpatrolled'] = ChangesList::isUnpatrolled( $row, $user );
585 $vals['autopatrolled'] = $row->rc_patrolled == RecentChange::PRC_AUTOPATROLLED;
586 }
587
588 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
589 if ( $row->rc_deleted & LogPage::DELETED_ACTION ) {
590 $vals['actionhidden'] = true;
591 $anyHidden = true;
592 }
593 if ( LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user ) ) {
594 $vals['logid'] = intval( $row->rc_logid );
595 $vals['logtype'] = $row->rc_log_type;
596 $vals['logaction'] = $row->rc_log_action;
597 $vals['logparams'] = LogFormatter::newFromRow( $row )->formatParametersForApi();
598 }
599 }
600
601 if ( $this->fld_tags ) {
602 if ( $row->ts_tags ) {
603 $tags = explode( ',', $row->ts_tags );
604 ApiResult::setIndexedTagName( $tags, 'tag' );
605 $vals['tags'] = $tags;
606 } else {
607 $vals['tags'] = [];
608 }
609 }
610
611 if ( $this->fld_sha1 && $row->rev_sha1 !== null ) {
612 if ( $row->rev_deleted & RevisionRecord::DELETED_TEXT ) {
613 $vals['sha1hidden'] = true;
614 $anyHidden = true;
615 }
616 if ( RevisionRecord::userCanBitfield(
617 $row->rev_deleted, RevisionRecord::DELETED_TEXT, $user
618 ) ) {
619 if ( $row->rev_sha1 !== '' ) {
620 $vals['sha1'] = Wikimedia\base_convert( $row->rev_sha1, 36, 16, 40 );
621 } else {
622 $vals['sha1'] = '';
623 }
624 }
625 }
626
627 if ( !is_null( $this->token ) ) {
628 $tokenFunctions = $this->getTokenFunctions();
629 foreach ( $this->token as $t ) {
630 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id,
631 $title, RecentChange::newFromRow( $row ) );
632 if ( $val === false ) {
633 $this->addWarning( [ 'apiwarn-tokennotallowed', $t ] );
634 } else {
635 $vals[$t . 'token'] = $val;
636 }
637 }
638 }
639
640 if ( $anyHidden && ( $row->rc_deleted & RevisionRecord::DELETED_RESTRICTED ) ) {
641 $vals['suppressed'] = true;
642 }
643
644 return $vals;
645 }
646
647 public function getCacheMode( $params ) {
648 if ( isset( $params['show'] ) ) {
649 foreach ( $params['show'] as $show ) {
650 if ( $show === 'patrolled' || $show === '!patrolled' ) {
651 return 'private';
652 }
653 }
654 }
655 if ( isset( $params['token'] ) ) {
656 return 'private';
657 }
658 if ( $this->userCanSeeRevDel() ) {
659 return 'private';
660 }
661 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
662 // formatComment() calls wfMessage() among other things
663 return 'anon-public-user-private';
664 }
665
666 return 'public';
667 }
668
669 public function getAllowedParams() {
670 return [
671 'start' => [
672 ApiBase::PARAM_TYPE => 'timestamp'
673 ],
674 'end' => [
675 ApiBase::PARAM_TYPE => 'timestamp'
676 ],
677 'dir' => [
678 ApiBase::PARAM_DFLT => 'older',
679 ApiBase::PARAM_TYPE => [
680 'newer',
681 'older'
682 ],
683 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
684 ],
685 'namespace' => [
686 ApiBase::PARAM_ISMULTI => true,
687 ApiBase::PARAM_TYPE => 'namespace',
688 ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
689 ],
690 'user' => [
691 ApiBase::PARAM_TYPE => 'user'
692 ],
693 'excludeuser' => [
694 ApiBase::PARAM_TYPE => 'user'
695 ],
696 'tag' => null,
697 'prop' => [
698 ApiBase::PARAM_ISMULTI => true,
699 ApiBase::PARAM_DFLT => 'title|timestamp|ids',
700 ApiBase::PARAM_TYPE => [
701 'user',
702 'userid',
703 'comment',
704 'parsedcomment',
705 'flags',
706 'timestamp',
707 'title',
708 'ids',
709 'sizes',
710 'redirect',
711 'patrolled',
712 'loginfo',
713 'tags',
714 'sha1',
715 ],
716 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
717 ],
718 'token' => [
719 ApiBase::PARAM_DEPRECATED => true,
720 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
721 ApiBase::PARAM_ISMULTI => true
722 ],
723 'show' => [
724 ApiBase::PARAM_ISMULTI => true,
725 ApiBase::PARAM_TYPE => [
726 'minor',
727 '!minor',
728 'bot',
729 '!bot',
730 'anon',
731 '!anon',
732 'redirect',
733 '!redirect',
734 'patrolled',
735 '!patrolled',
736 'unpatrolled',
737 'autopatrolled',
738 '!autopatrolled',
739 ]
740 ],
741 'limit' => [
742 ApiBase::PARAM_DFLT => 10,
743 ApiBase::PARAM_TYPE => 'limit',
744 ApiBase::PARAM_MIN => 1,
745 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
746 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
747 ],
748 'type' => [
749 ApiBase::PARAM_DFLT => 'edit|new|log|categorize',
750 ApiBase::PARAM_ISMULTI => true,
751 ApiBase::PARAM_TYPE => RecentChange::getChangeTypes()
752 ],
753 'toponly' => false,
754 'title' => null,
755 'continue' => [
756 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
757 ],
758 'generaterevisions' => false,
759 ];
760 }
761
762 protected function getExamplesMessages() {
763 return [
764 'action=query&list=recentchanges'
765 => 'apihelp-query+recentchanges-example-simple',
766 'action=query&generator=recentchanges&grcshow=!patrolled&prop=info'
767 => 'apihelp-query+recentchanges-example-generator',
768 ];
769 }
770
771 public function getHelpUrls() {
772 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Recentchanges';
773 }
774 }