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 ( !is_null( $show ) ) {
211 $show = array_flip( $show );
212 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
213 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
215 $this->dieUsageMsg( 'show' );
218 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
219 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
220 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
221 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
223 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
224 $index = array( 'revision' => 'usertext_timestamp' );
226 // Mandatory fields: timestamp allows request continuation
227 // ns+title checks if the user has access rights for this page
228 // user_text is necessary if multiple users were specified
229 $this->addFields( array(
238 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ||
241 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
243 'You need the patrol right to request the patrolled flag',
248 // Use a redundant join condition on both
249 // timestamp and ID so we can use the timestamp
251 $index['recentchanges'] = 'rc_user_text';
252 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
253 // Put the tables in the right order for
255 $tables = array( 'revision', 'recentchanges', 'page' );
256 $this->addOption( 'STRAIGHT_JOIN' );
257 $this->addWhere( 'rc_user_text=rev_user_text' );
258 $this->addWhere( 'rc_timestamp=rev_timestamp' );
259 $this->addWhere( 'rc_this_oldid=rev_id' );
261 $tables[] = 'recentchanges';
262 $this->addJoinConds( array( 'recentchanges' => array(
264 'rc_user_text=rev_user_text',
265 'rc_timestamp=rev_timestamp',
266 'rc_this_oldid=rev_id' ) ) ) );
270 $this->addTables( $tables );
271 $this->addFieldsIf( 'rev_page', $this->fld_ids
);
272 $this->addFieldsIf( 'rev_id', $this->fld_ids ||
$this->fld_flags
);
273 $this->addFieldsIf( 'page_latest', $this->fld_flags
);
274 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
275 $this->addFieldsIf( 'rev_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
276 $this->addFieldsIf( 'rev_len', $this->fld_size ||
$this->fld_sizediff
);
277 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags
);
278 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags ||
$this->fld_sizediff ||
$this->fld_ids
);
279 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
281 if ( $this->fld_tags
) {
282 $this->addTables( 'tag_summary' );
284 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
286 $this->addFields( 'ts_tags' );
289 if ( isset( $this->params
['tag'] ) ) {
290 $this->addTables( 'change_tag' );
292 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
294 $this->addWhereFld( 'ct_tag', $this->params
['tag'] );
297 if ( $this->params
['toponly'] ) {
298 $this->addWhere( 'rev_id = page_latest' );
301 $this->addOption( 'USE INDEX', $index );
305 * Extract fields from the database row and append them to a result array
310 private function extractRowInfo( $row ) {
314 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
315 $vals['texthidden'] = '';
319 // Any rows where we can't view the user were filtered out in the query.
320 $vals['userid'] = $row->rev_user
;
321 $vals['user'] = $row->rev_user_text
;
322 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
323 $vals['userhidden'] = '';
326 if ( $this->fld_ids
) {
327 $vals['pageid'] = intval( $row->rev_page
);
328 $vals['revid'] = intval( $row->rev_id
);
329 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
331 if ( !is_null( $row->rev_parent_id
) ) {
332 $vals['parentid'] = intval( $row->rev_parent_id
);
336 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
338 if ( $this->fld_title
) {
339 ApiQueryBase
::addTitleInfo( $vals, $title );
342 if ( $this->fld_timestamp
) {
343 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
346 if ( $this->fld_flags
) {
347 if ( $row->rev_parent_id
== 0 && !is_null( $row->rev_parent_id
) ) {
350 if ( $row->rev_minor_edit
) {
353 if ( $row->page_latest
== $row->rev_id
) {
358 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->rev_comment
) ) {
359 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
360 $vals['commenthidden'] = '';
363 if ( Revision
::userCanBitfield( $row->rev_deleted
, Revision
::DELETED_COMMENT
, $this->getUser() ) ) {
364 if ( $this->fld_comment
) {
365 $vals['comment'] = $row->rev_comment
;
368 if ( $this->fld_parsedcomment
) {
369 $vals['parsedcomment'] = Linker
::formatComment( $row->rev_comment
, $title );
374 if ( $this->fld_patrolled
&& $row->rc_patrolled
) {
375 $vals['patrolled'] = '';
378 if ( $this->fld_size
&& !is_null( $row->rev_len
) ) {
379 $vals['size'] = intval( $row->rev_len
);
382 if ( $this->fld_sizediff
383 && !is_null( $row->rev_len
)
384 && !is_null( $row->rev_parent_id
)
386 $parentLen = isset( $this->parentLens
[$row->rev_parent_id
] )
387 ?
$this->parentLens
[$row->rev_parent_id
]
389 $vals['sizediff'] = intval( $row->rev_len
- $parentLen );
392 if ( $this->fld_tags
) {
393 if ( $row->ts_tags
) {
394 $tags = explode( ',', $row->ts_tags
);
395 $this->getResult()->setIndexedTagName( $tags, 'tag' );
396 $vals['tags'] = $tags;
398 $vals['tags'] = array();
402 if ( $anyHidden && $row->rev_deleted
& Revision
::DELETED_RESTRICTED
) {
403 $vals['suppressed'] = '';
409 private function continueStr( $row ) {
410 return $row->rev_user_text
. '|' .
411 wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
414 public function getCacheMode( $params ) {
415 // This module provides access to deleted revisions and patrol flags if
416 // the requester is logged in
417 return 'anon-public-user-private';
420 public function getAllowedParams() {
423 ApiBase
::PARAM_DFLT
=> 10,
424 ApiBase
::PARAM_TYPE
=> 'limit',
425 ApiBase
::PARAM_MIN
=> 1,
426 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
427 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
430 ApiBase
::PARAM_TYPE
=> 'timestamp'
433 ApiBase
::PARAM_TYPE
=> 'timestamp'
437 ApiBase
::PARAM_ISMULTI
=> true
439 'userprefix' => null,
441 ApiBase
::PARAM_DFLT
=> 'older',
442 ApiBase
::PARAM_TYPE
=> array(
447 'namespace' => array(
448 ApiBase
::PARAM_ISMULTI
=> true,
449 ApiBase
::PARAM_TYPE
=> 'namespace'
452 ApiBase
::PARAM_ISMULTI
=> true,
453 ApiBase
::PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
454 ApiBase
::PARAM_TYPE
=> array(
468 ApiBase
::PARAM_ISMULTI
=> true,
469 ApiBase
::PARAM_TYPE
=> array(
481 public function getParamDescription() {
483 $p = $this->getModulePrefix();
486 'limit' => 'The maximum number of contributions to return',
487 'start' => 'The start timestamp to return from',
488 'end' => 'The end timestamp to return to',
489 'continue' => 'When more results are available, use this to continue',
490 'user' => 'The users to retrieve contributions for',
491 'userprefix' => array(
492 "Retrieve contributions for all users whose names begin with this value.",
493 "Overrides {$p}user",
495 'dir' => $this->getDirectionDescription( $p ),
496 'namespace' => 'Only list contributions in these namespaces',
498 'Include additional pieces of information',
499 ' ids - Adds the page ID and revision ID',
500 ' title - Adds the title and namespace ID of the page',
501 ' timestamp - Adds the timestamp of the edit',
502 ' comment - Adds the comment of the edit',
503 ' parsedcomment - Adds the parsed comment of the edit',
504 ' size - Adds the new size of the edit',
505 ' sizediff - Adds the size delta of the edit against its parent',
506 ' flags - Adds flags of the edit',
507 ' patrolled - Tags patrolled edits',
508 ' tags - Lists tags for the edit',
511 "Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
512 "NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
513 "\$wgRCMaxAge ($wgRCMaxAge) won't be shown",
515 'tag' => 'Only list revisions tagged with this tag',
516 'toponly' => 'Only list changes which are the latest revision',
520 public function getResultProperties() {
523 'userid' => 'integer',
525 'userhidden' => 'boolean'
528 'pageid' => 'integer',
529 'revid' => 'integer',
531 ApiBase
::PROP_TYPE
=> 'integer',
532 ApiBase
::PROP_NULLABLE
=> true
539 'timestamp' => array(
540 'timestamp' => 'timestamp'
544 'minor' => 'boolean',
548 'commenthidden' => 'boolean',
550 ApiBase
::PROP_TYPE
=> 'string',
551 ApiBase
::PROP_NULLABLE
=> true
554 'parsedcomment' => array(
555 'commenthidden' => 'boolean',
556 'parsedcomment' => array(
557 ApiBase
::PROP_TYPE
=> 'string',
558 ApiBase
::PROP_NULLABLE
=> true
561 'patrolled' => array(
562 'patrolled' => 'boolean'
566 ApiBase
::PROP_TYPE
=> 'integer',
567 ApiBase
::PROP_NULLABLE
=> true
572 ApiBase
::PROP_TYPE
=> 'integer',
573 ApiBase
::PROP_NULLABLE
=> true
579 public function getDescription() {
580 return 'Get all edits by a user';
583 public function getPossibleErrors() {
584 return array_merge( parent
::getPossibleErrors(), array(
585 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
586 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
589 'code' => 'permissiondenied',
590 'info' => 'You need the patrol right to request the patrolled flag'
595 public function getExamples() {
597 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
598 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
602 public function getHelpUrls() {
603 return 'https://www.mediawiki.org/wiki/API:Usercontribs';