4 * Created on Oct 16, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
32 * This query action adds a list of a specified user's contributions to the output.
36 class ApiQueryContributions
extends ApiQueryBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
:: __construct( $query, $moduleName, 'uc' );
42 private $params, $username;
43 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
44 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
45 $fld_patrolled = false, $fld_tags = false;
47 public function execute() {
48 // Parse some parameters
49 $this->params
= $this->extractRequestParams();
51 $prop = array_flip( $this->params
['prop'] );
52 $this->fld_ids
= isset( $prop['ids'] );
53 $this->fld_title
= isset( $prop['title'] );
54 $this->fld_comment
= isset( $prop['comment'] );
55 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
56 $this->fld_size
= isset( $prop['size'] );
57 $this->fld_flags
= isset( $prop['flags'] );
58 $this->fld_timestamp
= isset( $prop['timestamp'] );
59 $this->fld_patrolled
= isset( $prop['patrolled'] );
60 $this->fld_tags
= isset( $prop['tags'] );
62 // TODO: if the query is going only against the revision table, should this be done?
63 $this->selectNamedDB( 'contributions', DB_SLAVE
, 'contributions' );
66 if ( isset( $this->params
['userprefix'] ) )
68 $this->prefixMode
= true;
69 $this->multiUserMode
= true;
70 $this->userprefix
= $this->params
['userprefix'];
74 $this->usernames
= array();
75 if ( !is_array( $this->params
['user'] ) )
76 $this->params
['user'] = array( $this->params
['user'] );
77 if ( !count( $this->params
['user'] ) )
78 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
79 foreach ( $this->params
['user'] as $u )
80 $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 while ( $row = $db->fetchObject( $res ) ) {
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
) );
104 $vals = $this->extractRowInfo( $row );
105 $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
) );
116 // Free the database record so the connection can get on with other stuff
117 $db->freeResult( $res );
119 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
123 * Validate the 'user' parameter and set the value to compare
124 * against `revision`.`rev_user_text`
126 private function prepareUsername( $user ) {
127 if ( !is_null( $user ) && $user !== '' ) {
128 $name = User
::isIP( $user )
130 : User
::getCanonicalName( $user, 'valid' );
131 if ( $name === false ) {
132 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
134 $this->usernames
[] = $name;
137 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
142 * Prepares the query and returns the limit of rows requested
144 private function prepareQuery() {
145 // We're after the revision table, and the corresponding page
146 // row for anything we retrieve. We may also need the
147 // recentchanges row and/or tag summary row.
149 $tables = array( 'page', 'revision' ); // Order may change
150 $this->addWhere( 'page_id=rev_page' );
152 // Handle continue parameter
153 if ( $this->multiUserMode
&& !is_null( $this->params
['continue'] ) )
155 $continue = explode( '|', $this->params
['continue'] );
156 if ( count( $continue ) != 2 )
157 $this->dieUsage( "Invalid continue param. You should pass the original " .
158 "value returned by the previous query", "_badcontinue" );
159 $encUser = $this->getDB()->strencode( $continue[0] );
160 $encTS = wfTimestamp( TS_MW
, $continue[1] );
161 $op = ( $this->params
['dir'] == 'older' ?
'<' : '>' );
162 $this->addWhere( "rev_user_text $op '$encUser' OR " .
163 "(rev_user_text = '$encUser' AND " .
164 "rev_timestamp $op= '$encTS')" );
167 if ( !$wgUser->isAllowed( 'hideuser' ) )
168 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' );
169 // We only want pages by the specified users.
170 if ( $this->prefixMode
)
171 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix
, $this->getDB()->anyString() ) );
173 $this->addWhereFld( 'rev_user_text', $this->usernames
);
174 // ... and in the specified timeframe.
175 // Ensure the same sort order for rev_user_text and rev_timestamp
176 // so our query is indexed
177 if ( $this->multiUserMode
)
178 $this->addWhereRange( 'rev_user_text', $this->params
['dir'], null, null );
179 $this->addWhereRange( 'rev_timestamp',
180 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
181 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
183 $show = $this->params
['show'];
184 if ( !is_null( $show ) ) {
185 $show = array_flip( $show );
186 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
187 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) )
188 $this->dieUsage( "Incorrect parameter - mutually exclusive values may not be supplied", 'show' );
190 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
191 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
192 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
193 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
195 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
196 $index = array( 'revision' => 'usertext_timestamp' );
198 // Mandatory fields: timestamp allows request continuation
199 // ns+title checks if the user has access rights for this page
200 // user_text is necessary if multiple users were specified
201 $this->addFields( array(
209 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ||
210 $this->fld_patrolled
)
213 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
214 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
215 // Use a redundant join condition on both
216 // timestamp and ID so we can use the timestamp
218 $index['recentchanges'] = 'rc_user_text';
219 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) )
221 // Put the tables in the right order for
223 $tables = array( 'revision', 'recentchanges', 'page' );
224 $this->addOption( 'STRAIGHT_JOIN' );
225 $this->addWhere( 'rc_user_text=rev_user_text' );
226 $this->addWhere( 'rc_timestamp=rev_timestamp' );
227 $this->addWhere( 'rc_this_oldid=rev_id' );
231 $tables[] = 'recentchanges';
232 $this->addJoinConds( array( 'recentchanges' => array(
234 'rc_user_text=rev_user_text',
235 'rc_timestamp=rev_timestamp',
236 'rc_this_oldid=rev_id' ) ) ) );
240 $this->addTables( $tables );
241 $this->addFieldsIf( 'rev_page', $this->fld_ids
);
242 $this->addFieldsIf( 'rev_id', $this->fld_ids ||
$this->fld_flags
);
243 $this->addFieldsIf( 'page_latest', $this->fld_flags
);
244 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
245 $this->addFieldsIf( 'rev_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
246 $this->addFieldsIf( 'rev_len', $this->fld_size
);
247 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags
);
248 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags
);
249 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
251 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 ) {
276 $vals['user'] = $row->rev_user_text
;
277 if ( $row->rev_deleted
& Revision
::DELETED_USER
)
278 $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 );
290 if ( $this->fld_timestamp
)
291 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
293 if ( $this->fld_flags
) {
294 if ( $row->rev_parent_id
== 0 && !is_null( $row->rev_parent_id
) )
296 if ( $row->rev_minor_edit
)
298 if ( $row->page_latest
== $row->rev_id
)
302 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->rev_comment
) ) {
303 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
)
304 $vals['commenthidden'] = '';
306 if ( $this->fld_comment
)
307 $vals['comment'] = $row->rev_comment
;
309 if ( $this->fld_parsedcomment
) {
311 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rev_comment
, $title );
316 if ( $this->fld_patrolled
&& $row->rc_patrolled
)
317 $vals['patrolled'] = '';
319 if ( $this->fld_size
&& !is_null( $row->rev_len
) )
320 $vals['size'] = intval( $row->rev_len
);
322 if ( $this->fld_tags
) {
323 if ( $row->ts_tags
) {
324 $tags = explode( ',', $row->ts_tags
);
325 $this->getResult()->setIndexedTagName( $tags, 'tag' );
326 $vals['tags'] = $tags;
328 $vals['tags'] = array();
335 private function continueStr( $row )
337 return $row->rev_user_text
. '|' .
338 wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
341 public function getAllowedParams() {
344 ApiBase
:: PARAM_DFLT
=> 10,
345 ApiBase
:: PARAM_TYPE
=> 'limit',
346 ApiBase
:: PARAM_MIN
=> 1,
347 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
348 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
351 ApiBase
:: PARAM_TYPE
=> 'timestamp'
354 ApiBase
:: PARAM_TYPE
=> 'timestamp'
358 ApiBase
:: PARAM_ISMULTI
=> true
360 'userprefix' => null,
362 ApiBase
:: PARAM_DFLT
=> 'older',
363 ApiBase
:: PARAM_TYPE
=> array (
368 'namespace' => array (
369 ApiBase
:: PARAM_ISMULTI
=> true,
370 ApiBase
:: PARAM_TYPE
=> 'namespace'
373 ApiBase
:: PARAM_ISMULTI
=> true,
374 ApiBase
:: PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
375 ApiBase
:: PARAM_TYPE
=> array (
388 ApiBase
:: PARAM_ISMULTI
=> true,
389 ApiBase
:: PARAM_TYPE
=> array (
400 public function getParamDescription() {
402 'limit' => 'The maximum number of contributions to return.',
403 'start' => 'The start timestamp to return from.',
404 'end' => 'The end timestamp to return to.',
405 'continue' => 'When more results are available, use this to continue.',
406 'user' => 'The user to retrieve contributions for.',
407 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
408 'dir' => 'The direction to search (older or newer).',
409 'namespace' => 'Only list contributions in these namespaces',
410 'prop' => 'Include additional pieces of information',
411 'show' => array( 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
412 'NOTE: if show=patrolled or show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown', ),
413 'tag' => 'Only list revisions tagged with this tag',
417 public function getDescription() {
418 return 'Get all edits by a user';
421 protected function getExamples() {
423 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
424 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
428 public function getVersion() {
429 return __CLASS__
. ': $Id$';