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