3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Revision\RevisionRecord
;
27 * This query action allows clients to retrieve a list of recently modified pages
28 * that are part of the logged-in user's watchlist.
32 class ApiQueryWatchlist
extends ApiQueryGeneratorBase
{
34 /** @var CommentStore */
35 private $commentStore;
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,
53 $fld_loginfo = false, $fld_tags;
56 * @param ApiPageSet $resultPageSet
59 private function run( $resultPageSet = null ) {
60 $this->selectNamedDB( 'watchlist', DB_REPLICA
, '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'] );
82 $this->fld_tags
= isset( $prop['tags'] );
84 if ( $this->fld_patrol
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
85 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'patrol' );
88 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
89 $this->commentStore
= CommentStore
::getStore();
94 'dir' => $params['dir'] === 'older'
95 ? WatchedItemQueryService
::DIR_OLDER
96 : WatchedItemQueryService
::DIR_NEWER
,
99 if ( is_null( $resultPageSet ) ) {
100 $options['includeFields'] = $this->getFieldsToInclude();
102 $options['usedInGenerator'] = true;
105 if ( $params['start'] ) {
106 $options['start'] = $params['start'];
108 if ( $params['end'] ) {
109 $options['end'] = $params['end'];
113 if ( !is_null( $params['continue'] ) ) {
114 $cont = explode( '|', $params['continue'] );
115 $this->dieContinueUsageIf( count( $cont ) != 2 );
116 $continueTimestamp = $cont[0];
117 $continueId = (int)$cont[1];
118 $this->dieContinueUsageIf( $continueId != $cont[1] );
119 $startFrom = [ $continueTimestamp, $continueId ];
122 if ( $wlowner !== $user ) {
123 $options['watchlistOwner'] = $wlowner;
124 $options['watchlistOwnerToken'] = $params['token'];
127 if ( !is_null( $params['namespace'] ) ) {
128 $options['namespaceIds'] = $params['namespace'];
131 if ( $params['allrev'] ) {
132 $options['allRevisions'] = true;
135 if ( !is_null( $params['show'] ) ) {
136 $show = array_flip( $params['show'] );
138 /* Check for conflicting parameters. */
139 if ( $this->showParamsConflicting( $show ) ) {
140 $this->dieWithError( 'apierror-show' );
143 // Check permissions.
144 if ( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
145 ||
isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] )
147 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
148 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
152 $options['filters'] = array_keys( $show );
155 if ( !is_null( $params['type'] ) ) {
157 $rcTypes = RecentChange
::parseToRCType( $params['type'] );
159 $options['rcTypes'] = $rcTypes;
161 } catch ( Exception
$e ) {
162 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
166 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
167 if ( !is_null( $params['user'] ) ) {
168 $options['onlyByUser'] = $params['user'];
170 if ( !is_null( $params['excludeuser'] ) ) {
171 $options['notByUser'] = $params['excludeuser'];
174 $options['limit'] = $params['limit'];
176 Hooks
::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
177 $this, $params, &$options
181 $watchedItemQuery = MediaWikiServices
::getInstance()->getWatchedItemQueryService();
182 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
184 foreach ( $items as list( $watchedItem, $recentChangeInfo ) ) {
185 /** @var WatchedItem $watchedItem */
186 if ( is_null( $resultPageSet ) ) {
187 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
188 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
190 $startFrom = [ $recentChangeInfo['rc_timestamp'], $recentChangeInfo['rc_id'] ];
193 } elseif ( $params['allrev'] ) {
194 $ids[] = (int)$recentChangeInfo['rc_this_oldid'];
196 $ids[] = (int)$recentChangeInfo['rc_cur_id'];
200 if ( $startFrom !== null ) {
201 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
204 if ( is_null( $resultPageSet ) ) {
205 $this->getResult()->addIndexedTagName(
206 [ 'query', $this->getModuleName() ],
209 } elseif ( $params['allrev'] ) {
210 $resultPageSet->populateFromRevisionIDs( $ids );
212 $resultPageSet->populateFromPageIDs( $ids );
216 private function getFieldsToInclude() {
218 if ( $this->fld_flags
) {
219 $includeFields[] = WatchedItemQueryService
::INCLUDE_FLAGS
;
221 if ( $this->fld_user ||
$this->fld_userid
) {
222 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER_ID
;
224 if ( $this->fld_user
) {
225 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER
;
227 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
228 $includeFields[] = WatchedItemQueryService
::INCLUDE_COMMENT
;
230 if ( $this->fld_patrol
) {
231 $includeFields[] = WatchedItemQueryService
::INCLUDE_PATROL_INFO
;
232 $includeFields[] = WatchedItemQueryService
::INCLUDE_AUTOPATROL_INFO
;
234 if ( $this->fld_sizes
) {
235 $includeFields[] = WatchedItemQueryService
::INCLUDE_SIZES
;
237 if ( $this->fld_loginfo
) {
238 $includeFields[] = WatchedItemQueryService
::INCLUDE_LOG_INFO
;
240 if ( $this->fld_tags
) {
241 $includeFields[] = WatchedItemQueryService
::INCLUDE_TAGS
;
243 return $includeFields;
246 private function showParamsConflicting( array $show ) {
247 return ( isset( $show[WatchedItemQueryService
::FILTER_MINOR
] )
248 && isset( $show[WatchedItemQueryService
::FILTER_NOT_MINOR
] ) )
249 ||
( isset( $show[WatchedItemQueryService
::FILTER_BOT
] )
250 && isset( $show[WatchedItemQueryService
::FILTER_NOT_BOT
] ) )
251 ||
( isset( $show[WatchedItemQueryService
::FILTER_ANON
] )
252 && isset( $show[WatchedItemQueryService
::FILTER_NOT_ANON
] ) )
253 ||
( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
254 && isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] ) )
255 ||
( isset( $show[WatchedItemQueryService
::FILTER_AUTOPATROLLED
] )
256 && isset( $show[WatchedItemQueryService
::FILTER_NOT_AUTOPATROLLED
] ) )
257 ||
( isset( $show[WatchedItemQueryService
::FILTER_AUTOPATROLLED
] )
258 && isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] ) )
259 ||
( isset( $show[WatchedItemQueryService
::FILTER_UNREAD
] )
260 && isset( $show[WatchedItemQueryService
::FILTER_NOT_UNREAD
] ) );
263 private function extractOutputData( WatchedItem
$watchedItem, array $recentChangeInfo ) {
264 /* Determine the title of the page that has been changed. */
265 $title = Title
::newFromLinkTarget( $watchedItem->getLinkTarget() );
266 $user = $this->getUser();
268 /* Our output data. */
270 $type = (int)$recentChangeInfo['rc_type'];
271 $vals['type'] = RecentChange
::parseFromRCType( $type );
274 /* Create a new entry in the result for the title. */
275 if ( $this->fld_title ||
$this->fld_ids
) {
276 // These should already have been filtered out of the query, but just in case.
277 if ( $type === RC_LOG
&& ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) ) {
278 $vals['actionhidden'] = true;
281 if ( $type !== RC_LOG ||
282 LogEventsList
::userCanBitfield(
283 $recentChangeInfo['rc_deleted'],
284 LogPage
::DELETED_ACTION
,
288 if ( $this->fld_title
) {
289 ApiQueryBase
::addTitleInfo( $vals, $title );
291 if ( $this->fld_ids
) {
292 $vals['pageid'] = (int)$recentChangeInfo['rc_cur_id'];
293 $vals['revid'] = (int)$recentChangeInfo['rc_this_oldid'];
294 $vals['old_revid'] = (int)$recentChangeInfo['rc_last_oldid'];
299 /* Add user data and 'anon' flag, if user is anonymous. */
300 if ( $this->fld_user ||
$this->fld_userid
) {
301 if ( $recentChangeInfo['rc_deleted'] & RevisionRecord
::DELETED_USER
) {
302 $vals['userhidden'] = true;
305 if ( RevisionRecord
::userCanBitfield(
306 $recentChangeInfo['rc_deleted'],
307 RevisionRecord
::DELETED_USER
,
310 if ( $this->fld_userid
) {
311 $vals['userid'] = (int)$recentChangeInfo['rc_user'];
312 // for backwards compatibility
313 $vals['user'] = (int)$recentChangeInfo['rc_user'];
316 if ( $this->fld_user
) {
317 $vals['user'] = $recentChangeInfo['rc_user_text'];
320 if ( !$recentChangeInfo['rc_user'] ) {
321 $vals['anon'] = true;
326 /* Add flags, such as new, minor, bot. */
327 if ( $this->fld_flags
) {
328 $vals['bot'] = (bool)$recentChangeInfo['rc_bot'];
329 $vals['new'] = $recentChangeInfo['rc_type'] == RC_NEW
;
330 $vals['minor'] = (bool)$recentChangeInfo['rc_minor'];
333 /* Add sizes of each revision. (Only available on 1.10+) */
334 if ( $this->fld_sizes
) {
335 $vals['oldlen'] = (int)$recentChangeInfo['rc_old_len'];
336 $vals['newlen'] = (int)$recentChangeInfo['rc_new_len'];
339 /* Add the timestamp. */
340 if ( $this->fld_timestamp
) {
341 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $recentChangeInfo['rc_timestamp'] );
344 if ( $this->fld_notificationtimestamp
) {
345 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
347 : wfTimestamp( TS_ISO_8601
, $watchedItem->getNotificationTimestamp() );
350 /* Add edit summary / log summary. */
351 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
352 if ( $recentChangeInfo['rc_deleted'] & RevisionRecord
::DELETED_COMMENT
) {
353 $vals['commenthidden'] = true;
356 if ( RevisionRecord
::userCanBitfield(
357 $recentChangeInfo['rc_deleted'],
358 RevisionRecord
::DELETED_COMMENT
,
361 $comment = $this->commentStore
->getComment( 'rc_comment', $recentChangeInfo )->text
;
362 if ( $this->fld_comment
) {
363 $vals['comment'] = $comment;
366 if ( $this->fld_parsedcomment
) {
367 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
372 /* Add the patrolled flag */
373 if ( $this->fld_patrol
) {
374 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] != RecentChange
::PRC_UNPATROLLED
;
375 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( (object)$recentChangeInfo, $user );
376 $vals['autopatrolled'] = $recentChangeInfo['rc_patrolled'] == RecentChange
::PRC_AUTOPATROLLED
;
379 if ( $this->fld_loginfo
&& $recentChangeInfo['rc_type'] == RC_LOG
) {
380 if ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) {
381 $vals['actionhidden'] = true;
384 if ( LogEventsList
::userCanBitfield(
385 $recentChangeInfo['rc_deleted'],
386 LogPage
::DELETED_ACTION
,
389 $vals['logid'] = (int)$recentChangeInfo['rc_logid'];
390 $vals['logtype'] = $recentChangeInfo['rc_log_type'];
391 $vals['logaction'] = $recentChangeInfo['rc_log_action'];
392 $vals['logparams'] = LogFormatter
::newFromRow( $recentChangeInfo )->formatParametersForApi();
396 if ( $this->fld_tags
) {
397 if ( $recentChangeInfo['rc_tags'] ) {
398 $tags = explode( ',', $recentChangeInfo['rc_tags'] );
399 ApiResult
::setIndexedTagName( $tags, 'tag' );
400 $vals['tags'] = $tags;
406 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & RevisionRecord
::DELETED_RESTRICTED
) ) {
407 $vals['suppressed'] = true;
410 Hooks
::run( 'ApiQueryWatchlistExtractOutputData', [
411 $this, $watchedItem, $recentChangeInfo, &$vals
417 public function getAllowedParams() {
421 ApiBase
::PARAM_TYPE
=> 'timestamp'
424 ApiBase
::PARAM_TYPE
=> 'timestamp'
427 ApiBase
::PARAM_ISMULTI
=> true,
428 ApiBase
::PARAM_TYPE
=> 'namespace'
431 ApiBase
::PARAM_TYPE
=> 'user',
434 ApiBase
::PARAM_TYPE
=> 'user',
437 ApiBase
::PARAM_DFLT
=> 'older',
438 ApiBase
::PARAM_TYPE
=> [
442 ApiHelp
::PARAM_HELP_MSG
=> 'api-help-param-direction',
445 ApiBase
::PARAM_DFLT
=> 10,
446 ApiBase
::PARAM_TYPE
=> 'limit',
447 ApiBase
::PARAM_MIN
=> 1,
448 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
449 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
452 ApiBase
::PARAM_ISMULTI
=> true,
453 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
454 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
455 ApiBase
::PARAM_TYPE
=> [
466 'notificationtimestamp',
472 ApiBase
::PARAM_ISMULTI
=> true,
473 ApiBase
::PARAM_TYPE
=> [
474 WatchedItemQueryService
::FILTER_MINOR
,
475 WatchedItemQueryService
::FILTER_NOT_MINOR
,
476 WatchedItemQueryService
::FILTER_BOT
,
477 WatchedItemQueryService
::FILTER_NOT_BOT
,
478 WatchedItemQueryService
::FILTER_ANON
,
479 WatchedItemQueryService
::FILTER_NOT_ANON
,
480 WatchedItemQueryService
::FILTER_PATROLLED
,
481 WatchedItemQueryService
::FILTER_NOT_PATROLLED
,
482 WatchedItemQueryService
::FILTER_AUTOPATROLLED
,
483 WatchedItemQueryService
::FILTER_NOT_AUTOPATROLLED
,
484 WatchedItemQueryService
::FILTER_UNREAD
,
485 WatchedItemQueryService
::FILTER_NOT_UNREAD
,
489 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
490 ApiBase
::PARAM_ISMULTI
=> true,
491 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
492 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
495 ApiBase
::PARAM_TYPE
=> 'user'
498 ApiBase
::PARAM_TYPE
=> 'string',
499 ApiBase
::PARAM_SENSITIVE
=> true,
502 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
507 protected function getExamplesMessages() {
509 'action=query&list=watchlist'
510 => 'apihelp-query+watchlist-example-simple',
511 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
512 => 'apihelp-query+watchlist-example-props',
513 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
514 => 'apihelp-query+watchlist-example-allrev',
515 'action=query&generator=watchlist&prop=info'
516 => 'apihelp-query+watchlist-example-generator',
517 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
518 => 'apihelp-query+watchlist-example-generator-rev',
519 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
520 => 'apihelp-query+watchlist-example-wlowner',
524 public function getHelpUrls() {
525 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist';