Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[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 = (int)$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 ( $this->includesPatrollingFlags( $show ) ) {
218 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
219 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
220 }
221 }
222
223 /* Add additional conditions to query depending upon parameters. */
224 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
225 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
226 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
227 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
228 if ( isset( $show['anon'] ) || isset( $show['!anon'] ) ) {
229 $actorMigration = ActorMigration::newMigration();
230 $actorQuery = $actorMigration->getJoin( 'rc_user' );
231 $this->addTables( $actorQuery['tables'] );
232 $this->addJoinConds( $actorQuery['joins'] );
233 $this->addWhereIf(
234 $actorMigration->isAnon( $actorQuery['fields']['rc_user'] ), isset( $show['anon'] )
235 );
236 $this->addWhereIf(
237 $actorMigration->isNotAnon( $actorQuery['fields']['rc_user'] ), isset( $show['!anon'] )
238 );
239 }
240 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
241 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
242 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
243
244 if ( isset( $show['unpatrolled'] ) ) {
245 // See ChangesList::isUnpatrolled
246 if ( $user->useRCPatrol() ) {
247 $this->addWhere( 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED );
248 } elseif ( $user->useNPPatrol() ) {
249 $this->addWhere( 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED );
250 $this->addWhereFld( 'rc_type', RC_NEW );
251 }
252 }
253
254 $this->addWhereIf(
255 'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED,
256 isset( $show['!autopatrolled'] )
257 );
258 $this->addWhereIf(
259 'rc_patrolled = ' . RecentChange::PRC_AUTOPATROLLED,
260 isset( $show['autopatrolled'] )
261 );
262
263 // Don't throw log entries out the window here
264 $this->addWhereIf(
265 'page_is_redirect = 0 OR page_is_redirect IS NULL',
266 isset( $show['!redirect'] )
267 );
268 }
269
270 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
271
272 if ( !is_null( $params['user'] ) ) {
273 // Don't query by user ID here, it might be able to use the rc_user_text index.
274 $actorQuery = ActorMigration::newMigration()
275 ->getWhere( $this->getDB(), 'rc_user', User::newFromName( $params['user'], false ), false );
276 $this->addTables( $actorQuery['tables'] );
277 $this->addJoinConds( $actorQuery['joins'] );
278 $this->addWhere( $actorQuery['conds'] );
279 }
280
281 if ( !is_null( $params['excludeuser'] ) ) {
282 // Here there's no chance to use the rc_user_text index, so allow ID to be used.
283 $actorQuery = ActorMigration::newMigration()
284 ->getWhere( $this->getDB(), 'rc_user', User::newFromName( $params['excludeuser'], false ) );
285 $this->addTables( $actorQuery['tables'] );
286 $this->addJoinConds( $actorQuery['joins'] );
287 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
288 }
289
290 /* Add the fields we're concerned with to our query. */
291 $this->addFields( [
292 'rc_id',
293 'rc_timestamp',
294 'rc_namespace',
295 'rc_title',
296 'rc_cur_id',
297 'rc_type',
298 'rc_deleted'
299 ] );
300
301 $showRedirects = false;
302 /* Determine what properties we need to display. */
303 if ( !is_null( $params['prop'] ) ) {
304 $prop = array_flip( $params['prop'] );
305
306 /* Set up internal members based upon params. */
307 $this->initProperties( $prop );
308
309 if ( $this->fld_patrolled && !$user->useRCPatrol() && !$user->useNPPatrol() ) {
310 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
311 }
312
313 /* Add fields to our query if they are specified as a needed parameter. */
314 $this->addFieldsIf( [ 'rc_this_oldid', 'rc_last_oldid' ], $this->fld_ids );
315 if ( $this->fld_user || $this->fld_userid ) {
316 $actorQuery = ActorMigration::newMigration()->getJoin( 'rc_user' );
317 $this->addTables( $actorQuery['tables'] );
318 $this->addFields( $actorQuery['fields'] );
319 $this->addJoinConds( $actorQuery['joins'] );
320 }
321 $this->addFieldsIf( [ 'rc_minor', 'rc_type', 'rc_bot' ], $this->fld_flags );
322 $this->addFieldsIf( [ 'rc_old_len', 'rc_new_len' ], $this->fld_sizes );
323 $this->addFieldsIf( [ 'rc_patrolled', 'rc_log_type' ], $this->fld_patrolled );
324 $this->addFieldsIf(
325 [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ],
326 $this->fld_loginfo
327 );
328 $showRedirects = $this->fld_redirect || isset( $show['redirect'] )
329 || isset( $show['!redirect'] );
330 }
331 $this->addFieldsIf( [ 'rc_this_oldid' ],
332 $resultPageSet && $params['generaterevisions'] );
333
334 if ( $this->fld_tags ) {
335 $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'recentchanges' ) ] );
336 }
337
338 if ( $this->fld_sha1 ) {
339 $this->addTables( 'revision' );
340 $this->addJoinConds( [ 'revision' => [ 'LEFT JOIN',
341 [ 'rc_this_oldid=rev_id' ] ] ] );
342 $this->addFields( [ 'rev_sha1', 'rev_deleted' ] );
343 }
344
345 if ( $params['toponly'] || $showRedirects ) {
346 $this->addTables( 'page' );
347 $this->addJoinConds( [ 'page' => [ 'LEFT JOIN',
348 [ 'rc_namespace=page_namespace', 'rc_title=page_title' ] ] ] );
349 $this->addFields( 'page_is_redirect' );
350
351 if ( $params['toponly'] ) {
352 $this->addWhere( 'rc_this_oldid = page_latest' );
353 }
354 }
355
356 if ( !is_null( $params['tag'] ) ) {
357 $this->addTables( 'change_tag' );
358 $this->addJoinConds( [ 'change_tag' => [ 'JOIN', [ 'rc_id=ct_rc_id' ] ] ] );
359 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
360 try {
361 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
362 } catch ( NameTableAccessException $exception ) {
363 // Return nothing.
364 $this->addWhere( '1=0' );
365 }
366 }
367
368 // Paranoia: avoid brute force searches (T19342)
369 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
370 if ( !$user->isAllowed( 'deletedhistory' ) ) {
371 $bitmask = RevisionRecord::DELETED_USER;
372 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
373 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
374 } else {
375 $bitmask = 0;
376 }
377 if ( $bitmask ) {
378 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
379 }
380 }
381 if ( $this->getRequest()->getCheck( 'namespace' ) ) {
382 // LogPage::DELETED_ACTION hides the affected page, too.
383 if ( !$user->isAllowed( 'deletedhistory' ) ) {
384 $bitmask = LogPage::DELETED_ACTION;
385 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
386 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
387 } else {
388 $bitmask = 0;
389 }
390 if ( $bitmask ) {
391 $this->addWhere( $this->getDB()->makeList( [
392 'rc_type != ' . RC_LOG,
393 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
394 ], LIST_OR ) );
395 }
396 }
397
398 $this->token = $params['token'];
399
400 if ( $this->fld_comment || $this->fld_parsedcomment || $this->token ) {
401 $this->commentStore = CommentStore::getStore();
402 $commentQuery = $this->commentStore->getJoin( 'rc_comment' );
403 $this->addTables( $commentQuery['tables'] );
404 $this->addFields( $commentQuery['fields'] );
405 $this->addJoinConds( $commentQuery['joins'] );
406 }
407
408 $this->addOption( 'LIMIT', $params['limit'] + 1 );
409
410 $hookData = [];
411 $count = 0;
412 /* Perform the actual query. */
413 $res = $this->select( __METHOD__, [], $hookData );
414
415 $revids = [];
416 $titles = [];
417
418 $result = $this->getResult();
419
420 /* Iterate through the rows, adding data extracted from them to our query result. */
421 foreach ( $res as $row ) {
422 if ( $count === 0 && $resultPageSet !== null ) {
423 // Set the non-continue since the list of recentchanges is
424 // prone to having entries added at the start frequently.
425 $this->getContinuationManager()->addGeneratorNonContinueParam(
426 $this, 'continue', "$row->rc_timestamp|$row->rc_id"
427 );
428 }
429 if ( ++$count > $params['limit'] ) {
430 // We've reached the one extra which shows that there are
431 // additional pages to be had. Stop here...
432 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
433 break;
434 }
435
436 if ( is_null( $resultPageSet ) ) {
437 /* Extract the data from a single row. */
438 $vals = $this->extractRowInfo( $row );
439
440 /* Add that row's data to our final output. */
441 $fit = $this->processRow( $row, $vals, $hookData ) &&
442 $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
443 if ( !$fit ) {
444 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
445 break;
446 }
447 } elseif ( $params['generaterevisions'] ) {
448 $revid = (int)$row->rc_this_oldid;
449 if ( $revid > 0 ) {
450 $revids[] = $revid;
451 }
452 } else {
453 $titles[] = Title::makeTitle( $row->rc_namespace, $row->rc_title );
454 }
455 }
456
457 if ( is_null( $resultPageSet ) ) {
458 /* Format the result */
459 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'rc' );
460 } elseif ( $params['generaterevisions'] ) {
461 $resultPageSet->populateFromRevisionIDs( $revids );
462 } else {
463 $resultPageSet->populateFromTitles( $titles );
464 }
465 }
466
467 /**
468 * Extracts from a single sql row the data needed to describe one recent change.
469 *
470 * @param stdClass $row The row from which to extract the data.
471 * @return array An array mapping strings (descriptors) to their respective string values.
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 = (int)$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'] = (int)$row->rc_cur_id;
500 $vals['revid'] = (int)$row->rc_this_oldid;
501 $vals['old_revid'] = (int)$row->rc_last_oldid;
502 }
503 }
504 }
505
506 if ( $this->fld_ids ) {
507 $vals['rcid'] = (int)$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'] = (int)$row->rc_old_len;
541 $vals['newlen'] = (int)$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'] = (int)$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 /**
640 * @param array $flagsArray flipped array (string flags are keys)
641 * @return bool
642 */
643 private function includesPatrollingFlags( array $flagsArray ) {
644 return isset( $flagsArray['patrolled'] ) ||
645 isset( $flagsArray['!patrolled'] ) ||
646 isset( $flagsArray['unpatrolled'] ) ||
647 isset( $flagsArray['autopatrolled'] ) ||
648 isset( $flagsArray['!autopatrolled'] );
649 }
650
651 public function getCacheMode( $params ) {
652 if ( isset( $params['show'] ) &&
653 $this->includesPatrollingFlags( array_flip( $params['show'] ) )
654 ) {
655 return 'private';
656 }
657 if ( isset( $params['token'] ) ) {
658 return 'private';
659 }
660 if ( $this->userCanSeeRevDel() ) {
661 return 'private';
662 }
663 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
664 // formatComment() calls wfMessage() among other things
665 return 'anon-public-user-private';
666 }
667
668 return 'public';
669 }
670
671 public function getAllowedParams() {
672 return [
673 'start' => [
674 ApiBase::PARAM_TYPE => 'timestamp'
675 ],
676 'end' => [
677 ApiBase::PARAM_TYPE => 'timestamp'
678 ],
679 'dir' => [
680 ApiBase::PARAM_DFLT => 'older',
681 ApiBase::PARAM_TYPE => [
682 'newer',
683 'older'
684 ],
685 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
686 ],
687 'namespace' => [
688 ApiBase::PARAM_ISMULTI => true,
689 ApiBase::PARAM_TYPE => 'namespace',
690 ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
691 ],
692 'user' => [
693 ApiBase::PARAM_TYPE => 'user'
694 ],
695 'excludeuser' => [
696 ApiBase::PARAM_TYPE => 'user'
697 ],
698 'tag' => null,
699 'prop' => [
700 ApiBase::PARAM_ISMULTI => true,
701 ApiBase::PARAM_DFLT => 'title|timestamp|ids',
702 ApiBase::PARAM_TYPE => [
703 'user',
704 'userid',
705 'comment',
706 'parsedcomment',
707 'flags',
708 'timestamp',
709 'title',
710 'ids',
711 'sizes',
712 'redirect',
713 'patrolled',
714 'loginfo',
715 'tags',
716 'sha1',
717 ],
718 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
719 ],
720 'token' => [
721 ApiBase::PARAM_DEPRECATED => true,
722 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
723 ApiBase::PARAM_ISMULTI => true
724 ],
725 'show' => [
726 ApiBase::PARAM_ISMULTI => true,
727 ApiBase::PARAM_TYPE => [
728 'minor',
729 '!minor',
730 'bot',
731 '!bot',
732 'anon',
733 '!anon',
734 'redirect',
735 '!redirect',
736 'patrolled',
737 '!patrolled',
738 'unpatrolled',
739 'autopatrolled',
740 '!autopatrolled',
741 ]
742 ],
743 'limit' => [
744 ApiBase::PARAM_DFLT => 10,
745 ApiBase::PARAM_TYPE => 'limit',
746 ApiBase::PARAM_MIN => 1,
747 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
748 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
749 ],
750 'type' => [
751 ApiBase::PARAM_DFLT => 'edit|new|log|categorize',
752 ApiBase::PARAM_ISMULTI => true,
753 ApiBase::PARAM_TYPE => RecentChange::getChangeTypes()
754 ],
755 'toponly' => false,
756 'title' => null,
757 'continue' => [
758 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
759 ],
760 'generaterevisions' => false,
761 ];
762 }
763
764 protected function getExamplesMessages() {
765 return [
766 'action=query&list=recentchanges'
767 => 'apihelp-query+recentchanges-example-simple',
768 'action=query&generator=recentchanges&grcshow=!patrolled&prop=info'
769 => 'apihelp-query+recentchanges-example-generator',
770 ];
771 }
772
773 public function getHelpUrls() {
774 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Recentchanges';
775 }
776 }