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