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