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