API: (bug 16858) Revamped list=deletedrevs to make listing deleted contributions...
[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 if (!$wgUser->isLoggedIn())
60 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
61
62 $params = $this->extractRequestParams();
63
64 if (!is_null($params['prop']) && is_null($resultPageSet)) {
65
66 $prop = array_flip($params['prop']);
67
68 $this->fld_ids = isset($prop['ids']);
69 $this->fld_title = isset($prop['title']);
70 $this->fld_flags = isset($prop['flags']);
71 $this->fld_user = isset($prop['user']);
72 $this->fld_comment = isset($prop['comment']);
73 $this->fld_timestamp = isset($prop['timestamp']);
74 $this->fld_sizes = isset($prop['sizes']);
75 $this->fld_patrol = isset($prop['patrol']);
76
77 if ($this->fld_patrol) {
78 global $wgUser;
79 if (!$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
80 $this->dieUsage('patrol property is not available', 'patrol');
81 }
82 }
83
84 if (is_null($resultPageSet)) {
85 $this->addFields(array (
86 'rc_cur_id',
87 'rc_this_oldid',
88 'rc_namespace',
89 'rc_title',
90 'rc_timestamp'
91 ));
92
93 $this->addFieldsIf('rc_new', $this->fld_flags);
94 $this->addFieldsIf('rc_minor', $this->fld_flags);
95 $this->addFieldsIf('rc_user', $this->fld_user);
96 $this->addFieldsIf('rc_user_text', $this->fld_user);
97 $this->addFieldsIf('rc_comment', $this->fld_comment);
98 $this->addFieldsIf('rc_patrolled', $this->fld_patrol);
99 $this->addFieldsIf('rc_old_len', $this->fld_sizes);
100 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
101 }
102 elseif ($params['allrev']) {
103 $this->addFields(array (
104 'rc_this_oldid',
105 'rc_namespace',
106 'rc_title',
107 'rc_timestamp'
108 ));
109 } else {
110 $this->addFields(array (
111 'rc_cur_id',
112 'rc_namespace',
113 'rc_title',
114 'rc_timestamp'
115 ));
116 }
117
118 $this->addTables(array (
119 'watchlist',
120 'page',
121 'recentchanges'
122 ));
123
124 $userId = $wgUser->getId();
125 $this->addWhere(array (
126 'wl_namespace = rc_namespace',
127 'wl_title = rc_title',
128 'rc_cur_id = page_id',
129 'wl_user' => $userId,
130 'rc_deleted' => 0,
131 ));
132
133 $this->addWhereRange('rc_timestamp', $params['dir'], $params['start'], $params['end']);
134 $this->addWhereFld('wl_namespace', $params['namespace']);
135 $this->addWhereIf('rc_this_oldid=page_latest', !$params['allrev']);
136
137 if (!is_null($params['show'])) {
138 $show = array_flip($params['show']);
139
140 /* Check for conflicting parameters. */
141 if ((isset ($show['minor']) && isset ($show['!minor']))
142 || (isset ($show['bot']) && isset ($show['!bot']))
143 || (isset ($show['anon']) && isset ($show['!anon']))
144 || (isset ($show['patrolled']) && isset ($show['!patrolled']))) {
145
146 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
147 }
148
149 // Check permissions
150 global $wgUser;
151 if((isset($show['patrolled']) || isset($show['!patrolled'])) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol())
152 $this->dieUsage("You need the patrol right to request the patrolled flag", 'permissiondenied');
153
154 /* Add additional conditions to query depending upon parameters. */
155 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
156 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
157 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
158 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
159 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
160 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
161 $this->addWhereIf('rc_patrolled = 0', isset($show['!patrolled']));
162 $this->addWhereIf('rc_patrolled != 0', isset($show['patrolled']));
163 }
164
165
166 # This is an index optimization for mysql, as done in the Special:Watchlist page
167 $this->addWhereIf("rc_timestamp > ''", !isset ($params['start']) && !isset ($params['end']) && $wgDBtype == 'mysql');
168
169 $this->addOption('LIMIT', $params['limit'] +1);
170
171 $data = array ();
172 $count = 0;
173 $res = $this->select(__METHOD__);
174
175 $db = $this->getDB();
176 while ($row = $db->fetchObject($res)) {
177 if (++ $count > $params['limit']) {
178 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
179 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
180 break;
181 }
182
183 if (is_null($resultPageSet)) {
184 $vals = $this->extractRowInfo($row);
185 if ($vals)
186 $data[] = $vals;
187 } else {
188 if ($params['allrev']) {
189 $data[] = intval($row->rc_this_oldid);
190 } else {
191 $data[] = intval($row->rc_cur_id);
192 }
193 }
194 }
195
196 $db->freeResult($res);
197
198 if (is_null($resultPageSet)) {
199 $this->getResult()->setIndexedTagName($data, 'item');
200 $this->getResult()->addValue('query', $this->getModuleName(), $data);
201 }
202 elseif ($params['allrev']) {
203 $resultPageSet->populateFromRevisionIDs($data);
204 } else {
205 $resultPageSet->populateFromPageIDs($data);
206 }
207 }
208
209 private function extractRowInfo($row) {
210
211 $vals = array ();
212
213 if ($this->fld_ids) {
214 $vals['pageid'] = intval($row->rc_cur_id);
215 $vals['revid'] = intval($row->rc_this_oldid);
216 }
217
218 if ($this->fld_title)
219 ApiQueryBase :: addTitleInfo($vals, Title :: makeTitle($row->rc_namespace, $row->rc_title));
220
221 if ($this->fld_user) {
222 $vals['user'] = $row->rc_user_text;
223 if (!$row->rc_user)
224 $vals['anon'] = '';
225 }
226
227 if ($this->fld_flags) {
228 if ($row->rc_new)
229 $vals['new'] = '';
230 if ($row->rc_minor)
231 $vals['minor'] = '';
232 }
233
234 if ($this->fld_patrol && isset($row->rc_patrolled))
235 $vals['patrolled'] = '';
236
237 if ($this->fld_timestamp)
238 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
239
240 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
241
242 if ($this->fld_sizes) {
243 $vals['oldlen'] = intval($row->rc_old_len);
244 $vals['newlen'] = intval($row->rc_new_len);
245 }
246
247 if ($this->fld_comment && isset( $row->rc_comment ))
248 $vals['comment'] = $row->rc_comment;
249
250 return $vals;
251 }
252
253 public function getAllowedParams() {
254 return array (
255 'allrev' => false,
256 'start' => array (
257 ApiBase :: PARAM_TYPE => 'timestamp'
258 ),
259 'end' => array (
260 ApiBase :: PARAM_TYPE => 'timestamp'
261 ),
262 'namespace' => array (
263 ApiBase :: PARAM_ISMULTI => true,
264 ApiBase :: PARAM_TYPE => 'namespace'
265 ),
266 'dir' => array (
267 ApiBase :: PARAM_DFLT => 'older',
268 ApiBase :: PARAM_TYPE => array (
269 'newer',
270 'older'
271 )
272 ),
273 'limit' => array (
274 ApiBase :: PARAM_DFLT => 10,
275 ApiBase :: PARAM_TYPE => 'limit',
276 ApiBase :: PARAM_MIN => 1,
277 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
278 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
279 ),
280 'prop' => array (
281 APIBase :: PARAM_ISMULTI => true,
282 APIBase :: PARAM_DFLT => 'ids|title|flags',
283 APIBase :: PARAM_TYPE => array (
284 'ids',
285 'title',
286 'flags',
287 'user',
288 'comment',
289 'timestamp',
290 'patrol',
291 'sizes',
292 )
293 ),
294 'show' => array (
295 ApiBase :: PARAM_ISMULTI => true,
296 ApiBase :: PARAM_TYPE => array (
297 'minor',
298 '!minor',
299 'bot',
300 '!bot',
301 'anon',
302 '!anon',
303 'patrolled',
304 '!patrolled',
305 )
306 )
307 );
308 }
309
310 public function getParamDescription() {
311 return array (
312 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
313 'start' => 'The timestamp to start enumerating from.',
314 'end' => 'The timestamp to end enumerating.',
315 'namespace' => 'Filter changes to only the given namespace(s).',
316 'dir' => 'In which direction to enumerate pages.',
317 'limit' => 'How many total results to return per request.',
318 'prop' => 'Which additional items to get (non-generator mode only).',
319 'show' => array (
320 'Show only items that meet this criteria.',
321 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
322 )
323 );
324 }
325
326 public function getDescription() {
327 return "Get all recent changes to pages in the logged in user's watchlist";
328 }
329
330 protected function getExamples() {
331 return array (
332 'api.php?action=query&list=watchlist',
333 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
334 'api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment',
335 'api.php?action=query&generator=watchlist&prop=info',
336 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
337 );
338 }
339
340 public function getVersion() {
341 return __CLASS__ . ': $Id$';
342 }
343 }