* Probably want "$this->mIsGenerator = false;", not "$mIsGenerator = false;"
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2
3
4 /*
5 * Created on Sep 25, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
30 }
31
32 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
33
34 public function __construct($query, $moduleName) {
35 parent :: __construct($query, $moduleName, 'wl');
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function executeGenerator($resultPageSet) {
43 $this->run($resultPageSet);
44 }
45
46 private function run($resultPageSet = null) {
47 global $wgUser;
48
49 if (!$wgUser->isLoggedIn())
50 $this->dieUsage('You must be logged-in to have a watchlist', 'notloggedin');
51
52 $allrev = $start = $end = $namespace = $dir = $limit = $prop = null;
53 extract($this->extractRequestParams());
54
55 $db = $this->getDB();
56
57 $dirNewer = ($dir === 'newer');
58 $before = ($dirNewer ? '<=' : '>=');
59 $after = ($dirNewer ? '>=' : '<=');
60
61 $tables = array (
62 'watchlist',
63 'page',
64 'recentchanges'
65 );
66
67 $options = array (
68 'LIMIT' => $limit +1,
69 'ORDER BY' => 'rc_timestamp' . ($dirNewer ? '' : ' DESC'
70 ));
71
72 $patrol = $timestamp = $user = $comment = false;
73 if (!is_null($prop)) {
74 if (!is_null($resultPageSet))
75 $this->dieUsage('prop parameter may not be used in a generator', 'params');
76
77 $user = (false !== array_search('user', $prop));
78 $comment = (false !== array_search('comment', $prop));
79 $timestamp = (false !== array_search('timestamp', $prop));
80 $patrol = (false !== array_search('patrol', $prop));
81
82 if ($patrol) {
83 global $wgUseRCPatrol, $wgUser;
84 if (!$wgUseRCPatrol || !$wgUser->isAllowed('patrol'))
85 $this->dieUsage('patrol property is not available', 'patrol');
86 }
87 }
88
89 if (is_null($resultPageSet)) {
90 $fields = array (
91 'rc_cur_id AS page_id',
92 'rc_this_oldid AS rev_id',
93 'rc_namespace AS page_namespace',
94 'rc_title AS page_title',
95 'rc_new AS page_is_new',
96 'rc_minor AS rev_minor_edit'
97 );
98 if ($user) {
99 $fields[] = 'rc_user AS rev_user';
100 $fields[] = 'rc_user_text AS rev_user_text';
101 }
102 if ($comment)
103 $fields[] = 'rc_comment AS rev_comment';
104 if ($timestamp)
105 $fields[] = 'rc_timestamp AS rev_timestamp';
106 if ($patrol)
107 $fields[] = 'rc_patrolled';
108 }
109 elseif ($allrev) {
110 $fields = array (
111 'rc_this_oldid AS rev_id',
112 'rc_namespace AS page_namespace',
113 'rc_title AS page_title',
114 'rc_timestamp AS rev_timestamp'
115 );
116 } else {
117 $fields = array (
118 'rc_cur_id AS page_id',
119 'rc_namespace AS page_namespace',
120 'rc_title AS page_title',
121 'rc_timestamp AS rev_timestamp'
122 );
123 }
124
125 $where = array (
126 'wl_namespace = rc_namespace',
127 'wl_title = rc_title',
128 'rc_cur_id = page_id',
129 'wl_user' => $wgUser->getID());
130
131 if (!$allrev)
132 $where[] = 'rc_this_oldid=page_latest';
133 if (isset ($namespace))
134 $where['wl_namespace'] = $namespace;
135
136 if (isset ($start))
137 $where[] = 'rc_timestamp' . $after . $db->addQuotes($start);
138
139 if (isset ($end))
140 $where[] = 'rc_timestamp' . $before . $db->addQuotes($end);
141
142 if (!isset ($start) && !isset ($end))
143 $where[] = "rc_timestamp > ''";
144
145 $this->profileDBIn();
146 $res = $db->select($tables, $fields, $where, __METHOD__, $options);
147 $this->profileDBOut();
148
149 $data = array ();
150 $count = 0;
151 while ($row = $db->fetchObject($res)) {
152 if (++ $count > $limit) {
153 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
154 $this->setContinueEnumParameter('from', $row->rev_timestamp);
155 break;
156 }
157
158 $title = Title :: makeTitle($row->page_namespace, $row->page_title);
159 // skip any pages that user has no rights to read
160 if ($title->userCanRead()) {
161
162 if (is_null($resultPageSet)) {
163 $vals = array ();
164 $vals['pageid'] = intval($row->page_id);
165 $vals['revid'] = intval($row->rev_id);
166 $vals['ns'] = $title->getNamespace();
167 $vals['title'] = $title->getPrefixedText();
168
169 if ($row->page_is_new)
170 $vals['new'] = '';
171 if ($row->rev_minor_edit)
172 $vals['minor'] = '';
173
174 if ($user) {
175 if (!$row->rev_user)
176 $vals['anon'] = '';
177 $vals['user'] = $row->rev_user_text;
178 }
179 if ($comment)
180 $vals['comment'] = $row->rev_comment;
181 if ($timestamp)
182 $vals['timestamp'] = $row->rev_timestamp;
183 if ($patrol && $row->rc_patrolled)
184 $vals['patrolled'] = '';
185
186 $data[] = $vals;
187 }
188 elseif ($allrev) {
189 $data[] = intval($row->rev_id);
190 } else {
191 $data[] = intval($row->page_id);
192 }
193 }
194 }
195 $db->freeResult($res);
196
197 if (is_null($resultPageSet)) {
198 ApiResult :: setIndexedTagName($data, 'item');
199 $this->getResult()->addValue('query', $this->getModuleName(), $data);
200 }
201 elseif ($allrev) {
202 $resultPageSet->populateFromRevisionIDs($data);
203 } else {
204 $resultPageSet->populateFromPageIDs($data);
205 }
206 }
207
208 protected function getAllowedParams() {
209 $namespaces = $this->getQuery()->getValidNamespaces();
210 return array (
211 'allrev' => false,
212 'start' => array (
213 ApiBase :: PARAM_TYPE => 'timestamp'
214 ),
215 'end' => array (
216 ApiBase :: PARAM_TYPE => 'timestamp'
217 ),
218 'namespace' => array (
219 ApiBase :: PARAM_ISMULTI => true,
220 ApiBase :: PARAM_TYPE => $namespaces
221 ),
222 'dir' => array (
223 ApiBase :: PARAM_DFLT => 'older',
224 ApiBase :: PARAM_TYPE => array (
225 'newer',
226 'older'
227 )
228 ),
229 'limit' => array (
230 ApiBase :: PARAM_DFLT => 10,
231 ApiBase :: PARAM_TYPE => 'limit',
232 ApiBase :: PARAM_MIN => 1,
233 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
234 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
235 ),
236 'prop' => array (
237 APIBase :: PARAM_ISMULTI => true,
238 APIBase :: PARAM_TYPE => array (
239 'user',
240 'comment',
241 'timestamp',
242 'patrol'
243 )
244 )
245 );
246 }
247
248 protected function getParamDescription() {
249 return array (
250 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
251 'start' => 'The timestamp to start enumerating from.',
252 'end' => 'The timestamp to end enumerating.',
253 'namespace' => 'Filter changes to only the given namespace(s).',
254 'dir' => 'In which direction to enumerate pages.',
255 'limit' => 'How many total pages to return per request.',
256 'prop' => 'Which additional items to get (non-generator mode only).'
257 );
258 }
259
260 protected function getDescription() {
261 return '';
262 }
263
264 protected function getExamples() {
265 return array (
266 'api.php?action=query&list=watchlist',
267 'api.php?action=query&list=watchlist&wlallrev',
268 'api.php?action=query&generator=watchlist&prop=info',
269 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user'
270 );
271 }
272
273 public function getVersion() {
274 return __CLASS__ . ': $Id$';
275 }
276 }
277 ?>