Merge "Link to existing login help page by default from helplogin-url"
[lhc/web/wiklou.git] / includes / api / ApiQueryRecentChanges.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 19, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
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( $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'rc' );
37 }
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 = array();
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 * @return array array(tokenname => function)
51 */
52 protected function getTokenFunctions() {
53 // Don't call the hooks twice
54 if ( isset( $this->tokenFunctions ) ) {
55 return $this->tokenFunctions;
56 }
57
58 // If we're in JSON callback mode, no tokens can be obtained
59 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
60 return array();
61 }
62
63 $this->tokenFunctions = array(
64 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
65 );
66 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions ) );
67
68 return $this->tokenFunctions;
69 }
70
71 /**
72 * @param $pageid
73 * @param $title
74 * @param $rc RecentChange (optional)
75 * @return bool|string
76 */
77 public static function getPatrolToken( $pageid, $title, $rc = null ) {
78 global $wgUser;
79
80 $validTokenUser = false;
81
82 if ( $rc ) {
83 if ( ( $wgUser->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT ) ||
84 ( $wgUser->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW )
85 ) {
86 $validTokenUser = true;
87 }
88 } elseif ( $wgUser->useRCPatrol() || $wgUser->useNPPatrol() ) {
89 $validTokenUser = true;
90 }
91
92 if ( $validTokenUser ) {
93 // The patrol token is always the same, let's exploit that
94 static $cachedPatrolToken = null;
95
96 if ( is_null( $cachedPatrolToken ) ) {
97 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
98 }
99
100 return $cachedPatrolToken;
101 }
102
103 return false;
104 }
105
106 /**
107 * Sets internal state to include the desired properties in the output.
108 * @param array $prop associative array of properties, only keys are used here
109 */
110 public function initProperties( $prop ) {
111 $this->fld_comment = isset( $prop['comment'] );
112 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
113 $this->fld_user = isset( $prop['user'] );
114 $this->fld_userid = isset( $prop['userid'] );
115 $this->fld_flags = isset( $prop['flags'] );
116 $this->fld_timestamp = isset( $prop['timestamp'] );
117 $this->fld_title = isset( $prop['title'] );
118 $this->fld_ids = isset( $prop['ids'] );
119 $this->fld_sizes = isset( $prop['sizes'] );
120 $this->fld_redirect = isset( $prop['redirect'] );
121 $this->fld_patrolled = isset( $prop['patrolled'] );
122 $this->fld_loginfo = isset( $prop['loginfo'] );
123 $this->fld_tags = isset( $prop['tags'] );
124 $this->fld_sha1 = isset( $prop['sha1'] );
125 }
126
127 public function execute() {
128 $this->run();
129 }
130
131 public function executeGenerator( $resultPageSet ) {
132 $this->run( $resultPageSet );
133 }
134
135 /**
136 * Generates and outputs the result of this query based upon the provided parameters.
137 *
138 * @param $resultPageSet ApiPageSet
139 */
140 public function run( $resultPageSet = null ) {
141 $user = $this->getUser();
142 /* Get the parameters of the request. */
143 $params = $this->extractRequestParams();
144
145 /* Build our basic query. Namely, something along the lines of:
146 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
147 * AND rc_timestamp < $end AND rc_namespace = $namespace
148 */
149 $this->addTables( 'recentchanges' );
150 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
151 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
152
153 if ( !is_null( $params['continue'] ) ) {
154 $cont = explode( '|', $params['continue'] );
155 if ( count( $cont ) != 2 ) {
156 $this->dieUsage( 'Invalid continue param. You should pass the ' .
157 'original value returned by the previous query', '_badcontinue' );
158 }
159
160 $timestamp = $this->getDB()->addQuotes( wfTimestamp( TS_MW, $cont[0] ) );
161 $id = intval( $cont[1] );
162 $op = $params['dir'] === 'older' ? '<' : '>';
163
164 $this->addWhere(
165 "rc_timestamp $op $timestamp OR " .
166 "(rc_timestamp = $timestamp AND " .
167 "rc_id $op= $id)"
168 );
169 }
170
171 $order = $params['dir'] === 'older' ? 'DESC' : 'ASC';
172 $this->addOption( 'ORDER BY', array(
173 "rc_timestamp $order",
174 "rc_id $order",
175 ) );
176
177 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
178
179 if ( !is_null( $params['type'] ) ) {
180 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
181 }
182
183 if ( !is_null( $params['show'] ) ) {
184 $show = array_flip( $params['show'] );
185
186 /* Check for conflicting parameters. */
187 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
188 || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
189 || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
190 || ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
191 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
192 || ( isset( $show['patrolled'] ) && isset( $show['unpatrolled'] ) )
193 || ( isset( $show['!patrolled'] ) && isset( $show['unpatrolled'] ) )
194 ) {
195 $this->dieUsageMsg( 'show' );
196 }
197
198 // Check permissions
199 if ( isset( $show['patrolled'] )
200 || isset( $show['!patrolled'] )
201 || isset( $show['unpatrolled'] )
202 ) {
203 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
204 $this->dieUsage(
205 'You need the patrol right to request the patrolled flag',
206 'permissiondenied'
207 );
208 }
209 }
210
211 /* Add additional conditions to query depending upon parameters. */
212 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
213 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
214 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
215 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
216 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
217 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
218 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
219 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
220 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
221
222 if ( isset( $show['unpatrolled'] ) ) {
223 // See ChangesList:isUnpatrolled
224 if ( $user->useRCPatrol() ) {
225 $this->addWhere( 'rc_patrolled = 0' );
226 } elseif ( $user->useNPPatrol() ) {
227 $this->addWhere( 'rc_patrolled = 0' );
228 $this->addWhereFld( 'rc_type', RC_NEW );
229 }
230 }
231
232 // Don't throw log entries out the window here
233 $this->addWhereIf(
234 'page_is_redirect = 0 OR page_is_redirect IS NULL',
235 isset( $show['!redirect'] )
236 );
237 }
238
239 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
240 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
241 }
242
243 if ( !is_null( $params['user'] ) ) {
244 $this->addWhereFld( 'rc_user_text', $params['user'] );
245 $index['recentchanges'] = 'rc_user_text';
246 }
247
248 if ( !is_null( $params['excludeuser'] ) ) {
249 // We don't use the rc_user_text index here because
250 // * it would require us to sort by rc_user_text before rc_timestamp
251 // * the != condition doesn't throw out too many rows anyway
252 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
253 }
254
255 /* Add the fields we're concerned with to our query. */
256 $this->addFields( array(
257 'rc_timestamp',
258 'rc_namespace',
259 'rc_title',
260 'rc_cur_id',
261 'rc_type',
262 'rc_deleted'
263 ) );
264
265 $showRedirects = false;
266 /* Determine what properties we need to display. */
267 if ( !is_null( $params['prop'] ) ) {
268 $prop = array_flip( $params['prop'] );
269
270 /* Set up internal members based upon params. */
271 $this->initProperties( $prop );
272
273 if ( $this->fld_patrolled && !$user->useRCPatrol() && !$user->useNPPatrol() ) {
274 $this->dieUsage(
275 'You need the patrol right to request the patrolled flag',
276 'permissiondenied'
277 );
278 }
279
280 $this->addFields( 'rc_id' );
281 /* Add fields to our query if they are specified as a needed parameter. */
282 $this->addFieldsIf( array( 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids );
283 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
284 $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
285 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
286 $this->addFieldsIf( array( 'rc_minor', 'rc_type', 'rc_bot' ), $this->fld_flags );
287 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes );
288 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
289 $this->addFieldsIf(
290 array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ),
291 $this->fld_loginfo
292 );
293 $showRedirects = $this->fld_redirect || isset( $show['redirect'] )
294 || isset( $show['!redirect'] );
295 }
296
297 if ( $this->fld_tags ) {
298 $this->addTables( 'tag_summary' );
299 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
300 $this->addFields( 'ts_tags' );
301 }
302
303 if ( $this->fld_sha1 ) {
304 $this->addTables( 'revision' );
305 $this->addJoinConds( array( 'revision' => array( 'LEFT JOIN',
306 array( 'rc_this_oldid=rev_id' ) ) ) );
307 $this->addFields( array( 'rev_sha1', 'rev_deleted' ) );
308 }
309
310 if ( $params['toponly'] || $showRedirects ) {
311 $this->addTables( 'page' );
312 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN',
313 array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
314 $this->addFields( 'page_is_redirect' );
315
316 if ( $params['toponly'] ) {
317 $this->addWhere( 'rc_this_oldid = page_latest' );
318 }
319 }
320
321 if ( !is_null( $params['tag'] ) ) {
322 $this->addTables( 'change_tag' );
323 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
324 $this->addWhereFld( 'ct_tag', $params['tag'] );
325 }
326
327 // Paranoia: avoid brute force searches (bug 17342)
328 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
329 if ( !$user->isAllowed( 'deletedhistory' ) ) {
330 $bitmask = Revision::DELETED_USER;
331 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
332 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
333 } else {
334 $bitmask = 0;
335 }
336 if ( $bitmask ) {
337 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
338 }
339 }
340 if ( $this->getRequest()->getCheck( 'namespace' ) ) {
341 // LogPage::DELETED_ACTION hides the affected page, too.
342 if ( !$user->isAllowed( 'deletedhistory' ) ) {
343 $bitmask = LogPage::DELETED_ACTION;
344 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
345 $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
346 } else {
347 $bitmask = 0;
348 }
349 if ( $bitmask ) {
350 $this->addWhere( $this->getDB()->makeList( array(
351 'rc_type != ' . RC_LOG,
352 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
353 ), LIST_OR ) );
354 }
355 }
356
357 $this->token = $params['token'];
358 $this->addOption( 'LIMIT', $params['limit'] + 1 );
359 $this->addOption( 'USE INDEX', $index );
360
361 $count = 0;
362 /* Perform the actual query. */
363 $res = $this->select( __METHOD__ );
364
365 $titles = array();
366
367 $result = $this->getResult();
368
369 /* Iterate through the rows, adding data extracted from them to our query result. */
370 foreach ( $res as $row ) {
371 if ( ++$count > $params['limit'] ) {
372 // We've reached the one extra which shows that there are
373 // additional pages to be had. Stop here...
374 $this->setContinueEnumParameter(
375 'continue',
376 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) . '|' . $row->rc_id
377 );
378 break;
379 }
380
381 if ( is_null( $resultPageSet ) ) {
382 /* Extract the data from a single row. */
383 $vals = $this->extractRowInfo( $row );
384
385 /* Add that row's data to our final output. */
386 if ( !$vals ) {
387 continue;
388 }
389 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
390 if ( !$fit ) {
391 $this->setContinueEnumParameter(
392 'continue',
393 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) . '|' . $row->rc_id
394 );
395 break;
396 }
397 } else {
398 $titles[] = Title::makeTitle( $row->rc_namespace, $row->rc_title );
399 }
400 }
401
402 if ( is_null( $resultPageSet ) ) {
403 /* Format the result */
404 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
405 } else {
406 $resultPageSet->populateFromTitles( $titles );
407 }
408 }
409
410 /**
411 * Extracts from a single sql row the data needed to describe one recent change.
412 *
413 * @param mixed $row The row from which to extract the data.
414 * @return array An array mapping strings (descriptors) to their respective string values.
415 * @access public
416 */
417 public function extractRowInfo( $row ) {
418 /* Determine the title of the page that has been changed. */
419 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
420 $user = $this->getUser();
421
422 /* Our output data. */
423 $vals = array();
424
425 $type = intval( $row->rc_type );
426
427 /* Determine what kind of change this was. */
428 switch ( $type ) {
429 case RC_EDIT:
430 $vals['type'] = 'edit';
431 break;
432 case RC_NEW:
433 $vals['type'] = 'new';
434 break;
435 case RC_MOVE:
436 $vals['type'] = 'move';
437 break;
438 case RC_LOG:
439 $vals['type'] = 'log';
440 break;
441 case RC_EXTERNAL:
442 $vals['type'] = 'external';
443 break;
444 case RC_MOVE_OVER_REDIRECT:
445 $vals['type'] = 'move over redirect';
446 break;
447 default:
448 $vals['type'] = $type;
449 }
450
451 $anyHidden = false;
452
453 /* Create a new entry in the result for the title. */
454 if ( $this->fld_title || $this->fld_ids ) {
455 if ( $type === RC_LOG && ( $row->rc_deleted & LogPage::DELETED_ACTION ) ) {
456 $vals['actionhidden'] = '';
457 $anyHidden = true;
458 }
459 if ( $type !== RC_LOG ||
460 LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user )
461 ) {
462 if ( $this->fld_title ) {
463 ApiQueryBase::addTitleInfo( $vals, $title );
464 }
465 if ( $this->fld_ids ) {
466 $vals['pageid'] = intval( $row->rc_cur_id );
467 $vals['revid'] = intval( $row->rc_this_oldid );
468 $vals['old_revid'] = intval( $row->rc_last_oldid );
469 }
470 }
471 }
472
473 if ( $this->fld_ids ) {
474 $vals['rcid'] = intval( $row->rc_id );
475 }
476
477 /* Add user data and 'anon' flag, if user is anonymous. */
478 if ( $this->fld_user || $this->fld_userid ) {
479 if ( $row->rc_deleted & Revision::DELETED_USER ) {
480 $vals['userhidden'] = '';
481 $anyHidden = true;
482 }
483 if ( Revision::userCanBitfield( $row->rc_deleted, Revision::DELETED_USER, $user ) ) {
484 if ( $this->fld_user ) {
485 $vals['user'] = $row->rc_user_text;
486 }
487
488 if ( $this->fld_userid ) {
489 $vals['userid'] = $row->rc_user;
490 }
491
492 if ( !$row->rc_user ) {
493 $vals['anon'] = '';
494 }
495 }
496 }
497
498 /* Add flags, such as new, minor, bot. */
499 if ( $this->fld_flags ) {
500 if ( $row->rc_bot ) {
501 $vals['bot'] = '';
502 }
503 if ( $row->rc_type == RC_NEW ) {
504 $vals['new'] = '';
505 }
506 if ( $row->rc_minor ) {
507 $vals['minor'] = '';
508 }
509 }
510
511 /* Add sizes of each revision. (Only available on 1.10+) */
512 if ( $this->fld_sizes ) {
513 $vals['oldlen'] = intval( $row->rc_old_len );
514 $vals['newlen'] = intval( $row->rc_new_len );
515 }
516
517 /* Add the timestamp. */
518 if ( $this->fld_timestamp ) {
519 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
520 }
521
522 /* Add edit summary / log summary. */
523 if ( $this->fld_comment || $this->fld_parsedcomment ) {
524 if ( $row->rc_deleted & Revision::DELETED_COMMENT ) {
525 $vals['commenthidden'] = '';
526 $anyHidden = true;
527 }
528 if ( Revision::userCanBitfield( $row->rc_deleted, Revision::DELETED_COMMENT, $user ) ) {
529 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
530 $vals['comment'] = $row->rc_comment;
531 }
532
533 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
534 $vals['parsedcomment'] = Linker::formatComment( $row->rc_comment, $title );
535 }
536 }
537 }
538
539 if ( $this->fld_redirect ) {
540 if ( $row->page_is_redirect ) {
541 $vals['redirect'] = '';
542 }
543 }
544
545 /* Add the patrolled flag */
546 if ( $this->fld_patrolled && $row->rc_patrolled == 1 ) {
547 $vals['patrolled'] = '';
548 }
549
550 if ( $this->fld_patrolled && ChangesList::isUnpatrolled( $row, $user ) ) {
551 $vals['unpatrolled'] = '';
552 }
553
554 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
555 if ( $row->rc_deleted & LogPage::DELETED_ACTION ) {
556 $vals['actionhidden'] = '';
557 $anyHidden = true;
558 }
559 if ( LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user ) ) {
560 $vals['logid'] = intval( $row->rc_logid );
561 $vals['logtype'] = $row->rc_log_type;
562 $vals['logaction'] = $row->rc_log_action;
563 $logEntry = DatabaseLogEntry::newFromRow( (array)$row );
564 ApiQueryLogEvents::addLogParams(
565 $this->getResult(),
566 $vals,
567 $logEntry->getParameters(),
568 $logEntry->getType(),
569 $logEntry->getSubtype(),
570 $logEntry->getTimestamp()
571 );
572 }
573 }
574
575 if ( $this->fld_tags ) {
576 if ( $row->ts_tags ) {
577 $tags = explode( ',', $row->ts_tags );
578 $this->getResult()->setIndexedTagName( $tags, 'tag' );
579 $vals['tags'] = $tags;
580 } else {
581 $vals['tags'] = array();
582 }
583 }
584
585 if ( $this->fld_sha1 && $row->rev_sha1 !== null ) {
586 if ( $row->rev_deleted & Revision::DELETED_TEXT ) {
587 $vals['sha1hidden'] = '';
588 $anyHidden = true;
589 }
590 if ( Revision::userCanBitfield( $row->rev_deleted, Revision::DELETED_TEXT, $user ) ) {
591 if ( $row->rev_sha1 !== '' ) {
592 $vals['sha1'] = wfBaseConvert( $row->rev_sha1, 36, 16, 40 );
593 } else {
594 $vals['sha1'] = '';
595 }
596 }
597 }
598
599 if ( !is_null( $this->token ) ) {
600 $tokenFunctions = $this->getTokenFunctions();
601 foreach ( $this->token as $t ) {
602 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id,
603 $title, RecentChange::newFromRow( $row ) );
604 if ( $val === false ) {
605 $this->setWarning( "Action '$t' is not allowed for the current user" );
606 } else {
607 $vals[$t . 'token'] = $val;
608 }
609 }
610 }
611
612 if ( $anyHidden && ( $row->rc_deleted & Revision::DELETED_RESTRICTED ) ) {
613 $vals['suppressed'] = '';
614 }
615
616 return $vals;
617 }
618
619 private function parseRCType( $type ) {
620 if ( is_array( $type ) ) {
621 $retval = array();
622 foreach ( $type as $t ) {
623 $retval[] = $this->parseRCType( $t );
624 }
625
626 return $retval;
627 }
628
629 switch ( $type ) {
630 case 'edit':
631 return RC_EDIT;
632 case 'new':
633 return RC_NEW;
634 case 'log':
635 return RC_LOG;
636 case 'external':
637 return RC_EXTERNAL;
638 default:
639 ApiBase::dieDebug( __METHOD__, "Unknown type '$type'" );
640 }
641 }
642
643 public function getCacheMode( $params ) {
644 if ( isset( $params['show'] ) ) {
645 foreach ( $params['show'] as $show ) {
646 if ( $show === 'patrolled' || $show === '!patrolled' ) {
647 return 'private';
648 }
649 }
650 }
651 if ( isset( $params['token'] ) ) {
652 return 'private';
653 }
654 if ( $this->userCanSeeRevDel() ) {
655 return 'private';
656 }
657 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
658 // formatComment() calls wfMessage() among other things
659 return 'anon-public-user-private';
660 }
661
662 return 'public';
663 }
664
665 public function getAllowedParams() {
666 return array(
667 'start' => array(
668 ApiBase::PARAM_TYPE => 'timestamp'
669 ),
670 'end' => array(
671 ApiBase::PARAM_TYPE => 'timestamp'
672 ),
673 'dir' => array(
674 ApiBase::PARAM_DFLT => 'older',
675 ApiBase::PARAM_TYPE => array(
676 'newer',
677 'older'
678 )
679 ),
680 'namespace' => array(
681 ApiBase::PARAM_ISMULTI => true,
682 ApiBase::PARAM_TYPE => 'namespace'
683 ),
684 'user' => array(
685 ApiBase::PARAM_TYPE => 'user'
686 ),
687 'excludeuser' => array(
688 ApiBase::PARAM_TYPE => 'user'
689 ),
690 'tag' => null,
691 'prop' => array(
692 ApiBase::PARAM_ISMULTI => true,
693 ApiBase::PARAM_DFLT => 'title|timestamp|ids',
694 ApiBase::PARAM_TYPE => array(
695 'user',
696 'userid',
697 'comment',
698 'parsedcomment',
699 'flags',
700 'timestamp',
701 'title',
702 'ids',
703 'sizes',
704 'redirect',
705 'patrolled',
706 'loginfo',
707 'tags',
708 'sha1',
709 )
710 ),
711 'token' => array(
712 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
713 ApiBase::PARAM_ISMULTI => true
714 ),
715 'show' => array(
716 ApiBase::PARAM_ISMULTI => true,
717 ApiBase::PARAM_TYPE => array(
718 'minor',
719 '!minor',
720 'bot',
721 '!bot',
722 'anon',
723 '!anon',
724 'redirect',
725 '!redirect',
726 'patrolled',
727 '!patrolled',
728 'unpatrolled'
729 )
730 ),
731 'limit' => array(
732 ApiBase::PARAM_DFLT => 10,
733 ApiBase::PARAM_TYPE => 'limit',
734 ApiBase::PARAM_MIN => 1,
735 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
736 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
737 ),
738 'type' => array(
739 ApiBase::PARAM_ISMULTI => true,
740 ApiBase::PARAM_TYPE => array(
741 'edit',
742 'external',
743 'new',
744 'log'
745 )
746 ),
747 'toponly' => false,
748 'continue' => null,
749 );
750 }
751
752 public function getParamDescription() {
753 $p = $this->getModulePrefix();
754
755 return array(
756 'start' => 'The timestamp to start enumerating from',
757 'end' => 'The timestamp to end enumerating',
758 'dir' => $this->getDirectionDescription( $p ),
759 'namespace' => 'Filter log entries to only this namespace(s)',
760 'user' => 'Only list changes by this user',
761 'excludeuser' => 'Don\'t list changes by this user',
762 'prop' => array(
763 'Include additional pieces of information',
764 ' user - Adds the user responsible for the edit and tags if they are an IP',
765 ' userid - Adds the user id responsible for the edit',
766 ' comment - Adds the comment for the edit',
767 ' parsedcomment - Adds the parsed comment for the edit',
768 ' flags - Adds flags for the edit',
769 ' timestamp - Adds timestamp of the edit',
770 ' title - Adds the page title of the edit',
771 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
772 ' sizes - Adds the new and old page length in bytes',
773 ' redirect - Tags edit if page is a redirect',
774 ' patrolled - Tags patrollable edits as being patrolled or unpatrolled',
775 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
776 ' tags - Lists tags for the entry',
777 ' sha1 - Adds the content checksum for entries associated with a revision',
778 ),
779 'token' => 'Which tokens to obtain for each change',
780 'show' => array(
781 'Show only items that meet this criteria.',
782 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
783 ),
784 'type' => 'Which types of changes to show',
785 'limit' => 'How many total changes to return',
786 'tag' => 'Only list changes tagged with this tag',
787 'toponly' => 'Only list changes which are the latest revision',
788 'continue' => 'When more results are available, use this to continue',
789 );
790 }
791
792 public function getResultProperties() {
793 global $wgLogTypes;
794 $props = array(
795 '' => array(
796 'type' => array(
797 ApiBase::PROP_TYPE => array(
798 'edit',
799 'new',
800 'move',
801 'log',
802 'move over redirect'
803 )
804 )
805 ),
806 'title' => array(
807 'ns' => 'namespace',
808 'title' => 'string',
809 'new_ns' => array(
810 ApiBase::PROP_TYPE => 'namespace',
811 ApiBase::PROP_NULLABLE => true
812 ),
813 'new_title' => array(
814 ApiBase::PROP_TYPE => 'string',
815 ApiBase::PROP_NULLABLE => true
816 )
817 ),
818 'ids' => array(
819 'rcid' => 'integer',
820 'pageid' => 'integer',
821 'revid' => 'integer',
822 'old_revid' => 'integer'
823 ),
824 'user' => array(
825 'user' => 'string',
826 'anon' => 'boolean'
827 ),
828 'userid' => array(
829 'userid' => 'integer',
830 'anon' => 'boolean'
831 ),
832 'flags' => array(
833 'bot' => 'boolean',
834 'new' => 'boolean',
835 'minor' => 'boolean'
836 ),
837 'sizes' => array(
838 'oldlen' => 'integer',
839 'newlen' => 'integer'
840 ),
841 'timestamp' => array(
842 'timestamp' => 'timestamp'
843 ),
844 'comment' => array(
845 'comment' => array(
846 ApiBase::PROP_TYPE => 'string',
847 ApiBase::PROP_NULLABLE => true
848 )
849 ),
850 'parsedcomment' => array(
851 'parsedcomment' => array(
852 ApiBase::PROP_TYPE => 'string',
853 ApiBase::PROP_NULLABLE => true
854 )
855 ),
856 'redirect' => array(
857 'redirect' => 'boolean'
858 ),
859 'patrolled' => array(
860 'patrolled' => 'boolean',
861 'unpatrolled' => 'boolean'
862 ),
863 'loginfo' => array(
864 'logid' => array(
865 ApiBase::PROP_TYPE => 'integer',
866 ApiBase::PROP_NULLABLE => true
867 ),
868 'logtype' => array(
869 ApiBase::PROP_TYPE => $wgLogTypes,
870 ApiBase::PROP_NULLABLE => true
871 ),
872 'logaction' => array(
873 ApiBase::PROP_TYPE => 'string',
874 ApiBase::PROP_NULLABLE => true
875 )
876 ),
877 'sha1' => array(
878 'sha1' => array(
879 ApiBase::PROP_TYPE => 'string',
880 ApiBase::PROP_NULLABLE => true
881 ),
882 'sha1hidden' => array(
883 ApiBase::PROP_TYPE => 'boolean',
884 ApiBase::PROP_NULLABLE => true
885 ),
886 ),
887 );
888
889 self::addTokenProperties( $props, $this->getTokenFunctions() );
890
891 return $props;
892 }
893
894 public function getDescription() {
895 return 'Enumerate recent changes.';
896 }
897
898 public function getPossibleErrors() {
899 return array_merge( parent::getPossibleErrors(), array(
900 array( 'show' ),
901 array(
902 'code' => 'permissiondenied',
903 'info' => 'You need the patrol right to request the patrolled flag'
904 ),
905 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
906 ) );
907 }
908
909 public function getExamples() {
910 return array(
911 'api.php?action=query&list=recentchanges'
912 );
913 }
914
915 public function getHelpUrls() {
916 return 'https://www.mediawiki.org/wiki/API:Recentchanges';
917 }
918 }