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