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 private $commentStore;
39 public function __construct( ApiQuery
$query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'wl' );
43 public function execute() {
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
51 private $fld_ids = false, $fld_title = false, $fld_patrol = false,
52 $fld_flags = false, $fld_timestamp = false, $fld_user = false,
53 $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
54 $fld_notificationtimestamp = false, $fld_userid = false,
58 * @param ApiPageSet $resultPageSet
61 private function run( $resultPageSet = null ) {
62 $this->selectNamedDB( 'watchlist', DB_REPLICA
, 'watchlist' );
64 $params = $this->extractRequestParams();
66 $user = $this->getUser();
67 $wlowner = $this->getWatchlistUser( $params );
69 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
70 $prop = array_flip( $params['prop'] );
72 $this->fld_ids
= isset( $prop['ids'] );
73 $this->fld_title
= isset( $prop['title'] );
74 $this->fld_flags
= isset( $prop['flags'] );
75 $this->fld_user
= isset( $prop['user'] );
76 $this->fld_userid
= isset( $prop['userid'] );
77 $this->fld_comment
= isset( $prop['comment'] );
78 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
79 $this->fld_timestamp
= isset( $prop['timestamp'] );
80 $this->fld_sizes
= isset( $prop['sizes'] );
81 $this->fld_patrol
= isset( $prop['patrol'] );
82 $this->fld_notificationtimestamp
= isset( $prop['notificationtimestamp'] );
83 $this->fld_loginfo
= isset( $prop['loginfo'] );
85 if ( $this->fld_patrol
) {
86 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
87 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'patrol' );
91 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
92 $this->commentStore
= new CommentStore( 'rc_comment' );
97 'dir' => $params['dir'] === 'older'
98 ? WatchedItemQueryService
::DIR_OLDER
99 : WatchedItemQueryService
::DIR_NEWER
,
102 if ( is_null( $resultPageSet ) ) {
103 $options['includeFields'] = $this->getFieldsToInclude();
105 $options['usedInGenerator'] = true;
108 if ( $params['start'] ) {
109 $options['start'] = $params['start'];
111 if ( $params['end'] ) {
112 $options['end'] = $params['end'];
116 if ( !is_null( $params['continue'] ) ) {
117 $cont = explode( '|', $params['continue'] );
118 $this->dieContinueUsageIf( count( $cont ) != 2 );
119 $continueTimestamp = $cont[0];
120 $continueId = (int)$cont[1];
121 $this->dieContinueUsageIf( $continueId != $cont[1] );
122 $startFrom = [ $continueTimestamp, $continueId ];
125 if ( $wlowner !== $user ) {
126 $options['watchlistOwner'] = $wlowner;
127 $options['watchlistOwnerToken'] = $params['token'];
130 if ( !is_null( $params['namespace'] ) ) {
131 $options['namespaceIds'] = $params['namespace'];
134 if ( $params['allrev'] ) {
135 $options['allRevisions'] = true;
138 if ( !is_null( $params['show'] ) ) {
139 $show = array_flip( $params['show'] );
141 /* Check for conflicting parameters. */
142 if ( $this->showParamsConflicting( $show ) ) {
143 $this->dieWithError( 'apierror-show' );
146 // Check permissions.
147 if ( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
148 ||
isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] )
150 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
151 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
155 $options['filters'] = array_keys( $show );
158 if ( !is_null( $params['type'] ) ) {
160 $rcTypes = RecentChange
::parseToRCType( $params['type'] );
162 $options['rcTypes'] = $rcTypes;
164 } catch ( Exception
$e ) {
165 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
169 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
170 if ( !is_null( $params['user'] ) ) {
171 $options['onlyByUser'] = $params['user'];
173 if ( !is_null( $params['excludeuser'] ) ) {
174 $options['notByUser'] = $params['excludeuser'];
177 $options['limit'] = $params['limit'];
179 Hooks
::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
180 $this, $params, &$options
185 $watchedItemQuery = MediaWikiServices
::getInstance()->getWatchedItemQueryService();
186 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
188 foreach ( $items as list( $watchedItem, $recentChangeInfo ) ) {
189 /** @var WatchedItem $watchedItem */
190 if ( is_null( $resultPageSet ) ) {
191 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
192 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
194 $startFrom = [ $recentChangeInfo['rc_timestamp'], $recentChangeInfo['rc_id'] ];
198 if ( $params['allrev'] ) {
199 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
201 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
206 if ( $startFrom !== null ) {
207 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
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 $comment = $this->commentStore
->getComment( $recentChangeInfo )->text
;
363 if ( $this->fld_comment
) {
364 $vals['comment'] = $comment;
367 if ( $this->fld_parsedcomment
) {
368 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
373 /* Add the patrolled flag */
374 if ( $this->fld_patrol
) {
375 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
376 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( (object)$recentChangeInfo, $user );
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'] = intval( $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 ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_RESTRICTED
) ) {
397 $vals['suppressed'] = true;
400 Hooks
::run( 'ApiQueryWatchlistExtractOutputData', [
401 $this, $watchedItem, $recentChangeInfo, &$vals
407 public function getAllowedParams() {
411 ApiBase
::PARAM_TYPE
=> 'timestamp'
414 ApiBase
::PARAM_TYPE
=> 'timestamp'
417 ApiBase
::PARAM_ISMULTI
=> true,
418 ApiBase
::PARAM_TYPE
=> 'namespace'
421 ApiBase
::PARAM_TYPE
=> 'user',
424 ApiBase
::PARAM_TYPE
=> 'user',
427 ApiBase
::PARAM_DFLT
=> 'older',
428 ApiBase
::PARAM_TYPE
=> [
432 ApiHelp
::PARAM_HELP_MSG
=> 'api-help-param-direction',
435 ApiBase
::PARAM_DFLT
=> 10,
436 ApiBase
::PARAM_TYPE
=> 'limit',
437 ApiBase
::PARAM_MIN
=> 1,
438 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
439 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
442 ApiBase
::PARAM_ISMULTI
=> true,
443 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
444 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
445 ApiBase
::PARAM_TYPE
=> [
456 'notificationtimestamp',
461 ApiBase
::PARAM_ISMULTI
=> true,
462 ApiBase
::PARAM_TYPE
=> [
463 WatchedItemQueryService
::FILTER_MINOR
,
464 WatchedItemQueryService
::FILTER_NOT_MINOR
,
465 WatchedItemQueryService
::FILTER_BOT
,
466 WatchedItemQueryService
::FILTER_NOT_BOT
,
467 WatchedItemQueryService
::FILTER_ANON
,
468 WatchedItemQueryService
::FILTER_NOT_ANON
,
469 WatchedItemQueryService
::FILTER_PATROLLED
,
470 WatchedItemQueryService
::FILTER_NOT_PATROLLED
,
471 WatchedItemQueryService
::FILTER_UNREAD
,
472 WatchedItemQueryService
::FILTER_NOT_UNREAD
,
476 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
477 ApiBase
::PARAM_ISMULTI
=> true,
478 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
479 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
482 ApiBase
::PARAM_TYPE
=> 'user'
485 ApiBase
::PARAM_TYPE
=> 'string',
486 ApiBase
::PARAM_SENSITIVE
=> true,
489 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
494 protected function getExamplesMessages() {
496 'action=query&list=watchlist'
497 => 'apihelp-query+watchlist-example-simple',
498 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
499 => 'apihelp-query+watchlist-example-props',
500 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
501 => 'apihelp-query+watchlist-example-allrev',
502 'action=query&generator=watchlist&prop=info'
503 => 'apihelp-query+watchlist-example-generator',
504 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
505 => 'apihelp-query+watchlist-example-generator-rev',
506 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
507 => 'apihelp-query+watchlist-example-wlowner',
511 public function getHelpUrls() {
512 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist';