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