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