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