MediaWiki  REL1_20
ApiQueryUserContributions.php
Go to the documentation of this file.
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                         if ( count( $continue ) != 2 ) {
00164                                 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
00165                                         'value returned by the previous query', '_badcontinue' );
00166                         }
00167                         $db = $this->getDB();
00168                         $encUser = $db->addQuotes( $continue[0] );
00169                         $encTS = $db->addQuotes( $db->timestamp( $continue[1] ) );
00170                         $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
00171                         $this->addWhere(
00172                                 "rev_user_text $op $encUser OR " .
00173                                 "(rev_user_text = $encUser AND " .
00174                                 "rev_timestamp $op= $encTS)"
00175                         );
00176                 }
00177 
00178                 if ( !$user->isAllowed( 'hideuser' ) ) {
00179                         $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
00180                 }
00181                 // We only want pages by the specified users.
00182                 if ( $this->prefixMode ) {
00183                         $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
00184                 } else {
00185                         $this->addWhereFld( 'rev_user_text', $this->usernames );
00186                 }
00187                 // ... and in the specified timeframe.
00188                 // Ensure the same sort order for rev_user_text and rev_timestamp
00189                 // so our query is indexed
00190                 if ( $this->multiUserMode ) {
00191                         $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
00192                 }
00193                 $this->addTimestampWhereRange( 'rev_timestamp',
00194                         $this->params['dir'], $this->params['start'], $this->params['end'] );
00195                 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
00196 
00197                 $show = $this->params['show'];
00198                 if ( !is_null( $show ) ) {
00199                         $show = array_flip( $show );
00200                         if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
00201                                         || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) ) {
00202                                 $this->dieUsageMsg( 'show' );
00203                         }
00204 
00205                         $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
00206                         $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
00207                         $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
00208                         $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
00209                 }
00210                 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
00211                 $index = array( 'revision' => 'usertext_timestamp' );
00212 
00213                 // Mandatory fields: timestamp allows request continuation
00214                 // ns+title checks if the user has access rights for this page
00215                 // user_text is necessary if multiple users were specified
00216                 $this->addFields( array(
00217                         'rev_timestamp',
00218                         'page_namespace',
00219                         'page_title',
00220                         'rev_user',
00221                         'rev_user_text',
00222                         'rev_deleted'
00223                 ) );
00224 
00225                 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
00226                                  $this->fld_patrolled ) {
00227                         if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
00228                                 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
00229                         }
00230 
00231                         // Use a redundant join condition on both
00232                         // timestamp and ID so we can use the timestamp
00233                         // index
00234                         $index['recentchanges'] = 'rc_user_text';
00235                         if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
00236                                 // Put the tables in the right order for
00237                                 // STRAIGHT_JOIN
00238                                 $tables = array( 'revision', 'recentchanges', 'page' );
00239                                 $this->addOption( 'STRAIGHT_JOIN' );
00240                                 $this->addWhere( 'rc_user_text=rev_user_text' );
00241                                 $this->addWhere( 'rc_timestamp=rev_timestamp' );
00242                                 $this->addWhere( 'rc_this_oldid=rev_id' );
00243                         } else {
00244                                 $tables[] = 'recentchanges';
00245                                 $this->addJoinConds( array( 'recentchanges' => array(
00246                                         'LEFT JOIN', array(
00247                                                 'rc_user_text=rev_user_text',
00248                                                 'rc_timestamp=rev_timestamp',
00249                                                 'rc_this_oldid=rev_id' ) ) ) );
00250                         }
00251                 }
00252 
00253                 $this->addTables( $tables );
00254                 $this->addFieldsIf( 'rev_page', $this->fld_ids );
00255                 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags );
00256                 $this->addFieldsIf( 'page_latest', $this->fld_flags );
00257                 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
00258                 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
00259                 $this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff );
00260                 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
00261                 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff );
00262                 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
00263 
00264                 if ( $this->fld_tags ) {
00265                         $this->addTables( 'tag_summary' );
00266                         $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
00267                         $this->addFields( 'ts_tags' );
00268                 }
00269 
00270                 if ( isset( $this->params['tag'] ) ) {
00271                         $this->addTables( 'change_tag' );
00272                         $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
00273                         $this->addWhereFld( 'ct_tag', $this->params['tag'] );
00274                         global $wgOldChangeTagsIndex;
00275                         $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
00276                 }
00277 
00278                 if ( $this->params['toponly'] ) {
00279                         $this->addWhere( 'rev_id = page_latest' );
00280                 }
00281 
00282                 $this->addOption( 'USE INDEX', $index );
00283         }
00284 
00291         private function extractRowInfo( $row ) {
00292                 $vals = array();
00293 
00294                 $vals['userid'] = $row->rev_user;
00295                 $vals['user'] = $row->rev_user_text;
00296                 if ( $row->rev_deleted & Revision::DELETED_USER ) {
00297                         $vals['userhidden'] = '';
00298                 }
00299                 if ( $this->fld_ids ) {
00300                         $vals['pageid'] = intval( $row->rev_page );
00301                         $vals['revid'] = intval( $row->rev_id );
00302                         // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
00303                 }
00304 
00305                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00306 
00307                 if ( $this->fld_title ) {
00308                         ApiQueryBase::addTitleInfo( $vals, $title );
00309                 }
00310 
00311                 if ( $this->fld_timestamp ) {
00312                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
00313                 }
00314 
00315                 if ( $this->fld_flags ) {
00316                         if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
00317                                 $vals['new'] = '';
00318                         }
00319                         if ( $row->rev_minor_edit ) {
00320                                 $vals['minor'] = '';
00321                         }
00322                         if ( $row->page_latest == $row->rev_id ) {
00323                                 $vals['top'] = '';
00324                         }
00325                 }
00326 
00327                 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
00328                         if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
00329                                 $vals['commenthidden'] = '';
00330                         } else {
00331                                 if ( $this->fld_comment ) {
00332                                         $vals['comment'] = $row->rev_comment;
00333                                 }
00334 
00335                                 if ( $this->fld_parsedcomment ) {
00336                                         $vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title );
00337                                 }
00338                         }
00339                 }
00340 
00341                 if ( $this->fld_patrolled && $row->rc_patrolled ) {
00342                         $vals['patrolled'] = '';
00343                 }
00344 
00345                 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
00346                         $vals['size'] = intval( $row->rev_len );
00347                 }
00348 
00349                 if ( $this->fld_sizediff && !is_null( $row->rev_len ) && !is_null( $row->rev_parent_id ) ) {
00350                         $parentLen = isset( $this->parentLens[$row->rev_parent_id] ) ? $this->parentLens[$row->rev_parent_id] : 0;
00351                         $vals['sizediff'] = intval( $row->rev_len - $parentLen );
00352                 }
00353 
00354                 if ( $this->fld_tags ) {
00355                         if ( $row->ts_tags ) {
00356                                 $tags = explode( ',', $row->ts_tags );
00357                                 $this->getResult()->setIndexedTagName( $tags, 'tag' );
00358                                 $vals['tags'] = $tags;
00359                         } else {
00360                                 $vals['tags'] = array();
00361                         }
00362                 }
00363 
00364                 return $vals;
00365         }
00366 
00367         private function continueStr( $row ) {
00368                 return $row->rev_user_text . '|' .
00369                         wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
00370         }
00371 
00372         public function getCacheMode( $params ) {
00373                 // This module provides access to deleted revisions and patrol flags if
00374                 // the requester is logged in
00375                 return 'anon-public-user-private';
00376         }
00377 
00378         public function getAllowedParams() {
00379                 return array(
00380                         'limit' => array(
00381                                 ApiBase::PARAM_DFLT => 10,
00382                                 ApiBase::PARAM_TYPE => 'limit',
00383                                 ApiBase::PARAM_MIN => 1,
00384                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00385                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00386                         ),
00387                         'start' => array(
00388                                 ApiBase::PARAM_TYPE => 'timestamp'
00389                         ),
00390                         'end' => array(
00391                                 ApiBase::PARAM_TYPE => 'timestamp'
00392                         ),
00393                         'continue' => null,
00394                         'user' => array(
00395                                 ApiBase::PARAM_ISMULTI => true
00396                         ),
00397                         'userprefix' => null,
00398                         'dir' => array(
00399                                 ApiBase::PARAM_DFLT => 'older',
00400                                 ApiBase::PARAM_TYPE => array(
00401                                         'newer',
00402                                         'older'
00403                                 )
00404                         ),
00405                         'namespace' => array(
00406                                 ApiBase::PARAM_ISMULTI => true,
00407                                 ApiBase::PARAM_TYPE => 'namespace'
00408                         ),
00409                         'prop' => array(
00410                                 ApiBase::PARAM_ISMULTI => true,
00411                                 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
00412                                 ApiBase::PARAM_TYPE => array(
00413                                         'ids',
00414                                         'title',
00415                                         'timestamp',
00416                                         'comment',
00417                                         'parsedcomment',
00418                                         'size',
00419                                         'sizediff',
00420                                         'flags',
00421                                         'patrolled',
00422                                         'tags'
00423                                 )
00424                         ),
00425                         'show' => array(
00426                                 ApiBase::PARAM_ISMULTI => true,
00427                                 ApiBase::PARAM_TYPE => array(
00428                                         'minor',
00429                                         '!minor',
00430                                         'patrolled',
00431                                         '!patrolled',
00432                                 )
00433                         ),
00434                         'tag' => null,
00435                         'toponly' => false,
00436                 );
00437         }
00438 
00439         public function getParamDescription() {
00440                 global $wgRCMaxAge;
00441                 $p = $this->getModulePrefix();
00442                 return array(
00443                         'limit' => 'The maximum number of contributions to return',
00444                         'start' => 'The start timestamp to return from',
00445                         'end' => 'The end timestamp to return to',
00446                         'continue' => 'When more results are available, use this to continue',
00447                         'user' => 'The users to retrieve contributions for',
00448                         'userprefix' => "Retrieve contibutions for all users whose names begin with this value. Overrides {$p}user",
00449                         'dir' => $this->getDirectionDescription( $p ),
00450                         'namespace' => 'Only list contributions in these namespaces',
00451                         'prop' => array(
00452                                 'Include additional pieces of information',
00453                                 ' ids            - Adds the page ID and revision ID',
00454                                 ' title          - Adds the title and namespace ID of the page',
00455                                 ' timestamp      - Adds the timestamp of the edit',
00456                                 ' comment        - Adds the comment of the edit',
00457                                 ' parsedcomment  - Adds the parsed comment of the edit',
00458                                 ' size           - Adds the new size of the edit',
00459                                 ' sizediff       - Adds the size delta of the edit against its parent',
00460                                 ' flags          - Adds flags of the edit',
00461                                 ' patrolled      - Tags patrolled edits',
00462                                 ' tags           - Lists tags for the edit',
00463                         ),
00464                         'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor",
00465                                         "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than \$wgRCMaxAge ($wgRCMaxAge) won't be shown", ),
00466                         'tag' => 'Only list revisions tagged with this tag',
00467                         'toponly' => 'Only list changes which are the latest revision',
00468                 );
00469         }
00470 
00471         public function getResultProperties() {
00472                 return array(
00473                         '' => array(
00474                                 'userid' => 'integer',
00475                                 'user' => 'string',
00476                                 'userhidden' => 'boolean'
00477                         ),
00478                         'ids' => array(
00479                                 'pageid' => 'integer',
00480                                 'revid' => 'integer'
00481                         ),
00482                         'title' => array(
00483                                 'ns' => 'namespace',
00484                                 'title' => 'string'
00485                         ),
00486                         'timestamp' => array(
00487                                 'timestamp' => 'timestamp'
00488                         ),
00489                         'flags' => array(
00490                                 'new' => 'boolean',
00491                                 'minor' => 'boolean',
00492                                 'top' => 'boolean'
00493                         ),
00494                         'comment' => array(
00495                                 'commenthidden' => 'boolean',
00496                                 'comment' => array(
00497                                         ApiBase::PROP_TYPE => 'string',
00498                                         ApiBase::PROP_NULLABLE => true
00499                                 )
00500                         ),
00501                         'parsedcomment' => array(
00502                                 'commenthidden' => 'boolean',
00503                                 'parsedcomment' => array(
00504                                         ApiBase::PROP_TYPE => 'string',
00505                                         ApiBase::PROP_NULLABLE => true
00506                                 )
00507                         ),
00508                         'patrolled' => array(
00509                                 'patrolled' => 'boolean'
00510                         ),
00511                         'size' => array(
00512                                 'size' => array(
00513                                         ApiBase::PROP_TYPE => 'integer',
00514                                         ApiBase::PROP_NULLABLE => true
00515                                 )
00516                         ),
00517                         'sizediff' => array(
00518                                 'sizediff' => array(
00519                                         ApiBase::PROP_TYPE => 'integer',
00520                                         ApiBase::PROP_NULLABLE => true
00521                                 )
00522                         )
00523                 );
00524         }
00525 
00526         public function getDescription() {
00527                 return 'Get all edits by a user';
00528         }
00529 
00530         public function getPossibleErrors() {
00531                 return array_merge( parent::getPossibleErrors(), array(
00532                         array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
00533                         array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
00534                         array( 'show' ),
00535                         array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
00536                 ) );
00537         }
00538 
00539         public function getExamples() {
00540                 return array(
00541                         'api.php?action=query&list=usercontribs&ucuser=YurikBot',
00542                         'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
00543                 );
00544         }
00545 
00546         public function getHelpUrls() {
00547                 return 'https://www.mediawiki.org/wiki/API:Usercontribs';
00548         }
00549 
00550         public function getVersion() {
00551                 return __CLASS__ . ': $Id$';
00552         }
00553 }