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