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