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