More parameter documentation
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * A query action to enumerate the recent changes that were done to the wiki.
34 * Various filters are supported.
35 *
36 * @ingroup API
37 */
38 class ApiQueryRecentChanges extends ApiQueryGeneratorBase {
39
40 public function __construct( $query, $moduleName ) {
41 parent::__construct( $query, $moduleName, 'rc' );
42 }
43
44 private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
45 $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false,
46 $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false, $fld_tags = false;
47
48 private $tokenFunctions;
49
50 /**
51 * Get an array mapping token names to their handler functions.
52 * The prototype for a token function is func($pageid, $title, $rc)
53 * it should return a token or false (permission denied)
54 * @return array(tokenname => function)
55 */
56 protected function getTokenFunctions() {
57 // Don't call the hooks twice
58 if ( isset( $this->tokenFunctions ) ) {
59 return $this->tokenFunctions;
60 }
61
62 // If we're in JSON callback mode, no tokens can be obtained
63 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
64 return array();
65 }
66
67 $this->tokenFunctions = array(
68 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
69 );
70 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions ) );
71 return $this->tokenFunctions;
72 }
73
74 /**
75 * @static
76 * @param $pageid
77 * @param $title
78 * @param $rc RecentChange
79 * @return bool|String
80 */
81 public static function getPatrolToken( $pageid, $title, $rc ) {
82 global $wgUser;
83 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
84 $rc->getAttribute( 'rc_type' ) != RC_NEW ) )
85 {
86 return false;
87 }
88
89 // The patrol token is always the same, let's exploit that
90 static $cachedPatrolToken = null;
91 if ( is_null( $cachedPatrolToken ) ) {
92 $cachedPatrolToken = $wgUser->editToken( 'patrol' );
93 }
94
95 return $cachedPatrolToken;
96 }
97
98 /**
99 * Sets internal state to include the desired properties in the output.
100 * @param $prop Array associative array of properties, only keys are used here
101 */
102 public function initProperties( $prop ) {
103 $this->fld_comment = isset( $prop['comment'] );
104 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
105 $this->fld_user = isset( $prop['user'] );
106 $this->fld_userid = isset( $prop['userid'] );
107 $this->fld_flags = isset( $prop['flags'] );
108 $this->fld_timestamp = isset( $prop['timestamp'] );
109 $this->fld_title = isset( $prop['title'] );
110 $this->fld_ids = isset( $prop['ids'] );
111 $this->fld_sizes = isset( $prop['sizes'] );
112 $this->fld_redirect = isset( $prop['redirect'] );
113 $this->fld_patrolled = isset( $prop['patrolled'] );
114 $this->fld_loginfo = isset( $prop['loginfo'] );
115 $this->fld_tags = isset( $prop['tags'] );
116 }
117
118 public function execute() {
119 $this->run();
120 }
121
122 public function executeGenerator( $resultPageSet ) {
123 $this->run( $resultPageSet );
124 }
125
126 /**
127 * Generates and outputs the result of this query based upon the provided parameters.
128 *
129 * @param $resultPageSet ApiPageSet
130 */
131 public function run( $resultPageSet = null ) {
132 global $wgUser;
133 /* Get the parameters of the request. */
134 $params = $this->extractRequestParams();
135
136 /* Build our basic query. Namely, something along the lines of:
137 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
138 * AND rc_timestamp < $end AND rc_namespace = $namespace
139 * AND rc_deleted = '0'
140 */
141 $this->addTables( 'recentchanges' );
142 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
143 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
144 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
145 $this->addWhereFld( 'rc_deleted', 0 );
146
147 if ( !is_null( $params['type'] ) ) {
148 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
149 }
150
151 if ( !is_null( $params['show'] ) ) {
152 $show = array_flip( $params['show'] );
153
154 /* Check for conflicting parameters. */
155 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
156 || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
157 || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
158 || ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
159 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
160 )
161 {
162 $this->dieUsageMsg( array( 'show' ) );
163 }
164
165 // Check permissions
166 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
167 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
168 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
169 }
170 }
171
172 /* Add additional conditions to query depending upon parameters. */
173 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
174 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
175 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
176 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
177 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
178 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
179 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
180 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
181 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
182
183 // Don't throw log entries out the window here
184 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
185 }
186
187 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
188 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
189 }
190
191 if ( !is_null( $params['user'] ) ) {
192 $this->addWhereFld( 'rc_user_text', $params['user'] );
193 $index['recentchanges'] = 'rc_user_text';
194 }
195
196 if ( !is_null( $params['excludeuser'] ) ) {
197 // We don't use the rc_user_text index here because
198 // * it would require us to sort by rc_user_text before rc_timestamp
199 // * the != condition doesn't throw out too many rows anyway
200 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
201 }
202
203 /* Add the fields we're concerned with to our query. */
204 $this->addFields( array(
205 'rc_timestamp',
206 'rc_namespace',
207 'rc_title',
208 'rc_cur_id',
209 'rc_type',
210 'rc_moved_to_ns',
211 'rc_moved_to_title',
212 'rc_deleted'
213 ) );
214
215 /* Determine what properties we need to display. */
216 if ( !is_null( $params['prop'] ) ) {
217 $prop = array_flip( $params['prop'] );
218
219 /* Set up internal members based upon params. */
220 $this->initProperties( $prop );
221
222 if ( $this->fld_patrolled && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
223 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
224 }
225
226 /* Add fields to our query if they are specified as a needed parameter. */
227 $this->addFieldsIf( 'rc_id', $this->fld_ids );
228 $this->addFieldsIf( 'rc_this_oldid', $this->fld_ids );
229 $this->addFieldsIf( 'rc_last_oldid', $this->fld_ids );
230 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
231 $this->addFieldsIf( 'rc_user', $this->fld_user );
232 $this->addFieldsIf( 'rc_user_text', $this->fld_user || $this->fld_userid );
233 $this->addFieldsIf( 'rc_minor', $this->fld_flags );
234 $this->addFieldsIf( 'rc_bot', $this->fld_flags );
235 $this->addFieldsIf( 'rc_new', $this->fld_flags );
236 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
237 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
238 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
239 $this->addFieldsIf( 'rc_logid', $this->fld_loginfo );
240 $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo );
241 $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo );
242 $this->addFieldsIf( 'rc_params', $this->fld_loginfo );
243 if ( $this->fld_redirect || isset( $show['redirect'] ) || isset( $show['!redirect'] ) ) {
244 $this->addTables( 'page' );
245 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
246 $this->addFields( 'page_is_redirect' );
247 }
248 }
249
250 if ( $this->fld_tags ) {
251 $this->addTables( 'tag_summary' );
252 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
253 $this->addFields( 'ts_tags' );
254 }
255
256 if ( !is_null( $params['tag'] ) ) {
257 $this->addTables( 'change_tag' );
258 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
259 $this->addWhereFld( 'ct_tag' , $params['tag'] );
260 global $wgOldChangeTagsIndex;
261 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
262 }
263
264 $this->token = $params['token'];
265 $this->addOption( 'LIMIT', $params['limit'] + 1 );
266 $this->addOption( 'USE INDEX', $index );
267
268 $count = 0;
269 /* Perform the actual query. */
270 $res = $this->select( __METHOD__ );
271
272 $titles = array();
273
274 /* Iterate through the rows, adding data extracted from them to our query result. */
275 foreach ( $res as $row ) {
276 if ( ++ $count > $params['limit'] ) {
277 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
278 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
279 break;
280 }
281
282 if ( is_null( $resultPageSet ) ) {
283 /* Extract the data from a single row. */
284 $vals = $this->extractRowInfo( $row );
285
286 /* Add that row's data to our final output. */
287 if ( !$vals ) {
288 continue;
289 }
290 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
291 if ( !$fit ) {
292 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
293 break;
294 }
295 } else {
296 $titles[] = Title::makeTitle( $row->rc_namespace, $row->rc_title );
297 }
298 }
299
300 if ( is_null( $resultPageSet ) ) {
301 /* Format the result */
302 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
303 } else {
304 $resultPageSet->populateFromTitles( $titles );
305 }
306 }
307
308 /**
309 * Extracts from a single sql row the data needed to describe one recent change.
310 *
311 * @param $row The row from which to extract the data.
312 * @return An array mapping strings (descriptors) to their respective string values.
313 * @access public
314 */
315 public function extractRowInfo( $row ) {
316 /* If page was moved somewhere, get the title of the move target. */
317 $movedToTitle = false;
318 if ( isset( $row->rc_moved_to_title ) && $row->rc_moved_to_title !== '' ) {
319 $movedToTitle = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
320 }
321
322 /* Determine the title of the page that has been changed. */
323 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
324
325 /* Our output data. */
326 $vals = array();
327
328 $type = intval( $row->rc_type );
329
330 /* Determine what kind of change this was. */
331 switch ( $type ) {
332 case RC_EDIT:
333 $vals['type'] = 'edit';
334 break;
335 case RC_NEW:
336 $vals['type'] = 'new';
337 break;
338 case RC_MOVE:
339 $vals['type'] = 'move';
340 break;
341 case RC_LOG:
342 $vals['type'] = 'log';
343 break;
344 case RC_MOVE_OVER_REDIRECT:
345 $vals['type'] = 'move over redirect';
346 break;
347 default:
348 $vals['type'] = $type;
349 }
350
351 /* Create a new entry in the result for the title. */
352 if ( $this->fld_title ) {
353 ApiQueryBase::addTitleInfo( $vals, $title );
354 if ( $movedToTitle ) {
355 ApiQueryBase::addTitleInfo( $vals, $movedToTitle, 'new_' );
356 }
357 }
358
359 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
360 if ( $this->fld_ids ) {
361 $vals['rcid'] = intval( $row->rc_id );
362 $vals['pageid'] = intval( $row->rc_cur_id );
363 $vals['revid'] = intval( $row->rc_this_oldid );
364 $vals['old_revid'] = intval( $row->rc_last_oldid );
365 }
366
367 /* Add user data and 'anon' flag, if use is anonymous. */
368 if ( $this->fld_user || $this->fld_userid ) {
369
370 if ( $this->fld_user ) {
371 $vals['user'] = $row->rc_user_text;
372 }
373
374 if ( $this->fld_userid ) {
375 $vals['userid'] = $row->rc_user;
376 }
377
378 if ( !$row->rc_user ) {
379 $vals['anon'] = '';
380 }
381 }
382
383 /* Add flags, such as new, minor, bot. */
384 if ( $this->fld_flags ) {
385 if ( $row->rc_bot ) {
386 $vals['bot'] = '';
387 }
388 if ( $row->rc_new ) {
389 $vals['new'] = '';
390 }
391 if ( $row->rc_minor ) {
392 $vals['minor'] = '';
393 }
394 }
395
396 /* Add sizes of each revision. (Only available on 1.10+) */
397 if ( $this->fld_sizes ) {
398 $vals['oldlen'] = intval( $row->rc_old_len );
399 $vals['newlen'] = intval( $row->rc_new_len );
400 }
401
402 /* Add the timestamp. */
403 if ( $this->fld_timestamp ) {
404 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
405 }
406
407 /* Add edit summary / log summary. */
408 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
409 $vals['comment'] = $row->rc_comment;
410 }
411
412 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
413 global $wgUser;
414 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
415 }
416
417 if ( $this->fld_redirect ) {
418 if ( $row->page_is_redirect ) {
419 $vals['redirect'] = '';
420 }
421 }
422
423 /* Add the patrolled flag */
424 if ( $this->fld_patrolled && $row->rc_patrolled == 1 ) {
425 $vals['patrolled'] = '';
426 }
427
428 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
429 $vals['logid'] = intval( $row->rc_logid );
430 $vals['logtype'] = $row->rc_log_type;
431 $vals['logaction'] = $row->rc_log_action;
432 ApiQueryLogEvents::addLogParams(
433 $this->getResult(),
434 $vals, $row->rc_params,
435 $row->rc_log_type, $row->rc_timestamp
436 );
437 }
438
439 if ( $this->fld_tags ) {
440 if ( $row->ts_tags ) {
441 $tags = explode( ',', $row->ts_tags );
442 $this->getResult()->setIndexedTagName( $tags, 'tag' );
443 $vals['tags'] = $tags;
444 } else {
445 $vals['tags'] = array();
446 }
447 }
448
449 if ( !is_null( $this->token ) ) {
450 $tokenFunctions = $this->getTokenFunctions();
451 foreach ( $this->token as $t ) {
452 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id,
453 $title, RecentChange::newFromRow( $row ) );
454 if ( $val === false ) {
455 $this->setWarning( "Action '$t' is not allowed for the current user" );
456 } else {
457 $vals[$t . 'token'] = $val;
458 }
459 }
460 }
461
462 return $vals;
463 }
464
465 private function parseRCType( $type ) {
466 if ( is_array( $type ) ) {
467 $retval = array();
468 foreach ( $type as $t ) {
469 $retval[] = $this->parseRCType( $t );
470 }
471 return $retval;
472 }
473 switch( $type ) {
474 case 'edit':
475 return RC_EDIT;
476 case 'new':
477 return RC_NEW;
478 case 'log':
479 return RC_LOG;
480 }
481 }
482
483 public function getCacheMode( $params ) {
484 if ( isset( $params['show'] ) ) {
485 foreach ( $params['show'] as $show ) {
486 if ( $show === 'patrolled' || $show === '!patrolled' ) {
487 return 'private';
488 }
489 }
490 }
491 if ( isset( $params['token'] ) ) {
492 return 'private';
493 }
494 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
495 // formatComment() calls wfMsg() among other things
496 return 'anon-public-user-private';
497 }
498 return 'public';
499 }
500
501 public function getAllowedParams() {
502 return array(
503 'start' => array(
504 ApiBase::PARAM_TYPE => 'timestamp'
505 ),
506 'end' => array(
507 ApiBase::PARAM_TYPE => 'timestamp'
508 ),
509 'dir' => array(
510 ApiBase::PARAM_DFLT => 'older',
511 ApiBase::PARAM_TYPE => array(
512 'newer',
513 'older'
514 )
515 ),
516 'namespace' => array(
517 ApiBase::PARAM_ISMULTI => true,
518 ApiBase::PARAM_TYPE => 'namespace'
519 ),
520 'user' => array(
521 ApiBase::PARAM_TYPE => 'user'
522 ),
523 'excludeuser' => array(
524 ApiBase::PARAM_TYPE => 'user'
525 ),
526 'tag' => null,
527 'prop' => array(
528 ApiBase::PARAM_ISMULTI => true,
529 ApiBase::PARAM_DFLT => 'title|timestamp|ids',
530 ApiBase::PARAM_TYPE => array(
531 'user',
532 'userid',
533 'comment',
534 'parsedcomment',
535 'flags',
536 'timestamp',
537 'title',
538 'ids',
539 'sizes',
540 'redirect',
541 'patrolled',
542 'loginfo',
543 'tags'
544 )
545 ),
546 'token' => array(
547 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
548 ApiBase::PARAM_ISMULTI => true
549 ),
550 'show' => array(
551 ApiBase::PARAM_ISMULTI => true,
552 ApiBase::PARAM_TYPE => array(
553 'minor',
554 '!minor',
555 'bot',
556 '!bot',
557 'anon',
558 '!anon',
559 'redirect',
560 '!redirect',
561 'patrolled',
562 '!patrolled'
563 )
564 ),
565 'limit' => array(
566 ApiBase::PARAM_DFLT => 10,
567 ApiBase::PARAM_TYPE => 'limit',
568 ApiBase::PARAM_MIN => 1,
569 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
570 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
571 ),
572 'type' => array(
573 ApiBase::PARAM_ISMULTI => true,
574 ApiBase::PARAM_TYPE => array(
575 'edit',
576 'new',
577 'log'
578 )
579 )
580 );
581 }
582
583 public function getParamDescription() {
584 return array(
585 'start' => 'The timestamp to start enumerating from',
586 'end' => 'The timestamp to end enumerating',
587 'dir' => 'In which direction to enumerate',
588 'namespace' => 'Filter log entries to only this namespace(s)',
589 'user' => 'Only list changes by this user',
590 'excludeuser' => 'Don\'t list changes by this user',
591 'prop' => array(
592 'Include additional pieces of information',
593 ' user - Adds the user responsible for the edit and tags if they are an IP',
594 ' userid - Adds the user id responsible for the edit',
595 ' comment - Adds the comment for the edit',
596 ' parsedcomment - Adds the parsed comment for the edit',
597 ' flags - Adds flags for the edit',
598 ' timestamp - Adds timestamp of the edit',
599 ' title - Adds the page title of the edit',
600 ' ids - Adds the page ID, recent changes ID and the new and old revision ID',
601 ' sizes - Adds the new and old page length in bytes',
602 ' redirect - Tags edit if page is a redirect',
603 ' patrolled - Tags edits that have been patrolled',
604 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
605 ' tags - Lists tags for the entry',
606 ),
607 'token' => 'Which tokens to obtain for each change',
608 'show' => array(
609 'Show only items that meet this criteria.',
610 "For example, to see only minor edits done by logged-in users, set {$this->getModulePrefix()}show=minor|!anon"
611 ),
612 'type' => 'Which types of changes to show',
613 'limit' => 'How many total changes to return',
614 'tag' => 'Only list changes tagged with this tag',
615 );
616 }
617
618 public function getDescription() {
619 return 'Enumerate recent changes';
620 }
621
622 public function getPossibleErrors() {
623 return array_merge( parent::getPossibleErrors(), array(
624 array( 'show' ),
625 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
626 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
627 ) );
628 }
629
630 protected function getExamples() {
631 return array(
632 'api.php?action=query&list=recentchanges'
633 );
634 }
635
636 public function getVersion() {
637 return __CLASS__ . ': $Id$';
638 }
639 }