3 * API for MediaWiki 1.8+
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * This query action adds a list of a specified user's contributions to the output.
37 class ApiQueryContributions
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'uc' );
44 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
45 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
46 $fld_patrolled = false, $fld_tags = false;
48 public function execute() {
49 // Parse some parameters
50 $this->params
= $this->extractRequestParams();
52 $prop = array_flip( $this->params
['prop'] );
53 $this->fld_ids
= isset( $prop['ids'] );
54 $this->fld_title
= isset( $prop['title'] );
55 $this->fld_comment
= isset( $prop['comment'] );
56 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
57 $this->fld_size
= isset( $prop['size'] );
58 $this->fld_flags
= isset( $prop['flags'] );
59 $this->fld_timestamp
= isset( $prop['timestamp'] );
60 $this->fld_patrolled
= isset( $prop['patrolled'] );
61 $this->fld_tags
= isset( $prop['tags'] );
63 // TODO: if the query is going only against the revision table, should this be done?
64 $this->selectNamedDB( 'contributions', DB_SLAVE
, 'contributions' );
66 if ( isset( $this->params
['userprefix'] ) ) {
67 $this->prefixMode
= true;
68 $this->multiUserMode
= true;
69 $this->userprefix
= $this->params
['userprefix'];
71 $this->usernames
= array();
72 if ( !is_array( $this->params
['user'] ) ) {
73 $this->params
['user'] = array( $this->params
['user'] );
75 if ( !count( $this->params
['user'] ) ) {
76 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
78 foreach ( $this->params
['user'] as $u ) {
79 $this->prepareUsername( $u );
81 $this->prefixMode
= false;
82 $this->multiUserMode
= ( count( $this->params
['user'] ) > 1 );
84 $this->prepareQuery();
86 // Do the actual query.
87 $res = $this->select( __METHOD__
);
89 // Initialise some variables
91 $limit = $this->params
['limit'];
94 foreach ( $res as $row ) {
95 if ( ++
$count > $limit ) {
96 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
97 if ( $this->multiUserMode
) {
98 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
100 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
) );
105 $vals = $this->extractRowInfo( $row );
106 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
108 if ( $this->multiUserMode
) {
109 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
111 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
) );
117 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
121 * Validate the 'user' parameter and set the value to compare
122 * against `revision`.`rev_user_text`
124 private function prepareUsername( $user ) {
125 if ( !is_null( $user ) && $user !== '' ) {
126 $name = User
::isIP( $user )
128 : User
::getCanonicalName( $user, 'valid' );
129 if ( $name === false ) {
130 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
132 $this->usernames
[] = $name;
135 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
140 * Prepares the query and returns the limit of rows requested
142 private function prepareQuery() {
143 // We're after the revision table, and the corresponding page
144 // row for anything we retrieve. We may also need the
145 // recentchanges row and/or tag summary row.
147 $tables = array( 'page', 'revision' ); // Order may change
148 $this->addWhere( 'page_id=rev_page' );
150 // Handle continue parameter
151 if ( $this->multiUserMode
&& !is_null( $this->params
['continue'] ) ) {
152 $continue = explode( '|', $this->params
['continue'] );
153 if ( count( $continue ) != 2 ) {
154 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
155 'value returned by the previous query', '_badcontinue' );
157 $encUser = $this->getDB()->strencode( $continue[0] );
158 $encTS = wfTimestamp( TS_MW
, $continue[1] );
159 $op = ( $this->params
['dir'] == 'older' ?
'<' : '>' );
161 "rev_user_text $op '$encUser' OR " .
162 "(rev_user_text = '$encUser' AND " .
163 "rev_timestamp $op= '$encTS')"
167 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
168 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' );
170 // We only want pages by the specified users.
171 if ( $this->prefixMode
) {
172 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix
, $this->getDB()->anyString() ) );
174 $this->addWhereFld( 'rev_user_text', $this->usernames
);
176 // ... and in the specified timeframe.
177 // Ensure the same sort order for rev_user_text and rev_timestamp
178 // so our query is indexed
179 if ( $this->multiUserMode
) {
180 $this->addWhereRange( 'rev_user_text', $this->params
['dir'], null, null );
182 $this->addWhereRange( 'rev_timestamp',
183 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
184 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
186 $show = $this->params
['show'];
187 if ( !is_null( $show ) ) {
188 $show = array_flip( $show );
189 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
190 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) ) {
191 $this->dieUsageMsg( array( 'show' ) );
194 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
195 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
196 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
197 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
199 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
200 $index = array( 'revision' => 'usertext_timestamp' );
202 // Mandatory fields: timestamp allows request continuation
203 // ns+title checks if the user has access rights for this page
204 // user_text is necessary if multiple users were specified
205 $this->addFields( array(
213 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ||
214 $this->fld_patrolled
) {
215 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
216 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
219 // Use a redundant join condition on both
220 // timestamp and ID so we can use the timestamp
222 $index['recentchanges'] = 'rc_user_text';
223 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
224 // Put the tables in the right order for
226 $tables = array( 'revision', 'recentchanges', 'page' );
227 $this->addOption( 'STRAIGHT_JOIN' );
228 $this->addWhere( 'rc_user_text=rev_user_text' );
229 $this->addWhere( 'rc_timestamp=rev_timestamp' );
230 $this->addWhere( 'rc_this_oldid=rev_id' );
232 $tables[] = 'recentchanges';
233 $this->addJoinConds( array( 'recentchanges' => array(
235 'rc_user_text=rev_user_text',
236 'rc_timestamp=rev_timestamp',
237 'rc_this_oldid=rev_id' ) ) ) );
241 $this->addTables( $tables );
242 $this->addFieldsIf( 'rev_page', $this->fld_ids
);
243 $this->addFieldsIf( 'rev_id', $this->fld_ids ||
$this->fld_flags
);
244 $this->addFieldsIf( 'page_latest', $this->fld_flags
);
245 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
246 $this->addFieldsIf( 'rev_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
247 $this->addFieldsIf( 'rev_len', $this->fld_size
);
248 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags
);
249 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags
);
250 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
252 if ( $this->fld_tags
) {
253 $this->addTables( 'tag_summary' );
254 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
255 $this->addFields( 'ts_tags' );
258 if ( isset( $this->params
['tag'] ) ) {
259 $this->addTables( 'change_tag' );
260 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
261 $this->addWhereFld( 'ct_tag', $this->params
['tag'] );
262 global $wgOldChangeTagsIndex;
263 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
266 $this->addOption( 'USE INDEX', $index );
270 * Extract fields from the database row and append them to a result array
272 private function extractRowInfo( $row ) {
275 $vals['user'] = $row->rev_user_text
;
276 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
277 $vals['userhidden'] = '';
279 if ( $this->fld_ids
) {
280 $vals['pageid'] = intval( $row->rev_page
);
281 $vals['revid'] = intval( $row->rev_id
);
282 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
285 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
287 if ( $this->fld_title
) {
288 ApiQueryBase
::addTitleInfo( $vals, $title );
291 if ( $this->fld_timestamp
) {
292 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
295 if ( $this->fld_flags
) {
296 if ( $row->rev_parent_id
== 0 && !is_null( $row->rev_parent_id
) ) {
299 if ( $row->rev_minor_edit
) {
302 if ( $row->page_latest
== $row->rev_id
) {
307 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->rev_comment
) ) {
308 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
309 $vals['commenthidden'] = '';
311 if ( $this->fld_comment
) {
312 $vals['comment'] = $row->rev_comment
;
315 if ( $this->fld_parsedcomment
) {
317 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rev_comment
, $title );
322 if ( $this->fld_patrolled
&& $row->rc_patrolled
) {
323 $vals['patrolled'] = '';
326 if ( $this->fld_size
&& !is_null( $row->rev_len
) ) {
327 $vals['size'] = intval( $row->rev_len
);
330 if ( $this->fld_tags
) {
331 if ( $row->ts_tags
) {
332 $tags = explode( ',', $row->ts_tags
);
333 $this->getResult()->setIndexedTagName( $tags, 'tag' );
334 $vals['tags'] = $tags;
336 $vals['tags'] = array();
343 private function continueStr( $row ) {
344 return $row->rev_user_text
. '|' .
345 wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
348 public function getCacheMode( $params ) {
349 // This module provides access to deleted revisions and patrol flags if
350 // the requester is logged in
351 return 'anon-public-user-private';
354 public function getAllowedParams() {
357 ApiBase
::PARAM_DFLT
=> 10,
358 ApiBase
::PARAM_TYPE
=> 'limit',
359 ApiBase
::PARAM_MIN
=> 1,
360 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
361 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
364 ApiBase
::PARAM_TYPE
=> 'timestamp'
367 ApiBase
::PARAM_TYPE
=> 'timestamp'
371 ApiBase
::PARAM_ISMULTI
=> true
373 'userprefix' => null,
375 ApiBase
::PARAM_DFLT
=> 'older',
376 ApiBase
::PARAM_TYPE
=> array(
381 'namespace' => array(
382 ApiBase
::PARAM_ISMULTI
=> true,
383 ApiBase
::PARAM_TYPE
=> 'namespace'
386 ApiBase
::PARAM_ISMULTI
=> true,
387 ApiBase
::PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
388 ApiBase
::PARAM_TYPE
=> array(
401 ApiBase
::PARAM_ISMULTI
=> true,
402 ApiBase
::PARAM_TYPE
=> array(
413 public function getParamDescription() {
415 $p = $this->getModulePrefix();
417 'limit' => 'The maximum number of contributions to return',
418 'start' => 'The start timestamp to return from',
419 'end' => 'The end timestamp to return to',
420 'continue' => 'When more results are available, use this to continue',
421 'user' => 'The users to retrieve contributions for',
422 'userprefix' => "Retrieve contibutions for all users whose names begin with this value. Overrides {$p}user",
423 'dir' => 'The direction to search (older or newer)',
424 'namespace' => 'Only list contributions in these namespaces',
426 'Include additional pieces of information',
427 ' ids - Adds the page id and revision id',
428 ' title - Adds the title and namespace id of the page',
429 ' timestamp - Adds the timestamp of the edit',
430 ' comment - Adds the comment of the edit',
431 ' parsedcomment - Adds the parsed comment of the edit',
432 ' size - Adds the size of the page',
433 ' flags - Adds flags of the edit',
434 ' patrolled - Tags patrolled edits',
435 ' tags - Lists tags for the edit',
437 'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor",
438 "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown", ),
439 'tag' => 'Only list revisions tagged with this tag',
443 public function getDescription() {
444 return 'Get all edits by a user';
447 public function getPossibleErrors() {
448 return array_merge( parent
::getPossibleErrors(), array(
449 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
450 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
452 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
456 protected function getExamples() {
458 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
459 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
463 public function getVersion() {
464 return __CLASS__
. ': $Id$';