follow up r69339:
[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 /* Get the parameters of the request. */
112 $params = $this->extractRequestParams();
113
114 /* Build our basic query. Namely, something along the lines of:
115 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
116 * AND rc_timestamp < $end AND rc_namespace = $namespace
117 * AND rc_deleted = '0'
118 */
119 $db = $this->getDB();
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 global $wgUser;
146 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
147 $this->getMain()->setVaryCookie();
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( $param['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 global $wgUser;
203 if ( $this->fld_patrolled && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
204 {
205 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
206 }
207
208 /* Add fields to our query if they are specified as a needed parameter. */
209 $this->addFieldsIf( 'rc_id', $this->fld_ids );
210 $this->addFieldsIf( 'rc_this_oldid', $this->fld_ids );
211 $this->addFieldsIf( 'rc_last_oldid', $this->fld_ids );
212 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
213 $this->addFieldsIf( 'rc_user', $this->fld_user );
214 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
215 $this->addFieldsIf( 'rc_minor', $this->fld_flags );
216 $this->addFieldsIf( 'rc_bot', $this->fld_flags );
217 $this->addFieldsIf( 'rc_new', $this->fld_flags );
218 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
219 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
220 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
221 $this->addFieldsIf( 'rc_logid', $this->fld_loginfo );
222 $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo );
223 $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo );
224 $this->addFieldsIf( 'rc_params', $this->fld_loginfo );
225 if ( $this->fld_redirect || isset( $show['redirect'] ) || isset( $show['!redirect'] ) )
226 {
227 $this->addTables( 'page' );
228 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
229 $this->addFields( 'page_is_redirect' );
230 }
231 }
232
233 if ( $this->fld_tags ) {
234 $this->addTables( 'tag_summary' );
235 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
236 $this->addFields( 'ts_tags' );
237 }
238
239 if ( !is_null( $params['tag'] ) ) {
240 $this->addTables( 'change_tag' );
241 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
242 $this->addWhereFld( 'ct_tag' , $params['tag'] );
243 global $wgOldChangeTagsIndex;
244 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
245 }
246
247 $this->token = $params['token'];
248 $this->addOption( 'LIMIT', $params['limit'] + 1 );
249 $this->addOption( 'USE INDEX', $index );
250
251 $count = 0;
252 /* Perform the actual query. */
253 $db = $this->getDB();
254 $res = $this->select( __METHOD__ );
255
256 /* Iterate through the rows, adding data extracted from them to our query result. */
257 foreach ( $res as $row ) {
258 if ( ++ $count > $params['limit'] ) {
259 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
260 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
261 break;
262 }
263
264 /* Extract the data from a single row. */
265 $vals = $this->extractRowInfo( $row );
266
267 /* Add that row's data to our final output. */
268 if ( !$vals ) {
269 continue;
270 }
271 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
272 if ( !$fit ) {
273 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
274 break;
275 }
276 }
277
278 /* Format the result */
279 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
280 }
281
282 /**
283 * Extracts from a single sql row the data needed to describe one recent change.
284 *
285 * @param $row The row from which to extract the data.
286 * @return An array mapping strings (descriptors) to their respective string values.
287 * @access public
288 */
289 public function extractRowInfo( $row ) {
290 /* If page was moved somewhere, get the title of the move target. */
291 $movedToTitle = false;
292 if ( isset( $row->rc_moved_to_title ) && $row->rc_moved_to_title !== '' )
293 {
294 $movedToTitle = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
295 }
296
297 /* Determine the title of the page that has been changed. */
298 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
299
300 /* Our output data. */
301 $vals = array();
302
303 $type = intval( $row->rc_type );
304
305 /* Determine what kind of change this was. */
306 switch ( $type ) {
307 case RC_EDIT:
308 $vals['type'] = 'edit';
309 break;
310 case RC_NEW:
311 $vals['type'] = 'new';
312 break;
313 case RC_MOVE:
314 $vals['type'] = 'move';
315 break;
316 case RC_LOG:
317 $vals['type'] = 'log';
318 break;
319 case RC_MOVE_OVER_REDIRECT:
320 $vals['type'] = 'move over redirect';
321 break;
322 default:
323 $vals['type'] = $type;
324 }
325
326 /* Create a new entry in the result for the title. */
327 if ( $this->fld_title ) {
328 ApiQueryBase::addTitleInfo( $vals, $title );
329 if ( $movedToTitle ) {
330 ApiQueryBase::addTitleInfo( $vals, $movedToTitle, 'new_' );
331 }
332 }
333
334 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */
335 if ( $this->fld_ids ) {
336 $vals['rcid'] = intval( $row->rc_id );
337 $vals['pageid'] = intval( $row->rc_cur_id );
338 $vals['revid'] = intval( $row->rc_this_oldid );
339 $vals['old_revid'] = intval( $row->rc_last_oldid );
340 }
341
342 /* Add user data and 'anon' flag, if use is anonymous. */
343 if ( $this->fld_user ) {
344 $vals['user'] = $row->rc_user_text;
345 if ( !$row->rc_user ) {
346 $vals['anon'] = '';
347 }
348 }
349
350 /* Add flags, such as new, minor, bot. */
351 if ( $this->fld_flags ) {
352 if ( $row->rc_bot ) {
353 $vals['bot'] = '';
354 }
355 if ( $row->rc_new ) {
356 $vals['new'] = '';
357 }
358 if ( $row->rc_minor ) {
359 $vals['minor'] = '';
360 }
361 }
362
363 /* Add sizes of each revision. (Only available on 1.10+) */
364 if ( $this->fld_sizes ) {
365 $vals['oldlen'] = intval( $row->rc_old_len );
366 $vals['newlen'] = intval( $row->rc_new_len );
367 }
368
369 /* Add the timestamp. */
370 if ( $this->fld_timestamp ) {
371 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
372 }
373
374 /* Add edit summary / log summary. */
375 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
376 $vals['comment'] = $row->rc_comment;
377 }
378
379 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
380 global $wgUser;
381 $this->getMain()->setVaryCookie();
382 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
383 }
384
385 if ( $this->fld_redirect ) {
386 if ( $row->page_is_redirect ) {
387 $vals['redirect'] = '';
388 }
389 }
390
391 /* Add the patrolled flag */
392 if ( $this->fld_patrolled && $row->rc_patrolled == 1 ) {
393 $vals['patrolled'] = '';
394 }
395
396 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
397 $vals['logid'] = intval( $row->rc_logid );
398 $vals['logtype'] = $row->rc_log_type;
399 $vals['logaction'] = $row->rc_log_action;
400 ApiQueryLogEvents::addLogParams(
401 $this->getResult(),
402 $vals, $row->rc_params,
403 $row->rc_log_type, $row->rc_timestamp
404 );
405 }
406
407 if ( $this->fld_tags ) {
408 if ( $row->ts_tags ) {
409 $tags = explode( ',', $row->ts_tags );
410 $this->getResult()->setIndexedTagName( $tags, 'tag' );
411 $vals['tags'] = $tags;
412 } else {
413 $vals['tags'] = array();
414 }
415 }
416
417 if ( !is_null( $this->token ) ) {
418 // Don't cache tokens
419 $this->getMain()->setCachePrivate();
420
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 getAllowedParams() {
455 return array(
456 'start' => array(
457 ApiBase::PARAM_TYPE => 'timestamp'
458 ),
459 'end' => array(
460 ApiBase::PARAM_TYPE => 'timestamp'
461 ),
462 'dir' => array(
463 ApiBase::PARAM_DFLT => 'older',
464 ApiBase::PARAM_TYPE => array(
465 'newer',
466 'older'
467 )
468 ),
469 'namespace' => array(
470 ApiBase::PARAM_ISMULTI => true,
471 ApiBase::PARAM_TYPE => 'namespace'
472 ),
473 'user' => array(
474 ApiBase::PARAM_TYPE => 'user'
475 ),
476 'excludeuser' => array(
477 ApiBase::PARAM_TYPE => 'user'
478 ),
479 'tag' => null,
480 'prop' => array(
481 ApiBase::PARAM_ISMULTI => true,
482 ApiBase::PARAM_DFLT => 'title|timestamp|ids',
483 ApiBase::PARAM_TYPE => array(
484 'user',
485 'comment',
486 'parsedcomment',
487 'flags',
488 'timestamp',
489 'title',
490 'ids',
491 'sizes',
492 'redirect',
493 'patrolled',
494 'loginfo',
495 'tags'
496 )
497 ),
498 'token' => array(
499 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
500 ApiBase::PARAM_ISMULTI => true
501 ),
502 'show' => array(
503 ApiBase::PARAM_ISMULTI => true,
504 ApiBase::PARAM_TYPE => array(
505 'minor',
506 '!minor',
507 'bot',
508 '!bot',
509 'anon',
510 '!anon',
511 'redirect',
512 '!redirect',
513 'patrolled',
514 '!patrolled'
515 )
516 ),
517 'limit' => array(
518 ApiBase::PARAM_DFLT => 10,
519 ApiBase::PARAM_TYPE => 'limit',
520 ApiBase::PARAM_MIN => 1,
521 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
522 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
523 ),
524 'type' => array(
525 ApiBase::PARAM_ISMULTI => true,
526 ApiBase::PARAM_TYPE => array(
527 'edit',
528 'new',
529 'log'
530 )
531 )
532 );
533 }
534
535 public function getParamDescription() {
536 return array(
537 'start' => 'The timestamp to start enumerating from',
538 'end' => 'The timestamp to end enumerating',
539 'dir' => 'In which direction to enumerate',
540 'namespace' => 'Filter log entries to only this namespace(s)',
541 'user' => 'Only list changes by this user',
542 'excludeuser' => 'Don\'t list changes by this user',
543 'prop' => array(
544 'Include additional pieces of information',
545 ' user - Adds the user responsible for the edit and tags if they are an IP',
546 ' comment - Adds the comment for the edit',
547 ' parsedcomment - Adds the parsed comment for the edit',
548 ' flags - Adds flags for the edit',
549 ' timestamp - Adds timestamp of the edit',
550 ' title - Adds the page title of the edit',
551 ' ids - Adds the page id, recent changes id and the new and old revision id',
552 ' sizes - Adds the new and old page length in bytes',
553 ' redirect - Tags edit if page is a redirect',
554 ' patrolled - Tags edits have have been patrolled',
555 ' loginfo - Adds log information (logid, logtype, etc) to log entries',
556 ' tags - Lists tags for the entry',
557 ),
558 'token' => 'Which tokens to obtain for each change',
559 'show' => array(
560 'Show only items that meet this criteria.',
561 "For example, to see only minor edits done by logged-in users, set {$this->getModulePrefix()}show=minor|!anon"
562 ),
563 'type' => 'Which types of changes to show',
564 'limit' => 'How many total changes to return',
565 'tag' => 'Only list changes tagged with this tag',
566 );
567 }
568
569 public function getDescription() {
570 return 'Enumerate recent changes';
571 }
572
573 public function getPossibleErrors() {
574 return array_merge( parent::getPossibleErrors(), array(
575 array( 'show' ),
576 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
577 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
578 ) );
579 }
580
581 protected function getExamples() {
582 return array(
583 'api.php?action=query&list=recentchanges'
584 );
585 }
586
587 public function getVersion() {
588 return __CLASS__ . ': $Id$';
589 }
590 }