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 additional pages to be had. Stop here...
105 if ( $this->multiUserMode
) {
106 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
108 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
) );
113 $vals = $this->extractRowInfo( $row );
114 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
116 if ( $this->multiUserMode
) {
117 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
119 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
) );
125 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
129 * Validate the 'user' parameter and set the value to compare
130 * against `revision`.`rev_user_text`
132 * @param $user string
134 private function prepareUsername( $user ) {
135 if ( !is_null( $user ) && $user !== '' ) {
136 $name = User
::isIP( $user )
138 : User
::getCanonicalName( $user, 'valid' );
139 if ( $name === false ) {
140 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
142 $this->usernames
[] = $name;
145 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
150 * Prepares the query and returns the limit of rows requested
152 private function prepareQuery() {
153 // We're after the revision table, and the corresponding page
154 // row for anything we retrieve. We may also need the
155 // recentchanges row and/or tag summary row.
156 $user = $this->getUser();
157 $tables = array( 'page', 'revision' ); // Order may change
158 $this->addWhere( 'page_id=rev_page' );
160 // Handle continue parameter
161 if ( $this->multiUserMode
&& !is_null( $this->params
['continue'] ) ) {
162 $continue = explode( '|', $this->params
['continue'] );
163 $this->dieContinueUsageIf( count( $continue ) != 2 );
164 $db = $this->getDB();
165 $encUser = $db->addQuotes( $continue[0] );
166 $encTS = $db->addQuotes( $db->timestamp( $continue[1] ) );
167 $op = ( $this->params
['dir'] == 'older' ?
'<' : '>' );
169 "rev_user_text $op $encUser OR " .
170 "(rev_user_text = $encUser AND " .
171 "rev_timestamp $op= $encTS)"
175 if ( !$user->isAllowed( 'hideuser' ) ) {
176 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' );
178 // We only want pages by the specified users.
179 if ( $this->prefixMode
) {
180 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix
, $this->getDB()->anyString() ) );
182 $this->addWhereFld( 'rev_user_text', $this->usernames
);
184 // ... and in the specified timeframe.
185 // Ensure the same sort order for rev_user_text and rev_timestamp
186 // so our query is indexed
187 if ( $this->multiUserMode
) {
188 $this->addWhereRange( 'rev_user_text', $this->params
['dir'], null, null );
190 $this->addTimestampWhereRange( 'rev_timestamp',
191 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
192 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
194 $show = $this->params
['show'];
195 if ( !is_null( $show ) ) {
196 $show = array_flip( $show );
197 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
198 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) ) {
199 $this->dieUsageMsg( 'show' );
202 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
203 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
204 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
205 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
207 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
208 $index = array( 'revision' => 'usertext_timestamp' );
210 // Mandatory fields: timestamp allows request continuation
211 // ns+title checks if the user has access rights for this page
212 // user_text is necessary if multiple users were specified
213 $this->addFields( array(
222 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ||
223 $this->fld_patrolled
) {
224 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
225 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
228 // Use a redundant join condition on both
229 // timestamp and ID so we can use the timestamp
231 $index['recentchanges'] = 'rc_user_text';
232 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
233 // Put the tables in the right order for
235 $tables = array( 'revision', 'recentchanges', 'page' );
236 $this->addOption( 'STRAIGHT_JOIN' );
237 $this->addWhere( 'rc_user_text=rev_user_text' );
238 $this->addWhere( 'rc_timestamp=rev_timestamp' );
239 $this->addWhere( 'rc_this_oldid=rev_id' );
241 $tables[] = 'recentchanges';
242 $this->addJoinConds( array( 'recentchanges' => array(
244 'rc_user_text=rev_user_text',
245 'rc_timestamp=rev_timestamp',
246 'rc_this_oldid=rev_id' ) ) ) );
250 $this->addTables( $tables );
251 $this->addFieldsIf( 'rev_page', $this->fld_ids
);
252 $this->addFieldsIf( 'rev_id', $this->fld_ids ||
$this->fld_flags
);
253 $this->addFieldsIf( 'page_latest', $this->fld_flags
);
254 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
255 $this->addFieldsIf( 'rev_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
256 $this->addFieldsIf( 'rev_len', $this->fld_size ||
$this->fld_sizediff
);
257 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags
);
258 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags ||
$this->fld_sizediff
);
259 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
261 if ( $this->fld_tags
) {
262 $this->addTables( 'tag_summary' );
263 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
264 $this->addFields( 'ts_tags' );
267 if ( isset( $this->params
['tag'] ) ) {
268 $this->addTables( 'change_tag' );
269 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
270 $this->addWhereFld( 'ct_tag', $this->params
['tag'] );
271 global $wgOldChangeTagsIndex;
272 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
275 if ( $this->params
['toponly'] ) {
276 $this->addWhere( 'rev_id = page_latest' );
279 $this->addOption( 'USE INDEX', $index );
283 * Extract fields from the database row and append them to a result array
288 private function extractRowInfo( $row ) {
291 $vals['userid'] = $row->rev_user
;
292 $vals['user'] = $row->rev_user_text
;
293 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
294 $vals['userhidden'] = '';
296 if ( $this->fld_ids
) {
297 $vals['pageid'] = intval( $row->rev_page
);
298 $vals['revid'] = intval( $row->rev_id
);
299 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
302 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
304 if ( $this->fld_title
) {
305 ApiQueryBase
::addTitleInfo( $vals, $title );
308 if ( $this->fld_timestamp
) {
309 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
312 if ( $this->fld_flags
) {
313 if ( $row->rev_parent_id
== 0 && !is_null( $row->rev_parent_id
) ) {
316 if ( $row->rev_minor_edit
) {
319 if ( $row->page_latest
== $row->rev_id
) {
324 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->rev_comment
) ) {
325 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
326 $vals['commenthidden'] = '';
328 if ( $this->fld_comment
) {
329 $vals['comment'] = $row->rev_comment
;
332 if ( $this->fld_parsedcomment
) {
333 $vals['parsedcomment'] = Linker
::formatComment( $row->rev_comment
, $title );
338 if ( $this->fld_patrolled
&& $row->rc_patrolled
) {
339 $vals['patrolled'] = '';
342 if ( $this->fld_size
&& !is_null( $row->rev_len
) ) {
343 $vals['size'] = intval( $row->rev_len
);
346 if ( $this->fld_sizediff
&& !is_null( $row->rev_len
) && !is_null( $row->rev_parent_id
) ) {
347 $parentLen = isset( $this->parentLens
[$row->rev_parent_id
] ) ?
$this->parentLens
[$row->rev_parent_id
] : 0;
348 $vals['sizediff'] = intval( $row->rev_len
- $parentLen );
351 if ( $this->fld_tags
) {
352 if ( $row->ts_tags
) {
353 $tags = explode( ',', $row->ts_tags
);
354 $this->getResult()->setIndexedTagName( $tags, 'tag' );
355 $vals['tags'] = $tags;
357 $vals['tags'] = array();
364 private function continueStr( $row ) {
365 return $row->rev_user_text
. '|' .
366 wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
369 public function getCacheMode( $params ) {
370 // This module provides access to deleted revisions and patrol flags if
371 // the requester is logged in
372 return 'anon-public-user-private';
375 public function getAllowedParams() {
378 ApiBase
::PARAM_DFLT
=> 10,
379 ApiBase
::PARAM_TYPE
=> 'limit',
380 ApiBase
::PARAM_MIN
=> 1,
381 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
382 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
385 ApiBase
::PARAM_TYPE
=> 'timestamp'
388 ApiBase
::PARAM_TYPE
=> 'timestamp'
392 ApiBase
::PARAM_ISMULTI
=> true
394 'userprefix' => null,
396 ApiBase
::PARAM_DFLT
=> 'older',
397 ApiBase
::PARAM_TYPE
=> array(
402 'namespace' => array(
403 ApiBase
::PARAM_ISMULTI
=> true,
404 ApiBase
::PARAM_TYPE
=> 'namespace'
407 ApiBase
::PARAM_ISMULTI
=> true,
408 ApiBase
::PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
409 ApiBase
::PARAM_TYPE
=> array(
423 ApiBase
::PARAM_ISMULTI
=> true,
424 ApiBase
::PARAM_TYPE
=> array(
436 public function getParamDescription() {
438 $p = $this->getModulePrefix();
440 'limit' => 'The maximum number of contributions to return',
441 'start' => 'The start timestamp to return from',
442 'end' => 'The end timestamp to return to',
443 'continue' => 'When more results are available, use this to continue',
444 'user' => 'The users to retrieve contributions for',
445 'userprefix' => "Retrieve contributions for all users whose names begin with this value. Overrides {$p}user",
446 'dir' => $this->getDirectionDescription( $p ),
447 'namespace' => 'Only list contributions in these namespaces',
449 'Include additional pieces of information',
450 ' ids - Adds the page ID and revision ID',
451 ' title - Adds the title and namespace ID of the page',
452 ' timestamp - Adds the timestamp of the edit',
453 ' comment - Adds the comment of the edit',
454 ' parsedcomment - Adds the parsed comment of the edit',
455 ' size - Adds the new size of the edit',
456 ' sizediff - Adds the size delta of the edit against its parent',
457 ' flags - Adds flags of the edit',
458 ' patrolled - Tags patrolled edits',
459 ' tags - Lists tags for the edit',
461 'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor",
462 "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than \$wgRCMaxAge ($wgRCMaxAge) won't be shown", ),
463 'tag' => 'Only list revisions tagged with this tag',
464 'toponly' => 'Only list changes which are the latest revision',
468 public function getResultProperties() {
471 'userid' => 'integer',
473 'userhidden' => 'boolean'
476 'pageid' => 'integer',
483 'timestamp' => array(
484 'timestamp' => 'timestamp'
488 'minor' => 'boolean',
492 'commenthidden' => 'boolean',
494 ApiBase
::PROP_TYPE
=> 'string',
495 ApiBase
::PROP_NULLABLE
=> true
498 'parsedcomment' => array(
499 'commenthidden' => 'boolean',
500 'parsedcomment' => array(
501 ApiBase
::PROP_TYPE
=> 'string',
502 ApiBase
::PROP_NULLABLE
=> true
505 'patrolled' => array(
506 'patrolled' => 'boolean'
510 ApiBase
::PROP_TYPE
=> 'integer',
511 ApiBase
::PROP_NULLABLE
=> true
516 ApiBase
::PROP_TYPE
=> 'integer',
517 ApiBase
::PROP_NULLABLE
=> true
523 public function getDescription() {
524 return 'Get all edits by a user';
527 public function getPossibleErrors() {
528 return array_merge( parent
::getPossibleErrors(), array(
529 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
530 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
532 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
536 public function getExamples() {
538 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
539 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
543 public function getHelpUrls() {
544 return 'https://www.mediawiki.org/wiki/API:Usercontribs';