5 * Created on Oct 16, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * This query action adds a list of a specified user's contributions to the output.
32 class ApiQueryContributions
extends ApiQueryBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'uc' );
38 private $params, $prefixMode, $userprefix, $multiUserMode, $usernames, $parentLens;
39 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
40 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
41 $fld_patrolled = false, $fld_tags = false, $fld_size = false, $fld_sizediff = false;
43 public function execute() {
44 // Parse some parameters
45 $this->params
= $this->extractRequestParams();
47 $prop = array_flip( $this->params
['prop'] );
48 $this->fld_ids
= isset( $prop['ids'] );
49 $this->fld_title
= isset( $prop['title'] );
50 $this->fld_comment
= isset( $prop['comment'] );
51 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
52 $this->fld_size
= isset( $prop['size'] );
53 $this->fld_sizediff
= isset( $prop['sizediff'] );
54 $this->fld_flags
= isset( $prop['flags'] );
55 $this->fld_timestamp
= isset( $prop['timestamp'] );
56 $this->fld_patrolled
= isset( $prop['patrolled'] );
57 $this->fld_tags
= isset( $prop['tags'] );
59 // TODO: if the query is going only against the revision table, should this be done?
60 $this->selectNamedDB( 'contributions', DB_SLAVE
, 'contributions' );
62 if ( isset( $this->params
['userprefix'] ) ) {
63 $this->prefixMode
= true;
64 $this->multiUserMode
= true;
65 $this->userprefix
= $this->params
['userprefix'];
67 $this->usernames
= array();
68 if ( !is_array( $this->params
['user'] ) ) {
69 $this->params
['user'] = array( $this->params
['user'] );
71 if ( !count( $this->params
['user'] ) ) {
72 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
74 foreach ( $this->params
['user'] as $u ) {
75 $this->prepareUsername( $u );
77 $this->prefixMode
= false;
78 $this->multiUserMode
= ( count( $this->params
['user'] ) > 1 );
81 $this->prepareQuery();
83 // Do the actual query.
84 $res = $this->select( __METHOD__
);
86 if ( $this->fld_sizediff
) {
88 foreach ( $res as $row ) {
89 if ( $row->rev_parent_id
) {
90 $revIds[] = $row->rev_parent_id
;
93 $this->parentLens
= Revision
::getParentLengths( $this->getDB(), $revIds );
94 $res->rewind(); // reset
97 // Initialise some variables
99 $limit = $this->params
['limit'];
102 foreach ( $res as $row ) {
103 if ( ++
$count > $limit ) {
104 // We've reached the one extra which shows that there are
105 // additional pages to be had. Stop here...
106 if ( $this->multiUserMode
) {
107 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
109 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
) );
114 $vals = $this->extractRowInfo( $row );
115 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
117 if ( $this->multiUserMode
) {
118 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
120 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
) );
126 $this->getResult()->setIndexedTagName_internal(
127 array( 'query', $this->getModuleName() ),
133 * Validate the 'user' parameter and set the value to compare
134 * against `revision`.`rev_user_text`
136 * @param $user string
138 private function prepareUsername( $user ) {
139 if ( !is_null( $user ) && $user !== '' ) {
140 $name = User
::isIP( $user )
142 : User
::getCanonicalName( $user, 'valid' );
143 if ( $name === false ) {
144 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
146 $this->usernames
[] = $name;
149 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
154 * Prepares the query and returns the limit of rows requested
156 private function prepareQuery() {
157 // We're after the revision table, and the corresponding page
158 // row for anything we retrieve. We may also need the
159 // recentchanges row and/or tag summary row.
160 $user = $this->getUser();
161 $tables = array( 'page', 'revision' ); // Order may change
162 $this->addWhere( 'page_id=rev_page' );
164 // Handle continue parameter
165 if ( $this->multiUserMode
&& !is_null( $this->params
['continue'] ) ) {
166 $continue = explode( '|', $this->params
['continue'] );
167 $this->dieContinueUsageIf( count( $continue ) != 2 );
168 $db = $this->getDB();
169 $encUser = $db->addQuotes( $continue[0] );
170 $encTS = $db->addQuotes( $db->timestamp( $continue[1] ) );
171 $op = ( $this->params
['dir'] == 'older' ?
'<' : '>' );
173 "rev_user_text $op $encUser OR " .
174 "(rev_user_text = $encUser AND " .
175 "rev_timestamp $op= $encTS)"
179 // Don't include any revisions where we're not supposed to be able to
181 if ( !$user->isAllowed( 'deletedhistory' ) ) {
182 $bitmask = Revision
::DELETED_USER
;
183 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
184 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
189 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
192 // We only want pages by the specified users.
193 if ( $this->prefixMode
) {
194 $this->addWhere( 'rev_user_text' .
195 $this->getDB()->buildLike( $this->userprefix
, $this->getDB()->anyString() ) );
197 $this->addWhereFld( 'rev_user_text', $this->usernames
);
199 // ... and in the specified timeframe.
200 // Ensure the same sort order for rev_user_text and rev_timestamp
201 // so our query is indexed
202 if ( $this->multiUserMode
) {
203 $this->addWhereRange( 'rev_user_text', $this->params
['dir'], null, null );
205 $this->addTimestampWhereRange( 'rev_timestamp',
206 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
207 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
209 $show = $this->params
['show'];
210 if ( $this->params
['toponly'] ) { // deprecated/old param
213 if ( !is_null( $show ) ) {
214 $show = array_flip( $show );
216 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
217 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
218 ||
( isset( $show['top'] ) && isset( $show['!top'] ) )
219 ||
( isset( $show['new'] ) && isset( $show['!new'] ) )
221 $this->dieUsageMsg( 'show' );
224 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
225 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
226 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
227 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
228 $this->addWhereIf( 'rev_id != page_latest', isset( $show['!top'] ) );
229 $this->addWhereIf( 'rev_id = page_latest', isset( $show['top'] ) );
230 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
231 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
233 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
234 $index = array( 'revision' => 'usertext_timestamp' );
236 // Mandatory fields: timestamp allows request continuation
237 // ns+title checks if the user has access rights for this page
238 // user_text is necessary if multiple users were specified
239 $this->addFields( array(
248 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ||
251 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
253 'You need the patrol right to request the patrolled flag',
258 // Use a redundant join condition on both
259 // timestamp and ID so we can use the timestamp
261 $index['recentchanges'] = 'rc_user_text';
262 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
263 // Put the tables in the right order for
265 $tables = array( 'revision', 'recentchanges', 'page' );
266 $this->addOption( 'STRAIGHT_JOIN' );
267 $this->addWhere( 'rc_user_text=rev_user_text' );
268 $this->addWhere( 'rc_timestamp=rev_timestamp' );
269 $this->addWhere( 'rc_this_oldid=rev_id' );
271 $tables[] = 'recentchanges';
272 $this->addJoinConds( array( 'recentchanges' => array(
274 'rc_user_text=rev_user_text',
275 'rc_timestamp=rev_timestamp',
276 'rc_this_oldid=rev_id' ) ) ) );
280 $this->addTables( $tables );
281 $this->addFieldsIf( 'rev_page', $this->fld_ids
);
282 $this->addFieldsIf( 'rev_id', $this->fld_ids ||
$this->fld_flags
);
283 $this->addFieldsIf( 'page_latest', $this->fld_flags
);
284 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
285 $this->addFieldsIf( 'rev_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
286 $this->addFieldsIf( 'rev_len', $this->fld_size ||
$this->fld_sizediff
);
287 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags
);
288 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags ||
$this->fld_sizediff ||
$this->fld_ids
);
289 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
291 if ( $this->fld_tags
) {
292 $this->addTables( 'tag_summary' );
294 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
296 $this->addFields( 'ts_tags' );
299 if ( isset( $this->params
['tag'] ) ) {
300 $this->addTables( 'change_tag' );
302 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
304 $this->addWhereFld( 'ct_tag', $this->params
['tag'] );
307 $this->addOption( 'USE INDEX', $index );
311 * Extract fields from the database row and append them to a result array
316 private function extractRowInfo( $row ) {
320 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
321 $vals['texthidden'] = '';
325 // Any rows where we can't view the user were filtered out in the query.
326 $vals['userid'] = $row->rev_user
;
327 $vals['user'] = $row->rev_user_text
;
328 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
329 $vals['userhidden'] = '';
332 if ( $this->fld_ids
) {
333 $vals['pageid'] = intval( $row->rev_page
);
334 $vals['revid'] = intval( $row->rev_id
);
335 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
337 if ( !is_null( $row->rev_parent_id
) ) {
338 $vals['parentid'] = intval( $row->rev_parent_id
);
342 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
344 if ( $this->fld_title
) {
345 ApiQueryBase
::addTitleInfo( $vals, $title );
348 if ( $this->fld_timestamp
) {
349 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
352 if ( $this->fld_flags
) {
353 if ( $row->rev_parent_id
== 0 && !is_null( $row->rev_parent_id
) ) {
356 if ( $row->rev_minor_edit
) {
359 if ( $row->page_latest
== $row->rev_id
) {
364 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->rev_comment
) ) {
365 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
366 $vals['commenthidden'] = '';
370 $userCanView = Revision
::userCanBitfield(
372 Revision
::DELETED_COMMENT
, $this->getUser()
375 if ( $userCanView ) {
376 if ( $this->fld_comment
) {
377 $vals['comment'] = $row->rev_comment
;
380 if ( $this->fld_parsedcomment
) {
381 $vals['parsedcomment'] = Linker
::formatComment( $row->rev_comment
, $title );
386 if ( $this->fld_patrolled
&& $row->rc_patrolled
) {
387 $vals['patrolled'] = '';
390 if ( $this->fld_size
&& !is_null( $row->rev_len
) ) {
391 $vals['size'] = intval( $row->rev_len
);
394 if ( $this->fld_sizediff
395 && !is_null( $row->rev_len
)
396 && !is_null( $row->rev_parent_id
)
398 $parentLen = isset( $this->parentLens
[$row->rev_parent_id
] )
399 ?
$this->parentLens
[$row->rev_parent_id
]
401 $vals['sizediff'] = intval( $row->rev_len
- $parentLen );
404 if ( $this->fld_tags
) {
405 if ( $row->ts_tags
) {
406 $tags = explode( ',', $row->ts_tags
);
407 $this->getResult()->setIndexedTagName( $tags, 'tag' );
408 $vals['tags'] = $tags;
410 $vals['tags'] = array();
414 if ( $anyHidden && $row->rev_deleted
& Revision
::DELETED_RESTRICTED
) {
415 $vals['suppressed'] = '';
421 private function continueStr( $row ) {
422 return $row->rev_user_text
. '|' .
423 wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
426 public function getCacheMode( $params ) {
427 // This module provides access to deleted revisions and patrol flags if
428 // the requester is logged in
429 return 'anon-public-user-private';
432 public function getAllowedParams() {
435 ApiBase
::PARAM_DFLT
=> 10,
436 ApiBase
::PARAM_TYPE
=> 'limit',
437 ApiBase
::PARAM_MIN
=> 1,
438 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
439 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
442 ApiBase
::PARAM_TYPE
=> 'timestamp'
445 ApiBase
::PARAM_TYPE
=> 'timestamp'
449 ApiBase
::PARAM_ISMULTI
=> true
451 'userprefix' => null,
453 ApiBase
::PARAM_DFLT
=> 'older',
454 ApiBase
::PARAM_TYPE
=> array(
459 'namespace' => array(
460 ApiBase
::PARAM_ISMULTI
=> true,
461 ApiBase
::PARAM_TYPE
=> 'namespace'
464 ApiBase
::PARAM_ISMULTI
=> true,
465 ApiBase
::PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
466 ApiBase
::PARAM_TYPE
=> array(
480 ApiBase
::PARAM_ISMULTI
=> true,
481 ApiBase
::PARAM_TYPE
=> array(
494 ApiBase
::PARAM_DFLT
=> false,
495 ApiBase
::PARAM_DEPRECATED
=> true,
500 public function getParamDescription() {
502 $p = $this->getModulePrefix();
505 'limit' => 'The maximum number of contributions to return',
506 'start' => 'The start timestamp to return from',
507 'end' => 'The end timestamp to return to',
508 'continue' => 'When more results are available, use this to continue',
509 'user' => 'The users to retrieve contributions for',
510 'userprefix' => array(
511 "Retrieve contributions for all users whose names begin with this value.",
512 "Overrides {$p}user",
514 'dir' => $this->getDirectionDescription( $p ),
515 'namespace' => 'Only list contributions in these namespaces',
517 'Include additional pieces of information',
518 ' ids - Adds the page ID and revision ID',
519 ' title - Adds the title and namespace ID of the page',
520 ' timestamp - Adds the timestamp of the edit',
521 ' comment - Adds the comment of the edit',
522 ' parsedcomment - Adds the parsed comment of the edit',
523 ' size - Adds the new size of the edit',
524 ' sizediff - Adds the size delta of the edit against its parent',
525 ' flags - Adds flags of the edit',
526 ' patrolled - Tags patrolled edits',
527 ' tags - Lists tags for the edit',
530 "Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
531 "NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
532 "\$wgRCMaxAge ($wgRCMaxAge) won't be shown",
534 'tag' => 'Only list revisions tagged with this tag',
535 'toponly' => 'Only list changes which are the latest revision',
539 public function getResultProperties() {
542 'userid' => 'integer',
544 'userhidden' => 'boolean'
547 'pageid' => 'integer',
548 'revid' => 'integer',
550 ApiBase
::PROP_TYPE
=> 'integer',
551 ApiBase
::PROP_NULLABLE
=> true
558 'timestamp' => array(
559 'timestamp' => 'timestamp'
563 'minor' => 'boolean',
567 'commenthidden' => 'boolean',
569 ApiBase
::PROP_TYPE
=> 'string',
570 ApiBase
::PROP_NULLABLE
=> true
573 'parsedcomment' => array(
574 'commenthidden' => 'boolean',
575 'parsedcomment' => array(
576 ApiBase
::PROP_TYPE
=> 'string',
577 ApiBase
::PROP_NULLABLE
=> true
580 'patrolled' => array(
581 'patrolled' => 'boolean'
585 ApiBase
::PROP_TYPE
=> 'integer',
586 ApiBase
::PROP_NULLABLE
=> true
591 ApiBase
::PROP_TYPE
=> 'integer',
592 ApiBase
::PROP_NULLABLE
=> true
598 public function getDescription() {
599 return 'Get all edits by a user';
602 public function getPossibleErrors() {
603 return array_merge( parent
::getPossibleErrors(), array(
604 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
605 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
608 'code' => 'permissiondenied',
609 'info' => 'You need the patrol right to request the patrolled flag'
614 public function getExamples() {
616 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
617 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
621 public function getHelpUrls() {
622 return 'https://www.mediawiki.org/wiki/API:Usercontribs';