Remove double globals.
[lhc/web/wiklou.git] / includes / api / ApiQueryRecentChanges.php
1 <?php
2
3 /**
4 * Created on Oct 19, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * A query action to enumerate the recent changes that were done to the wiki.
33 * Various filters are supported.
34 *
35 * @ingroup API
36 */
37 class ApiQueryRecentChanges extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'rc' );
41 }
42
43 private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_flags = false,
44 $fld_timestamp = false, $fld_title = false, $fld_ids = false,
45 $fld_sizes = false;
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(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 return $this->tokenFunctions;
68 }
69
70 public static function getPatrolToken( $pageid, $title, $rc ) {
71 global $wgUser;
72 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
73 $rc->getAttribute( 'rc_type' ) != RC_NEW ) )
74 {
75 return false;
76 }
77
78 // The patrol token is always the same, let's exploit that
79 static $cachedPatrolToken = null;
80 if ( !is_null( $cachedPatrolToken ) ) {
81 return $cachedPatrolToken;
82 }
83
84 $cachedPatrolToken = $wgUser->editToken();
85 return $cachedPatrolToken;
86 }
87
88 /**
89 * Sets internal state to include the desired properties in the output.
90 * @param $prop associative array of properties, only keys are used here
91 */
92 public function initProperties( $prop ) {
93 $this->fld_comment = isset( $prop['comment'] );
94 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
95 $this->fld_user = isset( $prop['user'] );
96 $this->fld_flags = isset( $prop['flags'] );
97 $this->fld_timestamp = isset( $prop['timestamp'] );
98 $this->fld_title = isset( $prop['title'] );
99 $this->fld_ids = isset( $prop['ids'] );
100 $this->fld_sizes = isset( $prop['sizes'] );
101 $this->fld_redirect = isset( $prop['redirect'] );
102 $this->fld_patrolled = isset( $prop['patrolled'] );
103 $this->fld_loginfo = isset( $prop['loginfo'] );
104 $this->fld_tags = isset( $prop['tags'] );
105 }
106
107 /**
108 * Generates and outputs the result of this query based upon the provided parameters.
109 */
110 public function execute() {
111 global $wgUser;
112 /* Get the parameters of the request. */
113 $params = $this->extractRequestParams();
114
115 /* Build our basic query. Namely, something along the lines of:
116 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
117 * AND rc_timestamp < $end AND rc_namespace = $namespace
118 * AND rc_deleted = '0'
119 */
120 $this->addTables( 'recentchanges' );
121 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
122 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
123 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
124 $this->addWhereFld( 'rc_deleted', 0 );
125
126 if ( !is_null( $params['type'] ) ) {
127 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
128 }
129
130 if ( !is_null( $params['show'] ) ) {
131 $show = array_flip( $params['show'] );
132
133 /* Check for conflicting parameters. */
134 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
135 || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
136 || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
137 || ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
138 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
139 )
140 {
141 $this->dieUsageMsg( array( 'show' ) );
142 }
143
144 // Check permissions
145 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
146 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
147 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
148 }
149 }
150
151 /* Add additional conditions to query depending upon parameters. */
152 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
153 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
154 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
155 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
156 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
157 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
158 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
159 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
160 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
161
162 // Don't throw log entries out the window here
163 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) );
164 }
165
166 if ( !is_null( $params['user'] ) && !is_null( $param['excludeuser'] ) ) {
167 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
168 }
169
170 if ( !is_null( $params['user'] ) ) {
171 $this->addWhereFld( 'rc_user_text', $params['user'] );
172 $index['recentchanges'] = 'rc_user_text';
173 }
174
175 if ( !is_null( $params['excludeuser'] ) ) {
176 // We don't use the rc_user_text index here because
177 // * it would require us to sort by rc_user_text before rc_timestamp
178 // * the != condition doesn't throw out too many rows anyway
179 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
180 }
181
182 /* Add the fields we're concerned with to our query. */
183 $this->addFields( array(
184 'rc_timestamp',
185 'rc_namespace',
186 'rc_title',
187 'rc_cur_id',
188 'rc_type',
189 'rc_moved_to_ns',
190 'rc_moved_to_title'
191 ) );
192
193 /* Determine what properties we need to display. */
194 if ( !is_null( $params['prop'] ) ) {
195 $prop = array_flip( $params['prop'] );
196
197 /* Set up internal members based upon params. */
198 $this->initProperties( $prop );
199
200 if ( $this->fld_patrolled && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
201 {
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 {
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 ) {
340 $vals['user'] = $row->rc_user_text;
341 if ( !$row->rc_user ) {
342 $vals['anon'] = '';
343 }
344 }
345
346 /* Add flags, such as new, minor, bot. */
347 if ( $this->fld_flags ) {
348 if ( $row->rc_bot ) {
349 $vals['bot'] = '';
350 }
351 if ( $row->rc_new ) {
352 $vals['new'] = '';
353 }
354 if ( $row->rc_minor ) {
355 $vals['minor'] = '';
356 }
357 }
358
359 /* Add sizes of each revision. (Only available on 1.10+) */
360 if ( $this->fld_sizes ) {
361 $vals['oldlen'] = intval( $row->rc_old_len );
362 $vals['newlen'] = intval( $row->rc_new_len );
363 }
364
365 /* Add the timestamp. */
366 if ( $this->fld_timestamp ) {
367 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
368 }
369
370 /* Add edit summary / log summary. */
371 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
372 $vals['comment'] = $row->rc_comment;
373 }
374
375 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
376 global $wgUser;
377 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
378 }
379
380 if ( $this->fld_redirect ) {
381 if ( $row->page_is_redirect ) {
382 $vals['redirect'] = '';
383 }
384 }
385
386 /* Add the patrolled flag */
387 if ( $this->fld_patrolled && $row->rc_patrolled == 1 ) {
388 $vals['patrolled'] = '';
389 }
390
391 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
392 $vals['logid'] = intval( $row->rc_logid );
393 $vals['logtype'] = $row->rc_log_type;
394 $vals['logaction'] = $row->rc_log_action;
395 ApiQueryLogEvents::addLogParams(
396 $this->getResult(),
397 $vals, $row->rc_params,
398 $row->rc_log_type, $row->rc_timestamp
399 );
400 }
401
402 if ( $this->fld_tags ) {
403 if ( $row->ts_tags ) {
404 $tags = explode( ',', $row->ts_tags );
405 $this->getResult()->setIndexedTagName( $tags, 'tag' );
406 $vals['tags'] = $tags;
407 } else {
408 $vals['tags'] = array();
409 }
410 }
411
412 if ( !is_null( $this->token ) ) {
413 $tokenFunctions = $this->getTokenFunctions();
414 foreach ( $this->token as $t ) {
415 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id,
416 $title, RecentChange::newFromRow( $row ) );
417 if ( $val === false ) {
418 $this->setWarning( "Action '$t' is not allowed for the current user" );
419 } else {
420 $vals[$t . 'token'] = $val;
421 }
422 }
423 }
424
425 return $vals;
426 }
427
428 private function parseRCType( $type ) {
429 if ( is_array( $type ) ) {
430 $retval = array();
431 foreach ( $type as $t ) {
432 $retval[] = $this->parseRCType( $t );
433 }
434 return $retval;
435 }
436 switch( $type ) {
437 case 'edit':
438 return RC_EDIT;
439 case 'new':
440 return RC_NEW;
441 case 'log':
442 return RC_LOG;
443 }
444 }
445
446 public function getCacheMode( $params ) {
447 if ( isset( $params['show'] ) ) {
448 foreach ( $params['show'] as $show ) {
449 if ( $show === 'patrolled' || $show === '!patrolled' ) {
450 return 'private';
451 }
452 }
453 }
454 if ( isset( $params['token'] ) ) {
455 return 'private';
456 }
457 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
458 // formatComment() calls wfMsg() among other things
459 return 'anon-public-user-private';
460 }
461 return 'public';
462 }
463
464 public function getAllowedParams() {
465 return array(
466 'start' => array(
467 ApiBase::PARAM_TYPE => 'timestamp'
468 ),
469 'end' => array(
470 ApiBase::PARAM_TYPE => 'timestamp'
471 ),
472 'dir' => array(
473 ApiBase::PARAM_DFLT => 'older',
474 ApiBase::PARAM_TYPE => array(
475 'newer',
476 'older'
477 )
478 ),
479 'namespace' => array(
480 ApiBase::PARAM_ISMULTI => true,
481 ApiBase::PARAM_TYPE => 'namespace'
482 ),
483 'user' => array(
484 ApiBase::PARAM_TYPE => 'user'
485 ),
486 'excludeuser' => array(
487 ApiBase::PARAM_TYPE => 'user'
488 ),
489 'tag' => null,
490 'prop' => array(
491 ApiBase::PARAM_ISMULTI => true,
492 ApiBase::PARAM_DFLT => 'title|timestamp|ids',
493 ApiBase::PARAM_TYPE => array(
494 'user',
495 'comment',
496 'parsedcomment',
497 'flags',
498 'timestamp',
499 'title',
500 'ids',
501 'sizes',
502 'redirect',
503 'patrolled',
504 'loginfo',
505 'tags'
506 )
507 ),
508 'token' => array(
509 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
510 ApiBase::PARAM_ISMULTI => true
511 ),
512 'show' => array(
513 ApiBase::PARAM_ISMULTI => true,
514 ApiBase::PARAM_TYPE => array(
515 'minor',
516 '!minor',
517 'bot',
518 '!bot',
519 'anon',
520 '!anon',
521 'redirect',
522 '!redirect',
523 'patrolled',
524 '!patrolled'
525 )
526 ),
527 'limit' => array(
528 ApiBase::PARAM_DFLT => 10,
529 ApiBase::PARAM_TYPE => 'limit',
530 ApiBase::PARAM_MIN => 1,
531 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
532 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
533 ),
534 'type' => array(
535 ApiBase::PARAM_ISMULTI => true,
536 ApiBase::PARAM_TYPE => array(
537 'edit',
538 'new',
539 'log'
540 )
541 )
542 );
543 }
544
545 public function getParamDescription() {
546 return array(
547 'start' => 'The timestamp to start enumerating from',
548 'end' => 'The timestamp to end enumerating',
549 'dir' => 'In which direction to enumerate',
550 'namespace' => 'Filter log entries to only this namespace(s)',
551 'user' => 'Only list changes by this user',
552 'excludeuser' => 'Don\'t list changes by this user',
553 'prop' => array(
554 'Include additional pieces of information',
555 ' user - Adds the user responsible for the edit and tags if they are an IP',
556 ' comment - Adds the comment for the edit',
557 ' parsedcomment - Adds the parsed comment for the edit',
558 ' flags - Adds flags for the edit',
559 ' timestamp - Adds timestamp of the edit',
560 ' title - Adds the page title of the edit',
561 ' ids - Adds the page id, recent changes id and the new and old revision id',
562 ' sizes - Adds the new and old page length in bytes',
563 ' redirect - Tags edit if page is a redirect',
564 ' patrolled - Tags edits have have been patrolled',
565 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
566 ' tags - Lists tags for the entry',
567 ),
568 'token' => 'Which tokens to obtain for each change',
569 'show' => array(
570 'Show only items that meet this criteria.',
571 "For example, to see only minor edits done by logged-in users, set {$this->getModulePrefix()}show=minor|!anon"
572 ),
573 'type' => 'Which types of changes to show',
574 'limit' => 'How many total changes to return',
575 'tag' => 'Only list changes tagged with this tag',
576 );
577 }
578
579 public function getDescription() {
580 return 'Enumerate recent changes';
581 }
582
583 public function getPossibleErrors() {
584 return array_merge( parent::getPossibleErrors(), array(
585 array( 'show' ),
586 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
587 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
588 ) );
589 }
590
591 protected function getExamples() {
592 return array(
593 'api.php?action=query&list=recentchanges'
594 );
595 }
596
597 public function getVersion() {
598 return __CLASS__ . ': $Id$';
599 }
600 }