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');
63 if(isset($this->params
['userprefix']))
65 $this->prefixMode
= 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;
77 $this->prepareQuery();
79 //Do the actual query.
80 $res = $this->select( __METHOD__
);
82 //Initialise some variables
85 $limit = $this->params
['limit'];
88 while ( $row = $db->fetchObject( $res ) ) {
89 if (++
$count > $limit) {
90 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
91 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
));
95 $vals = $this->extractRowInfo($row);
100 //Free the database record so the connection can get on with other stuff
101 $db->freeResult($res);
103 //And send the whole shebang out as output.
104 $this->getResult()->setIndexedTagName($data, 'item');
105 $this->getResult()->addValue('query', $this->getModuleName(), $data);
109 * Validate the 'user' parameter and set the value to compare
110 * against `revision`.`rev_user_text`
112 private function prepareUsername($user) {
114 $name = User
::isIP( $user )
116 : User
::getCanonicalName( $user, 'valid' );
117 if( $name === false ) {
118 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
120 $this->usernames
[] = $name;
123 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
128 * Prepares the query and returns the limit of rows requested
130 private function prepareQuery() {
132 //We're after the revision table, and the corresponding page row for
133 //anything we retrieve.
134 list ($tbl_page, $tbl_revision) = $this->getDB()->tableNamesN('page', 'revision');
135 $this->addTables("$tbl_revision LEFT OUTER JOIN $tbl_page ON page_id=rev_page");
137 $this->addWhereFld('rev_deleted', 0);
138 // We only want pages by the specified users.
139 if($this->prefixMode
)
140 $this->addWhere("rev_user_text LIKE '" . $this->getDb()->escapeLike($this->userprefix
) . "%'");
142 $this->addWhereFld( 'rev_user_text', $this->usernames
);
143 // ... and in the specified timeframe.
144 // Ensure the same sort order for rev_user_text and rev_timestamp
145 // so our query is indexed
146 $this->addWhereRange('rev_user_text', $this->params
['dir'], null, null);
147 $this->addWhereRange('rev_timestamp',
148 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
149 $this->addWhereFld('page_namespace', $this->params
['namespace']);
151 $show = $this->params
['show'];
152 if (!is_null($show)) {
153 $show = array_flip($show);
154 if (isset ($show['minor']) && isset ($show['!minor']))
155 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
157 $this->addWhereIf('rev_minor_edit = 0', isset ($show['!minor']));
158 $this->addWhereIf('rev_minor_edit != 0', isset ($show['minor']));
160 $this->addOption('LIMIT', $this->params
['limit'] +
1);
162 // Mandatory fields: timestamp allows request continuation
163 // ns+title checks if the user has access rights for this page
164 // user_text is necessary if multiple users were specified
165 $this->addFields(array(
172 $this->addFieldsIf('rev_page', $this->fld_ids
);
173 $this->addFieldsIf('rev_id', $this->fld_ids
);
174 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
175 $this->addFieldsIf('rev_comment', $this->fld_comment
);
176 $this->addFieldsIf('rev_minor_edit', $this->fld_flags
);
177 $this->addFieldsIf('page_is_new', $this->fld_flags
);
181 * Extract fields from the database row and append them to a result array
183 private function extractRowInfo($row) {
187 $vals['user'] = $row->rev_user_text
;
188 if ($this->fld_ids
) {
189 $vals['pageid'] = intval($row->rev_page
);
190 $vals['revid'] = intval($row->rev_id
);
191 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
194 if ($this->fld_title
)
195 ApiQueryBase
:: addTitleInfo($vals,
196 Title
:: makeTitle($row->page_namespace
, $row->page_title
));
198 if ($this->fld_timestamp
)
199 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
201 if ($this->fld_flags
) {
202 if ($row->page_is_new
)
204 if ($row->rev_minor_edit
)
208 if ($this->fld_comment
&& !empty ($row->rev_comment
))
209 $vals['comment'] = $row->rev_comment
;
214 public function getAllowedParams() {
217 ApiBase
:: PARAM_DFLT
=> 10,
218 ApiBase
:: PARAM_TYPE
=> 'limit',
219 ApiBase
:: PARAM_MIN
=> 1,
220 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
221 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
224 ApiBase
:: PARAM_TYPE
=> 'timestamp'
227 ApiBase
:: PARAM_TYPE
=> 'timestamp'
230 ApiBase
:: PARAM_ISMULTI
=> true
232 'userprefix' => null,
234 ApiBase
:: PARAM_DFLT
=> 'older',
235 ApiBase
:: PARAM_TYPE
=> array (
240 'namespace' => array (
241 ApiBase
:: PARAM_ISMULTI
=> true,
242 ApiBase
:: PARAM_TYPE
=> 'namespace'
245 ApiBase
:: PARAM_ISMULTI
=> true,
246 ApiBase
:: PARAM_DFLT
=> 'ids|title|timestamp|flags|comment',
247 ApiBase
:: PARAM_TYPE
=> array (
256 ApiBase
:: PARAM_ISMULTI
=> true,
257 ApiBase
:: PARAM_TYPE
=> array (
265 public function getParamDescription() {
267 'limit' => 'The maximum number of contributions to return.',
268 'start' => 'The start timestamp to return from.',
269 'end' => 'The end timestamp to return to.',
270 'user' => 'The user to retrieve contributions for.',
271 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
272 'dir' => 'The direction to search (older or newer).',
273 'namespace' => 'Only list contributions in these namespaces',
274 'prop' => 'Include additional pieces of information',
275 'show' => 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
279 public function getDescription() {
280 return 'Get all edits by a user';
283 protected function getExamples() {
285 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
286 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
290 public function getVersion() {
291 return __CLASS__
. ': $Id$';