API: Fixed continuation bug in list=categorymembers. Thanks to Roberto Leandrini.
[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 * @addtogroup 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;
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 = 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_patrol = isset($prop['patrol']);
76
77 if ($this->fld_patrol) {
78 global $wgUseRCPatrol, $wgUser;
79 if (!$wgUseRCPatrol || !$wgUser->isAllowed('patrol'))
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 }
100 elseif ($allrev) {
101 $this->addFields(array (
102 'rc_this_oldid',
103 'rc_namespace',
104 'rc_title',
105 'rc_timestamp'
106 ));
107 } else {
108 $this->addFields(array (
109 'rc_cur_id',
110 'rc_namespace',
111 'rc_title',
112 'rc_timestamp'
113 ));
114 }
115
116 $this->addTables(array (
117 'watchlist',
118 'page',
119 'recentchanges'
120 ));
121
122 $userId = $wgUser->getID();
123 $this->addWhere(array (
124 'wl_namespace = rc_namespace',
125 'wl_title = rc_title',
126 'rc_cur_id = page_id',
127 'wl_user' => $userId,
128 'rc_deleted' => 0,
129 ));
130
131 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
132 $this->addWhereFld('wl_namespace', $namespace);
133 $this->addWhereIf('rc_this_oldid=page_latest', !$allrev);
134
135 # This is a index optimization for mysql, as done in the Special:Watchlist page
136 $this->addWhereIf("rc_timestamp > ''", !isset ($start) && !isset ($end) && $wgDBtype == 'mysql');
137
138 $this->addOption('LIMIT', $limit +1);
139
140 $data = array ();
141 $count = 0;
142 $res = $this->select(__METHOD__);
143
144 $db = $this->getDB();
145 while ($row = $db->fetchObject($res)) {
146 if (++ $count > $limit) {
147 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
148 $this->setContinueEnumParameter('start', $row->rc_timestamp);
149 break;
150 }
151
152 if (is_null($resultPageSet)) {
153 $vals = $this->extractRowInfo($row);
154 if ($vals)
155 $data[] = $vals;
156 } else {
157 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
158 // skip any pages that user has no rights to read
159 if ($title->userCanRead()) {
160 if ($allrev) {
161 $data[] = intval($row->rc_this_oldid);
162 } else {
163 $data[] = intval($row->rc_cur_id);
164 }
165 }
166 }
167 }
168
169 $db->freeResult($res);
170
171 if (is_null($resultPageSet)) {
172 $this->getResult()->setIndexedTagName($data, 'item');
173 $this->getResult()->addValue('query', $this->getModuleName(), $data);
174 }
175 elseif ($allrev) {
176 $resultPageSet->populateFromRevisionIDs($data);
177 } else {
178 $resultPageSet->populateFromPageIDs($data);
179 }
180 }
181
182 private function extractRowInfo($row) {
183
184 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
185 if (!$title->userCanRead())
186 return false;
187
188 $vals = array ();
189
190 if ($this->fld_ids) {
191 $vals['pageid'] = intval($row->rc_cur_id);
192 $vals['revid'] = intval($row->rc_this_oldid);
193 }
194
195 if ($this->fld_title)
196 ApiQueryBase :: addTitleInfo($vals, $title);
197
198 if ($this->fld_user) {
199 $vals['user'] = $row->rc_user_text;
200 if (!$row->rc_user)
201 $vals['anon'] = '';
202 }
203
204 if ($this->fld_flags) {
205 if ($row->rc_new)
206 $vals['new'] = '';
207 if ($row->rc_minor)
208 $vals['minor'] = '';
209 }
210
211 if ($this->fld_patrol && isset($row->rc_patrolled))
212 $vals['patrolled'] = '';
213
214 if ($this->fld_timestamp)
215 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
216
217 if ($this->fld_comment && !empty ($row->rc_comment))
218 $vals['comment'] = $row->rc_comment;
219
220 return $vals;
221 }
222
223 protected function getAllowedParams() {
224 return array (
225 'allrev' => false,
226 'start' => array (
227 ApiBase :: PARAM_TYPE => 'timestamp'
228 ),
229 'end' => array (
230 ApiBase :: PARAM_TYPE => 'timestamp'
231 ),
232 'namespace' => array (
233 ApiBase :: PARAM_ISMULTI => true,
234 ApiBase :: PARAM_TYPE => 'namespace'
235 ),
236 'dir' => array (
237 ApiBase :: PARAM_DFLT => 'older',
238 ApiBase :: PARAM_TYPE => array (
239 'newer',
240 'older'
241 )
242 ),
243 'limit' => array (
244 ApiBase :: PARAM_DFLT => 10,
245 ApiBase :: PARAM_TYPE => 'limit',
246 ApiBase :: PARAM_MIN => 1,
247 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
248 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
249 ),
250 'prop' => array (
251 APIBase :: PARAM_ISMULTI => true,
252 APIBase :: PARAM_DFLT => 'ids|title|flags',
253 APIBase :: PARAM_TYPE => array (
254 'ids',
255 'title',
256 'flags',
257 'user',
258 'comment',
259 'timestamp',
260 'patrol'
261 )
262 )
263 );
264 }
265
266 protected function getParamDescription() {
267 return array (
268 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
269 'start' => 'The timestamp to start enumerating from.',
270 'end' => 'The timestamp to end enumerating.',
271 'namespace' => 'Filter changes to only the given namespace(s).',
272 'dir' => 'In which direction to enumerate pages.',
273 'limit' => 'How many total pages to return per request.',
274 'prop' => 'Which additional items to get (non-generator mode only).'
275 );
276 }
277
278 protected function getDescription() {
279 return '';
280 }
281
282 protected function getExamples() {
283 return array (
284 'api.php?action=query&list=watchlist',
285 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
286 'api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment',
287 'api.php?action=query&generator=watchlist&prop=info',
288 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
289 );
290 }
291
292 public function getVersion() {
293 return __CLASS__ . ': $Id$';
294 }
295 }
296