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_flags = false,
45 $fld_patrolled = false, $fld_tags = false;
47 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_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 foreach($this->params
['user'] as $u)
78 $this->prepareUsername($u);
79 $this->prefixMode
= false;
80 $this->multiUserMode
= (count($this->params
['user']) > 1);
82 $this->prepareQuery();
84 //Do the actual query.
85 $res = $this->select( __METHOD__
);
87 //Initialise some variables
89 $limit = $this->params
['limit'];
92 while ( $row = $db->fetchObject( $res ) ) {
93 if (++
$count > $limit) {
94 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
95 if($this->multiUserMode
)
96 $this->setContinueEnumParameter('continue', $this->continueStr($row));
98 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
));
102 $vals = $this->extractRowInfo($row);
103 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
106 if($this->multiUserMode
)
107 $this->setContinueEnumParameter('continue', $this->continueStr($row));
109 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
));
114 //Free the database record so the connection can get on with other stuff
115 $db->freeResult($res);
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']))
153 $continue = explode('|', $this->params
['continue']);
154 if(count($continue) != 2)
155 $this->dieUsage("Invalid continue param. You should pass the original " .
156 "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' ?
'<' : '>');
160 $this->addWhere("rev_user_text $op '$encUser' OR " .
161 "(rev_user_text = '$encUser' AND " .
162 "rev_timestamp $op= '$encTS')");
165 if(!$wgUser->isAllowed('hideuser'))
166 $this->addWhere($this->getDB()->bitAnd('rev_deleted',Revision
::DELETED_USER
) . ' = 0');
167 // We only want pages by the specified users.
168 if($this->prefixMode
)
169 $this->addWhere('rev_user_text' . $this->getDB()->buildLike($this->userprefix
, $this->getDB()->anyString()));
171 $this->addWhereFld('rev_user_text', $this->usernames
);
172 // ... and in the specified timeframe.
173 // Ensure the same sort order for rev_user_text and rev_timestamp
174 // so our query is indexed
175 if($this->multiUserMode
)
176 $this->addWhereRange('rev_user_text', $this->params
['dir'], null, null);
177 $this->addWhereRange('rev_timestamp',
178 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
179 $this->addWhereFld('page_namespace', $this->params
['namespace']);
181 $show = $this->params
['show'];
182 if (!is_null($show)) {
183 $show = array_flip($show);
184 if ((isset($show['minor']) && isset($show['!minor']))
185 ||
(isset($show['patrolled']) && isset($show['!patrolled'])))
186 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
188 $this->addWhereIf('rev_minor_edit = 0', isset($show['!minor']));
189 $this->addWhereIf('rev_minor_edit != 0', isset($show['minor']));
190 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
191 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
193 $this->addOption('LIMIT', $this->params
['limit'] +
1);
194 $index['revision'] = 'usertext_timestamp';
196 // Mandatory fields: timestamp allows request continuation
197 // ns+title checks if the user has access rights for this page
198 // user_text is necessary if multiple users were specified
199 $this->addFields(array(
207 if(isset($show['patrolled']) ||
isset($show['!patrolled']) ||
208 $this->fld_patrolled
)
211 if(!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
212 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
213 // Use a redundant join condition on both
214 // timestamp and ID so we can use the timestamp
216 $index['recentchanges'] = 'rc_user_text';
217 if(isset($show['patrolled']) ||
isset($show['!patrolled']))
219 // Put the tables in the right order for
221 $tables = array('revision', 'recentchanges', 'page');
222 $this->addOption('STRAIGHT_JOIN');
223 $this->addWhere('rc_user_text=rev_user_text');
224 $this->addWhere('rc_timestamp=rev_timestamp');
225 $this->addWhere('rc_this_oldid=rev_id');
229 $tables[] = 'recentchanges';
230 $this->addJoinConds(array('recentchanges' => array(
232 'rc_user_text=rev_user_text',
233 'rc_timestamp=rev_timestamp',
234 'rc_this_oldid=rev_id'))));
238 $this->addTables($tables);
239 $this->addOption('USE INDEX', $index);
240 $this->addFieldsIf('rev_page', $this->fld_ids
);
241 $this->addFieldsIf('rev_id', $this->fld_ids ||
$this->fld_flags
);
242 $this->addFieldsIf('page_latest', $this->fld_flags
);
243 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
244 $this->addFieldsIf('rev_comment', $this->fld_comment
);
245 $this->addFieldsIf('rev_len', $this->fld_size
);
246 $this->addFieldsIf('rev_minor_edit', $this->fld_flags
);
247 $this->addFieldsIf('rev_parent_id', $this->fld_flags
);
248 $this->addFieldsIf('rc_patrolled', $this->fld_patrolled
);
252 $this->addTables('tag_summary');
253 $this->addJoinConds(array('tag_summary' => array('LEFT JOIN', array('rev_id=ts_rev_id'))));
254 $this->addFields('ts_tags');
257 if( !is_null($this->params
['tag']) ) {
258 $this->addTables('change_tag');
259 $this->addJoinConds(array('change_tag' => array('INNER JOIN', array('rev_id=ct_rev_id'))));
260 $this->addWhereFld('ct_tag', $this->params
['tag']);
265 * Extract fields from the database row and append them to a result array
267 private function extractRowInfo($row) {
271 $vals['user'] = $row->rev_user_text
;
272 if ($row->rev_deleted
& Revision
::DELETED_USER
)
273 $vals['userhidden'] = '';
274 if ($this->fld_ids
) {
275 $vals['pageid'] = intval($row->rev_page
);
276 $vals['revid'] = intval($row->rev_id
);
277 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
280 if ($this->fld_title
)
281 ApiQueryBase
:: addTitleInfo($vals,
282 Title
:: makeTitle($row->page_namespace
, $row->page_title
));
284 if ($this->fld_timestamp
)
285 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
287 if ($this->fld_flags
) {
288 if ($row->rev_parent_id
== 0 && !is_null($row->rev_parent_id
))
290 if ($row->rev_minor_edit
)
292 if ($row->page_latest
== $row->rev_id
)
296 if ($this->fld_comment
&& isset($row->rev_comment
)) {
297 if ($row->rev_deleted
& Revision
::DELETED_COMMENT
)
298 $vals['commenthidden'] = '';
300 $vals['comment'] = $row->rev_comment
;
303 if ($this->fld_patrolled
&& $row->rc_patrolled
)
304 $vals['patrolled'] = '';
306 if ($this->fld_size
&& !is_null($row->rev_len
))
307 $vals['size'] = intval($row->rev_len
);
309 if ($this->fld_tags
) {
311 $tags = explode(',', $row->ts_tags
);
312 $this->getResult()->setIndexedTagName($tags, 'tag');
313 $vals['tags'] = $tags;
315 $vals['tags'] = array();
322 private function continueStr($row)
324 return $row->rev_user_text
. '|' .
325 wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
328 public function getAllowedParams() {
331 ApiBase
:: PARAM_DFLT
=> 10,
332 ApiBase
:: PARAM_TYPE
=> 'limit',
333 ApiBase
:: PARAM_MIN
=> 1,
334 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
335 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
338 ApiBase
:: PARAM_TYPE
=> 'timestamp'
341 ApiBase
:: PARAM_TYPE
=> 'timestamp'
345 ApiBase
:: PARAM_ISMULTI
=> true
347 'userprefix' => null,
349 ApiBase
:: PARAM_DFLT
=> 'older',
350 ApiBase
:: PARAM_TYPE
=> array (
355 'namespace' => array (
356 ApiBase
:: PARAM_ISMULTI
=> true,
357 ApiBase
:: PARAM_TYPE
=> 'namespace'
360 ApiBase
:: PARAM_ISMULTI
=> true,
361 ApiBase
:: PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
362 ApiBase
:: PARAM_TYPE
=> array (
374 ApiBase
:: PARAM_ISMULTI
=> true,
375 ApiBase
:: PARAM_TYPE
=> array (
385 public function getParamDescription() {
387 'limit' => 'The maximum number of contributions to return.',
388 'start' => 'The start timestamp to return from.',
389 'end' => 'The end timestamp to return to.',
390 'continue' => 'When more results are available, use this to continue.',
391 'user' => 'The user to retrieve contributions for.',
392 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
393 'dir' => 'The direction to search (older or newer).',
394 'namespace' => 'Only list contributions in these namespaces',
395 'prop' => 'Include additional pieces of information',
396 'show' => array('Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
397 'NOTE: if show=patrolled or show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown',),
401 public function getDescription() {
402 return 'Get all edits by a user';
405 protected function getExamples() {
407 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
408 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
412 public function getVersion() {
413 return __CLASS__
. ': $Id$';