* (bug 20699) API watchlist should list log-events
[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 ) );
102
103 $this->addFieldsIf( 'rc_new', $this->fld_flags );
104 $this->addFieldsIf( 'rc_minor', $this->fld_flags );
105 $this->addFieldsIf( 'rc_bot', $this->fld_flags );
106 $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
107 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
108 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
109 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
110 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
111 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
112 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
113 $this->addFieldsIf( 'rc_logid', $this->fld_loginfo );
114 $this->addFieldsIf( 'rc_log_type', $this->fld_loginfo );
115 $this->addFieldsIf( 'rc_log_action', $this->fld_loginfo );
116 $this->addFieldsIf( 'rc_params', $this->fld_loginfo );
117 } elseif ( $params['allrev'] ) {
118 $this->addFields( 'rc_this_oldid' );
119 } else {
120 $this->addFields( 'rc_cur_id' );
121 }
122
123 $this->addTables( array(
124 'recentchanges',
125 'watchlist',
126 'page',
127 ) );
128
129 $userId = $user->getId();
130 $this->addJoinConds( array( 'watchlist' => array('INNER JOIN',
131 array(
132 'wl_user' => $userId,
133 'wl_namespace=rc_namespace',
134 'wl_title=rc_title'
135 ) ) ) );
136 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN','rc_cur_id=page_id' ) ) );
137
138 $this->addWhere( array(
139 'rc_deleted' => 0,
140 ) );
141
142 $db = $this->getDB();
143
144 $this->addWhereRange( 'rc_timestamp', $params['dir'],
145 $db->timestamp( $params['start'] ),
146 $db->timestamp( $params['end'] ) );
147 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
148 $this->addWhereIf( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG, !$params['allrev'] );
149
150 if ( !is_null( $params['show'] ) ) {
151 $show = array_flip( $params['show'] );
152
153 /* Check for conflicting parameters. */
154 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
155 || ( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
156 || ( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
157 || ( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) )
158 )
159 {
160 $this->dieUsageMsg( 'show' );
161 }
162
163 // Check permissions.
164 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
165 global $wgUser;
166 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
167 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
168 }
169 }
170
171 /* Add additional conditions to query depending upon parameters. */
172 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
173 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
174 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
175 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
176 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
177 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
178 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
179 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
180 }
181
182 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
183 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
184 }
185 if ( !is_null( $params['user'] ) ) {
186 $this->addWhereFld( 'rc_user_text', $params['user'] );
187 }
188 if ( !is_null( $params['excludeuser'] ) ) {
189 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
190 }
191
192 // This is an index optimization for mysql, as done in the Special:Watchlist page
193 $this->addWhereIf( "rc_timestamp > ''", !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' );
194
195 $this->addOption( 'LIMIT', $params['limit'] + 1 );
196
197 $ids = array();
198 $count = 0;
199 $res = $this->select( __METHOD__ );
200
201 foreach ( $res as $row ) {
202 if ( ++ $count > $params['limit'] ) {
203 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
204 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
205 break;
206 }
207
208 if ( is_null( $resultPageSet ) ) {
209 $vals = $this->extractRowInfo( $row );
210 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
211 if ( !$fit ) {
212 $this->setContinueEnumParameter( 'start',
213 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
214 break;
215 }
216 } else {
217 if ( $params['allrev'] ) {
218 $ids[] = intval( $row->rc_this_oldid );
219 } else {
220 $ids[] = intval( $row->rc_cur_id );
221 }
222 }
223 }
224
225 if ( is_null( $resultPageSet ) ) {
226 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
227 } elseif ( $params['allrev'] ) {
228 $resultPageSet->populateFromRevisionIDs( $ids );
229 } else {
230 $resultPageSet->populateFromPageIDs( $ids );
231 }
232 }
233
234 private function extractRowInfo( $row ) {
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 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
243
244 if ( $this->fld_title ) {
245 ApiQueryBase::addTitleInfo( $vals, $title );
246 }
247
248 if ( $this->fld_user || $this->fld_userid ) {
249
250 if ( $this->fld_user ) {
251 $vals['user'] = $row->rc_user_text;
252 }
253
254 if ( $this->fld_userid ) {
255 $vals['user'] = $row->rc_user;
256 }
257
258 if ( !$row->rc_user ) {
259 $vals['anon'] = '';
260 }
261 }
262
263 if ( $this->fld_flags ) {
264 if ( $row->rc_new ) {
265 $vals['new'] = '';
266 }
267 if ( $row->rc_minor ) {
268 $vals['minor'] = '';
269 }
270 if ( $row->rc_bot ) {
271 $vals['bot'] = '';
272 }
273 }
274
275 if ( $this->fld_patrol && isset( $row->rc_patrolled ) ) {
276 $vals['patrolled'] = '';
277 }
278
279 if ( $this->fld_timestamp ) {
280 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
281 }
282
283 if ( $this->fld_sizes ) {
284 $vals['oldlen'] = intval( $row->rc_old_len );
285 $vals['newlen'] = intval( $row->rc_new_len );
286 }
287
288 if ( $this->fld_notificationtimestamp ) {
289 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null )
290 ? ''
291 : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
292 }
293
294 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
295 $vals['comment'] = $row->rc_comment;
296 }
297
298 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
299 global $wgUser;
300 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
301 }
302
303 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
304 $vals['logid'] = intval( $row->rc_logid );
305 $vals['logtype'] = $row->rc_log_type;
306 $vals['logaction'] = $row->rc_log_action;
307 ApiQueryLogEvents::addLogParams(
308 $this->getResult(),
309 $vals, $row->rc_params,
310 $row->rc_log_type, $row->rc_timestamp
311 );
312 }
313
314 return $vals;
315 }
316
317 public function getAllowedParams() {
318 return array(
319 'allrev' => false,
320 'start' => array(
321 ApiBase::PARAM_TYPE => 'timestamp'
322 ),
323 'end' => array(
324 ApiBase::PARAM_TYPE => 'timestamp'
325 ),
326 'namespace' => array (
327 ApiBase::PARAM_ISMULTI => true,
328 ApiBase::PARAM_TYPE => 'namespace'
329 ),
330 'user' => array(
331 ApiBase::PARAM_TYPE => 'user',
332 ),
333 'excludeuser' => array(
334 ApiBase::PARAM_TYPE => 'user',
335 ),
336 'dir' => array(
337 ApiBase::PARAM_DFLT => 'older',
338 ApiBase::PARAM_TYPE => array(
339 'newer',
340 'older'
341 )
342 ),
343 'limit' => array(
344 ApiBase::PARAM_DFLT => 10,
345 ApiBase::PARAM_TYPE => 'limit',
346 ApiBase::PARAM_MIN => 1,
347 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
348 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
349 ),
350 'prop' => array(
351 ApiBase::PARAM_ISMULTI => true,
352 ApiBase::PARAM_DFLT => 'ids|title|flags',
353 ApiBase::PARAM_TYPE => array(
354 'ids',
355 'title',
356 'flags',
357 'user',
358 'userid',
359 'comment',
360 'parsedcomment',
361 'timestamp',
362 'patrol',
363 'sizes',
364 'notificationtimestamp',
365 'loginfo',
366 )
367 ),
368 'show' => array(
369 ApiBase::PARAM_ISMULTI => true,
370 ApiBase::PARAM_TYPE => array(
371 'minor',
372 '!minor',
373 'bot',
374 '!bot',
375 'anon',
376 '!anon',
377 'patrolled',
378 '!patrolled',
379 )
380 ),
381 'owner' => array(
382 ApiBase::PARAM_TYPE => 'user'
383 ),
384 'token' => array(
385 ApiBase::PARAM_TYPE => 'string'
386 )
387 );
388 }
389
390 public function getParamDescription() {
391 $p = $this->getModulePrefix();
392 return array(
393 'allrev' => 'Include multiple revisions of the same page within given timeframe',
394 'start' => 'The timestamp to start enumerating from',
395 'end' => 'The timestamp to end enumerating',
396 'namespace' => 'Filter changes to only the given namespace(s)',
397 'user' => 'Only list changes by this user',
398 'excludeuser' => 'Don\'t list changes by this user',
399 'dir' => $this->getDirectionDescription( $p ),
400 'limit' => 'How many total results to return per request',
401 'prop' => array(
402 'Which additional items to get (non-generator mode only).',
403 ' ids - Adds revision ids and page ids',
404 ' title - Adds title of the page',
405 ' flags - Adds flags for the edit',
406 ' user - Adds the user who made the edit',
407 ' userid - Adds user id of whom made the edit',
408 ' comment - Adds comment of the edit',
409 ' parsedcomment - Adds parsed comment of the edit',
410 ' timestamp - Adds timestamp of the edit',
411 ' patrol - Tags edits that are patrolled',
412 ' size - Adds the old and new lengths of the page',
413 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
414 ' loginfo - Adds log information where appropriate',
415 ),
416 'show' => array(
417 'Show only items that meet this criteria.',
418 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
419 ),
420 'owner' => 'The name of the user whose watchlist you\'d like to access',
421 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
422 );
423 }
424
425 public function getDescription() {
426 return "Get all recent changes to pages in the logged in user's watchlist";
427 }
428
429 public function getPossibleErrors() {
430 return array_merge( parent::getPossibleErrors(), array(
431 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
432 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
433 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
434 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
435 array( 'show' ),
436 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
437 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
438 ) );
439 }
440
441 protected function getExamples() {
442 return array(
443 'api.php?action=query&list=watchlist',
444 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
445 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
446 'api.php?action=query&generator=watchlist&prop=info',
447 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
448 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=d8d562e9725ea1512894cdab28e5ceebc7f20237'
449 );
450 }
451
452 public function getVersion() {
453 return __CLASS__ . ': $Id$';
454 }
455 }