MediaWiki
REL1_21
|
00001 <?php 00032 class ApiQueryContributions extends ApiQueryBase { 00033 00034 public function __construct( $query, $moduleName ) { 00035 parent::__construct( $query, $moduleName, 'uc' ); 00036 } 00037 00038 private $params, $prefixMode, $userprefix, $multiUserMode, $usernames, $parentLens; 00039 private $fld_ids = false, $fld_title = false, $fld_timestamp = false, 00040 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false, 00041 $fld_patrolled = false, $fld_tags = false, $fld_size = false, $fld_sizediff = false; 00042 00043 public function execute() { 00044 // Parse some parameters 00045 $this->params = $this->extractRequestParams(); 00046 00047 $prop = array_flip( $this->params['prop'] ); 00048 $this->fld_ids = isset( $prop['ids'] ); 00049 $this->fld_title = isset( $prop['title'] ); 00050 $this->fld_comment = isset( $prop['comment'] ); 00051 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] ); 00052 $this->fld_size = isset( $prop['size'] ); 00053 $this->fld_sizediff = isset( $prop['sizediff'] ); 00054 $this->fld_flags = isset( $prop['flags'] ); 00055 $this->fld_timestamp = isset( $prop['timestamp'] ); 00056 $this->fld_patrolled = isset( $prop['patrolled'] ); 00057 $this->fld_tags = isset( $prop['tags'] ); 00058 00059 // TODO: if the query is going only against the revision table, should this be done? 00060 $this->selectNamedDB( 'contributions', DB_SLAVE, 'contributions' ); 00061 00062 if ( isset( $this->params['userprefix'] ) ) { 00063 $this->prefixMode = true; 00064 $this->multiUserMode = true; 00065 $this->userprefix = $this->params['userprefix']; 00066 } else { 00067 $this->usernames = array(); 00068 if ( !is_array( $this->params['user'] ) ) { 00069 $this->params['user'] = array( $this->params['user'] ); 00070 } 00071 if ( !count( $this->params['user'] ) ) { 00072 $this->dieUsage( 'User parameter may not be empty.', 'param_user' ); 00073 } 00074 foreach ( $this->params['user'] as $u ) { 00075 $this->prepareUsername( $u ); 00076 } 00077 $this->prefixMode = false; 00078 $this->multiUserMode = ( count( $this->params['user'] ) > 1 ); 00079 } 00080 00081 $this->prepareQuery(); 00082 00083 // Do the actual query. 00084 $res = $this->select( __METHOD__ ); 00085 00086 if( $this->fld_sizediff ) { 00087 $revIds = array(); 00088 foreach ( $res as $row ) { 00089 if( $row->rev_parent_id ) { 00090 $revIds[] = $row->rev_parent_id; 00091 } 00092 } 00093 $this->parentLens = Revision::getParentLengths( $this->getDB(), $revIds ); 00094 $res->rewind(); // reset 00095 } 00096 00097 // Initialise some variables 00098 $count = 0; 00099 $limit = $this->params['limit']; 00100 00101 // Fetch each row 00102 foreach ( $res as $row ) { 00103 if ( ++ $count > $limit ) { 00104 // We've reached the one extra which shows that there are additional pages to be had. Stop here... 00105 if ( $this->multiUserMode ) { 00106 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) ); 00107 } else { 00108 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) ); 00109 } 00110 break; 00111 } 00112 00113 $vals = $this->extractRowInfo( $row ); 00114 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals ); 00115 if ( !$fit ) { 00116 if ( $this->multiUserMode ) { 00117 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) ); 00118 } else { 00119 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) ); 00120 } 00121 break; 00122 } 00123 } 00124 00125 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' ); 00126 } 00127 00134 private function prepareUsername( $user ) { 00135 if ( !is_null( $user ) && $user !== '' ) { 00136 $name = User::isIP( $user ) 00137 ? $user 00138 : User::getCanonicalName( $user, 'valid' ); 00139 if ( $name === false ) { 00140 $this->dieUsage( "User name {$user} is not valid", 'param_user' ); 00141 } else { 00142 $this->usernames[] = $name; 00143 } 00144 } else { 00145 $this->dieUsage( 'User parameter may not be empty', 'param_user' ); 00146 } 00147 } 00148 00152 private function prepareQuery() { 00153 // We're after the revision table, and the corresponding page 00154 // row for anything we retrieve. We may also need the 00155 // recentchanges row and/or tag summary row. 00156 $user = $this->getUser(); 00157 $tables = array( 'page', 'revision' ); // Order may change 00158 $this->addWhere( 'page_id=rev_page' ); 00159 00160 // Handle continue parameter 00161 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) { 00162 $continue = explode( '|', $this->params['continue'] ); 00163 $this->dieContinueUsageIf( count( $continue ) != 2 ); 00164 $db = $this->getDB(); 00165 $encUser = $db->addQuotes( $continue[0] ); 00166 $encTS = $db->addQuotes( $db->timestamp( $continue[1] ) ); 00167 $op = ( $this->params['dir'] == 'older' ? '<' : '>' ); 00168 $this->addWhere( 00169 "rev_user_text $op $encUser OR " . 00170 "(rev_user_text = $encUser AND " . 00171 "rev_timestamp $op= $encTS)" 00172 ); 00173 } 00174 00175 if ( !$user->isAllowed( 'hideuser' ) ) { 00176 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ); 00177 } 00178 // We only want pages by the specified users. 00179 if ( $this->prefixMode ) { 00180 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) ); 00181 } else { 00182 $this->addWhereFld( 'rev_user_text', $this->usernames ); 00183 } 00184 // ... and in the specified timeframe. 00185 // Ensure the same sort order for rev_user_text and rev_timestamp 00186 // so our query is indexed 00187 if ( $this->multiUserMode ) { 00188 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null ); 00189 } 00190 $this->addTimestampWhereRange( 'rev_timestamp', 00191 $this->params['dir'], $this->params['start'], $this->params['end'] ); 00192 $this->addWhereFld( 'page_namespace', $this->params['namespace'] ); 00193 00194 $show = $this->params['show']; 00195 if ( !is_null( $show ) ) { 00196 $show = array_flip( $show ); 00197 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) ) 00198 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) ) { 00199 $this->dieUsageMsg( 'show' ); 00200 } 00201 00202 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) ); 00203 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) ); 00204 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) ); 00205 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) ); 00206 } 00207 $this->addOption( 'LIMIT', $this->params['limit'] + 1 ); 00208 $index = array( 'revision' => 'usertext_timestamp' ); 00209 00210 // Mandatory fields: timestamp allows request continuation 00211 // ns+title checks if the user has access rights for this page 00212 // user_text is necessary if multiple users were specified 00213 $this->addFields( array( 00214 'rev_timestamp', 00215 'page_namespace', 00216 'page_title', 00217 'rev_user', 00218 'rev_user_text', 00219 'rev_deleted' 00220 ) ); 00221 00222 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) || 00223 $this->fld_patrolled ) { 00224 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) { 00225 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' ); 00226 } 00227 00228 // Use a redundant join condition on both 00229 // timestamp and ID so we can use the timestamp 00230 // index 00231 $index['recentchanges'] = 'rc_user_text'; 00232 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) { 00233 // Put the tables in the right order for 00234 // STRAIGHT_JOIN 00235 $tables = array( 'revision', 'recentchanges', 'page' ); 00236 $this->addOption( 'STRAIGHT_JOIN' ); 00237 $this->addWhere( 'rc_user_text=rev_user_text' ); 00238 $this->addWhere( 'rc_timestamp=rev_timestamp' ); 00239 $this->addWhere( 'rc_this_oldid=rev_id' ); 00240 } else { 00241 $tables[] = 'recentchanges'; 00242 $this->addJoinConds( array( 'recentchanges' => array( 00243 'LEFT JOIN', array( 00244 'rc_user_text=rev_user_text', 00245 'rc_timestamp=rev_timestamp', 00246 'rc_this_oldid=rev_id' ) ) ) ); 00247 } 00248 } 00249 00250 $this->addTables( $tables ); 00251 $this->addFieldsIf( 'rev_page', $this->fld_ids ); 00252 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags ); 00253 $this->addFieldsIf( 'page_latest', $this->fld_flags ); 00254 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed? 00255 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment ); 00256 $this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff ); 00257 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags ); 00258 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff ); 00259 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled ); 00260 00261 if ( $this->fld_tags ) { 00262 $this->addTables( 'tag_summary' ); 00263 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) ); 00264 $this->addFields( 'ts_tags' ); 00265 } 00266 00267 if ( isset( $this->params['tag'] ) ) { 00268 $this->addTables( 'change_tag' ); 00269 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) ); 00270 $this->addWhereFld( 'ct_tag', $this->params['tag'] ); 00271 global $wgOldChangeTagsIndex; 00272 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id'; 00273 } 00274 00275 if ( $this->params['toponly'] ) { 00276 $this->addWhere( 'rev_id = page_latest' ); 00277 } 00278 00279 $this->addOption( 'USE INDEX', $index ); 00280 } 00281 00288 private function extractRowInfo( $row ) { 00289 $vals = array(); 00290 00291 $vals['userid'] = $row->rev_user; 00292 $vals['user'] = $row->rev_user_text; 00293 if ( $row->rev_deleted & Revision::DELETED_USER ) { 00294 $vals['userhidden'] = ''; 00295 } 00296 if ( $this->fld_ids ) { 00297 $vals['pageid'] = intval( $row->rev_page ); 00298 $vals['revid'] = intval( $row->rev_id ); 00299 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed? 00300 } 00301 00302 $title = Title::makeTitle( $row->page_namespace, $row->page_title ); 00303 00304 if ( $this->fld_title ) { 00305 ApiQueryBase::addTitleInfo( $vals, $title ); 00306 } 00307 00308 if ( $this->fld_timestamp ) { 00309 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp ); 00310 } 00311 00312 if ( $this->fld_flags ) { 00313 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) { 00314 $vals['new'] = ''; 00315 } 00316 if ( $row->rev_minor_edit ) { 00317 $vals['minor'] = ''; 00318 } 00319 if ( $row->page_latest == $row->rev_id ) { 00320 $vals['top'] = ''; 00321 } 00322 } 00323 00324 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) { 00325 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) { 00326 $vals['commenthidden'] = ''; 00327 } else { 00328 if ( $this->fld_comment ) { 00329 $vals['comment'] = $row->rev_comment; 00330 } 00331 00332 if ( $this->fld_parsedcomment ) { 00333 $vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title ); 00334 } 00335 } 00336 } 00337 00338 if ( $this->fld_patrolled && $row->rc_patrolled ) { 00339 $vals['patrolled'] = ''; 00340 } 00341 00342 if ( $this->fld_size && !is_null( $row->rev_len ) ) { 00343 $vals['size'] = intval( $row->rev_len ); 00344 } 00345 00346 if ( $this->fld_sizediff && !is_null( $row->rev_len ) && !is_null( $row->rev_parent_id ) ) { 00347 $parentLen = isset( $this->parentLens[$row->rev_parent_id] ) ? $this->parentLens[$row->rev_parent_id] : 0; 00348 $vals['sizediff'] = intval( $row->rev_len - $parentLen ); 00349 } 00350 00351 if ( $this->fld_tags ) { 00352 if ( $row->ts_tags ) { 00353 $tags = explode( ',', $row->ts_tags ); 00354 $this->getResult()->setIndexedTagName( $tags, 'tag' ); 00355 $vals['tags'] = $tags; 00356 } else { 00357 $vals['tags'] = array(); 00358 } 00359 } 00360 00361 return $vals; 00362 } 00363 00364 private function continueStr( $row ) { 00365 return $row->rev_user_text . '|' . 00366 wfTimestamp( TS_ISO_8601, $row->rev_timestamp ); 00367 } 00368 00369 public function getCacheMode( $params ) { 00370 // This module provides access to deleted revisions and patrol flags if 00371 // the requester is logged in 00372 return 'anon-public-user-private'; 00373 } 00374 00375 public function getAllowedParams() { 00376 return array( 00377 'limit' => array( 00378 ApiBase::PARAM_DFLT => 10, 00379 ApiBase::PARAM_TYPE => 'limit', 00380 ApiBase::PARAM_MIN => 1, 00381 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00382 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00383 ), 00384 'start' => array( 00385 ApiBase::PARAM_TYPE => 'timestamp' 00386 ), 00387 'end' => array( 00388 ApiBase::PARAM_TYPE => 'timestamp' 00389 ), 00390 'continue' => null, 00391 'user' => array( 00392 ApiBase::PARAM_ISMULTI => true 00393 ), 00394 'userprefix' => null, 00395 'dir' => array( 00396 ApiBase::PARAM_DFLT => 'older', 00397 ApiBase::PARAM_TYPE => array( 00398 'newer', 00399 'older' 00400 ) 00401 ), 00402 'namespace' => array( 00403 ApiBase::PARAM_ISMULTI => true, 00404 ApiBase::PARAM_TYPE => 'namespace' 00405 ), 00406 'prop' => array( 00407 ApiBase::PARAM_ISMULTI => true, 00408 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags', 00409 ApiBase::PARAM_TYPE => array( 00410 'ids', 00411 'title', 00412 'timestamp', 00413 'comment', 00414 'parsedcomment', 00415 'size', 00416 'sizediff', 00417 'flags', 00418 'patrolled', 00419 'tags' 00420 ) 00421 ), 00422 'show' => array( 00423 ApiBase::PARAM_ISMULTI => true, 00424 ApiBase::PARAM_TYPE => array( 00425 'minor', 00426 '!minor', 00427 'patrolled', 00428 '!patrolled', 00429 ) 00430 ), 00431 'tag' => null, 00432 'toponly' => false, 00433 ); 00434 } 00435 00436 public function getParamDescription() { 00437 global $wgRCMaxAge; 00438 $p = $this->getModulePrefix(); 00439 return array( 00440 'limit' => 'The maximum number of contributions to return', 00441 'start' => 'The start timestamp to return from', 00442 'end' => 'The end timestamp to return to', 00443 'continue' => 'When more results are available, use this to continue', 00444 'user' => 'The users to retrieve contributions for', 00445 'userprefix' => "Retrieve contributions for all users whose names begin with this value. Overrides {$p}user", 00446 'dir' => $this->getDirectionDescription( $p ), 00447 'namespace' => 'Only list contributions in these namespaces', 00448 'prop' => array( 00449 'Include additional pieces of information', 00450 ' ids - Adds the page ID and revision ID', 00451 ' title - Adds the title and namespace ID of the page', 00452 ' timestamp - Adds the timestamp of the edit', 00453 ' comment - Adds the comment of the edit', 00454 ' parsedcomment - Adds the parsed comment of the edit', 00455 ' size - Adds the new size of the edit', 00456 ' sizediff - Adds the size delta of the edit against its parent', 00457 ' flags - Adds flags of the edit', 00458 ' patrolled - Tags patrolled edits', 00459 ' tags - Lists tags for the edit', 00460 ), 00461 'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor", 00462 "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than \$wgRCMaxAge ($wgRCMaxAge) won't be shown", ), 00463 'tag' => 'Only list revisions tagged with this tag', 00464 'toponly' => 'Only list changes which are the latest revision', 00465 ); 00466 } 00467 00468 public function getResultProperties() { 00469 return array( 00470 '' => array( 00471 'userid' => 'integer', 00472 'user' => 'string', 00473 'userhidden' => 'boolean' 00474 ), 00475 'ids' => array( 00476 'pageid' => 'integer', 00477 'revid' => 'integer' 00478 ), 00479 'title' => array( 00480 'ns' => 'namespace', 00481 'title' => 'string' 00482 ), 00483 'timestamp' => array( 00484 'timestamp' => 'timestamp' 00485 ), 00486 'flags' => array( 00487 'new' => 'boolean', 00488 'minor' => 'boolean', 00489 'top' => 'boolean' 00490 ), 00491 'comment' => array( 00492 'commenthidden' => 'boolean', 00493 'comment' => array( 00494 ApiBase::PROP_TYPE => 'string', 00495 ApiBase::PROP_NULLABLE => true 00496 ) 00497 ), 00498 'parsedcomment' => array( 00499 'commenthidden' => 'boolean', 00500 'parsedcomment' => array( 00501 ApiBase::PROP_TYPE => 'string', 00502 ApiBase::PROP_NULLABLE => true 00503 ) 00504 ), 00505 'patrolled' => array( 00506 'patrolled' => 'boolean' 00507 ), 00508 'size' => array( 00509 'size' => array( 00510 ApiBase::PROP_TYPE => 'integer', 00511 ApiBase::PROP_NULLABLE => true 00512 ) 00513 ), 00514 'sizediff' => array( 00515 'sizediff' => array( 00516 ApiBase::PROP_TYPE => 'integer', 00517 ApiBase::PROP_NULLABLE => true 00518 ) 00519 ) 00520 ); 00521 } 00522 00523 public function getDescription() { 00524 return 'Get all edits by a user'; 00525 } 00526 00527 public function getPossibleErrors() { 00528 return array_merge( parent::getPossibleErrors(), array( 00529 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ), 00530 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ), 00531 array( 'show' ), 00532 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ), 00533 ) ); 00534 } 00535 00536 public function getExamples() { 00537 return array( 00538 'api.php?action=query&list=usercontribs&ucuser=YurikBot', 00539 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.', 00540 ); 00541 } 00542 00543 public function getHelpUrls() { 00544 return 'https://www.mediawiki.org/wiki/API:Usercontribs'; 00545 } 00546 }