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