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