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