MediaWiki  REL1_22
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             $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 || $this->fld_ids );
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             $index['change_tag'] = 'change_tag_tag_id';
00272         }
00273 
00274         if ( $this->params['toponly'] ) {
00275             $this->addWhere( 'rev_id = page_latest' );
00276         }
00277 
00278         $this->addOption( 'USE INDEX', $index );
00279     }
00280 
00287     private function extractRowInfo( $row ) {
00288         $vals = array();
00289 
00290         $vals['userid'] = $row->rev_user;
00291         $vals['user'] = $row->rev_user_text;
00292         if ( $row->rev_deleted & Revision::DELETED_USER ) {
00293             $vals['userhidden'] = '';
00294         }
00295         if ( $this->fld_ids ) {
00296             $vals['pageid'] = intval( $row->rev_page );
00297             $vals['revid'] = intval( $row->rev_id );
00298             // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
00299 
00300             if ( !is_null( $row->rev_parent_id ) ) {
00301                 $vals['parentid'] = intval( $row->rev_parent_id );
00302             }
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 contributions 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                 'parentid' => array(
00482                     ApiBase::PROP_TYPE => 'integer',
00483                     ApiBase::PROP_NULLABLE => true
00484                 )
00485             ),
00486             'title' => array(
00487                 'ns' => 'namespace',
00488                 'title' => 'string'
00489             ),
00490             'timestamp' => array(
00491                 'timestamp' => 'timestamp'
00492             ),
00493             'flags' => array(
00494                 'new' => 'boolean',
00495                 'minor' => 'boolean',
00496                 'top' => 'boolean'
00497             ),
00498             'comment' => array(
00499                 'commenthidden' => 'boolean',
00500                 'comment' => array(
00501                     ApiBase::PROP_TYPE => 'string',
00502                     ApiBase::PROP_NULLABLE => true
00503                 )
00504             ),
00505             'parsedcomment' => array(
00506                 'commenthidden' => 'boolean',
00507                 'parsedcomment' => array(
00508                     ApiBase::PROP_TYPE => 'string',
00509                     ApiBase::PROP_NULLABLE => true
00510                 )
00511             ),
00512             'patrolled' => array(
00513                 'patrolled' => 'boolean'
00514             ),
00515             'size' => array(
00516                 'size' => array(
00517                     ApiBase::PROP_TYPE => 'integer',
00518                     ApiBase::PROP_NULLABLE => true
00519                 )
00520             ),
00521             'sizediff' => array(
00522                 'sizediff' => array(
00523                     ApiBase::PROP_TYPE => 'integer',
00524                     ApiBase::PROP_NULLABLE => true
00525                 )
00526             )
00527         );
00528     }
00529 
00530     public function getDescription() {
00531         return 'Get all edits by a user';
00532     }
00533 
00534     public function getPossibleErrors() {
00535         return array_merge( parent::getPossibleErrors(), array(
00536             array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
00537             array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
00538             array( 'show' ),
00539             array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
00540         ) );
00541     }
00542 
00543     public function getExamples() {
00544         return array(
00545             'api.php?action=query&list=usercontribs&ucuser=YurikBot',
00546             'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
00547         );
00548     }
00549 
00550     public function getHelpUrls() {
00551         return 'https://www.mediawiki.org/wiki/API:Usercontribs';
00552     }
00553 }