00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( 'ApiQueryBase.php' );
00029 }
00030
00037 class ApiQueryRecentChanges extends ApiQueryBase {
00038
00039 public function __construct( $query, $moduleName ) {
00040 parent :: __construct( $query, $moduleName, 'rc' );
00041 }
00042
00043 private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_flags = false,
00044 $fld_timestamp = false, $fld_title = false, $fld_ids = false,
00045 $fld_sizes = false;
00052 protected function getTokenFunctions() {
00053
00054 if ( isset( $this->tokenFunctions ) )
00055 return $this->tokenFunctions;
00056
00057
00058 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) )
00059 return array();
00060
00061 $this->tokenFunctions = array(
00062 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
00063 );
00064 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions ) );
00065 return $this->tokenFunctions;
00066 }
00067
00068 public static function getPatrolToken( $pageid, $title, $rc )
00069 {
00070 global $wgUser;
00071 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() ||
00072 $rc->getAttribute( 'rc_type' ) != RC_NEW ) )
00073 return false;
00074
00075
00076 static $cachedPatrolToken = null;
00077 if ( !is_null( $cachedPatrolToken ) )
00078 return $cachedPatrolToken;
00079
00080 $cachedPatrolToken = $wgUser->editToken();
00081 return $cachedPatrolToken;
00082 }
00083
00088 public function initProperties( $prop ) {
00089 $this->fld_comment = isset ( $prop['comment'] );
00090 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
00091 $this->fld_user = isset ( $prop['user'] );
00092 $this->fld_flags = isset ( $prop['flags'] );
00093 $this->fld_timestamp = isset ( $prop['timestamp'] );
00094 $this->fld_title = isset ( $prop['title'] );
00095 $this->fld_ids = isset ( $prop['ids'] );
00096 $this->fld_sizes = isset ( $prop['sizes'] );
00097 $this->fld_redirect = isset( $prop['redirect'] );
00098 $this->fld_patrolled = isset( $prop['patrolled'] );
00099 $this->fld_loginfo = isset( $prop['loginfo'] );
00100 $this->fld_tags = isset( $prop['tags'] );
00101 }
00102
00106 public function execute() {
00107
00108 $params = $this->extractRequestParams();
00109
00110
00111
00112
00113
00114
00115 $db = $this->getDB();
00116 $this->addTables( 'recentchanges' );
00117 $index = array( 'recentchanges' => 'rc_timestamp' );
00118 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
00119 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
00120 $this->addWhereFld( 'rc_deleted', 0 );
00121
00122 if ( !is_null( $params['type'] ) )
00123 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) );
00124
00125 if ( !is_null( $params['show'] ) ) {
00126 $show = array_flip( $params['show'] );
00127
00128
00129 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
00130 || ( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
00131 || ( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
00132 || ( isset ( $show['redirect'] ) && isset ( $show['!redirect'] ) )
00133 || ( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) ) ) {
00134
00135 $this->dieUsageMsg( array( 'show' ) );
00136 }
00137
00138
00139 global $wgUser;
00140 if ( ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
00141 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
00142
00143
00144 $this->addWhereIf( 'rc_minor = 0', isset ( $show['!minor'] ) );
00145 $this->addWhereIf( 'rc_minor != 0', isset ( $show['minor'] ) );
00146 $this->addWhereIf( 'rc_bot = 0', isset ( $show['!bot'] ) );
00147 $this->addWhereIf( 'rc_bot != 0', isset ( $show['bot'] ) );
00148 $this->addWhereIf( 'rc_user = 0', isset ( $show['anon'] ) );
00149 $this->addWhereIf( 'rc_user != 0', isset ( $show['!anon'] ) );
00150 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
00151 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
00152 $this->addWhereIf( 'page_is_redirect = 1', isset ( $show['redirect'] ) );
00153
00154
00155 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset ( $show['!redirect'] ) );
00156 }
00157
00158 if ( !is_null( $params['user'] ) && !is_null( $param['excludeuser'] ) )
00159 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
00160
00161 if ( !is_null( $params['user'] ) )
00162 {
00163 $this->addWhereFld( 'rc_user_text', $params['user'] );
00164 $index['recentchanges'] = 'rc_user_text';
00165 }
00166
00167 if ( !is_null( $params['excludeuser'] ) )
00168
00169
00170
00171 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
00172
00173
00174 $this->addFields( array (
00175 'rc_timestamp',
00176 'rc_namespace',
00177 'rc_title',
00178 'rc_cur_id',
00179 'rc_type',
00180 'rc_moved_to_ns',
00181 'rc_moved_to_title'
00182 ) );
00183
00184
00185 if ( !is_null( $params['prop'] ) ) {
00186 $prop = array_flip( $params['prop'] );
00187
00188
00189 $this->initProperties( $prop );
00190
00191 global $wgUser;
00192 if ( $this->fld_patrolled && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
00193 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
00194
00195
00196 $this->addFieldsIf( 'rc_id', $this->fld_ids );
00197 $this->addFieldsIf( 'rc_this_oldid', $this->fld_ids );
00198 $this->addFieldsIf( 'rc_last_oldid', $this->fld_ids );
00199 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
00200 $this->addFieldsIf( 'rc_user', $this->fld_user );
00201 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
00202 $this->addFieldsIf( 'rc_minor', $this->fld_flags );
00203 $this->addFieldsIf( 'rc_bot', $this->fld_flags );
00204 $this->addFieldsIf( 'rc_new', $this->fld_flags );
00205 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
00206 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
00207 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
00208 $this->addFieldsIf( 'rc_logid', $this->fld_loginfo );
00209 $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo );
00210 $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo );
00211 $this->addFieldsIf( 'rc_params', $this->fld_loginfo );
00212 if ( $this->fld_redirect || isset( $show['redirect'] ) || isset( $show['!redirect'] ) )
00213 {
00214 $this->addTables( 'page' );
00215 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
00216 $this->addFields( 'page_is_redirect' );
00217 }
00218 }
00219
00220 if ( $this->fld_tags ) {
00221 $this->addTables( 'tag_summary' );
00222 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
00223 $this->addFields( 'ts_tags' );
00224 }
00225
00226 if ( !is_null( $params['tag'] ) ) {
00227 $this->addTables( 'change_tag' );
00228 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
00229 $this->addWhereFld( 'ct_tag' , $params['tag'] );
00230 global $wgOldChangeTagsIndex;
00231 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
00232 }
00233
00234 $this->token = $params['token'];
00235 $this->addOption( 'LIMIT', $params['limit'] + 1 );
00236 $this->addOption( 'USE INDEX', $index );
00237
00238 $count = 0;
00239
00240 $db = $this->getDB();
00241 $res = $this->select( __METHOD__ );
00242
00243
00244 while ( $row = $db->fetchObject( $res ) ) {
00245 if ( ++ $count > $params['limit'] ) {
00246
00247 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
00248 break;
00249 }
00250
00251
00252 $vals = $this->extractRowInfo( $row );
00253
00254
00255 if ( !$vals )
00256 continue;
00257 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00258 if ( !$fit )
00259 {
00260 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
00261 break;
00262 }
00263 }
00264
00265 $db->freeResult( $res );
00266
00267
00268 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
00269 }
00270
00278 public function extractRowInfo( $row ) {
00279
00280 $movedToTitle = false;
00281 if ( isset( $row->rc_moved_to_title ) && $row->rc_moved_to_title !== '' )
00282 $movedToTitle = Title :: makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title );
00283
00284
00285 $title = Title :: makeTitle( $row->rc_namespace, $row->rc_title );
00286
00287
00288 $vals = array ();
00289
00290 $type = intval ( $row->rc_type );
00291
00292
00293 switch ( $type ) {
00294 case RC_EDIT:
00295 $vals['type'] = 'edit';
00296 break;
00297 case RC_NEW:
00298 $vals['type'] = 'new';
00299 break;
00300 case RC_MOVE:
00301 $vals['type'] = 'move';
00302 break;
00303 case RC_LOG:
00304 $vals['type'] = 'log';
00305 break;
00306 case RC_MOVE_OVER_REDIRECT:
00307 $vals['type'] = 'move over redirect';
00308 break;
00309 default:
00310 $vals['type'] = $type;
00311 }
00312
00313
00314 if ( $this->fld_title ) {
00315 ApiQueryBase :: addTitleInfo( $vals, $title );
00316 if ( $movedToTitle )
00317 ApiQueryBase :: addTitleInfo( $vals, $movedToTitle, "new_" );
00318 }
00319
00320
00321 if ( $this->fld_ids ) {
00322 $vals['rcid'] = intval( $row->rc_id );
00323 $vals['pageid'] = intval( $row->rc_cur_id );
00324 $vals['revid'] = intval( $row->rc_this_oldid );
00325 $vals['old_revid'] = intval( $row->rc_last_oldid );
00326 }
00327
00328
00329 if ( $this->fld_user ) {
00330 $vals['user'] = $row->rc_user_text;
00331 if ( !$row->rc_user )
00332 $vals['anon'] = '';
00333 }
00334
00335
00336 if ( $this->fld_flags ) {
00337 if ( $row->rc_bot )
00338 $vals['bot'] = '';
00339 if ( $row->rc_new )
00340 $vals['new'] = '';
00341 if ( $row->rc_minor )
00342 $vals['minor'] = '';
00343 }
00344
00345
00346 if ( $this->fld_sizes ) {
00347 $vals['oldlen'] = intval( $row->rc_old_len );
00348 $vals['newlen'] = intval( $row->rc_new_len );
00349 }
00350
00351
00352 if ( $this->fld_timestamp )
00353 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
00354
00355
00356 if ( $this->fld_comment && isset( $row->rc_comment ) )
00357 $vals['comment'] = $row->rc_comment;
00358
00359 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
00360 global $wgUser;
00361 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
00362 }
00363
00364 if ( $this->fld_redirect )
00365 if ( $row->page_is_redirect )
00366 $vals['redirect'] = '';
00367
00368
00369 if ( $this->fld_patrolled && $row->rc_patrolled == 1 )
00370 $vals['patrolled'] = '';
00371
00372 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
00373 $vals['logid'] = intval( $row->rc_logid );
00374 $vals['logtype'] = $row->rc_log_type;
00375 $vals['logaction'] = $row->rc_log_action;
00376 ApiQueryLogEvents::addLogParams( $this->getResult(),
00377 $vals, $row->rc_params,
00378 $row->rc_log_type, $row->rc_timestamp );
00379 }
00380
00381 if ( $this->fld_tags ) {
00382 if ( $row->ts_tags ) {
00383 $tags = explode( ',', $row->ts_tags );
00384 $this->getResult()->setIndexedTagName( $tags, 'tag' );
00385 $vals['tags'] = $tags;
00386 } else {
00387 $vals['tags'] = array();
00388 }
00389 }
00390
00391 if ( !is_null( $this->token ) )
00392 {
00393 $tokenFunctions = $this->getTokenFunctions();
00394 foreach ( $this->token as $t )
00395 {
00396 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id,
00397 $title, RecentChange::newFromRow( $row ) );
00398 if ( $val === false )
00399 $this->setWarning( "Action '$t' is not allowed for the current user" );
00400 else
00401 $vals[$t . 'token'] = $val;
00402 }
00403 }
00404
00405 return $vals;
00406 }
00407
00408 private function parseRCType( $type )
00409 {
00410 if ( is_array( $type ) )
00411 {
00412 $retval = array();
00413 foreach ( $type as $t )
00414 $retval[] = $this->parseRCType( $t );
00415 return $retval;
00416 }
00417 switch( $type )
00418 {
00419 case 'edit': return RC_EDIT;
00420 case 'new': return RC_NEW;
00421 case 'log': return RC_LOG;
00422 }
00423 }
00424
00425 public function getCacheMode( $params ) {
00426 if ( isset( $params['show'] ) ) {
00427 foreach ( $params['show'] as $show ) {
00428 if ( $show === 'patrolled' || $show === '!patrolled' ) {
00429 return 'private';
00430 }
00431 }
00432 }
00433 if ( isset( $params['token'] ) ) {
00434 return 'private';
00435 }
00436 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
00437
00438 return 'anon-public-user-private';
00439 }
00440 return 'public';
00441 }
00442
00443 public function getAllowedParams() {
00444 return array (
00445 'start' => array (
00446 ApiBase :: PARAM_TYPE => 'timestamp'
00447 ),
00448 'end' => array (
00449 ApiBase :: PARAM_TYPE => 'timestamp'
00450 ),
00451 'dir' => array (
00452 ApiBase :: PARAM_DFLT => 'older',
00453 ApiBase :: PARAM_TYPE => array (
00454 'newer',
00455 'older'
00456 )
00457 ),
00458 'namespace' => array (
00459 ApiBase :: PARAM_ISMULTI => true,
00460 ApiBase :: PARAM_TYPE => 'namespace'
00461 ),
00462 'user' => array(
00463 ApiBase :: PARAM_TYPE => 'user'
00464 ),
00465 'excludeuser' => array(
00466 ApiBase :: PARAM_TYPE => 'user'
00467 ),
00468 'tag' => null,
00469 'prop' => array (
00470 ApiBase :: PARAM_ISMULTI => true,
00471 ApiBase :: PARAM_DFLT => 'title|timestamp|ids',
00472 ApiBase :: PARAM_TYPE => array (
00473 'user',
00474 'comment',
00475 'parsedcomment',
00476 'flags',
00477 'timestamp',
00478 'title',
00479 'ids',
00480 'sizes',
00481 'redirect',
00482 'patrolled',
00483 'loginfo',
00484 'tags'
00485 )
00486 ),
00487 'token' => array(
00488 ApiBase :: PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
00489 ApiBase :: PARAM_ISMULTI => true
00490 ),
00491 'show' => array (
00492 ApiBase :: PARAM_ISMULTI => true,
00493 ApiBase :: PARAM_TYPE => array (
00494 'minor',
00495 '!minor',
00496 'bot',
00497 '!bot',
00498 'anon',
00499 '!anon',
00500 'redirect',
00501 '!redirect',
00502 'patrolled',
00503 '!patrolled'
00504 )
00505 ),
00506 'limit' => array (
00507 ApiBase :: PARAM_DFLT => 10,
00508 ApiBase :: PARAM_TYPE => 'limit',
00509 ApiBase :: PARAM_MIN => 1,
00510 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00511 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00512 ),
00513 'type' => array (
00514 ApiBase :: PARAM_ISMULTI => true,
00515 ApiBase :: PARAM_TYPE => array (
00516 'edit',
00517 'new',
00518 'log'
00519 )
00520 )
00521 );
00522 }
00523
00524 public function getParamDescription() {
00525 return array (
00526 'start' => 'The timestamp to start enumerating from.',
00527 'end' => 'The timestamp to end enumerating.',
00528 'dir' => 'In which direction to enumerate.',
00529 'namespace' => 'Filter log entries to only this namespace(s)',
00530 'user' => 'Only list changes by this user',
00531 'excludeuser' => 'Don\'t list changes by this user',
00532 'prop' => 'Include additional pieces of information',
00533 'token' => 'Which tokens to obtain for each change',
00534 'show' => array (
00535 'Show only items that meet this criteria.',
00536 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
00537 ),
00538 'type' => 'Which types of changes to show.',
00539 'limit' => 'How many total changes to return.',
00540 'tag' => 'Only list changes tagged with this tag.',
00541 );
00542 }
00543
00544 public function getDescription() {
00545 return 'Enumerate recent changes';
00546 }
00547
00548 public function getPossibleErrors() {
00549 return array_merge( parent::getPossibleErrors(), array(
00550 array( 'show' ),
00551 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
00552 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
00553 ) );
00554 }
00555
00556 protected function getExamples() {
00557 return array (
00558 'api.php?action=query&list=recentchanges'
00559 );
00560 }
00561
00562 public function getVersion() {
00563 return __CLASS__ . ': $Id: ApiQueryRecentChanges.php 69932 2010-07-26 08:03:21Z tstarling $';
00564 }
00565 }