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