* Re-applying r34449, r34500 and r34518 which Brion reverted by accident
[lhc/web/wiklou.git] / includes / api / ApiQueryUserContributions.php
1 <?php
2
3 /*
4 * Created on Oct 16, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * This query action adds a list of a specified user's contributions to the output.
33 *
34 * @addtogroup API
35 */
36 class ApiQueryContributions extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'uc');
40 }
41
42 private $params, $username;
43 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
44 $fld_comment = false, $fld_flags = false;
45
46 public function execute() {
47
48 // Parse some parameters
49 $this->params = $this->extractRequestParams();
50
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']);
57
58 // TODO: if the query is going only against the revision table, should this be done?
59 $this->selectNamedDB('contributions', DB_SLAVE, 'contributions');
60 $db = $this->getDB();
61
62 if(isset($this->params['userprefix']))
63 {
64 $this->prefixMode = true;
65 $this->userprefix = $this->params['userprefix'];
66 }
67 else
68 {
69 $this->usernames = array();
70 if(!is_array($this->params['user']))
71 $this->params['user'] = array($this->params['user']);
72 foreach($this->params['user'] as $u)
73 $this->prepareUsername($u);
74 $this->prefixMode = false;
75 }
76 $this->prepareQuery();
77
78 //Do the actual query.
79 $res = $this->select( __METHOD__ );
80
81 //Initialise some variables
82 $data = array ();
83 $count = 0;
84 $limit = $this->params['limit'];
85
86 //Fetch each row
87 while ( $row = $db->fetchObject( $res ) ) {
88 if (++ $count > $limit) {
89 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
90 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rev_timestamp));
91 break;
92 }
93
94 $vals = $this->extractRowInfo($row);
95 if ($vals)
96 $data[] = $vals;
97 }
98
99 //Free the database record so the connection can get on with other stuff
100 $db->freeResult($res);
101
102 //And send the whole shebang out as output.
103 $this->getResult()->setIndexedTagName($data, 'item');
104 $this->getResult()->addValue('query', $this->getModuleName(), $data);
105 }
106
107 /**
108 * Validate the 'user' parameter and set the value to compare
109 * against `revision`.`rev_user_text`
110 */
111 private function prepareUsername($user) {
112 if( $user ) {
113 $name = User::isIP( $user )
114 ? $user
115 : User::getCanonicalName( $user, 'valid' );
116 if( $name === false ) {
117 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
118 } else {
119 $this->usernames[] = $name;
120 }
121 } else {
122 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
123 }
124 }
125
126 /**
127 * Prepares the query and returns the limit of rows requested
128 */
129 private function prepareQuery() {
130
131 //We're after the revision table, and the corresponding page row for
132 //anything we retrieve.
133 $this->addTables(array('revision', 'page'));
134 $this->addWhere('page_id=rev_page');
135
136 $this->addWhereFld('rev_deleted', 0);
137 // We only want pages by the specified users.
138 if($this->prefixMode)
139 $this->addWhere("rev_user_text LIKE '" . $this->getDb()->escapeLike($this->userprefix) . "%'");
140 else
141 $this->addWhereFld( 'rev_user_text', $this->usernames );
142 // ... and in the specified timeframe.
143 // Ensure the same sort order for rev_user_text and rev_timestamp
144 // so our query is indexed
145 $this->addWhereRange('rev_user_text', $this->params['dir'], null, null);
146 $this->addWhereRange('rev_timestamp',
147 $this->params['dir'], $this->params['start'], $this->params['end'] );
148 $this->addWhereFld('page_namespace', $this->params['namespace']);
149
150 $show = $this->params['show'];
151 if (!is_null($show)) {
152 $show = array_flip($show);
153 if (isset ($show['minor']) && isset ($show['!minor']))
154 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
155
156 $this->addWhereIf('rev_minor_edit = 0', isset ($show['!minor']));
157 $this->addWhereIf('rev_minor_edit != 0', isset ($show['minor']));
158 }
159 $this->addOption('LIMIT', $this->params['limit'] + 1);
160
161 // Mandatory fields: timestamp allows request continuation
162 // ns+title checks if the user has access rights for this page
163 // user_text is necessary if multiple users were specified
164 $this->addFields(array(
165 'rev_timestamp',
166 'page_namespace',
167 'page_title',
168 'rev_user_text',
169 ));
170
171 $this->addFieldsIf('rev_page', $this->fld_ids);
172 $this->addFieldsIf('rev_id', $this->fld_ids);
173 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
174 $this->addFieldsIf('rev_comment', $this->fld_comment);
175 $this->addFieldsIf('rev_minor_edit', $this->fld_flags);
176 $this->addFieldsIf('page_is_new', $this->fld_flags);
177 }
178
179 /**
180 * Extract fields from the database row and append them to a result array
181 */
182 private function extractRowInfo($row) {
183
184 $vals = array();
185
186 $vals['user'] = $row->rev_user_text;
187 if ($this->fld_ids) {
188 $vals['pageid'] = intval($row->rev_page);
189 $vals['revid'] = intval($row->rev_id);
190 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
191 }
192
193 if ($this->fld_title)
194 ApiQueryBase :: addTitleInfo($vals,
195 Title :: makeTitle($row->page_namespace, $row->page_title));
196
197 if ($this->fld_timestamp)
198 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
199
200 if ($this->fld_flags) {
201 if ($row->page_is_new)
202 $vals['new'] = '';
203 if ($row->rev_minor_edit)
204 $vals['minor'] = '';
205 }
206
207 if ($this->fld_comment && !empty ($row->rev_comment))
208 $vals['comment'] = $row->rev_comment;
209
210 return $vals;
211 }
212
213 public function getAllowedParams() {
214 return array (
215 'limit' => array (
216 ApiBase :: PARAM_DFLT => 10,
217 ApiBase :: PARAM_TYPE => 'limit',
218 ApiBase :: PARAM_MIN => 1,
219 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
220 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
221 ),
222 'start' => array (
223 ApiBase :: PARAM_TYPE => 'timestamp'
224 ),
225 'end' => array (
226 ApiBase :: PARAM_TYPE => 'timestamp'
227 ),
228 'user' => array (
229 ApiBase :: PARAM_ISMULTI => true
230 ),
231 'userprefix' => null,
232 'dir' => array (
233 ApiBase :: PARAM_DFLT => 'older',
234 ApiBase :: PARAM_TYPE => array (
235 'newer',
236 'older'
237 )
238 ),
239 'namespace' => array (
240 ApiBase :: PARAM_ISMULTI => true,
241 ApiBase :: PARAM_TYPE => 'namespace'
242 ),
243 'prop' => array (
244 ApiBase :: PARAM_ISMULTI => true,
245 ApiBase :: PARAM_DFLT => 'ids|title|timestamp|flags|comment',
246 ApiBase :: PARAM_TYPE => array (
247 'ids',
248 'title',
249 'timestamp',
250 'comment',
251 'flags'
252 )
253 ),
254 'show' => array (
255 ApiBase :: PARAM_ISMULTI => true,
256 ApiBase :: PARAM_TYPE => array (
257 'minor',
258 '!minor',
259 )
260 ),
261 );
262 }
263
264 public function getParamDescription() {
265 return array (
266 'limit' => 'The maximum number of contributions to return.',
267 'start' => 'The start timestamp to return from.',
268 'end' => 'The end timestamp to return to.',
269 'user' => 'The user to retrieve contributions for.',
270 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
271 'dir' => 'The direction to search (older or newer).',
272 'namespace' => 'Only list contributions in these namespaces',
273 'prop' => 'Include additional pieces of information',
274 'show' => 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
275 );
276 }
277
278 public function getDescription() {
279 return 'Get all edits by a user';
280 }
281
282 protected function getExamples() {
283 return array (
284 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
285 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
286 );
287 }
288
289 public function getVersion() {
290 return __CLASS__ . ': $Id$';
291 }
292 }