Fix regression in unwatch links sent out by enotif. {{FULLPAGENAMEE}} happens to...
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2
3 /**
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * This query action allows clients to retrieve a list of recently modified pages
33 * that are part of the logged-in user's watchlist.
34 *
35 * @ingroup API
36 */
37 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'wl' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
52 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
53 $fld_notificationtimestamp = false;
54
55 private function run( $resultPageSet = null ) {
56 global $wgUser;
57
58 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
59
60 $params = $this->extractRequestParams();
61
62 if ( !is_null( $params['owner'] ) && !is_null( $params['token'] ) ) {
63 $user = User::newFromName( $params['owner'], false );
64 if ( !$user->getId() ) {
65 $this->dieUsage( 'Specified user does not exist', 'bad_wlowner' );
66 }
67 $token = $user->getOption( 'watchlisttoken' );
68 if ( $token == '' || $token != $params['token'] ) {
69 $this->dieUsage( 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences', 'bad_wltoken' );
70 }
71 } elseif ( !$wgUser->isLoggedIn() ) {
72 $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
73 } else {
74 $user = $wgUser;
75 }
76
77 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
78 $prop = array_flip( $params['prop'] );
79
80 $this->fld_ids = isset( $prop['ids'] );
81 $this->fld_title = isset( $prop['title'] );
82 $this->fld_flags = isset( $prop['flags'] );
83 $this->fld_user = isset( $prop['user'] );
84 $this->fld_comment = isset( $prop['comment'] );
85 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
86 $this->fld_timestamp = isset( $prop['timestamp'] );
87 $this->fld_sizes = isset( $prop['sizes'] );
88 $this->fld_patrol = isset( $prop['patrol'] );
89 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
90
91 if ( $this->fld_patrol ) {
92 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
93 $this->dieUsage( 'patrol property is not available', 'patrol' );
94 }
95 }
96 }
97
98 $this->addFields( array(
99 'rc_namespace',
100 'rc_title',
101 'rc_timestamp'
102 ) );
103
104 if ( is_null( $resultPageSet ) ) {
105 $this->addFields( array(
106 'rc_cur_id',
107 'rc_this_oldid'
108 ) );
109
110 $this->addFieldsIf( 'rc_new', $this->fld_flags );
111 $this->addFieldsIf( 'rc_minor', $this->fld_flags );
112 $this->addFieldsIf( 'rc_bot', $this->fld_flags );
113 $this->addFieldsIf( 'rc_user', $this->fld_user );
114 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
115 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
116 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
117 $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
118 $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
119 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
120 } elseif ( $params['allrev'] ) {
121 $this->addFields( 'rc_this_oldid' );
122 } else {
123 $this->addFields( 'rc_cur_id' );
124 }
125
126 $this->addTables( array(
127 'watchlist',
128 'page',
129 'recentchanges'
130 ) );
131
132 $userId = $user->getId();
133 $this->addWhere( array(
134 'wl_namespace = rc_namespace',
135 'wl_title = rc_title',
136 'rc_cur_id = page_id',
137 'wl_user' => $userId,
138 'rc_deleted' => 0,
139 ) );
140
141 $this->addWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
142 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
143 $this->addWhereIf( 'rc_this_oldid=page_latest', !$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( array( 'show' ) );
156 }
157
158 // Check permissions. FIXME: should this check $user instead of $wgUser?
159 if ( ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) && !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
160 {
161 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
162 }
163
164 /* Add additional conditions to query depending upon parameters. */
165 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
166 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
167 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
168 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
169 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
170 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
171 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
172 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
173 }
174
175 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
176 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
177 }
178 if ( !is_null( $params['user'] ) ) {
179 $this->addWhereFld( 'rc_user_text', $params['user'] );
180 }
181 if ( !is_null( $params['excludeuser'] ) ) {
182 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
183 }
184
185 $db = $this->getDB();
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 while ( $row = $db->fetchObject( $res ) ) {
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 $db->freeResult( $res );
221
222 if ( is_null( $resultPageSet ) ) {
223 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
224 } elseif ( $params['allrev'] ) {
225 $resultPageSet->populateFromRevisionIDs( $ids );
226 } else {
227 $resultPageSet->populateFromPageIDs( $ids );
228 }
229 }
230
231 private function extractRowInfo( $row ) {
232 $vals = array();
233
234 if ( $this->fld_ids ) {
235 $vals['pageid'] = intval( $row->rc_cur_id );
236 $vals['revid'] = intval( $row->rc_this_oldid );
237 }
238
239 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
240
241 if ( $this->fld_title ) {
242 ApiQueryBase::addTitleInfo( $vals, $title );
243 }
244
245 if ( $this->fld_user ) {
246 $vals['user'] = $row->rc_user_text;
247 if ( !$row->rc_user ) {
248 $vals['anon'] = '';
249 }
250 }
251
252 if ( $this->fld_flags ) {
253 if ( $row->rc_new ) {
254 $vals['new'] = '';
255 }
256 if ( $row->rc_minor ) {
257 $vals['minor'] = '';
258 }
259 if ( $row->rc_bot ) {
260 $vals['bot'] = '';
261 }
262 }
263
264 if ( $this->fld_patrol && isset( $row->rc_patrolled ) ) {
265 $vals['patrolled'] = '';
266 }
267
268 if ( $this->fld_timestamp ) {
269 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
270 }
271
272 if ( $this->fld_sizes ) {
273 $vals['oldlen'] = intval( $row->rc_old_len );
274 $vals['newlen'] = intval( $row->rc_new_len );
275 }
276
277 if ( $this->fld_notificationtimestamp ) {
278 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null ) ? '' : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
279 }
280
281 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
282 $vals['comment'] = $row->rc_comment;
283 }
284
285 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
286 global $wgUser;
287 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
288 }
289
290 return $vals;
291 }
292
293 public function getAllowedParams() {
294 return array(
295 'allrev' => false,
296 'start' => array(
297 ApiBase::PARAM_TYPE => 'timestamp'
298 ),
299 'end' => array(
300 ApiBase::PARAM_TYPE => 'timestamp'
301 ),
302 'namespace' => array (
303 ApiBase::PARAM_ISMULTI => true,
304 ApiBase::PARAM_TYPE => 'namespace'
305 ),
306 'user' => array(
307 ApiBase::PARAM_TYPE => 'user',
308 ),
309 'excludeuser' => array(
310 ApiBase::PARAM_TYPE => 'user',
311 ),
312 'dir' => array(
313 ApiBase::PARAM_DFLT => 'older',
314 ApiBase::PARAM_TYPE => array(
315 'newer',
316 'older'
317 )
318 ),
319 'limit' => array(
320 ApiBase::PARAM_DFLT => 10,
321 ApiBase::PARAM_TYPE => 'limit',
322 ApiBase::PARAM_MIN => 1,
323 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
324 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
325 ),
326 'prop' => array(
327 APIBase::PARAM_ISMULTI => true,
328 APIBase::PARAM_DFLT => 'ids|title|flags',
329 APIBase::PARAM_TYPE => array(
330 'ids',
331 'title',
332 'flags',
333 'user',
334 'comment',
335 'parsedcomment',
336 'timestamp',
337 'patrol',
338 'sizes',
339 'notificationtimestamp'
340 )
341 ),
342 'show' => array(
343 ApiBase::PARAM_ISMULTI => true,
344 ApiBase::PARAM_TYPE => array(
345 'minor',
346 '!minor',
347 'bot',
348 '!bot',
349 'anon',
350 '!anon',
351 'patrolled',
352 '!patrolled',
353 )
354 ),
355 'owner' => array(
356 ApiBase::PARAM_TYPE => 'user'
357 ),
358 'token' => array(
359 ApiBase::PARAM_TYPE => 'string'
360 )
361 );
362 }
363
364 public function getParamDescription() {
365 return array(
366 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
367 'start' => 'The timestamp to start enumerating from.',
368 'end' => 'The timestamp to end enumerating.',
369 'namespace' => 'Filter changes to only the given namespace(s).',
370 'user' => 'Only list changes by this user',
371 'excludeuser' => 'Don\'t list changes by this user',
372 'dir' => 'In which direction to enumerate pages.',
373 'limit' => 'How many total results to return per request.',
374 'prop' => 'Which additional items to get (non-generator mode only).',
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 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 }