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