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