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