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;
46 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_flags
= isset($prop['flags']);
56 $this->fld_timestamp
= isset($prop['timestamp']);
58 // TODO: if the query is going only against the revision table, should this be done?
59 $this->selectNamedDB('contributions', DB_SLAVE
, 'contributions');
62 if(isset($this->params
['userprefix']))
64 $this->prefixMode
= true;
65 $this->multiUserMode
= true;
66 $this->userprefix
= $this->params
['userprefix'];
70 $this->usernames
= array();
71 if(!is_array($this->params
['user']))
72 $this->params
['user'] = array($this->params
['user']);
73 foreach($this->params
['user'] as $u)
74 $this->prepareUsername($u);
75 $this->prefixMode
= false;
76 $this->multiUserMode
= (count($this->params
['user']) > 1);
78 $this->prepareQuery();
80 //Do the actual query.
81 $res = $this->select( __METHOD__
);
83 //Initialise some variables
86 $limit = $this->params
['limit'];
89 while ( $row = $db->fetchObject( $res ) ) {
90 if (++
$count > $limit) {
91 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
92 if($this->multiUserMode
)
93 $this->setContinueEnumParameter('continue', $this->continueStr($row));
95 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
));
99 $vals = $this->extractRowInfo($row);
104 //Free the database record so the connection can get on with other stuff
105 $db->freeResult($res);
107 //And send the whole shebang out as output.
108 $this->getResult()->setIndexedTagName($data, 'item');
109 $this->getResult()->addValue('query', $this->getModuleName(), $data);
113 * Validate the 'user' parameter and set the value to compare
114 * against `revision`.`rev_user_text`
116 private function prepareUsername($user) {
118 $name = User
::isIP( $user )
120 : User
::getCanonicalName( $user, 'valid' );
121 if( $name === false ) {
122 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
124 $this->usernames
[] = $name;
127 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
132 * Prepares the query and returns the limit of rows requested
134 private function prepareQuery() {
136 //We're after the revision table, and the corresponding page row for
137 //anything we retrieve.
138 $this->addTables(array('revision', 'page'));
139 $this->addWhere('page_id=rev_page');
141 // Handle continue parameter
142 if($this->multiUserMode
&& !is_null($this->params
['continue']))
144 $continue = explode('|', $this->params
['continue']);
145 if(count($continue) != 2)
146 $this->dieUsage("Invalid continue param. You should pass the original " .
147 "value returned by the previous query", "_badcontinue");
148 $encUser = $this->getDB()->strencode($continue[0]);
149 $encTS = wfTimestamp(TS_MW
, $continue[1]);
150 $op = ($this->params
['dir'] == 'older' ?
'<' : '>');
151 $this->addWhere("rev_user_text $op '$encUser' OR " .
152 "(rev_user_text = '$encUser' AND " .
153 "rev_timestamp $op= '$encTS')");
156 $this->addWhereFld('rev_deleted', 0);
157 // We only want pages by the specified users.
158 if($this->prefixMode
)
159 $this->addWhere("rev_user_text LIKE '" . $this->getDB()->escapeLike($this->userprefix
) . "%'");
161 $this->addWhereFld('rev_user_text', $this->usernames
);
162 // ... and in the specified timeframe.
163 // Ensure the same sort order for rev_user_text and rev_timestamp
164 // so our query is indexed
165 $this->addWhereRange('rev_user_text', $this->params
['dir'], null, null);
166 $this->addWhereRange('rev_timestamp',
167 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
168 $this->addWhereFld('page_namespace', $this->params
['namespace']);
170 $show = $this->params
['show'];
171 if (!is_null($show)) {
172 $show = array_flip($show);
173 if (isset ($show['minor']) && isset ($show['!minor']))
174 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
176 $this->addWhereIf('rev_minor_edit = 0', isset ($show['!minor']));
177 $this->addWhereIf('rev_minor_edit != 0', isset ($show['minor']));
179 $this->addOption('LIMIT', $this->params
['limit'] +
1);
180 $this->addOption( 'USE INDEX', array( 'revision' => 'usertext_timestamp' ) );
182 // Mandatory fields: timestamp allows request continuation
183 // ns+title checks if the user has access rights for this page
184 // user_text is necessary if multiple users were specified
185 $this->addFields(array(
192 $this->addFieldsIf('rev_page', $this->fld_ids
);
193 $this->addFieldsIf('rev_id', $this->fld_ids ||
$this->fld_flags
);
194 $this->addFieldsIf('page_latest', $this->fld_flags
);
195 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
196 $this->addFieldsIf('rev_comment', $this->fld_comment
);
197 $this->addFieldsIf('rev_minor_edit', $this->fld_flags
);
198 $this->addFieldsIf('page_is_new', $this->fld_flags
);
202 * Extract fields from the database row and append them to a result array
204 private function extractRowInfo($row) {
208 $vals['user'] = $row->rev_user_text
;
209 if ($this->fld_ids
) {
210 $vals['pageid'] = intval($row->rev_page
);
211 $vals['revid'] = intval($row->rev_id
);
212 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
215 if ($this->fld_title
)
216 ApiQueryBase
:: addTitleInfo($vals,
217 Title
:: makeTitle($row->page_namespace
, $row->page_title
));
219 if ($this->fld_timestamp
)
220 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
222 if ($this->fld_flags
) {
223 if ($row->page_is_new
)
225 if ($row->rev_minor_edit
)
227 if ($row->page_latest
== $row->rev_id
)
231 if ($this->fld_comment
&& isset( $row->rev_comment
) )
232 $vals['comment'] = $row->rev_comment
;
237 private function continueStr($row)
239 return $row->rev_user_text
. '|' .
240 wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
243 public function getAllowedParams() {
246 ApiBase
:: PARAM_DFLT
=> 10,
247 ApiBase
:: PARAM_TYPE
=> 'limit',
248 ApiBase
:: PARAM_MIN
=> 1,
249 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
250 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
253 ApiBase
:: PARAM_TYPE
=> 'timestamp'
256 ApiBase
:: PARAM_TYPE
=> 'timestamp'
260 ApiBase
:: PARAM_ISMULTI
=> true
262 'userprefix' => null,
264 ApiBase
:: PARAM_DFLT
=> 'older',
265 ApiBase
:: PARAM_TYPE
=> array (
270 'namespace' => array (
271 ApiBase
:: PARAM_ISMULTI
=> true,
272 ApiBase
:: PARAM_TYPE
=> 'namespace'
275 ApiBase
:: PARAM_ISMULTI
=> true,
276 ApiBase
:: PARAM_DFLT
=> 'ids|title|timestamp|flags|comment',
277 ApiBase
:: PARAM_TYPE
=> array (
286 ApiBase
:: PARAM_ISMULTI
=> true,
287 ApiBase
:: PARAM_TYPE
=> array (
295 public function getParamDescription() {
297 'limit' => 'The maximum number of contributions to return.',
298 'start' => 'The start timestamp to return from.',
299 'end' => 'The end timestamp to return to.',
300 'continue' => 'When more results are available, use this to continue.',
301 'user' => 'The user to retrieve contributions for.',
302 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
303 'dir' => 'The direction to search (older or newer).',
304 'namespace' => 'Only list contributions in these namespaces',
305 'prop' => 'Include additional pieces of information',
306 'show' => 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
310 public function getDescription() {
311 return 'Get all edits by a user';
314 protected function getExamples() {
316 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
317 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
321 public function getVersion() {
322 return __CLASS__
. ': $Id$';