Merge "Make update.php and install.php use wfPHPVersionError() and reorganise it"
[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 /**
28 * This query action allows clients to retrieve a list of recently modified pages
29 * that are part of the logged-in user's watchlist.
30 *
31 * @ingroup API
32 */
33 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
34
35 public function __construct( $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'wl' );
37 }
38
39 public function execute() {
40 $this->run();
41 }
42
43 public function executeGenerator( $resultPageSet ) {
44 $this->run( $resultPageSet );
45 }
46
47 private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
48 $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
49 $fld_notificationtimestamp = false, $fld_userid = false, $fld_loginfo = false;
50
51 /**
52 * @param $resultPageSet ApiPageSet
53 * @return void
54 */
55 private function run( $resultPageSet = null ) {
56 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
57
58 $params = $this->extractRequestParams();
59
60 $user = $this->getWatchlistUser( $params );
61
62 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
63 $prop = array_flip( $params['prop'] );
64
65 $this->fld_ids = isset( $prop['ids'] );
66 $this->fld_title = isset( $prop['title'] );
67 $this->fld_flags = isset( $prop['flags'] );
68 $this->fld_user = isset( $prop['user'] );
69 $this->fld_userid = isset( $prop['userid'] );
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 $this->fld_loginfo = isset( $prop['loginfo'] );
77
78 if ( $this->fld_patrol ) {
79 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
80 $this->dieUsage( 'patrol property is not available', 'patrol' );
81 }
82 }
83 }
84
85 $this->addFields( array(
86 'rc_namespace',
87 'rc_title',
88 'rc_timestamp',
89 'rc_type',
90 ) );
91
92 if ( is_null( $resultPageSet ) ) {
93 $this->addFields( array(
94 'rc_cur_id',
95 'rc_this_oldid',
96 'rc_last_oldid',
97 ) );
98
99 $this->addFieldsIf( array( 'rc_type', 'rc_minor', 'rc_bot' ), $this->fld_flags );
100 $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
101 $this->addFieldsIf( 'rc_user_text', $this->fld_user );
102 $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
103 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
104 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes );
105 $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
106 $this->addFieldsIf( array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ), $this->fld_loginfo );
107 } elseif ( $params['allrev'] ) {
108 $this->addFields( 'rc_this_oldid' );
109 } else {
110 $this->addFields( 'rc_cur_id' );
111 }
112
113 $this->addTables( array(
114 'recentchanges',
115 'watchlist',
116 ) );
117
118 $userId = $user->getId();
119 $this->addJoinConds( array( 'watchlist' => array( 'INNER JOIN',
120 array(
121 'wl_user' => $userId,
122 'wl_namespace=rc_namespace',
123 'wl_title=rc_title'
124 ) ) ) );
125
126 $this->addWhere( array(
127 'rc_deleted' => 0,
128 ) );
129
130 $db = $this->getDB();
131
132 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
133 $params['start'], $params['end'] );
134 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
135
136 if ( !$params['allrev'] ) {
137 $this->addTables( 'page' );
138 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN','rc_cur_id=page_id' ) ) );
139 $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG );
140 }
141
142 if ( !is_null( $params['show'] ) ) {
143 $show = array_flip( $params['show'] );
144
145 /* Check for conflicting parameters. */
146 if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
147 || ( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
148 || ( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
149 || ( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) )
150 )
151 {
152 $this->dieUsageMsg( 'show' );
153 }
154
155 // Check permissions.
156 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
157 $user = $this->getUser();
158 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
159 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
160 }
161 }
162
163 /* Add additional conditions to query depending upon parameters. */
164 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
165 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
166 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
167 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
168 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
169 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
170 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
171 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
172 }
173
174 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
175 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
176 }
177 if ( !is_null( $params['user'] ) ) {
178 $this->addWhereFld( 'rc_user_text', $params['user'] );
179 }
180 if ( !is_null( $params['excludeuser'] ) ) {
181 $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
182 }
183
184 // This is an index optimization for mysql, as done in the Special:Watchlist page
185 $this->addWhereIf( "rc_timestamp > ''", !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' );
186
187 $this->addOption( 'LIMIT', $params['limit'] + 1 );
188
189 $ids = array();
190 $count = 0;
191 $res = $this->select( __METHOD__ );
192
193 foreach ( $res as $row ) {
194 if ( ++ $count > $params['limit'] ) {
195 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
196 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
197 break;
198 }
199
200 if ( is_null( $resultPageSet ) ) {
201 $vals = $this->extractRowInfo( $row );
202 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
203 if ( !$fit ) {
204 $this->setContinueEnumParameter( 'start',
205 wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
206 break;
207 }
208 } else {
209 if ( $params['allrev'] ) {
210 $ids[] = intval( $row->rc_this_oldid );
211 } else {
212 $ids[] = intval( $row->rc_cur_id );
213 }
214 }
215 }
216
217 if ( is_null( $resultPageSet ) ) {
218 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
219 } elseif ( $params['allrev'] ) {
220 $resultPageSet->populateFromRevisionIDs( $ids );
221 } else {
222 $resultPageSet->populateFromPageIDs( $ids );
223 }
224 }
225
226 private function extractRowInfo( $row ) {
227 $vals = array();
228
229 if ( $this->fld_ids ) {
230 $vals['pageid'] = intval( $row->rc_cur_id );
231 $vals['revid'] = intval( $row->rc_this_oldid );
232 $vals['old_revid'] = intval( $row->rc_last_oldid );
233 }
234
235 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
236
237 if ( $this->fld_title ) {
238 ApiQueryBase::addTitleInfo( $vals, $title );
239 }
240
241 if ( $this->fld_user || $this->fld_userid ) {
242
243 if ( $this->fld_userid ) {
244 $vals['userid'] = $row->rc_user;
245 // for backwards compatibility
246 $vals['user'] = $row->rc_user;
247 }
248
249 if ( $this->fld_user ) {
250 $vals['user'] = $row->rc_user_text;
251 }
252
253 if ( !$row->rc_user ) {
254 $vals['anon'] = '';
255 }
256 }
257
258 if ( $this->fld_flags ) {
259 if ( $row->rc_type == RC_NEW ) {
260 $vals['new'] = '';
261 }
262 if ( $row->rc_minor ) {
263 $vals['minor'] = '';
264 }
265 if ( $row->rc_bot ) {
266 $vals['bot'] = '';
267 }
268 }
269
270 if ( $this->fld_patrol && isset( $row->rc_patrolled ) ) {
271 $vals['patrolled'] = '';
272 }
273
274 if ( $this->fld_timestamp ) {
275 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
276 }
277
278 if ( $this->fld_sizes ) {
279 $vals['oldlen'] = intval( $row->rc_old_len );
280 $vals['newlen'] = intval( $row->rc_new_len );
281 }
282
283 if ( $this->fld_notificationtimestamp ) {
284 $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null )
285 ? ''
286 : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
287 }
288
289 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
290 $vals['comment'] = $row->rc_comment;
291 }
292
293 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
294 $vals['parsedcomment'] = Linker::formatComment( $row->rc_comment, $title );
295 }
296
297 if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
298 $vals['logid'] = intval( $row->rc_logid );
299 $vals['logtype'] = $row->rc_log_type;
300 $vals['logaction'] = $row->rc_log_action;
301 $logEntry = DatabaseLogEntry::newFromRow( (array)$row );
302 ApiQueryLogEvents::addLogParams(
303 $this->getResult(),
304 $vals,
305 $logEntry->getParameters(),
306 $logEntry->getType(),
307 $logEntry->getSubtype(),
308 $logEntry->getTimestamp()
309 );
310 }
311
312 return $vals;
313 }
314
315 public function getAllowedParams() {
316 return array(
317 'allrev' => false,
318 'start' => array(
319 ApiBase::PARAM_TYPE => 'timestamp'
320 ),
321 'end' => array(
322 ApiBase::PARAM_TYPE => 'timestamp'
323 ),
324 'namespace' => array (
325 ApiBase::PARAM_ISMULTI => true,
326 ApiBase::PARAM_TYPE => 'namespace'
327 ),
328 'user' => array(
329 ApiBase::PARAM_TYPE => 'user',
330 ),
331 'excludeuser' => array(
332 ApiBase::PARAM_TYPE => 'user',
333 ),
334 'dir' => array(
335 ApiBase::PARAM_DFLT => 'older',
336 ApiBase::PARAM_TYPE => array(
337 'newer',
338 'older'
339 )
340 ),
341 'limit' => array(
342 ApiBase::PARAM_DFLT => 10,
343 ApiBase::PARAM_TYPE => 'limit',
344 ApiBase::PARAM_MIN => 1,
345 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
346 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
347 ),
348 'prop' => array(
349 ApiBase::PARAM_ISMULTI => true,
350 ApiBase::PARAM_DFLT => 'ids|title|flags',
351 ApiBase::PARAM_TYPE => array(
352 'ids',
353 'title',
354 'flags',
355 'user',
356 'userid',
357 'comment',
358 'parsedcomment',
359 'timestamp',
360 'patrol',
361 'sizes',
362 'notificationtimestamp',
363 'loginfo',
364 )
365 ),
366 'show' => array(
367 ApiBase::PARAM_ISMULTI => true,
368 ApiBase::PARAM_TYPE => array(
369 'minor',
370 '!minor',
371 'bot',
372 '!bot',
373 'anon',
374 '!anon',
375 'patrolled',
376 '!patrolled',
377 )
378 ),
379 'owner' => array(
380 ApiBase::PARAM_TYPE => 'user'
381 ),
382 'token' => array(
383 ApiBase::PARAM_TYPE => 'string'
384 )
385 );
386 }
387
388 public function getParamDescription() {
389 $p = $this->getModulePrefix();
390 return array(
391 'allrev' => 'Include multiple revisions of the same page within given timeframe',
392 'start' => 'The timestamp to start enumerating from',
393 'end' => 'The timestamp to end enumerating',
394 'namespace' => 'Filter changes to only the given namespace(s)',
395 'user' => 'Only list changes by this user',
396 'excludeuser' => 'Don\'t list changes by this user',
397 'dir' => $this->getDirectionDescription( $p ),
398 'limit' => 'How many total results to return per request',
399 'prop' => array(
400 'Which additional items to get (non-generator mode only).',
401 ' ids - Adds revision ids and page ids',
402 ' title - Adds title of the page',
403 ' flags - Adds flags for the edit',
404 ' user - Adds the user who made the edit',
405 ' userid - Adds user id of whom made the edit',
406 ' comment - Adds comment of the edit',
407 ' parsedcomment - Adds parsed comment of the edit',
408 ' timestamp - Adds timestamp of the edit',
409 ' patrol - Tags edits that are patrolled',
410 ' sizes - Adds the old and new lengths of the page',
411 ' notificationtimestamp - Adds timestamp of when the user was last notified about the edit',
412 ' loginfo - Adds log information where appropriate',
413 ),
414 'show' => array(
415 'Show only items that meet this criteria.',
416 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
417 ),
418 'owner' => 'The name of the user whose watchlist you\'d like to access',
419 'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
420 );
421 }
422
423 public function getResultProperties() {
424 global $wgLogTypes;
425 return array(
426 'ids' => array(
427 'pageid' => 'integer',
428 'revid' => 'integer',
429 'old_revid' => 'integer'
430 ),
431 'title' => array(
432 'ns' => 'namespace',
433 'title' => 'string'
434 ),
435 'user' => array(
436 'user' => 'string',
437 'anon' => 'boolean'
438 ),
439 'userid' => array(
440 'userid' => 'integer',
441 'anon' => 'boolean'
442 ),
443 'flags' => array(
444 'new' => 'boolean',
445 'minor' => 'boolean',
446 'bot' => 'boolean'
447 ),
448 'patrol' => array(
449 'patrolled' => 'boolean'
450 ),
451 'timestamp' => array(
452 'timestamp' => 'timestamp'
453 ),
454 'sizes' => array(
455 'oldlen' => 'integer',
456 'newlen' => 'integer'
457 ),
458 'notificationtimestamp' => array(
459 'notificationtimestamp' => array(
460 ApiBase::PROP_TYPE => 'timestamp',
461 ApiBase::PROP_NULLABLE => true
462 )
463 ),
464 'comment' => array(
465 'comment' => array(
466 ApiBase::PROP_TYPE => 'string',
467 ApiBase::PROP_NULLABLE => true
468 )
469 ),
470 'parsedcomment' => array(
471 'parsedcomment' => array(
472 ApiBase::PROP_TYPE => 'string',
473 ApiBase::PROP_NULLABLE => true
474 )
475 ),
476 'loginfo' => array(
477 'logid' => array(
478 ApiBase::PROP_TYPE => 'integer',
479 ApiBase::PROP_NULLABLE => true
480 ),
481 'logtype' => array(
482 ApiBase::PROP_TYPE => $wgLogTypes,
483 ApiBase::PROP_NULLABLE => true
484 ),
485 'logaction' => array(
486 ApiBase::PROP_TYPE => 'string',
487 ApiBase::PROP_NULLABLE => true
488 )
489 )
490 );
491 }
492
493 public function getDescription() {
494 return "Get all recent changes to pages in the logged in user's watchlist";
495 }
496
497 public function getPossibleErrors() {
498 return array_merge( parent::getPossibleErrors(), array(
499 array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
500 array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
501 array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
502 array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
503 array( 'show' ),
504 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
505 array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
506 ) );
507 }
508
509 public function getExamples() {
510 return array(
511 'api.php?action=query&list=watchlist',
512 'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
513 'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
514 'api.php?action=query&generator=watchlist&prop=info',
515 'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
516 'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=123ABC'
517 );
518 }
519
520 public function getHelpUrls() {
521 return 'https://www.mediawiki.org/wiki/API:Watchlist';
522 }
523 }