5 * Created on Sep 25, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
27 use MediaWiki\MediaWikiServices
;
30 * This query action allows clients to retrieve a list of recently modified pages
31 * that are part of the logged-in user's watchlist.
35 class ApiQueryWatchlist
extends ApiQueryGeneratorBase
{
37 public function __construct( ApiQuery
$query, $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'wl' );
41 public function execute() {
45 public function executeGenerator( $resultPageSet ) {
46 $this->run( $resultPageSet );
49 private $fld_ids = false, $fld_title = false, $fld_patrol = false,
50 $fld_flags = false, $fld_timestamp = false, $fld_user = false,
51 $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
52 $fld_notificationtimestamp = false, $fld_userid = false,
56 * @param ApiPageSet $resultPageSet
59 private function run( $resultPageSet = null ) {
60 $this->selectNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
62 $params = $this->extractRequestParams();
64 $user = $this->getUser();
65 $wlowner = $this->getWatchlistUser( $params );
67 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
68 $prop = array_flip( $params['prop'] );
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'] );
83 if ( $this->fld_patrol
) {
84 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
85 $this->dieUsage( 'patrol property is not available', 'patrol' );
91 'dir' => $params['dir'] === 'older'
92 ? WatchedItemQueryService
::DIR_OLDER
93 : WatchedItemQueryService
::DIR_NEWER
,
96 if ( is_null( $resultPageSet ) ) {
97 $options['includeFields'] = $this->getFieldsToInclude();
99 $options['usedInGenerator'] = true;
102 if ( $params['start'] ) {
103 $options['start'] = $params['start'];
105 if ( $params['end'] ) {
106 $options['end'] = $params['end'];
109 if ( !is_null( $params['continue'] ) ) {
110 $cont = explode( '|', $params['continue'] );
111 $this->dieContinueUsageIf( count( $cont ) != 2 );
112 $continueTimestamp = $cont[0];
113 $continueId = (int)$cont[1];
114 $this->dieContinueUsageIf( $continueId != $cont[1] );
115 $options['startFrom'] = [ $continueTimestamp, $continueId ];
118 if ( $wlowner !== $user ) {
119 $options['watchlistOwner'] = $wlowner;
120 $options['watchlistOwnerToken'] = $params['token'];
123 if ( !is_null( $params['namespace'] ) ) {
124 $options['namespaceIds'] = $params['namespace'];
127 if ( $params['allrev'] ) {
128 $options['allRevisions'] = true;
131 if ( !is_null( $params['show'] ) ) {
132 $show = array_flip( $params['show'] );
134 /* Check for conflicting parameters. */
135 if ( $this->showParamsConflicting( $show ) ) {
136 $this->dieUsageMsg( 'show' );
139 // Check permissions.
140 if ( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
141 ||
isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] )
143 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
145 'You need the patrol right to request the patrolled flag',
151 $options['filters'] = array_keys( $show );
154 if ( !is_null( $params['type'] ) ) {
156 $options['rcTypes'] = RecentChange
::parseToRCType( $params['type'] );
157 } catch ( Exception
$e ) {
158 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
162 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
163 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
165 if ( !is_null( $params['user'] ) ) {
166 $options['onlyByUser'] = $params['user'];
168 if ( !is_null( $params['excludeuser'] ) ) {
169 $options['notByUser'] = $params['excludeuser'];
172 $options['limit'] = $params['limit'] +
1;
176 $watchedItemQuery = MediaWikiServices
::getInstance()->getWatchedItemQueryService();
177 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options );
179 foreach ( $items as list ( $watchedItem, $recentChangeInfo ) ) {
180 /** @var WatchedItem $watchedItem */
181 if ( ++
$count > $params['limit'] ) {
182 // We've reached the one extra which shows that there are
183 // additional pages to be had. Stop here...
184 $this->setContinueEnumParameter(
186 $recentChangeInfo['rc_timestamp'] . '|' . $recentChangeInfo['rc_id']
191 if ( is_null( $resultPageSet ) ) {
192 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
193 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
195 $this->setContinueEnumParameter(
197 $recentChangeInfo['rc_timestamp'] . '|' . $recentChangeInfo['rc_id']
202 if ( $params['allrev'] ) {
203 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
205 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
210 if ( is_null( $resultPageSet ) ) {
211 $this->getResult()->addIndexedTagName(
212 [ 'query', $this->getModuleName() ],
215 } elseif ( $params['allrev'] ) {
216 $resultPageSet->populateFromRevisionIDs( $ids );
218 $resultPageSet->populateFromPageIDs( $ids );
222 private function getFieldsToInclude() {
224 if ( $this->fld_flags
) {
225 $includeFields[] = WatchedItemQueryService
::INCLUDE_FLAGS
;
227 if ( $this->fld_user ||
$this->fld_userid
) {
228 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER_ID
;
230 if ( $this->fld_user
) {
231 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER
;
233 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
234 $includeFields[] = WatchedItemQueryService
::INCLUDE_COMMENT
;
236 if ( $this->fld_patrol
) {
237 $includeFields[] = WatchedItemQueryService
::INCLUDE_PATROL_INFO
;
239 if ( $this->fld_sizes
) {
240 $includeFields[] = WatchedItemQueryService
::INCLUDE_SIZES
;
242 if ( $this->fld_loginfo
) {
243 $includeFields[] = WatchedItemQueryService
::INCLUDE_LOG_INFO
;
245 return $includeFields;
248 private function showParamsConflicting( array $show ) {
249 return ( isset( $show[WatchedItemQueryService
::FILTER_MINOR
] )
250 && isset( $show[WatchedItemQueryService
::FILTER_NOT_MINOR
] ) )
251 ||
( isset( $show[WatchedItemQueryService
::FILTER_BOT
] )
252 && isset( $show[WatchedItemQueryService
::FILTER_NOT_BOT
] ) )
253 ||
( isset( $show[WatchedItemQueryService
::FILTER_ANON
] )
254 && isset( $show[WatchedItemQueryService
::FILTER_NOT_ANON
] ) )
255 ||
( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
256 && isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] ) )
257 ||
( isset( $show[WatchedItemQueryService
::FILTER_UNREAD
] )
258 && isset( $show[WatchedItemQueryService
::FILTER_NOT_UNREAD
] ) );
261 private function extractOutputData( WatchedItem
$watchedItem, array $recentChangeInfo ) {
262 /* Determine the title of the page that has been changed. */
263 $title = Title
::makeTitle(
264 $watchedItem->getLinkTarget()->getNamespace(),
265 $watchedItem->getLinkTarget()->getDBkey()
267 $user = $this->getUser();
269 /* Our output data. */
271 $type = intval( $recentChangeInfo['rc_type'] );
272 $vals['type'] = RecentChange
::parseFromRCType( $type );
275 /* Create a new entry in the result for the title. */
276 if ( $this->fld_title ||
$this->fld_ids
) {
277 // These should already have been filtered out of the query, but just in case.
278 if ( $type === RC_LOG
&& ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) ) {
279 $vals['actionhidden'] = true;
282 if ( $type !== RC_LOG ||
283 LogEventsList
::userCanBitfield(
284 $recentChangeInfo['rc_deleted'],
285 LogPage
::DELETED_ACTION
,
289 if ( $this->fld_title
) {
290 ApiQueryBase
::addTitleInfo( $vals, $title );
292 if ( $this->fld_ids
) {
293 $vals['pageid'] = intval( $recentChangeInfo['rc_cur_id'] );
294 $vals['revid'] = intval( $recentChangeInfo['rc_this_oldid'] );
295 $vals['old_revid'] = intval( $recentChangeInfo['rc_last_oldid'] );
300 /* Add user data and 'anon' flag, if user is anonymous. */
301 if ( $this->fld_user ||
$this->fld_userid
) {
302 if ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_USER
) {
303 $vals['userhidden'] = true;
306 if ( Revision
::userCanBitfield(
307 $recentChangeInfo['rc_deleted'],
308 Revision
::DELETED_USER
,
311 if ( $this->fld_userid
) {
312 $vals['userid'] = (int)$recentChangeInfo['rc_user'];
313 // for backwards compatibility
314 $vals['user'] = (int)$recentChangeInfo['rc_user'];
317 if ( $this->fld_user
) {
318 $vals['user'] = $recentChangeInfo['rc_user_text'];
321 if ( !$recentChangeInfo['rc_user'] ) {
322 $vals['anon'] = true;
327 /* Add flags, such as new, minor, bot. */
328 if ( $this->fld_flags
) {
329 $vals['bot'] = (bool)$recentChangeInfo['rc_bot'];
330 $vals['new'] = $recentChangeInfo['rc_type'] == RC_NEW
;
331 $vals['minor'] = (bool)$recentChangeInfo['rc_minor'];
334 /* Add sizes of each revision. (Only available on 1.10+) */
335 if ( $this->fld_sizes
) {
336 $vals['oldlen'] = intval( $recentChangeInfo['rc_old_len'] );
337 $vals['newlen'] = intval( $recentChangeInfo['rc_new_len'] );
340 /* Add the timestamp. */
341 if ( $this->fld_timestamp
) {
342 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $recentChangeInfo['rc_timestamp'] );
345 if ( $this->fld_notificationtimestamp
) {
346 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
348 : wfTimestamp( TS_ISO_8601
, $watchedItem->getNotificationTimestamp() );
351 /* Add edit summary / log summary. */
352 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
353 if ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_COMMENT
) {
354 $vals['commenthidden'] = true;
357 if ( Revision
::userCanBitfield(
358 $recentChangeInfo['rc_deleted'],
359 Revision
::DELETED_COMMENT
,
362 if ( $this->fld_comment
&& isset( $recentChangeInfo['rc_comment'] ) ) {
363 $vals['comment'] = $recentChangeInfo['rc_comment'];
366 if ( $this->fld_parsedcomment
&& isset( $recentChangeInfo['rc_comment'] ) ) {
367 $vals['parsedcomment'] = Linker
::formatComment( $recentChangeInfo['rc_comment'], $title );
372 /* Add the patrolled flag */
373 if ( $this->fld_patrol
) {
374 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
375 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( (object)$recentChangeInfo, $user );
378 if ( $this->fld_loginfo
&& $recentChangeInfo['rc_type'] == RC_LOG
) {
379 if ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) {
380 $vals['actionhidden'] = true;
383 if ( LogEventsList
::userCanBitfield(
384 $recentChangeInfo['rc_deleted'],
385 LogPage
::DELETED_ACTION
,
388 $vals['logid'] = intval( $recentChangeInfo['rc_logid'] );
389 $vals['logtype'] = $recentChangeInfo['rc_log_type'];
390 $vals['logaction'] = $recentChangeInfo['rc_log_action'];
391 $vals['logparams'] = LogFormatter
::newFromRow( $recentChangeInfo )->formatParametersForApi();
395 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_RESTRICTED
) ) {
396 $vals['suppressed'] = true;
402 public function getAllowedParams() {
406 ApiBase
::PARAM_TYPE
=> 'timestamp'
409 ApiBase
::PARAM_TYPE
=> 'timestamp'
412 ApiBase
::PARAM_ISMULTI
=> true,
413 ApiBase
::PARAM_TYPE
=> 'namespace'
416 ApiBase
::PARAM_TYPE
=> 'user',
419 ApiBase
::PARAM_TYPE
=> 'user',
422 ApiBase
::PARAM_DFLT
=> 'older',
423 ApiBase
::PARAM_TYPE
=> [
427 ApiHelp
::PARAM_HELP_MSG
=> 'api-help-param-direction',
430 ApiBase
::PARAM_DFLT
=> 10,
431 ApiBase
::PARAM_TYPE
=> 'limit',
432 ApiBase
::PARAM_MIN
=> 1,
433 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
434 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
437 ApiBase
::PARAM_ISMULTI
=> true,
438 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
439 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
440 ApiBase
::PARAM_TYPE
=> [
451 'notificationtimestamp',
456 ApiBase
::PARAM_ISMULTI
=> true,
457 ApiBase
::PARAM_TYPE
=> [
458 WatchedItemQueryService
::FILTER_MINOR
,
459 WatchedItemQueryService
::FILTER_NOT_MINOR
,
460 WatchedItemQueryService
::FILTER_BOT
,
461 WatchedItemQueryService
::FILTER_NOT_BOT
,
462 WatchedItemQueryService
::FILTER_ANON
,
463 WatchedItemQueryService
::FILTER_NOT_ANON
,
464 WatchedItemQueryService
::FILTER_PATROLLED
,
465 WatchedItemQueryService
::FILTER_NOT_PATROLLED
,
466 WatchedItemQueryService
::FILTER_UNREAD
,
467 WatchedItemQueryService
::FILTER_NOT_UNREAD
,
471 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
472 ApiBase
::PARAM_ISMULTI
=> true,
473 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
474 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
477 ApiBase
::PARAM_TYPE
=> 'user'
480 ApiBase
::PARAM_TYPE
=> 'string'
483 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
488 protected function getExamplesMessages() {
490 'action=query&list=watchlist'
491 => 'apihelp-query+watchlist-example-simple',
492 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
493 => 'apihelp-query+watchlist-example-props',
494 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
495 => 'apihelp-query+watchlist-example-allrev',
496 'action=query&generator=watchlist&prop=info'
497 => 'apihelp-query+watchlist-example-generator',
498 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
499 => 'apihelp-query+watchlist-example-generator-rev',
500 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
501 => 'apihelp-query+watchlist-example-wlowner',
505 public function getHelpUrls() {
506 return 'https://www.mediawiki.org/wiki/API:Watchlist';