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