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 if ( !$user->isAllowed( 'hideuser' ) ) {
180 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' );
182 // We only want pages by the specified users.
183 if ( $this->prefixMode
) {
184 $this->addWhere( 'rev_user_text' .
185 $this->getDB()->buildLike( $this->userprefix
, $this->getDB()->anyString() ) );
187 $this->addWhereFld( 'rev_user_text', $this->usernames
);
189 // ... and in the specified timeframe.
190 // Ensure the same sort order for rev_user_text and rev_timestamp
191 // so our query is indexed
192 if ( $this->multiUserMode
) {
193 $this->addWhereRange( 'rev_user_text', $this->params
['dir'], null, null );
195 $this->addTimestampWhereRange( 'rev_timestamp',
196 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
197 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
199 $show = $this->params
['show'];
200 if ( !is_null( $show ) ) {
201 $show = array_flip( $show );
202 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
203 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
205 $this->dieUsageMsg( 'show' );
208 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
209 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
210 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
211 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
213 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
214 $index = array( 'revision' => 'usertext_timestamp' );
216 // Mandatory fields: timestamp allows request continuation
217 // ns+title checks if the user has access rights for this page
218 // user_text is necessary if multiple users were specified
219 $this->addFields( array(
228 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ||
231 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
233 'You need the patrol right to request the patrolled flag',
238 // Use a redundant join condition on both
239 // timestamp and ID so we can use the timestamp
241 $index['recentchanges'] = 'rc_user_text';
242 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
243 // Put the tables in the right order for
245 $tables = array( 'revision', 'recentchanges', 'page' );
246 $this->addOption( 'STRAIGHT_JOIN' );
247 $this->addWhere( 'rc_user_text=rev_user_text' );
248 $this->addWhere( 'rc_timestamp=rev_timestamp' );
249 $this->addWhere( 'rc_this_oldid=rev_id' );
251 $tables[] = 'recentchanges';
252 $this->addJoinConds( array( 'recentchanges' => array(
254 'rc_user_text=rev_user_text',
255 'rc_timestamp=rev_timestamp',
256 'rc_this_oldid=rev_id' ) ) ) );
260 $this->addTables( $tables );
261 $this->addFieldsIf( 'rev_page', $this->fld_ids
);
262 $this->addFieldsIf( 'rev_id', $this->fld_ids ||
$this->fld_flags
);
263 $this->addFieldsIf( 'page_latest', $this->fld_flags
);
264 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
265 $this->addFieldsIf( 'rev_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
266 $this->addFieldsIf( 'rev_len', $this->fld_size ||
$this->fld_sizediff
);
267 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags
);
268 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags ||
$this->fld_sizediff ||
$this->fld_ids
);
269 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
271 if ( $this->fld_tags
) {
272 $this->addTables( 'tag_summary' );
274 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
276 $this->addFields( 'ts_tags' );
279 if ( isset( $this->params
['tag'] ) ) {
280 $this->addTables( 'change_tag' );
282 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
284 $this->addWhereFld( 'ct_tag', $this->params
['tag'] );
287 if ( $this->params
['toponly'] ) {
288 $this->addWhere( 'rev_id = page_latest' );
291 $this->addOption( 'USE INDEX', $index );
295 * Extract fields from the database row and append them to a result array
300 private function extractRowInfo( $row ) {
303 $vals['userid'] = $row->rev_user
;
304 $vals['user'] = $row->rev_user_text
;
305 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
306 $vals['userhidden'] = '';
308 if ( $this->fld_ids
) {
309 $vals['pageid'] = intval( $row->rev_page
);
310 $vals['revid'] = intval( $row->rev_id
);
311 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
313 if ( !is_null( $row->rev_parent_id
) ) {
314 $vals['parentid'] = intval( $row->rev_parent_id
);
318 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
320 if ( $this->fld_title
) {
321 ApiQueryBase
::addTitleInfo( $vals, $title );
324 if ( $this->fld_timestamp
) {
325 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
328 if ( $this->fld_flags
) {
329 if ( $row->rev_parent_id
== 0 && !is_null( $row->rev_parent_id
) ) {
332 if ( $row->rev_minor_edit
) {
335 if ( $row->page_latest
== $row->rev_id
) {
340 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->rev_comment
) ) {
341 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
342 $vals['commenthidden'] = '';
344 if ( $this->fld_comment
) {
345 $vals['comment'] = $row->rev_comment
;
348 if ( $this->fld_parsedcomment
) {
349 $vals['parsedcomment'] = Linker
::formatComment( $row->rev_comment
, $title );
354 if ( $this->fld_patrolled
&& $row->rc_patrolled
) {
355 $vals['patrolled'] = '';
358 if ( $this->fld_size
&& !is_null( $row->rev_len
) ) {
359 $vals['size'] = intval( $row->rev_len
);
362 if ( $this->fld_sizediff
363 && !is_null( $row->rev_len
)
364 && !is_null( $row->rev_parent_id
)
366 $parentLen = isset( $this->parentLens
[$row->rev_parent_id
] )
367 ?
$this->parentLens
[$row->rev_parent_id
]
369 $vals['sizediff'] = intval( $row->rev_len
- $parentLen );
372 if ( $this->fld_tags
) {
373 if ( $row->ts_tags
) {
374 $tags = explode( ',', $row->ts_tags
);
375 $this->getResult()->setIndexedTagName( $tags, 'tag' );
376 $vals['tags'] = $tags;
378 $vals['tags'] = array();
385 private function continueStr( $row ) {
386 return $row->rev_user_text
. '|' .
387 wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
390 public function getCacheMode( $params ) {
391 // This module provides access to deleted revisions and patrol flags if
392 // the requester is logged in
393 return 'anon-public-user-private';
396 public function getAllowedParams() {
399 ApiBase
::PARAM_DFLT
=> 10,
400 ApiBase
::PARAM_TYPE
=> 'limit',
401 ApiBase
::PARAM_MIN
=> 1,
402 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
403 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
406 ApiBase
::PARAM_TYPE
=> 'timestamp'
409 ApiBase
::PARAM_TYPE
=> 'timestamp'
413 ApiBase
::PARAM_ISMULTI
=> true
415 'userprefix' => null,
417 ApiBase
::PARAM_DFLT
=> 'older',
418 ApiBase
::PARAM_TYPE
=> array(
423 'namespace' => array(
424 ApiBase
::PARAM_ISMULTI
=> true,
425 ApiBase
::PARAM_TYPE
=> 'namespace'
428 ApiBase
::PARAM_ISMULTI
=> true,
429 ApiBase
::PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
430 ApiBase
::PARAM_TYPE
=> array(
444 ApiBase
::PARAM_ISMULTI
=> true,
445 ApiBase
::PARAM_TYPE
=> array(
457 public function getParamDescription() {
459 $p = $this->getModulePrefix();
462 'limit' => 'The maximum number of contributions to return',
463 'start' => 'The start timestamp to return from',
464 'end' => 'The end timestamp to return to',
465 'continue' => 'When more results are available, use this to continue',
466 'user' => 'The users to retrieve contributions for',
467 'userprefix' => array(
468 "Retrieve contributions for all users whose names begin with this value.",
469 "Overrides {$p}user",
471 'dir' => $this->getDirectionDescription( $p ),
472 'namespace' => 'Only list contributions in these namespaces',
474 'Include additional pieces of information',
475 ' ids - Adds the page ID and revision ID',
476 ' title - Adds the title and namespace ID of the page',
477 ' timestamp - Adds the timestamp of the edit',
478 ' comment - Adds the comment of the edit',
479 ' parsedcomment - Adds the parsed comment of the edit',
480 ' size - Adds the new size of the edit',
481 ' sizediff - Adds the size delta of the edit against its parent',
482 ' flags - Adds flags of the edit',
483 ' patrolled - Tags patrolled edits',
484 ' tags - Lists tags for the edit',
487 "Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
488 "NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
489 "\$wgRCMaxAge ($wgRCMaxAge) won't be shown",
491 'tag' => 'Only list revisions tagged with this tag',
492 'toponly' => 'Only list changes which are the latest revision',
496 public function getResultProperties() {
499 'userid' => 'integer',
501 'userhidden' => 'boolean'
504 'pageid' => 'integer',
505 'revid' => 'integer',
507 ApiBase
::PROP_TYPE
=> 'integer',
508 ApiBase
::PROP_NULLABLE
=> true
515 'timestamp' => array(
516 'timestamp' => 'timestamp'
520 'minor' => 'boolean',
524 'commenthidden' => 'boolean',
526 ApiBase
::PROP_TYPE
=> 'string',
527 ApiBase
::PROP_NULLABLE
=> true
530 'parsedcomment' => array(
531 'commenthidden' => 'boolean',
532 'parsedcomment' => array(
533 ApiBase
::PROP_TYPE
=> 'string',
534 ApiBase
::PROP_NULLABLE
=> true
537 'patrolled' => array(
538 'patrolled' => 'boolean'
542 ApiBase
::PROP_TYPE
=> 'integer',
543 ApiBase
::PROP_NULLABLE
=> true
548 ApiBase
::PROP_TYPE
=> 'integer',
549 ApiBase
::PROP_NULLABLE
=> true
555 public function getDescription() {
556 return 'Get all edits by a user';
559 public function getPossibleErrors() {
560 return array_merge( parent
::getPossibleErrors(), array(
561 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
562 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
565 'code' => 'permissiondenied',
566 'info' => 'You need the patrol right to request the patrolled flag'
571 public function getExamples() {
573 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
574 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
578 public function getHelpUrls() {
579 return 'https://www.mediawiki.org/wiki/API:Usercontribs';