Change usages of $wgUser->getSkin() in special pages to use $this->getSkin()
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * This query action allows clients to retrieve a list of recently modified pages
34 * that are part of the logged-in user's watchlist.
35 *
36 * @ingroup API
37 */
38 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
39
40 public function __construct( $query, $moduleName ) {
41 parent::__construct( $query, $moduleName, 'wl' );
42 }
43
44 public function execute() {
45 $this->run();
46 }
47
48 public function executeGenerator( $resultPageSet ) {
49 $this->run( $resultPageSet );
50 }
51
52 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
53 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
54 $fld_notificationtimestamp = false, $fld_userid = false, $fld_loginfo = false;
55
56 /**
57 * @param $resultPageSet ApiPageSet
58 * @return void
59 */
60 private function run( $resultPageSet = null ) {
61 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
62
63 $params = $this->extractRequestParams();
64
65 $user = $this->getWatchlistUser( $params );
66
67 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
68 $prop = array_flip( $params['prop'] );
69
70 $this->fld_ids = isset( $prop['ids'] );
71 $this->fld_title = isset( $prop['title'] );
72 $this->fld_flags = isset( $prop['flags'] );
73 $this->fld_user = isset( $prop['user'] );
74 $this->fld_userid = isset( $prop['userid'] );
75 $this->fld_comment = isset( $prop['comment'] );
76 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
77 $this->fld_timestamp = isset( $prop['timestamp'] );
78 $this->fld_sizes = isset( $prop['sizes'] );
79 $this->fld_patrol = isset( $prop['patrol'] );
80 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
81 $this->fld_loginfo = isset( $prop['loginfo'] );
82
83 if ( $this->fld_patrol ) {
84 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
85 $this->dieUsage( 'patrol property is not available', 'patrol' );
86 }
87 }
88 }
89
90 $this->addFields( array(
91 'rc_namespace',
92 'rc_title',
93 'rc_timestamp',
94 'rc_type',
95 ) );
96
97 if ( is_null( $resultPageSet ) ) {
98 $this->addFields( array(
99 'rc_cur_id',
100 'rc_this_oldid',
101 'rc_last_oldid',
102 ) );
103
104 $this->addFieldsIf( array( 'rc_new', 'rc_minor', 'rc_bot' ), $this->fld_flags );
105 $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
106 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
107 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
108 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
109 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes );
110 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
111 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo );
112 } elseif ( $params['allrev'] ) {
113 $this->addFields( 'rc_this_oldid' );
114 } else {
115 $this->addFields( 'rc_cur_id' );
116 }
117
118 $this->addTables( array(
119 'recentchanges',
120 'watchlist',
121 'page',
122 ) );
123
124 $userId = $user->getId();
125 $this->addJoinConds( array( 'watchlist' => array('INNER JOIN',
126 array(
127 'wl_user' => $userId,
128 'wl_namespace=rc_namespace',
129 'wl_title=rc_title'
130 ) ) ) );
131 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN','rc_cur_id=page_id' ) ) );
132
133 $this->addWhere( array(
134 'rc_deleted' => 0,
135 ) );
136
137 $db = $this->getDB();
138
139 $this->addWhereRange( 'rc_timestamp', $params['dir'],
140 $db->timestamp( $params['start'] ),
141 $db->timestamp( $params['end'] ) );
142 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
143 $this->addWhereIf( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG, !$params['allrev'] );
144
145 if ( !is_null( $params['show'] ) ) {
146 $show = array_flip( $params['show'] );
147
148 /* Check for conflicting parameters. */
149 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
150 || ( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
151 || ( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
152 || ( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) )
153 )
154 {
155 $this->dieUsageMsg( 'show' );
156 }
157
158 // Check permissions.
159 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
160 global $wgUser;
161 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
162 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
163 }
164 }
165
166 /* Add additional conditions to query depending upon parameters. */
167 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
168 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
169 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
170 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
171 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
172 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
173 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
174 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
175 }
176
177 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
178 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
179 }
180 if ( !is_null( $params['user'] ) ) {
181 $this->addWhereFld( 'rc_user_text', $params['user'] );
182 }
183 if ( !is_null( $params['excludeuser'] ) ) {
184 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
185 }
186
187 // This is an index optimization for mysql, as done in the Special:Watchlist page
188 $this->addWhereIf( "rc_timestamp > ''", !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' );
189
190 $this->addOption( 'LIMIT', $params['limit'] + 1 );
191
192 $ids = array();
193 $count = 0;
194 $res = $this->select( __METHOD__ );
195
196 foreach ( $res as $row ) {
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 $this->setContinueEnumParameter( 'start',
208 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
209 break;
210 }
211 } else {
212 if ( $params['allrev'] ) {
213 $ids[] = intval( $row->rc_this_oldid );
214 } else {
215 $ids[] = intval( $row->rc_cur_id );
216 }
217 }
218 }
219
220 if ( is_null( $resultPageSet ) ) {
221 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
222 } elseif ( $params['allrev'] ) {
223 $resultPageSet->populateFromRevisionIDs( $ids );
224 } else {
225 $resultPageSet->populateFromPageIDs( $ids );
226 }
227 }
228
229 private function extractRowInfo( $row ) {
230 $vals = array();
231
232 if ( $this->fld_ids ) {
233 $vals['pageid'] = intval( $row->rc_cur_id );
234 $vals['revid'] = intval( $row->rc_this_oldid );
235 $vals['old_revid'] = intval( $row->rc_last_oldid );
236 }
237
238 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
239
240 if ( $this->fld_title ) {
241 ApiQueryBase::addTitleInfo( $vals, $title );
242 }
243
244 if ( $this->fld_user || $this->fld_userid ) {
245
246 if ( $this->fld_user ) {
247 $vals['user'] = $row->rc_user_text;
248 }
249
250 if ( $this->fld_userid ) {
251 $vals['user'] = $row->rc_user;
252 }
253
254 if ( !$row->rc_user ) {
255 $vals['anon'] = '';
256 }
257 }
258
259 if ( $this->fld_flags ) {
260 if ( $row->rc_new ) {
261 $vals['new'] = '';
262 }
263 if ( $row->rc_minor ) {
264 $vals['minor'] = '';
265 }
266 if ( $row->rc_bot ) {
267 $vals['bot'] = '';
268 }
269 }
270
271 if ( $this->fld_patrol && isset( $row->rc_patrolled ) ) {
272 $vals['patrolled'] = '';
273 }
274
275 if ( $this->fld_timestamp ) {
276 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
277 }
278
279 if ( $this->fld_sizes ) {
280 $vals['oldlen'] = intval( $row->rc_old_len );
281 $vals['newlen'] = intval( $row->rc_new_len );
282 }
283
284 if ( $this->fld_notificationtimestamp ) {
285 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null )
286 ? ''
287 : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
288 }
289
290 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
291 $vals['comment'] = $row->rc_comment;
292 }
293
294 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
295 global $wgUser;
296 $vals['parsedcomment'] = $this->getSkin()->formatComment( $row->rc_comment, $title );
297 }
298
299 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
300 $vals['logid'] = intval( $row->rc_logid );
301 $vals['logtype'] = $row->rc_log_type;
302 $vals['logaction'] = $row->rc_log_action;
303 ApiQueryLogEvents::addLogParams(
304 $this->getResult(),
305 $vals,
306 $row->rc_params,
307 $row->rc_log_type,
308 $row->rc_log_action,
309 $row->rc_timestamp
310 );
311 }
312
313 return $vals;
314 }
315
316 public function getAllowedParams() {
317 return array(
318 'allrev' => false,
319 'start' => array(
320 ApiBase::PARAM_TYPE => 'timestamp'
321 ),
322 'end' => array(
323 ApiBase::PARAM_TYPE => 'timestamp'
324 ),
325 'namespace' => array (
326 ApiBase::PARAM_ISMULTI => true,
327 ApiBase::PARAM_TYPE => 'namespace'
328 ),
329 'user' => array(
330 ApiBase::PARAM_TYPE => 'user',
331 ),
332 'excludeuser' => array(
333 ApiBase::PARAM_TYPE => 'user',
334 ),
335 'dir' => array(
336 ApiBase::PARAM_DFLT => 'older',
337 ApiBase::PARAM_TYPE => array(
338 'newer',
339 'older'
340 )
341 ),
342 'limit' => array(
343 ApiBase::PARAM_DFLT => 10,
344 ApiBase::PARAM_TYPE => 'limit',
345 ApiBase::PARAM_MIN => 1,
346 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
347 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
348 ),
349 'prop' => array(
350 ApiBase::PARAM_ISMULTI => true,
351 ApiBase::PARAM_DFLT => 'ids|title|flags',
352 ApiBase::PARAM_TYPE => array(
353 'ids',
354 'title',
355 'flags',
356 'user',
357 'userid',
358 'comment',
359 'parsedcomment',
360 'timestamp',
361 'patrol',
362 'sizes',
363 'notificationtimestamp',
364 'loginfo',
365 )
366 ),
367 'show' => array(
368 ApiBase::PARAM_ISMULTI => true,
369 ApiBase::PARAM_TYPE => array(
370 'minor',
371 '!minor',
372 'bot',
373 '!bot',
374 'anon',
375 '!anon',
376 'patrolled',
377 '!patrolled',
378 )
379 ),
380 'owner' => array(
381 ApiBase::PARAM_TYPE => 'user'
382 ),
383 'token' => array(
384 ApiBase::PARAM_TYPE => 'string'
385 )
386 );
387 }
388
389 public function getParamDescription() {
390 $p = $this->getModulePrefix();
391 return array(
392 'allrev' => 'Include multiple revisions of the same page within given timeframe',
393 'start' => 'The timestamp to start enumerating from',
394 'end' => 'The timestamp to end enumerating',
395 'namespace' => 'Filter changes to only the given namespace(s)',
396 'user' => 'Only list changes by this user',
397 'excludeuser' => 'Don\'t list changes by this user',
398 'dir' => $this->getDirectionDescription( $p ),
399 'limit' => 'How many total results to return per request',
400 'prop' => array(
401 'Which additional items to get (non-generator mode only).',
402 ' ids - Adds revision ids and page ids',
403 ' title - Adds title of the page',
404 ' flags - Adds flags for the edit',
405 ' user - Adds the user who made the edit',
406 ' userid - Adds user id of whom made the edit',
407 ' comment - Adds comment of the edit',
408 ' parsedcomment - Adds parsed comment of the edit',
409 ' timestamp - Adds timestamp of the edit',
410 ' patrol - Tags edits that are patrolled',
411 ' size - Adds the old and new lengths of the page',
412 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
413 ' loginfo - Adds log information where appropriate',
414 ),
415 'show' => array(
416 'Show only items that meet this criteria.',
417 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
418 ),
419 'owner' => 'The name of the user whose watchlist you\'d like to access',
420 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
421 );
422 }
423
424 public function getDescription() {
425 return "Get all recent changes to pages in the logged in user's watchlist";
426 }
427
428 public function getPossibleErrors() {
429 return array_merge( parent::getPossibleErrors(), array(
430 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
431 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
432 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
433 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
434 array( 'show' ),
435 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
436 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
437 ) );
438 }
439
440 protected function getExamples() {
441 return array(
442 'api.php?action=query&list=watchlist',
443 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
444 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
445 'api.php?action=query&generator=watchlist&prop=info',
446 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
447 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=d8d562e9725ea1512894cdab28e5ceebc7f20237'
448 );
449 }
450
451 public function getVersion() {
452 return __CLASS__ . ': $Id$';
453 }
454 }