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_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'] );
83 if ( $this->fld_patrol
) {
84 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
85 $this->dieWithError( 'apierror-permissiondenied-patrolflag', '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'];
110 if ( !is_null( $params['continue'] ) ) {
111 $cont = explode( '|', $params['continue'] );
112 $this->dieContinueUsageIf( count( $cont ) != 2 );
113 $continueTimestamp = $cont[0];
114 $continueId = (int)$cont[1];
115 $this->dieContinueUsageIf( $continueId != $cont[1] );
116 $startFrom = [ $continueTimestamp, $continueId ];
119 if ( $wlowner !== $user ) {
120 $options['watchlistOwner'] = $wlowner;
121 $options['watchlistOwnerToken'] = $params['token'];
124 if ( !is_null( $params['namespace'] ) ) {
125 $options['namespaceIds'] = $params['namespace'];
128 if ( $params['allrev'] ) {
129 $options['allRevisions'] = true;
132 if ( !is_null( $params['show'] ) ) {
133 $show = array_flip( $params['show'] );
135 /* Check for conflicting parameters. */
136 if ( $this->showParamsConflicting( $show ) ) {
137 $this->dieWithError( 'apierror-show' );
140 // Check permissions.
141 if ( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
142 ||
isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] )
144 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
145 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
149 $options['filters'] = array_keys( $show );
152 if ( !is_null( $params['type'] ) ) {
154 $options['rcTypes'] = RecentChange
::parseToRCType( $params['type'] );
155 } catch ( Exception
$e ) {
156 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
160 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
161 if ( !is_null( $params['user'] ) ) {
162 $options['onlyByUser'] = $params['user'];
164 if ( !is_null( $params['excludeuser'] ) ) {
165 $options['notByUser'] = $params['excludeuser'];
168 $options['limit'] = $params['limit'];
170 Hooks
::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
171 $this, $params, &$options
176 $watchedItemQuery = MediaWikiServices
::getInstance()->getWatchedItemQueryService();
177 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
179 foreach ( $items as list ( $watchedItem, $recentChangeInfo ) ) {
180 /** @var WatchedItem $watchedItem */
181 if ( is_null( $resultPageSet ) ) {
182 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
183 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
185 $startFrom = [ $recentChangeInfo['rc_timestamp'], $recentChangeInfo['rc_id'] ];
189 if ( $params['allrev'] ) {
190 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
192 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
197 if ( $startFrom !== null ) {
198 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
201 if ( is_null( $resultPageSet ) ) {
202 $this->getResult()->addIndexedTagName(
203 [ 'query', $this->getModuleName() ],
206 } elseif ( $params['allrev'] ) {
207 $resultPageSet->populateFromRevisionIDs( $ids );
209 $resultPageSet->populateFromPageIDs( $ids );
213 private function getFieldsToInclude() {
215 if ( $this->fld_flags
) {
216 $includeFields[] = WatchedItemQueryService
::INCLUDE_FLAGS
;
218 if ( $this->fld_user ||
$this->fld_userid
) {
219 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER_ID
;
221 if ( $this->fld_user
) {
222 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER
;
224 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
225 $includeFields[] = WatchedItemQueryService
::INCLUDE_COMMENT
;
227 if ( $this->fld_patrol
) {
228 $includeFields[] = WatchedItemQueryService
::INCLUDE_PATROL_INFO
;
230 if ( $this->fld_sizes
) {
231 $includeFields[] = WatchedItemQueryService
::INCLUDE_SIZES
;
233 if ( $this->fld_loginfo
) {
234 $includeFields[] = WatchedItemQueryService
::INCLUDE_LOG_INFO
;
236 return $includeFields;
239 private function showParamsConflicting( array $show ) {
240 return ( isset( $show[WatchedItemQueryService
::FILTER_MINOR
] )
241 && isset( $show[WatchedItemQueryService
::FILTER_NOT_MINOR
] ) )
242 ||
( isset( $show[WatchedItemQueryService
::FILTER_BOT
] )
243 && isset( $show[WatchedItemQueryService
::FILTER_NOT_BOT
] ) )
244 ||
( isset( $show[WatchedItemQueryService
::FILTER_ANON
] )
245 && isset( $show[WatchedItemQueryService
::FILTER_NOT_ANON
] ) )
246 ||
( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
247 && isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] ) )
248 ||
( isset( $show[WatchedItemQueryService
::FILTER_UNREAD
] )
249 && isset( $show[WatchedItemQueryService
::FILTER_NOT_UNREAD
] ) );
252 private function extractOutputData( WatchedItem
$watchedItem, array $recentChangeInfo ) {
253 /* Determine the title of the page that has been changed. */
254 $title = Title
::makeTitle(
255 $watchedItem->getLinkTarget()->getNamespace(),
256 $watchedItem->getLinkTarget()->getDBkey()
258 $user = $this->getUser();
260 /* Our output data. */
262 $type = intval( $recentChangeInfo['rc_type'] );
263 $vals['type'] = RecentChange
::parseFromRCType( $type );
266 /* Create a new entry in the result for the title. */
267 if ( $this->fld_title ||
$this->fld_ids
) {
268 // These should already have been filtered out of the query, but just in case.
269 if ( $type === RC_LOG
&& ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) ) {
270 $vals['actionhidden'] = true;
273 if ( $type !== RC_LOG ||
274 LogEventsList
::userCanBitfield(
275 $recentChangeInfo['rc_deleted'],
276 LogPage
::DELETED_ACTION
,
280 if ( $this->fld_title
) {
281 ApiQueryBase
::addTitleInfo( $vals, $title );
283 if ( $this->fld_ids
) {
284 $vals['pageid'] = intval( $recentChangeInfo['rc_cur_id'] );
285 $vals['revid'] = intval( $recentChangeInfo['rc_this_oldid'] );
286 $vals['old_revid'] = intval( $recentChangeInfo['rc_last_oldid'] );
291 /* Add user data and 'anon' flag, if user is anonymous. */
292 if ( $this->fld_user ||
$this->fld_userid
) {
293 if ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_USER
) {
294 $vals['userhidden'] = true;
297 if ( Revision
::userCanBitfield(
298 $recentChangeInfo['rc_deleted'],
299 Revision
::DELETED_USER
,
302 if ( $this->fld_userid
) {
303 $vals['userid'] = (int)$recentChangeInfo['rc_user'];
304 // for backwards compatibility
305 $vals['user'] = (int)$recentChangeInfo['rc_user'];
308 if ( $this->fld_user
) {
309 $vals['user'] = $recentChangeInfo['rc_user_text'];
312 if ( !$recentChangeInfo['rc_user'] ) {
313 $vals['anon'] = true;
318 /* Add flags, such as new, minor, bot. */
319 if ( $this->fld_flags
) {
320 $vals['bot'] = (bool)$recentChangeInfo['rc_bot'];
321 $vals['new'] = $recentChangeInfo['rc_type'] == RC_NEW
;
322 $vals['minor'] = (bool)$recentChangeInfo['rc_minor'];
325 /* Add sizes of each revision. (Only available on 1.10+) */
326 if ( $this->fld_sizes
) {
327 $vals['oldlen'] = intval( $recentChangeInfo['rc_old_len'] );
328 $vals['newlen'] = intval( $recentChangeInfo['rc_new_len'] );
331 /* Add the timestamp. */
332 if ( $this->fld_timestamp
) {
333 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $recentChangeInfo['rc_timestamp'] );
336 if ( $this->fld_notificationtimestamp
) {
337 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
339 : wfTimestamp( TS_ISO_8601
, $watchedItem->getNotificationTimestamp() );
342 /* Add edit summary / log summary. */
343 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
344 if ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_COMMENT
) {
345 $vals['commenthidden'] = true;
348 if ( Revision
::userCanBitfield(
349 $recentChangeInfo['rc_deleted'],
350 Revision
::DELETED_COMMENT
,
353 if ( $this->fld_comment
&& isset( $recentChangeInfo['rc_comment'] ) ) {
354 $vals['comment'] = $recentChangeInfo['rc_comment'];
357 if ( $this->fld_parsedcomment
&& isset( $recentChangeInfo['rc_comment'] ) ) {
358 $vals['parsedcomment'] = Linker
::formatComment( $recentChangeInfo['rc_comment'], $title );
363 /* Add the patrolled flag */
364 if ( $this->fld_patrol
) {
365 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
366 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( (object)$recentChangeInfo, $user );
369 if ( $this->fld_loginfo
&& $recentChangeInfo['rc_type'] == RC_LOG
) {
370 if ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) {
371 $vals['actionhidden'] = true;
374 if ( LogEventsList
::userCanBitfield(
375 $recentChangeInfo['rc_deleted'],
376 LogPage
::DELETED_ACTION
,
379 $vals['logid'] = intval( $recentChangeInfo['rc_logid'] );
380 $vals['logtype'] = $recentChangeInfo['rc_log_type'];
381 $vals['logaction'] = $recentChangeInfo['rc_log_action'];
382 $vals['logparams'] = LogFormatter
::newFromRow( $recentChangeInfo )->formatParametersForApi();
386 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_RESTRICTED
) ) {
387 $vals['suppressed'] = true;
390 Hooks
::run( 'ApiQueryWatchlistExtractOutputData', [
391 $this, $watchedItem, $recentChangeInfo, &$vals
397 public function getAllowedParams() {
401 ApiBase
::PARAM_TYPE
=> 'timestamp'
404 ApiBase
::PARAM_TYPE
=> 'timestamp'
407 ApiBase
::PARAM_ISMULTI
=> true,
408 ApiBase
::PARAM_TYPE
=> 'namespace'
411 ApiBase
::PARAM_TYPE
=> 'user',
414 ApiBase
::PARAM_TYPE
=> 'user',
417 ApiBase
::PARAM_DFLT
=> 'older',
418 ApiBase
::PARAM_TYPE
=> [
422 ApiHelp
::PARAM_HELP_MSG
=> 'api-help-param-direction',
425 ApiBase
::PARAM_DFLT
=> 10,
426 ApiBase
::PARAM_TYPE
=> 'limit',
427 ApiBase
::PARAM_MIN
=> 1,
428 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
429 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
432 ApiBase
::PARAM_ISMULTI
=> true,
433 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
434 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
435 ApiBase
::PARAM_TYPE
=> [
446 'notificationtimestamp',
451 ApiBase
::PARAM_ISMULTI
=> true,
452 ApiBase
::PARAM_TYPE
=> [
453 WatchedItemQueryService
::FILTER_MINOR
,
454 WatchedItemQueryService
::FILTER_NOT_MINOR
,
455 WatchedItemQueryService
::FILTER_BOT
,
456 WatchedItemQueryService
::FILTER_NOT_BOT
,
457 WatchedItemQueryService
::FILTER_ANON
,
458 WatchedItemQueryService
::FILTER_NOT_ANON
,
459 WatchedItemQueryService
::FILTER_PATROLLED
,
460 WatchedItemQueryService
::FILTER_NOT_PATROLLED
,
461 WatchedItemQueryService
::FILTER_UNREAD
,
462 WatchedItemQueryService
::FILTER_NOT_UNREAD
,
466 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
467 ApiBase
::PARAM_ISMULTI
=> true,
468 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
469 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
472 ApiBase
::PARAM_TYPE
=> 'user'
475 ApiBase
::PARAM_TYPE
=> 'string'
478 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
483 protected function getExamplesMessages() {
485 'action=query&list=watchlist'
486 => 'apihelp-query+watchlist-example-simple',
487 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
488 => 'apihelp-query+watchlist-example-props',
489 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
490 => 'apihelp-query+watchlist-example-allrev',
491 'action=query&generator=watchlist&prop=info'
492 => 'apihelp-query+watchlist-example-generator',
493 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
494 => 'apihelp-query+watchlist-example-generator-rev',
495 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
496 => 'apihelp-query+watchlist-example-wlowner',
500 public function getHelpUrls() {
501 return 'https://www.mediawiki.org/wiki/API:Watchlist';