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