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