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