Followup r61663 per Chads comment. Move $db declaration earlier, use $db->getType()
[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 * @ingroup 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;
56
57 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
58
59 $params = $this->extractRequestParams();
60
61 if ( !is_null( $params['owner'] ) && !is_null( $params['token'] ) ) {
62 $user = User::newFromName( $params['owner'], false );
63 if ( !$user->getId() ) {
64 $this->dieUsage( 'Specified user does not exist', 'bad_wlowner' );
65 }
66 $token = $user->getOption( 'watchlisttoken' );
67 if ( $token == '' || $token != $params['token'] ) {
68 $this->dieUsage( 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences', 'bad_wltoken' );
69 }
70 } elseif ( !$wgUser->isLoggedIn() ) {
71 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
72 } else {
73 $user = $wgUser;
74 }
75
76 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
77
78 $prop = array_flip( $params['prop'] );
79
80 $this->fld_ids = isset( $prop['ids'] );
81 $this->fld_title = isset( $prop['title'] );
82 $this->fld_flags = isset( $prop['flags'] );
83 $this->fld_user = isset( $prop['user'] );
84 $this->fld_comment = isset( $prop['comment'] );
85 $this->fld_timestamp = isset( $prop['timestamp'] );
86 $this->fld_sizes = isset( $prop['sizes'] );
87 $this->fld_patrol = isset( $prop['patrol'] );
88
89 if ( $this->fld_patrol ) {
90 if ( !$user->useRCPatrol() && !$user->useNPPatrol() )
91 $this->dieUsage( 'patrol property is not available', 'patrol' );
92 }
93 }
94
95 $this->addFields( array (
96 'rc_namespace',
97 'rc_title',
98 'rc_timestamp'
99 ) );
100
101 if ( is_null( $resultPageSet ) ) {
102 $this->addFields( array (
103 'rc_cur_id',
104 'rc_this_oldid'
105 ) );
106
107 $this->addFieldsIf( 'rc_new', $this->fld_flags );
108 $this->addFieldsIf( 'rc_minor', $this->fld_flags );
109 $this->addFieldsIf( 'rc_bot', $this->fld_flags );
110 $this->addFieldsIf( 'rc_user', $this->fld_user );
111 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
112 $this->addFieldsIf( 'rc_comment', $this->fld_comment );
113 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
114 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
115 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
116 } elseif ( $params['allrev'] ) {
117 $this->addFields( 'rc_this_oldid' );
118 } else {
119 $this->addFields( 'rc_cur_id' );
120 }
121
122 $this->addTables( array (
123 'watchlist',
124 'page',
125 'recentchanges'
126 ) );
127
128 $userId = $user->getId();
129 $this->addWhere( array (
130 'wl_namespace = rc_namespace',
131 'wl_title = rc_title',
132 'rc_cur_id = page_id',
133 'wl_user' => $userId,
134 'rc_deleted' => 0,
135 ) );
136
137 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
138 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
139 $this->addWhereIf( 'rc_this_oldid=page_latest', !$params['allrev'] );
140
141 if ( !is_null( $params['show'] ) ) {
142 $show = array_flip( $params['show'] );
143
144 /* Check for conflicting parameters. */
145 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
146 || ( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
147 || ( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
148 || ( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) ) ) {
149
150 $this->dieUsage( "Incorrect parameter - mutually exclusive values may not be supplied", 'show' );
151 }
152
153 // Check permissions. FIXME: should this check $user instead of $wgUser?
154 if ( ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
155 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
156
157 /* Add additional conditions to query depending upon parameters. */
158 $this->addWhereIf( 'rc_minor = 0', isset ( $show['!minor'] ) );
159 $this->addWhereIf( 'rc_minor != 0', isset ( $show['minor'] ) );
160 $this->addWhereIf( 'rc_bot = 0', isset ( $show['!bot'] ) );
161 $this->addWhereIf( 'rc_bot != 0', isset ( $show['bot'] ) );
162 $this->addWhereIf( 'rc_user = 0', isset ( $show['anon'] ) );
163 $this->addWhereIf( 'rc_user != 0', isset ( $show['!anon'] ) );
164 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
165 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
166 }
167
168 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) )
169 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
170 if ( !is_null( $params['user'] ) )
171 $this->addWhereFld( 'rc_user_text', $params['user'] );
172 if ( !is_null( $params['excludeuser'] ) )
173 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
174
175 $db = $this->getDB();
176
177 // This is an index optimization for mysql, as done in the Special:Watchlist page
178 $this->addWhereIf( "rc_timestamp > ''", !isset ( $params['start'] ) && !isset ( $params['end'] ) && $db->getType() == 'mysql' );
179
180 $this->addOption( 'LIMIT', $params['limit'] + 1 );
181
182 $ids = array ();
183 $count = 0;
184 $res = $this->select( __METHOD__ );
185
186 while ( $row = $db->fetchObject( $res ) ) {
187 if ( ++ $count > $params['limit'] ) {
188 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
189 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
190 break;
191 }
192
193 if ( is_null( $resultPageSet ) ) {
194 $vals = $this->extractRowInfo( $row );
195 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
196 if ( !$fit )
197 {
198 $this->setContinueEnumParameter( 'start',
199 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
200 break;
201 }
202 } else {
203 if ( $params['allrev'] ) {
204 $ids[] = intval( $row->rc_this_oldid );
205 } else {
206 $ids[] = intval( $row->rc_cur_id );
207 }
208 }
209 }
210
211 $db->freeResult( $res );
212
213 if ( is_null( $resultPageSet ) ) {
214 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
215 }
216 elseif ( $params['allrev'] ) {
217 $resultPageSet->populateFromRevisionIDs( $ids );
218 } else {
219 $resultPageSet->populateFromPageIDs( $ids );
220 }
221 }
222
223 private function extractRowInfo( $row ) {
224
225 $vals = array ();
226
227 if ( $this->fld_ids ) {
228 $vals['pageid'] = intval( $row->rc_cur_id );
229 $vals['revid'] = intval( $row->rc_this_oldid );
230 }
231
232 if ( $this->fld_title )
233 ApiQueryBase :: addTitleInfo( $vals, Title :: makeTitle( $row->rc_namespace, $row->rc_title ) );
234
235 if ( $this->fld_user ) {
236 $vals['user'] = $row->rc_user_text;
237 if ( !$row->rc_user )
238 $vals['anon'] = '';
239 }
240
241 if ( $this->fld_flags ) {
242 if ( $row->rc_new )
243 $vals['new'] = '';
244 if ( $row->rc_minor )
245 $vals['minor'] = '';
246 if ( $row->rc_bot )
247 $vals['bot'] = '';
248 }
249
250 if ( $this->fld_patrol && isset( $row->rc_patrolled ) )
251 $vals['patrolled'] = '';
252
253 if ( $this->fld_timestamp )
254 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
255
256 if ( $this->fld_sizes ) {
257 $vals['oldlen'] = intval( $row->rc_old_len );
258 $vals['newlen'] = intval( $row->rc_new_len );
259 }
260
261 if ( $this->fld_comment && isset( $row->rc_comment ) )
262 $vals['comment'] = $row->rc_comment;
263
264 return $vals;
265 }
266
267 public function getAllowedParams() {
268 return array (
269 'allrev' => false,
270 'start' => array (
271 ApiBase :: PARAM_TYPE => 'timestamp'
272 ),
273 'end' => array (
274 ApiBase :: PARAM_TYPE => 'timestamp'
275 ),
276 'namespace' => array (
277 ApiBase :: PARAM_ISMULTI => true,
278 ApiBase :: PARAM_TYPE => 'namespace'
279 ),
280 'user' => array(
281 ApiBase :: PARAM_TYPE => 'user',
282 ),
283 'excludeuser' => array(
284 ApiBase :: PARAM_TYPE => 'user',
285 ),
286 'dir' => array (
287 ApiBase :: PARAM_DFLT => 'older',
288 ApiBase :: PARAM_TYPE => array (
289 'newer',
290 'older'
291 )
292 ),
293 'limit' => array (
294 ApiBase :: PARAM_DFLT => 10,
295 ApiBase :: PARAM_TYPE => 'limit',
296 ApiBase :: PARAM_MIN => 1,
297 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
298 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
299 ),
300 'prop' => array (
301 APIBase :: PARAM_ISMULTI => true,
302 APIBase :: PARAM_DFLT => 'ids|title|flags',
303 APIBase :: PARAM_TYPE => array (
304 'ids',
305 'title',
306 'flags',
307 'user',
308 'comment',
309 'timestamp',
310 'patrol',
311 'sizes',
312 )
313 ),
314 'show' => array (
315 ApiBase :: PARAM_ISMULTI => true,
316 ApiBase :: PARAM_TYPE => array (
317 'minor',
318 '!minor',
319 'bot',
320 '!bot',
321 'anon',
322 '!anon',
323 'patrolled',
324 '!patrolled',
325 )
326 ),
327 'owner' => array (
328 ApiBase :: PARAM_TYPE => 'user'
329 ),
330 'token' => array (
331 ApiBase :: PARAM_TYPE => 'string'
332 )
333 );
334 }
335
336 public function getParamDescription() {
337 return array (
338 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
339 'start' => 'The timestamp to start enumerating from.',
340 'end' => 'The timestamp to end enumerating.',
341 'namespace' => 'Filter changes to only the given namespace(s).',
342 'user' => 'Only list changes by this user',
343 'excludeuser' => 'Don\'t list changes by this user',
344 'dir' => 'In which direction to enumerate pages.',
345 'limit' => 'How many total results to return per request.',
346 'prop' => 'Which additional items to get (non-generator mode only).',
347 'show' => array (
348 'Show only items that meet this criteria.',
349 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
350 ),
351 'owner' => "The name of the user whose watchlist you'd like to access",
352 'token' => "Give a security token (settable in preferences) to allow access to another user's watchlist"
353 );
354 }
355
356 public function getDescription() {
357 return "Get all recent changes to pages in the logged in user's watchlist";
358 }
359
360 protected function getExamples() {
361 return array (
362 'api.php?action=query&list=watchlist',
363 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
364 'api.php?action=query&list=watchlist&wlallrev&wlprop=ids|title|timestamp|user|comment',
365 'api.php?action=query&generator=watchlist&prop=info',
366 'api.php?action=query&generator=watchlist&gwlallrev&prop=revisions&rvprop=timestamp|user',
367 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=d8d562e9725ea1512894cdab28e5ceebc7f20237'
368 );
369 }
370
371 public function getVersion() {
372 return __CLASS__ . ': $Id$';
373 }
374 }