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