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