(bug 19914) ApiUpload needs to use write mode.
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2
3 /*
4 * Created on Sep 25, 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 allows clients to retrieve a list of recently modified pages
33 * that are part of the logged-in user's watchlist.
34 *
35 * @ingroup API
36 */
37 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
38
39 public function __construct($query, $moduleName) {
40 parent :: __construct($query, $moduleName, 'wl');
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator($resultPageSet) {
48 $this->run($resultPageSet);
49 }
50
51 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
52 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_sizes = false;
53
54 private function run($resultPageSet = null) {
55 global $wgUser, $wgDBtype;
56
57 $this->selectNamedDB('watchlist', DB_SLAVE, 'watchlist');
58
59 $params = $this->extractRequestParams();
60
61 if (!is_null($params['user']) && !is_null($params['token'])) {
62 $user = User::newFromName($params['user']);
63 $token = $user->getOption('watchlisttoken');
64 if ($token == '' || $token != $params['token']) {
65 $this->dieUsage('Incorrect watchlist token provided', 'bad_wltoken');
66 }
67 } elseif (!$wgUser->isLoggedIn()) {
68 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
69 } else {
70 $user = $wgUser;
71 }
72
73 if (!is_null($params['prop']) && is_null($resultPageSet)) {
74
75 $prop = array_flip($params['prop']);
76
77 $this->fld_ids = isset($prop['ids']);
78 $this->fld_title = isset($prop['title']);
79 $this->fld_flags = isset($prop['flags']);
80 $this->fld_user = isset($prop['user']);
81 $this->fld_comment = isset($prop['comment']);
82 $this->fld_timestamp = isset($prop['timestamp']);
83 $this->fld_sizes = isset($prop['sizes']);
84 $this->fld_patrol = isset($prop['patrol']);
85
86 if ($this->fld_patrol) {
87 global $wgUser;
88 if (!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
89 $this->dieUsage('patrol property is not available', 'patrol');
90 }
91 }
92
93 if (is_null($resultPageSet)) {
94 $this->addFields(array (
95 'rc_cur_id',
96 'rc_this_oldid',
97 'rc_namespace',
98 'rc_title',
99 'rc_timestamp'
100 ));
101
102 $this->addFieldsIf('rc_new', $this->fld_flags);
103 $this->addFieldsIf('rc_minor', $this->fld_flags);
104 $this->addFieldsIf('rc_bot', $this->fld_flags);
105 $this->addFieldsIf('rc_user', $this->fld_user);
106 $this->addFieldsIf('rc_user_text', $this->fld_user);
107 $this->addFieldsIf('rc_comment', $this->fld_comment);
108 $this->addFieldsIf('rc_patrolled', $this->fld_patrol);
109 $this->addFieldsIf('rc_old_len', $this->fld_sizes);
110 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
111 }
112 elseif ($params['allrev']) {
113 $this->addFields(array (
114 'rc_this_oldid',
115 'rc_namespace',
116 'rc_title',
117 'rc_timestamp'
118 ));
119 } else {
120 $this->addFields(array (
121 'rc_cur_id',
122 'rc_namespace',
123 'rc_title',
124 'rc_timestamp'
125 ));
126 }
127
128 $this->addTables(array (
129 'watchlist',
130 'page',
131 'recentchanges'
132 ));
133
134 $userId = $user->getId();
135 $this->addWhere(array (
136 'wl_namespace = rc_namespace',
137 'wl_title = rc_title',
138 'rc_cur_id = page_id',
139 'wl_user' => $userId,
140 'rc_deleted' => 0,
141 ));
142
143 $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
144 $this->addWhereFld('wl_namespace', $params['namespace']);
145 $this->addWhereIf('rc_this_oldid=page_latest', !$params['allrev']);
146
147 if (!is_null($params['show'])) {
148 $show = array_flip($params['show']);
149
150 /* Check for conflicting parameters. */
151 if ((isset ($show['minor']) && isset ($show['!minor']))
152 || (isset ($show['bot']) && isset ($show['!bot']))
153 || (isset ($show['anon']) && isset ($show['!anon']))
154 || (isset ($show['patrolled']) && isset ($show['!patrolled']))) {
155
156 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
157 }
158
159 // Check permissions. FIXME: should this check $user instead of
160 // $wgUser?
161 global $wgUser;
162 if((isset($show['patrolled']) || isset($show['!patrolled'])) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
163 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
164
165 /* Add additional conditions to query depending upon parameters. */
166 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
167 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
168 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
169 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
170 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
171 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
172 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
173 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
174 }
175
176 # Ignore extra user conditions if we're using token mode, since the
177 # user was already manually specified.
178 if(is_null($params['user']) || is_null($params['token'])) {
179 if(!is_null($params['user']) && !is_null($params['excludeuser']))
180 $this->dieUsage('user and excludeuser cannot be used together', 'user-excludeuser');
181 if(!is_null($params['user']))
182 $this->addWhereFld('rc_user_text', $params['user']);
183 if(!is_null($params['excludeuser']))
184 $this->addWhere('rc_user_text != ' . $this->getDB()->addQuotes($params['excludeuser']));
185 }
186
187
188 # This is an index optimization for mysql, as done in the Special:Watchlist page
189 $this->addWhereIf("rc_timestamp > ''", !isset ($params['start']) && !isset ($params['end']) && $wgDBtype == 'mysql');
190
191 $this->addOption('LIMIT', $params['limit'] +1);
192
193 $ids = array ();
194 $count = 0;
195 $res = $this->select(__METHOD__);
196
197 $db = $this->getDB();
198 while ($row = $db->fetchObject($res)) {
199 if (++ $count > $params['limit']) {
200 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
201 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
202 break;
203 }
204
205 if (is_null($resultPageSet)) {
206 $vals = $this->extractRowInfo($row);
207 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
208 if(!$fit)
209 {
210 $this->setContinueEnumParameter('start',
211 wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
212 break;
213 }
214 } else {
215 if ($params['allrev']) {
216 $ids[] = intval($row->rc_this_oldid);
217 } else {
218 $ids[] = intval($row->rc_cur_id);
219 }
220 }
221 }
222
223 $db->freeResult($res);
224
225 if (is_null($resultPageSet)) {
226 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
227 }
228 elseif ($params['allrev']) {
229 $resultPageSet->populateFromRevisionIDs($ids);
230 } else {
231 $resultPageSet->populateFromPageIDs($ids);
232 }
233 }
234
235 private function extractRowInfo($row) {
236
237 $vals = array ();
238
239 if ($this->fld_ids) {
240 $vals['pageid'] = intval($row->rc_cur_id);
241 $vals['revid'] = intval($row->rc_this_oldid);
242 }
243
244 if ($this->fld_title)
245 ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->rc_namespace, $row->rc_title));
246
247 if ($this->fld_user) {
248 $vals['user'] = $row->rc_user_text;
249 if (!$row->rc_user)
250 $vals['anon'] = '';
251 }
252
253 if ($this->fld_flags) {
254 if ($row->rc_new)
255 $vals['new'] = '';
256 if ($row->rc_minor)
257 $vals['minor'] = '';
258 if ($row->rc_bot)
259 $vals['bot'] = '';
260 }
261
262 if ($this->fld_patrol && isset($row->rc_patrolled))
263 $vals['patrolled'] = '';
264
265 if ($this->fld_timestamp)
266 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
267
268 if ($this->fld_sizes) {
269 $vals['oldlen'] = intval($row->rc_old_len);
270 $vals['newlen'] = intval($row->rc_new_len);
271 }
272
273 if ($this->fld_comment && isset( $row->rc_comment ))
274 $vals['comment'] = $row->rc_comment;
275
276 return $vals;
277 }
278
279 public function getAllowedParams() {
280 return array (
281 'allrev' => false,
282 'start' => array (
283 ApiBase :: PARAM_TYPE => 'timestamp'
284 ),
285 'end' => array (
286 ApiBase :: PARAM_TYPE => 'timestamp'
287 ),
288 'namespace' => array (
289 ApiBase :: PARAM_ISMULTI => true,
290 ApiBase :: PARAM_TYPE => 'namespace'
291 ),
292 'user' => array(
293 ApiBase :: PARAM_TYPE => 'user',
294 ),
295 'excludeuser' => array(
296 ApiBase :: PARAM_TYPE => 'user',
297 ),
298 'dir' => array (
299 ApiBase :: PARAM_DFLT => 'older',
300 ApiBase :: PARAM_TYPE => array (
301 'newer',
302 'older'
303 )
304 ),
305 'limit' => array (
306 ApiBase :: PARAM_DFLT => 10,
307 ApiBase :: PARAM_TYPE => 'limit',
308 ApiBase :: PARAM_MIN => 1,
309 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
310 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
311 ),
312 'prop' => array (
313 APIBase :: PARAM_ISMULTI => true,
314 APIBase :: PARAM_DFLT => 'ids|title|flags',
315 APIBase :: PARAM_TYPE => array (
316 'ids',
317 'title',
318 'flags',
319 'user',
320 'comment',
321 'timestamp',
322 'patrol',
323 'sizes',
324 )
325 ),
326 'show' => array (
327 ApiBase :: PARAM_ISMULTI => true,
328 ApiBase :: PARAM_TYPE => array (
329 'minor',
330 '!minor',
331 'bot',
332 '!bot',
333 'anon',
334 '!anon',
335 'patrolled',
336 '!patrolled',
337 )
338 ),
339 'token' => array (
340 ApiBase :: PARAM_TYPE => 'string'
341 )
342 );
343 }
344
345 public function getParamDescription() {
346 return array (
347 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
348 'start' => 'The timestamp to start enumerating from.',
349 'end' => 'The timestamp to end enumerating.',
350 'namespace' => 'Filter changes to only the given namespace(s).',
351 'user' => 'Only list changes by this user',
352 'excludeuser' => 'Don\'t list changes by this user',
353 'dir' => 'In which direction to enumerate pages.',
354 'limit' => 'How many total results to return per request.',
355 'prop' => 'Which additional items to get (non-generator mode only).',
356 'show' => array (
357 'Show only items that meet this criteria.',
358 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
359 ),
360 'token' => "Give a security token (settable in preferences) to allow access to another user's watchlist"
361 );
362 }
363
364 public function getDescription() {
365 return "Get all recent changes to pages in the logged in user's watchlist";
366 }
367
368 protected function getExamples() {
369 return array (
370 'api.php?action=query&list=watchlist',
371 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
372 'api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment',
373 'api.php?action=query&generator=watchlist&prop=info',
374 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
375 );
376 }
377
378 public function getVersion() {
379 return __CLASS__ . ': $Id$';
380 }
381 }