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