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 /** @var CommentStore */
38 private $commentStore;
40 public function __construct( ApiQuery
$query, $moduleName ) {
41 parent
::__construct( $query, $moduleName, 'wl' );
44 public function execute() {
48 public function executeGenerator( $resultPageSet ) {
49 $this->run( $resultPageSet );
52 private $fld_ids = false, $fld_title = false, $fld_patrol = false,
53 $fld_flags = false, $fld_timestamp = false, $fld_user = false,
54 $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
55 $fld_notificationtimestamp = false, $fld_userid = false,
56 $fld_loginfo = false, $fld_tags;
59 * @param ApiPageSet $resultPageSet
62 private function run( $resultPageSet = null ) {
63 $this->selectNamedDB( 'watchlist', DB_REPLICA
, 'watchlist' );
65 $params = $this->extractRequestParams();
67 $user = $this->getUser();
68 $wlowner = $this->getWatchlistUser( $params );
70 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
71 $prop = array_flip( $params['prop'] );
73 $this->fld_ids
= isset( $prop['ids'] );
74 $this->fld_title
= isset( $prop['title'] );
75 $this->fld_flags
= isset( $prop['flags'] );
76 $this->fld_user
= isset( $prop['user'] );
77 $this->fld_userid
= isset( $prop['userid'] );
78 $this->fld_comment
= isset( $prop['comment'] );
79 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
80 $this->fld_timestamp
= isset( $prop['timestamp'] );
81 $this->fld_sizes
= isset( $prop['sizes'] );
82 $this->fld_patrol
= isset( $prop['patrol'] );
83 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
84 $this->fld_loginfo
= isset( $prop['loginfo'] );
85 $this->fld_tags
= isset( $prop['tags'] );
87 if ( $this->fld_patrol
) {
88 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
89 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'patrol' );
93 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
94 $this->commentStore
= new CommentStore( 'rc_comment' );
99 'dir' => $params['dir'] === 'older'
100 ? WatchedItemQueryService
::DIR_OLDER
101 : WatchedItemQueryService
::DIR_NEWER
,
104 if ( is_null( $resultPageSet ) ) {
105 $options['includeFields'] = $this->getFieldsToInclude();
107 $options['usedInGenerator'] = true;
110 if ( $params['start'] ) {
111 $options['start'] = $params['start'];
113 if ( $params['end'] ) {
114 $options['end'] = $params['end'];
118 if ( !is_null( $params['continue'] ) ) {
119 $cont = explode( '|', $params['continue'] );
120 $this->dieContinueUsageIf( count( $cont ) != 2 );
121 $continueTimestamp = $cont[0];
122 $continueId = (int)$cont[1];
123 $this->dieContinueUsageIf( $continueId != $cont[1] );
124 $startFrom = [ $continueTimestamp, $continueId ];
127 if ( $wlowner !== $user ) {
128 $options['watchlistOwner'] = $wlowner;
129 $options['watchlistOwnerToken'] = $params['token'];
132 if ( !is_null( $params['namespace'] ) ) {
133 $options['namespaceIds'] = $params['namespace'];
136 if ( $params['allrev'] ) {
137 $options['allRevisions'] = true;
140 if ( !is_null( $params['show'] ) ) {
141 $show = array_flip( $params['show'] );
143 /* Check for conflicting parameters. */
144 if ( $this->showParamsConflicting( $show ) ) {
145 $this->dieWithError( 'apierror-show' );
148 // Check permissions.
149 if ( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
150 ||
isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] )
152 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
153 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
157 $options['filters'] = array_keys( $show );
160 if ( !is_null( $params['type'] ) ) {
162 $rcTypes = RecentChange
::parseToRCType( $params['type'] );
164 $options['rcTypes'] = $rcTypes;
166 } catch ( Exception
$e ) {
167 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
171 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
172 if ( !is_null( $params['user'] ) ) {
173 $options['onlyByUser'] = $params['user'];
175 if ( !is_null( $params['excludeuser'] ) ) {
176 $options['notByUser'] = $params['excludeuser'];
179 $options['limit'] = $params['limit'];
181 Hooks
::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
182 $this, $params, &$options
187 $watchedItemQuery = MediaWikiServices
::getInstance()->getWatchedItemQueryService();
188 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
190 foreach ( $items as list( $watchedItem, $recentChangeInfo ) ) {
191 /** @var WatchedItem $watchedItem */
192 if ( is_null( $resultPageSet ) ) {
193 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
194 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
196 $startFrom = [ $recentChangeInfo['rc_timestamp'], $recentChangeInfo['rc_id'] ];
200 if ( $params['allrev'] ) {
201 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
203 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
208 if ( $startFrom !== null ) {
209 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
212 if ( is_null( $resultPageSet ) ) {
213 $this->getResult()->addIndexedTagName(
214 [ 'query', $this->getModuleName() ],
217 } elseif ( $params['allrev'] ) {
218 $resultPageSet->populateFromRevisionIDs( $ids );
220 $resultPageSet->populateFromPageIDs( $ids );
224 private function getFieldsToInclude() {
226 if ( $this->fld_flags
) {
227 $includeFields[] = WatchedItemQueryService
::INCLUDE_FLAGS
;
229 if ( $this->fld_user ||
$this->fld_userid
) {
230 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER_ID
;
232 if ( $this->fld_user
) {
233 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER
;
235 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
236 $includeFields[] = WatchedItemQueryService
::INCLUDE_COMMENT
;
238 if ( $this->fld_patrol
) {
239 $includeFields[] = WatchedItemQueryService
::INCLUDE_PATROL_INFO
;
241 if ( $this->fld_sizes
) {
242 $includeFields[] = WatchedItemQueryService
::INCLUDE_SIZES
;
244 if ( $this->fld_loginfo
) {
245 $includeFields[] = WatchedItemQueryService
::INCLUDE_LOG_INFO
;
247 if ( $this->fld_tags
) {
248 $includeFields[] = WatchedItemQueryService
::INCLUDE_TAGS
;
250 return $includeFields;
253 private function showParamsConflicting( array $show ) {
254 return ( isset( $show[WatchedItemQueryService
::FILTER_MINOR
] )
255 && isset( $show[WatchedItemQueryService
::FILTER_NOT_MINOR
] ) )
256 ||
( isset( $show[WatchedItemQueryService
::FILTER_BOT
] )
257 && isset( $show[WatchedItemQueryService
::FILTER_NOT_BOT
] ) )
258 ||
( isset( $show[WatchedItemQueryService
::FILTER_ANON
] )
259 && isset( $show[WatchedItemQueryService
::FILTER_NOT_ANON
] ) )
260 ||
( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
261 && isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] ) )
262 ||
( isset( $show[WatchedItemQueryService
::FILTER_UNREAD
] )
263 && isset( $show[WatchedItemQueryService
::FILTER_NOT_UNREAD
] ) );
266 private function extractOutputData( WatchedItem
$watchedItem, array $recentChangeInfo ) {
267 /* Determine the title of the page that has been changed. */
268 $title = Title
::newFromLinkTarget( $watchedItem->getLinkTarget() );
269 $user = $this->getUser();
271 /* Our output data. */
273 $type = intval( $recentChangeInfo['rc_type'] );
274 $vals['type'] = RecentChange
::parseFromRCType( $type );
277 /* Create a new entry in the result for the title. */
278 if ( $this->fld_title ||
$this->fld_ids
) {
279 // These should already have been filtered out of the query, but just in case.
280 if ( $type === RC_LOG
&& ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) ) {
281 $vals['actionhidden'] = true;
284 if ( $type !== RC_LOG ||
285 LogEventsList
::userCanBitfield(
286 $recentChangeInfo['rc_deleted'],
287 LogPage
::DELETED_ACTION
,
291 if ( $this->fld_title
) {
292 ApiQueryBase
::addTitleInfo( $vals, $title );
294 if ( $this->fld_ids
) {
295 $vals['pageid'] = intval( $recentChangeInfo['rc_cur_id'] );
296 $vals['revid'] = intval( $recentChangeInfo['rc_this_oldid'] );
297 $vals['old_revid'] = intval( $recentChangeInfo['rc_last_oldid'] );
302 /* Add user data and 'anon' flag, if user is anonymous. */
303 if ( $this->fld_user ||
$this->fld_userid
) {
304 if ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_USER
) {
305 $vals['userhidden'] = true;
308 if ( Revision
::userCanBitfield(
309 $recentChangeInfo['rc_deleted'],
310 Revision
::DELETED_USER
,
313 if ( $this->fld_userid
) {
314 $vals['userid'] = (int)$recentChangeInfo['rc_user'];
315 // for backwards compatibility
316 $vals['user'] = (int)$recentChangeInfo['rc_user'];
319 if ( $this->fld_user
) {
320 $vals['user'] = $recentChangeInfo['rc_user_text'];
323 if ( !$recentChangeInfo['rc_user'] ) {
324 $vals['anon'] = true;
329 /* Add flags, such as new, minor, bot. */
330 if ( $this->fld_flags
) {
331 $vals['bot'] = (bool)$recentChangeInfo['rc_bot'];
332 $vals['new'] = $recentChangeInfo['rc_type'] == RC_NEW
;
333 $vals['minor'] = (bool)$recentChangeInfo['rc_minor'];
336 /* Add sizes of each revision. (Only available on 1.10+) */
337 if ( $this->fld_sizes
) {
338 $vals['oldlen'] = intval( $recentChangeInfo['rc_old_len'] );
339 $vals['newlen'] = intval( $recentChangeInfo['rc_new_len'] );
342 /* Add the timestamp. */
343 if ( $this->fld_timestamp
) {
344 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $recentChangeInfo['rc_timestamp'] );
347 if ( $this->fld_notificationtimestamp
) {
348 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
350 : wfTimestamp( TS_ISO_8601
, $watchedItem->getNotificationTimestamp() );
353 /* Add edit summary / log summary. */
354 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
355 if ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_COMMENT
) {
356 $vals['commenthidden'] = true;
359 if ( Revision
::userCanBitfield(
360 $recentChangeInfo['rc_deleted'],
361 Revision
::DELETED_COMMENT
,
364 $comment = $this->commentStore
->getComment( $recentChangeInfo )->text
;
365 if ( $this->fld_comment
) {
366 $vals['comment'] = $comment;
369 if ( $this->fld_parsedcomment
) {
370 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
375 /* Add the patrolled flag */
376 if ( $this->fld_patrol
) {
377 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
378 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( (object)$recentChangeInfo, $user );
381 if ( $this->fld_loginfo
&& $recentChangeInfo['rc_type'] == RC_LOG
) {
382 if ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) {
383 $vals['actionhidden'] = true;
386 if ( LogEventsList
::userCanBitfield(
387 $recentChangeInfo['rc_deleted'],
388 LogPage
::DELETED_ACTION
,
391 $vals['logid'] = intval( $recentChangeInfo['rc_logid'] );
392 $vals['logtype'] = $recentChangeInfo['rc_log_type'];
393 $vals['logaction'] = $recentChangeInfo['rc_log_action'];
394 $vals['logparams'] = LogFormatter
::newFromRow( $recentChangeInfo )->formatParametersForApi();
398 if ( $this->fld_tags
) {
399 if ( $recentChangeInfo['rc_tags'] ) {
400 $tags = explode( ',', $recentChangeInfo['rc_tags'] );
401 ApiResult
::setIndexedTagName( $tags, 'tag' );
402 $vals['tags'] = $tags;
408 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_RESTRICTED
) ) {
409 $vals['suppressed'] = true;
412 Hooks
::run( 'ApiQueryWatchlistExtractOutputData', [
413 $this, $watchedItem, $recentChangeInfo, &$vals
419 public function getAllowedParams() {
423 ApiBase
::PARAM_TYPE
=> 'timestamp'
426 ApiBase
::PARAM_TYPE
=> 'timestamp'
429 ApiBase
::PARAM_ISMULTI
=> true,
430 ApiBase
::PARAM_TYPE
=> 'namespace'
433 ApiBase
::PARAM_TYPE
=> 'user',
436 ApiBase
::PARAM_TYPE
=> 'user',
439 ApiBase
::PARAM_DFLT
=> 'older',
440 ApiBase
::PARAM_TYPE
=> [
444 ApiHelp
::PARAM_HELP_MSG
=> 'api-help-param-direction',
447 ApiBase
::PARAM_DFLT
=> 10,
448 ApiBase
::PARAM_TYPE
=> 'limit',
449 ApiBase
::PARAM_MIN
=> 1,
450 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
451 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
454 ApiBase
::PARAM_ISMULTI
=> true,
455 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
456 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
457 ApiBase
::PARAM_TYPE
=> [
468 'notificationtimestamp',
474 ApiBase
::PARAM_ISMULTI
=> true,
475 ApiBase
::PARAM_TYPE
=> [
476 WatchedItemQueryService
::FILTER_MINOR
,
477 WatchedItemQueryService
::FILTER_NOT_MINOR
,
478 WatchedItemQueryService
::FILTER_BOT
,
479 WatchedItemQueryService
::FILTER_NOT_BOT
,
480 WatchedItemQueryService
::FILTER_ANON
,
481 WatchedItemQueryService
::FILTER_NOT_ANON
,
482 WatchedItemQueryService
::FILTER_PATROLLED
,
483 WatchedItemQueryService
::FILTER_NOT_PATROLLED
,
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';