59a469f53617d0e69a9c89cbb37ae1a14fde81de
[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
63 if(isset($this->params['userprefix']))
64 {
65 global $wgAPIUCUserPrefixMinLength;
66 if(strlen($this->params['userprefix']) < $wgAPIUCUserPrefixMinLength)
67 $this->dieUsage("User prefixes must be at least $wgAPIUCUserPrefixMinLength characters", 'userprefix-tooshort');
68
69 $this->prefixMode = true;
70 $this->userprefix = $this->params['userprefix'];
71 }
72 else
73 {
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 }
81 $this->prepareQuery();
82
83 //Do the actual query.
84 $res = $this->select( __METHOD__ );
85
86 //Initialise some variables
87 $data = array ();
88 $count = 0;
89 $limit = $this->params['limit'];
90
91 //Fetch each row
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 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rev_timestamp));
96 break;
97 }
98
99 $vals = $this->extractRowInfo($row);
100 if ($vals)
101 $data[] = $vals;
102 }
103
104 //Free the database record so the connection can get on with other stuff
105 $db->freeResult($res);
106
107 //And send the whole shebang out as output.
108 $this->getResult()->setIndexedTagName($data, 'item');
109 $this->getResult()->addValue('query', $this->getModuleName(), $data);
110 }
111
112 /**
113 * Validate the 'user' parameter and set the value to compare
114 * against `revision`.`rev_user_text`
115 */
116 private function prepareUsername($user) {
117 if( $user ) {
118 $name = User::isIP( $user )
119 ? $user
120 : User::getCanonicalName( $user, 'valid' );
121 if( $name === false ) {
122 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
123 } else {
124 $this->usernames[] = $name;
125 }
126 } else {
127 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
128 }
129 }
130
131 /**
132 * Prepares the query and returns the limit of rows requested
133 */
134 private function prepareQuery() {
135
136 //We're after the revision table, and the corresponding page row for
137 //anything we retrieve.
138 list ($tbl_page, $tbl_revision) = $this->getDB()->tableNamesN('page', 'revision');
139 $this->addTables("$tbl_revision LEFT OUTER JOIN $tbl_page ON page_id=rev_page");
140
141 $this->addWhereFld('rev_deleted', 0);
142 // We only want pages by the specified users.
143 if($this->prefixMode)
144 $this->addWhere("rev_user_text LIKE '" . $this->getDb()->escapeLike($this->userprefix) . "%'");
145 else
146 $this->addWhereFld( 'rev_user_text', $this->usernames );
147 // ... and in the specified timeframe.
148 $this->addWhereRange('rev_timestamp',
149 $this->params['dir'], $this->params['start'], $this->params['end'] );
150 $this->addWhereFld('page_namespace', $this->params['namespace']);
151
152 $show = $this->params['show'];
153 if (!is_null($show)) {
154 $show = array_flip($show);
155 if (isset ($show['minor']) && isset ($show['!minor']))
156 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
157
158 $this->addWhereIf('rev_minor_edit = 0', isset ($show['!minor']));
159 $this->addWhereIf('rev_minor_edit != 0', isset ($show['minor']));
160 }
161 $this->addOption('LIMIT', $this->params['limit'] + 1);
162
163 // Mandatory fields: timestamp allows request continuation
164 // ns+title checks if the user has access rights for this page
165 // user_text is necessary if multiple users were specified
166 $this->addFields(array(
167 'rev_timestamp',
168 'page_namespace',
169 'page_title',
170 'rev_user_text',
171 ));
172
173 $this->addFieldsIf('rev_page', $this->fld_ids);
174 $this->addFieldsIf('rev_id', $this->fld_ids);
175 // $this->addFieldsIf('rev_text_id', $this->fld_ids); // Should this field be exposed?
176 $this->addFieldsIf('rev_comment', $this->fld_comment);
177 $this->addFieldsIf('rev_minor_edit', $this->fld_flags);
178 $this->addFieldsIf('page_is_new', $this->fld_flags);
179 }
180
181 /**
182 * Extract fields from the database row and append them to a result array
183 */
184 private function extractRowInfo($row) {
185
186 $vals = array();
187
188 $vals['user'] = $row->rev_user_text;
189 if ($this->fld_ids) {
190 $vals['pageid'] = intval($row->rev_page);
191 $vals['revid'] = intval($row->rev_id);
192 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
193 }
194
195 if ($this->fld_title)
196 ApiQueryBase :: addTitleInfo($vals,
197 Title :: makeTitle($row->page_namespace, $row->page_title));
198
199 if ($this->fld_timestamp)
200 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
201
202 if ($this->fld_flags) {
203 if ($row->page_is_new)
204 $vals['new'] = '';
205 if ($row->rev_minor_edit)
206 $vals['minor'] = '';
207 }
208
209 if ($this->fld_comment && !empty ($row->rev_comment))
210 $vals['comment'] = $row->rev_comment;
211
212 return $vals;
213 }
214
215 public function getAllowedParams() {
216 return array (
217 'limit' => array (
218 ApiBase :: PARAM_DFLT => 10,
219 ApiBase :: PARAM_TYPE => 'limit',
220 ApiBase :: PARAM_MIN => 1,
221 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
222 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
223 ),
224 'start' => array (
225 ApiBase :: PARAM_TYPE => 'timestamp'
226 ),
227 'end' => array (
228 ApiBase :: PARAM_TYPE => 'timestamp'
229 ),
230 'user' => array (
231 ApiBase :: PARAM_ISMULTI => true
232 ),
233 'userprefix' => null,
234 'dir' => array (
235 ApiBase :: PARAM_DFLT => 'older',
236 ApiBase :: PARAM_TYPE => array (
237 'newer',
238 'older'
239 )
240 ),
241 'namespace' => array (
242 ApiBase :: PARAM_ISMULTI => true,
243 ApiBase :: PARAM_TYPE => 'namespace'
244 ),
245 'prop' => array (
246 ApiBase :: PARAM_ISMULTI => true,
247 ApiBase :: PARAM_DFLT => 'ids|title|timestamp|flags|comment',
248 ApiBase :: PARAM_TYPE => array (
249 'ids',
250 'title',
251 'timestamp',
252 'comment',
253 'flags'
254 )
255 ),
256 'show' => array (
257 ApiBase :: PARAM_ISMULTI => true,
258 ApiBase :: PARAM_TYPE => array (
259 'minor',
260 '!minor',
261 )
262 ),
263 );
264 }
265
266 public function getParamDescription() {
267 return array (
268 'limit' => 'The maximum number of contributions to return.',
269 'start' => 'The start timestamp to return from.',
270 'end' => 'The end timestamp to return to.',
271 'user' => 'The user to retrieve contributions for.',
272 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
273 'dir' => 'The direction to search (older or newer).',
274 'namespace' => 'Only list contributions in these namespaces',
275 'prop' => 'Include additional pieces of information',
276 'show' => 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
277 );
278 }
279
280 public function getDescription() {
281 return 'Get all edits by a user';
282 }
283
284 protected function getExamples() {
285 return array (
286 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
287 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
288 );
289 }
290
291 public function getVersion() {
292 return __CLASS__ . ': $Id$';
293 }
294 }
295