MediaWiki
REL1_19
|
00001 <?php 00033 class ApiQueryRecentChanges extends ApiQueryGeneratorBase { 00034 00035 public function __construct( $query, $moduleName ) { 00036 parent::__construct( $query, $moduleName, 'rc' ); 00037 } 00038 00039 private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false, 00040 $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false, 00041 $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false, 00042 $fld_tags = false, $token = array(); 00043 00044 private $tokenFunctions; 00045 00052 protected function getTokenFunctions() { 00053 // Don't call the hooks twice 00054 if ( isset( $this->tokenFunctions ) ) { 00055 return $this->tokenFunctions; 00056 } 00057 00058 // If we're in JSON callback mode, no tokens can be obtained 00059 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) { 00060 return array(); 00061 } 00062 00063 $this->tokenFunctions = array( 00064 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' ) 00065 ); 00066 wfRunHooks( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions ) ); 00067 return $this->tokenFunctions; 00068 } 00069 00076 public static function getPatrolToken( $pageid, $title, $rc ) { 00077 global $wgUser; 00078 if ( !$wgUser->useRCPatrol() && ( !$wgUser->useNPPatrol() || 00079 $rc->getAttribute( 'rc_type' ) != RC_NEW ) ) 00080 { 00081 return false; 00082 } 00083 00084 // The patrol token is always the same, let's exploit that 00085 static $cachedPatrolToken = null; 00086 if ( is_null( $cachedPatrolToken ) ) { 00087 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' ); 00088 } 00089 00090 return $cachedPatrolToken; 00091 } 00092 00097 public function initProperties( $prop ) { 00098 $this->fld_comment = isset( $prop['comment'] ); 00099 $this->fld_parsedcomment = isset( $prop['parsedcomment'] ); 00100 $this->fld_user = isset( $prop['user'] ); 00101 $this->fld_userid = isset( $prop['userid'] ); 00102 $this->fld_flags = isset( $prop['flags'] ); 00103 $this->fld_timestamp = isset( $prop['timestamp'] ); 00104 $this->fld_title = isset( $prop['title'] ); 00105 $this->fld_ids = isset( $prop['ids'] ); 00106 $this->fld_sizes = isset( $prop['sizes'] ); 00107 $this->fld_redirect = isset( $prop['redirect'] ); 00108 $this->fld_patrolled = isset( $prop['patrolled'] ); 00109 $this->fld_loginfo = isset( $prop['loginfo'] ); 00110 $this->fld_tags = isset( $prop['tags'] ); 00111 } 00112 00113 public function execute() { 00114 $this->run(); 00115 } 00116 00117 public function executeGenerator( $resultPageSet ) { 00118 $this->run( $resultPageSet ); 00119 } 00120 00126 public function run( $resultPageSet = null ) { 00127 $user = $this->getUser(); 00128 /* Get the parameters of the request. */ 00129 $params = $this->extractRequestParams(); 00130 00131 /* Build our basic query. Namely, something along the lines of: 00132 * SELECT * FROM recentchanges WHERE rc_timestamp > $start 00133 * AND rc_timestamp < $end AND rc_namespace = $namespace 00134 * AND rc_deleted = '0' 00135 */ 00136 $this->addTables( 'recentchanges' ); 00137 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change 00138 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] ); 00139 $this->addWhereFld( 'rc_namespace', $params['namespace'] ); 00140 $this->addWhereFld( 'rc_deleted', 0 ); 00141 00142 if ( !is_null( $params['type'] ) ) { 00143 $this->addWhereFld( 'rc_type', $this->parseRCType( $params['type'] ) ); 00144 } 00145 00146 if ( !is_null( $params['show'] ) ) { 00147 $show = array_flip( $params['show'] ); 00148 00149 /* Check for conflicting parameters. */ 00150 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) ) 00151 || ( isset( $show['bot'] ) && isset( $show['!bot'] ) ) 00152 || ( isset( $show['anon'] ) && isset( $show['!anon'] ) ) 00153 || ( isset( $show['redirect'] ) && isset( $show['!redirect'] ) ) 00154 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) 00155 ) { 00156 $this->dieUsageMsg( 'show' ); 00157 } 00158 00159 // Check permissions 00160 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) { 00161 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) { 00162 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' ); 00163 } 00164 } 00165 00166 /* Add additional conditions to query depending upon parameters. */ 00167 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) ); 00168 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) ); 00169 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) ); 00170 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) ); 00171 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) ); 00172 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) ); 00173 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) ); 00174 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) ); 00175 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) ); 00176 00177 // Don't throw log entries out the window here 00178 $this->addWhereIf( 'page_is_redirect = 0 OR page_is_redirect IS NULL', isset( $show['!redirect'] ) ); 00179 } 00180 00181 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) { 00182 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' ); 00183 } 00184 00185 if ( !is_null( $params['user'] ) ) { 00186 $this->addWhereFld( 'rc_user_text', $params['user'] ); 00187 $index['recentchanges'] = 'rc_user_text'; 00188 } 00189 00190 if ( !is_null( $params['excludeuser'] ) ) { 00191 // We don't use the rc_user_text index here because 00192 // * it would require us to sort by rc_user_text before rc_timestamp 00193 // * the != condition doesn't throw out too many rows anyway 00194 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) ); 00195 } 00196 00197 /* Add the fields we're concerned with to our query. */ 00198 $this->addFields( array( 00199 'rc_timestamp', 00200 'rc_namespace', 00201 'rc_title', 00202 'rc_cur_id', 00203 'rc_type', 00204 'rc_moved_to_ns', 00205 'rc_moved_to_title', 00206 'rc_deleted' 00207 ) ); 00208 00209 $showRedirects = false; 00210 /* Determine what properties we need to display. */ 00211 if ( !is_null( $params['prop'] ) ) { 00212 $prop = array_flip( $params['prop'] ); 00213 00214 /* Set up internal members based upon params. */ 00215 $this->initProperties( $prop ); 00216 00217 if ( $this->fld_patrolled && !$user->useRCPatrol() && !$user->useNPPatrol() ) { 00218 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' ); 00219 } 00220 00221 /* Add fields to our query if they are specified as a needed parameter. */ 00222 $this->addFieldsIf( array( 'rc_id', 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids ); 00223 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment ); 00224 $this->addFieldsIf( 'rc_user', $this->fld_user ); 00225 $this->addFieldsIf( 'rc_user_text', $this->fld_user || $this->fld_userid ); 00226 $this->addFieldsIf( array( 'rc_minor', 'rc_new', 'rc_bot' ) , $this->fld_flags ); 00227 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes ); 00228 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled ); 00229 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo ); 00230 $showRedirects = $this->fld_redirect || isset( $show['redirect'] ) || isset( $show['!redirect'] ); 00231 } 00232 00233 if ( $this->fld_tags ) { 00234 $this->addTables( 'tag_summary' ); 00235 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) ); 00236 $this->addFields( 'ts_tags' ); 00237 } 00238 00239 if ( $params['toponly'] || $showRedirects ) { 00240 $this->addTables( 'page' ); 00241 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) ); 00242 $this->addFields( 'page_is_redirect' ); 00243 00244 if ( $params['toponly'] ) { 00245 $this->addWhere( 'rc_this_oldid = page_latest' ); 00246 } 00247 } 00248 00249 if ( !is_null( $params['tag'] ) ) { 00250 $this->addTables( 'change_tag' ); 00251 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) ); 00252 $this->addWhereFld( 'ct_tag' , $params['tag'] ); 00253 global $wgOldChangeTagsIndex; 00254 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id'; 00255 } 00256 00257 $this->token = $params['token']; 00258 $this->addOption( 'LIMIT', $params['limit'] + 1 ); 00259 $this->addOption( 'USE INDEX', $index ); 00260 00261 $count = 0; 00262 /* Perform the actual query. */ 00263 $res = $this->select( __METHOD__ ); 00264 00265 $titles = array(); 00266 00267 $result = $this->getResult(); 00268 00269 /* Iterate through the rows, adding data extracted from them to our query result. */ 00270 foreach ( $res as $row ) { 00271 if ( ++ $count > $params['limit'] ) { 00272 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00273 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) ); 00274 break; 00275 } 00276 00277 if ( is_null( $resultPageSet ) ) { 00278 /* Extract the data from a single row. */ 00279 $vals = $this->extractRowInfo( $row ); 00280 00281 /* Add that row's data to our final output. */ 00282 if ( !$vals ) { 00283 continue; 00284 } 00285 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals ); 00286 if ( !$fit ) { 00287 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) ); 00288 break; 00289 } 00290 } else { 00291 $titles[] = Title::makeTitle( $row->rc_namespace, $row->rc_title ); 00292 } 00293 } 00294 00295 if ( is_null( $resultPageSet ) ) { 00296 /* Format the result */ 00297 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' ); 00298 } else { 00299 $resultPageSet->populateFromTitles( $titles ); 00300 } 00301 } 00302 00310 public function extractRowInfo( $row ) { 00311 /* If page was moved somewhere, get the title of the move target. */ 00312 $movedToTitle = false; 00313 if ( isset( $row->rc_moved_to_title ) && $row->rc_moved_to_title !== '' ) { 00314 $movedToTitle = Title::makeTitle( $row->rc_moved_to_ns, $row->rc_moved_to_title ); 00315 } 00316 00317 /* Determine the title of the page that has been changed. */ 00318 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title ); 00319 00320 /* Our output data. */ 00321 $vals = array(); 00322 00323 $type = intval( $row->rc_type ); 00324 00325 /* Determine what kind of change this was. */ 00326 switch ( $type ) { 00327 case RC_EDIT: 00328 $vals['type'] = 'edit'; 00329 break; 00330 case RC_NEW: 00331 $vals['type'] = 'new'; 00332 break; 00333 case RC_MOVE: 00334 $vals['type'] = 'move'; 00335 break; 00336 case RC_LOG: 00337 $vals['type'] = 'log'; 00338 break; 00339 case RC_MOVE_OVER_REDIRECT: 00340 $vals['type'] = 'move over redirect'; 00341 break; 00342 default: 00343 $vals['type'] = $type; 00344 } 00345 00346 /* Create a new entry in the result for the title. */ 00347 if ( $this->fld_title ) { 00348 ApiQueryBase::addTitleInfo( $vals, $title ); 00349 if ( $movedToTitle ) { 00350 ApiQueryBase::addTitleInfo( $vals, $movedToTitle, 'new_' ); 00351 } 00352 } 00353 00354 /* Add ids, such as rcid, pageid, revid, and oldid to the change's info. */ 00355 if ( $this->fld_ids ) { 00356 $vals['rcid'] = intval( $row->rc_id ); 00357 $vals['pageid'] = intval( $row->rc_cur_id ); 00358 $vals['revid'] = intval( $row->rc_this_oldid ); 00359 $vals['old_revid'] = intval( $row->rc_last_oldid ); 00360 } 00361 00362 /* Add user data and 'anon' flag, if use is anonymous. */ 00363 if ( $this->fld_user || $this->fld_userid ) { 00364 00365 if ( $this->fld_user ) { 00366 $vals['user'] = $row->rc_user_text; 00367 } 00368 00369 if ( $this->fld_userid ) { 00370 $vals['userid'] = $row->rc_user; 00371 } 00372 00373 if ( !$row->rc_user ) { 00374 $vals['anon'] = ''; 00375 } 00376 } 00377 00378 /* Add flags, such as new, minor, bot. */ 00379 if ( $this->fld_flags ) { 00380 if ( $row->rc_bot ) { 00381 $vals['bot'] = ''; 00382 } 00383 if ( $row->rc_new ) { 00384 $vals['new'] = ''; 00385 } 00386 if ( $row->rc_minor ) { 00387 $vals['minor'] = ''; 00388 } 00389 } 00390 00391 /* Add sizes of each revision. (Only available on 1.10+) */ 00392 if ( $this->fld_sizes ) { 00393 $vals['oldlen'] = intval( $row->rc_old_len ); 00394 $vals['newlen'] = intval( $row->rc_new_len ); 00395 } 00396 00397 /* Add the timestamp. */ 00398 if ( $this->fld_timestamp ) { 00399 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp ); 00400 } 00401 00402 /* Add edit summary / log summary. */ 00403 if ( $this->fld_comment && isset( $row->rc_comment ) ) { 00404 $vals['comment'] = $row->rc_comment; 00405 } 00406 00407 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) { 00408 $vals['parsedcomment'] = Linker::formatComment( $row->rc_comment, $title ); 00409 } 00410 00411 if ( $this->fld_redirect ) { 00412 if ( $row->page_is_redirect ) { 00413 $vals['redirect'] = ''; 00414 } 00415 } 00416 00417 /* Add the patrolled flag */ 00418 if ( $this->fld_patrolled && $row->rc_patrolled == 1 ) { 00419 $vals['patrolled'] = ''; 00420 } 00421 00422 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) { 00423 $vals['logid'] = intval( $row->rc_logid ); 00424 $vals['logtype'] = $row->rc_log_type; 00425 $vals['logaction'] = $row->rc_log_action; 00426 ApiQueryLogEvents::addLogParams( 00427 $this->getResult(), 00428 $vals, 00429 $row->rc_params, 00430 $row->rc_log_action, 00431 $row->rc_log_type, 00432 $row->rc_timestamp 00433 ); 00434 } 00435 00436 if ( $this->fld_tags ) { 00437 if ( $row->ts_tags ) { 00438 $tags = explode( ',', $row->ts_tags ); 00439 $this->getResult()->setIndexedTagName( $tags, 'tag' ); 00440 $vals['tags'] = $tags; 00441 } else { 00442 $vals['tags'] = array(); 00443 } 00444 } 00445 00446 if ( !is_null( $this->token ) ) { 00447 $tokenFunctions = $this->getTokenFunctions(); 00448 foreach ( $this->token as $t ) { 00449 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id, 00450 $title, RecentChange::newFromRow( $row ) ); 00451 if ( $val === false ) { 00452 $this->setWarning( "Action '$t' is not allowed for the current user" ); 00453 } else { 00454 $vals[$t . 'token'] = $val; 00455 } 00456 } 00457 } 00458 00459 return $vals; 00460 } 00461 00462 private function parseRCType( $type ) { 00463 if ( is_array( $type ) ) { 00464 $retval = array(); 00465 foreach ( $type as $t ) { 00466 $retval[] = $this->parseRCType( $t ); 00467 } 00468 return $retval; 00469 } 00470 switch( $type ) { 00471 case 'edit': 00472 return RC_EDIT; 00473 case 'new': 00474 return RC_NEW; 00475 case 'log': 00476 return RC_LOG; 00477 } 00478 } 00479 00480 public function getCacheMode( $params ) { 00481 if ( isset( $params['show'] ) ) { 00482 foreach ( $params['show'] as $show ) { 00483 if ( $show === 'patrolled' || $show === '!patrolled' ) { 00484 return 'private'; 00485 } 00486 } 00487 } 00488 if ( isset( $params['token'] ) ) { 00489 return 'private'; 00490 } 00491 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) { 00492 // formatComment() calls wfMsg() among other things 00493 return 'anon-public-user-private'; 00494 } 00495 return 'public'; 00496 } 00497 00498 public function getAllowedParams() { 00499 return array( 00500 'start' => array( 00501 ApiBase::PARAM_TYPE => 'timestamp' 00502 ), 00503 'end' => array( 00504 ApiBase::PARAM_TYPE => 'timestamp' 00505 ), 00506 'dir' => array( 00507 ApiBase::PARAM_DFLT => 'older', 00508 ApiBase::PARAM_TYPE => array( 00509 'newer', 00510 'older' 00511 ) 00512 ), 00513 'namespace' => array( 00514 ApiBase::PARAM_ISMULTI => true, 00515 ApiBase::PARAM_TYPE => 'namespace' 00516 ), 00517 'user' => array( 00518 ApiBase::PARAM_TYPE => 'user' 00519 ), 00520 'excludeuser' => array( 00521 ApiBase::PARAM_TYPE => 'user' 00522 ), 00523 'tag' => null, 00524 'prop' => array( 00525 ApiBase::PARAM_ISMULTI => true, 00526 ApiBase::PARAM_DFLT => 'title|timestamp|ids', 00527 ApiBase::PARAM_TYPE => array( 00528 'user', 00529 'userid', 00530 'comment', 00531 'parsedcomment', 00532 'flags', 00533 'timestamp', 00534 'title', 00535 'ids', 00536 'sizes', 00537 'redirect', 00538 'patrolled', 00539 'loginfo', 00540 'tags' 00541 ) 00542 ), 00543 'token' => array( 00544 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ), 00545 ApiBase::PARAM_ISMULTI => true 00546 ), 00547 'show' => array( 00548 ApiBase::PARAM_ISMULTI => true, 00549 ApiBase::PARAM_TYPE => array( 00550 'minor', 00551 '!minor', 00552 'bot', 00553 '!bot', 00554 'anon', 00555 '!anon', 00556 'redirect', 00557 '!redirect', 00558 'patrolled', 00559 '!patrolled' 00560 ) 00561 ), 00562 'limit' => array( 00563 ApiBase::PARAM_DFLT => 10, 00564 ApiBase::PARAM_TYPE => 'limit', 00565 ApiBase::PARAM_MIN => 1, 00566 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00567 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00568 ), 00569 'type' => array( 00570 ApiBase::PARAM_ISMULTI => true, 00571 ApiBase::PARAM_TYPE => array( 00572 'edit', 00573 'new', 00574 'log' 00575 ) 00576 ), 00577 'toponly' => false, 00578 ); 00579 } 00580 00581 public function getParamDescription() { 00582 $p = $this->getModulePrefix(); 00583 return array( 00584 'start' => 'The timestamp to start enumerating from', 00585 'end' => 'The timestamp to end enumerating', 00586 'dir' => $this->getDirectionDescription( $p ), 00587 'namespace' => 'Filter log entries to only this namespace(s)', 00588 'user' => 'Only list changes by this user', 00589 'excludeuser' => 'Don\'t list changes by this user', 00590 'prop' => array( 00591 'Include additional pieces of information', 00592 ' user - Adds the user responsible for the edit and tags if they are an IP', 00593 ' userid - Adds the user id responsible for the edit', 00594 ' comment - Adds the comment for the edit', 00595 ' parsedcomment - Adds the parsed comment for the edit', 00596 ' flags - Adds flags for the edit', 00597 ' timestamp - Adds timestamp of the edit', 00598 ' title - Adds the page title of the edit', 00599 ' ids - Adds the page ID, recent changes ID and the new and old revision ID', 00600 ' sizes - Adds the new and old page length in bytes', 00601 ' redirect - Tags edit if page is a redirect', 00602 ' patrolled - Tags edits that have been patrolled', 00603 ' loginfo - Adds log information (logid, logtype, etc) to log entries', 00604 ' tags - Lists tags for the entry', 00605 ), 00606 'token' => 'Which tokens to obtain for each change', 00607 'show' => array( 00608 'Show only items that meet this criteria.', 00609 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon" 00610 ), 00611 'type' => 'Which types of changes to show', 00612 'limit' => 'How many total changes to return', 00613 'tag' => 'Only list changes tagged with this tag', 00614 'toponly' => 'Only list changes which are the latest revision', 00615 ); 00616 } 00617 00618 public function getDescription() { 00619 return 'Enumerate recent changes'; 00620 } 00621 00622 public function getPossibleErrors() { 00623 return array_merge( parent::getPossibleErrors(), array( 00624 array( 'show' ), 00625 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ), 00626 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ), 00627 ) ); 00628 } 00629 00630 public function getExamples() { 00631 return array( 00632 'api.php?action=query&list=recentchanges' 00633 ); 00634 } 00635 00636 public function getHelpUrls() { 00637 return 'https://www.mediawiki.org/wiki/API:Recentchanges'; 00638 } 00639 00640 public function getVersion() { 00641 return __CLASS__ . ': $Id$'; 00642 } 00643 }