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,
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'] );
86 if ( $this->fld_patrol
) {
87 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
88 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'patrol' );
92 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
93 $this->commentStore
= new CommentStore( 'rc_comment' );
98 'dir' => $params['dir'] === 'older'
99 ? WatchedItemQueryService
::DIR_OLDER
100 : WatchedItemQueryService
::DIR_NEWER
,
103 if ( is_null( $resultPageSet ) ) {
104 $options['includeFields'] = $this->getFieldsToInclude();
106 $options['usedInGenerator'] = true;
109 if ( $params['start'] ) {
110 $options['start'] = $params['start'];
112 if ( $params['end'] ) {
113 $options['end'] = $params['end'];
117 if ( !is_null( $params['continue'] ) ) {
118 $cont = explode( '|', $params['continue'] );
119 $this->dieContinueUsageIf( count( $cont ) != 2 );
120 $continueTimestamp = $cont[0];
121 $continueId = (int)$cont[1];
122 $this->dieContinueUsageIf( $continueId != $cont[1] );
123 $startFrom = [ $continueTimestamp, $continueId ];
126 if ( $wlowner !== $user ) {
127 $options['watchlistOwner'] = $wlowner;
128 $options['watchlistOwnerToken'] = $params['token'];
131 if ( !is_null( $params['namespace'] ) ) {
132 $options['namespaceIds'] = $params['namespace'];
135 if ( $params['allrev'] ) {
136 $options['allRevisions'] = true;
139 if ( !is_null( $params['show'] ) ) {
140 $show = array_flip( $params['show'] );
142 /* Check for conflicting parameters. */
143 if ( $this->showParamsConflicting( $show ) ) {
144 $this->dieWithError( 'apierror-show' );
147 // Check permissions.
148 if ( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
149 ||
isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] )
151 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
152 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
156 $options['filters'] = array_keys( $show );
159 if ( !is_null( $params['type'] ) ) {
161 $rcTypes = RecentChange
::parseToRCType( $params['type'] );
163 $options['rcTypes'] = $rcTypes;
165 } catch ( Exception
$e ) {
166 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
170 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
171 if ( !is_null( $params['user'] ) ) {
172 $options['onlyByUser'] = $params['user'];
174 if ( !is_null( $params['excludeuser'] ) ) {
175 $options['notByUser'] = $params['excludeuser'];
178 $options['limit'] = $params['limit'];
180 Hooks
::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
181 $this, $params, &$options
186 $watchedItemQuery = MediaWikiServices
::getInstance()->getWatchedItemQueryService();
187 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
189 foreach ( $items as list( $watchedItem, $recentChangeInfo ) ) {
190 /** @var WatchedItem $watchedItem */
191 if ( is_null( $resultPageSet ) ) {
192 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
193 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
195 $startFrom = [ $recentChangeInfo['rc_timestamp'], $recentChangeInfo['rc_id'] ];
199 if ( $params['allrev'] ) {
200 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
202 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
207 if ( $startFrom !== null ) {
208 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
211 if ( is_null( $resultPageSet ) ) {
212 $this->getResult()->addIndexedTagName(
213 [ 'query', $this->getModuleName() ],
216 } elseif ( $params['allrev'] ) {
217 $resultPageSet->populateFromRevisionIDs( $ids );
219 $resultPageSet->populateFromPageIDs( $ids );
223 private function getFieldsToInclude() {
225 if ( $this->fld_flags
) {
226 $includeFields[] = WatchedItemQueryService
::INCLUDE_FLAGS
;
228 if ( $this->fld_user ||
$this->fld_userid
) {
229 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER_ID
;
231 if ( $this->fld_user
) {
232 $includeFields[] = WatchedItemQueryService
::INCLUDE_USER
;
234 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
235 $includeFields[] = WatchedItemQueryService
::INCLUDE_COMMENT
;
237 if ( $this->fld_patrol
) {
238 $includeFields[] = WatchedItemQueryService
::INCLUDE_PATROL_INFO
;
240 if ( $this->fld_sizes
) {
241 $includeFields[] = WatchedItemQueryService
::INCLUDE_SIZES
;
243 if ( $this->fld_loginfo
) {
244 $includeFields[] = WatchedItemQueryService
::INCLUDE_LOG_INFO
;
246 return $includeFields;
249 private function showParamsConflicting( array $show ) {
250 return ( isset( $show[WatchedItemQueryService
::FILTER_MINOR
] )
251 && isset( $show[WatchedItemQueryService
::FILTER_NOT_MINOR
] ) )
252 ||
( isset( $show[WatchedItemQueryService
::FILTER_BOT
] )
253 && isset( $show[WatchedItemQueryService
::FILTER_NOT_BOT
] ) )
254 ||
( isset( $show[WatchedItemQueryService
::FILTER_ANON
] )
255 && isset( $show[WatchedItemQueryService
::FILTER_NOT_ANON
] ) )
256 ||
( isset( $show[WatchedItemQueryService
::FILTER_PATROLLED
] )
257 && isset( $show[WatchedItemQueryService
::FILTER_NOT_PATROLLED
] ) )
258 ||
( isset( $show[WatchedItemQueryService
::FILTER_UNREAD
] )
259 && isset( $show[WatchedItemQueryService
::FILTER_NOT_UNREAD
] ) );
262 private function extractOutputData( WatchedItem
$watchedItem, array $recentChangeInfo ) {
263 /* Determine the title of the page that has been changed. */
264 $title = Title
::newFromLinkTarget( $watchedItem->getLinkTarget() );
265 $user = $this->getUser();
267 /* Our output data. */
269 $type = intval( $recentChangeInfo['rc_type'] );
270 $vals['type'] = RecentChange
::parseFromRCType( $type );
273 /* Create a new entry in the result for the title. */
274 if ( $this->fld_title ||
$this->fld_ids
) {
275 // These should already have been filtered out of the query, but just in case.
276 if ( $type === RC_LOG
&& ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) ) {
277 $vals['actionhidden'] = true;
280 if ( $type !== RC_LOG ||
281 LogEventsList
::userCanBitfield(
282 $recentChangeInfo['rc_deleted'],
283 LogPage
::DELETED_ACTION
,
287 if ( $this->fld_title
) {
288 ApiQueryBase
::addTitleInfo( $vals, $title );
290 if ( $this->fld_ids
) {
291 $vals['pageid'] = intval( $recentChangeInfo['rc_cur_id'] );
292 $vals['revid'] = intval( $recentChangeInfo['rc_this_oldid'] );
293 $vals['old_revid'] = intval( $recentChangeInfo['rc_last_oldid'] );
298 /* Add user data and 'anon' flag, if user is anonymous. */
299 if ( $this->fld_user ||
$this->fld_userid
) {
300 if ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_USER
) {
301 $vals['userhidden'] = true;
304 if ( Revision
::userCanBitfield(
305 $recentChangeInfo['rc_deleted'],
306 Revision
::DELETED_USER
,
309 if ( $this->fld_userid
) {
310 $vals['userid'] = (int)$recentChangeInfo['rc_user'];
311 // for backwards compatibility
312 $vals['user'] = (int)$recentChangeInfo['rc_user'];
315 if ( $this->fld_user
) {
316 $vals['user'] = $recentChangeInfo['rc_user_text'];
319 if ( !$recentChangeInfo['rc_user'] ) {
320 $vals['anon'] = true;
325 /* Add flags, such as new, minor, bot. */
326 if ( $this->fld_flags
) {
327 $vals['bot'] = (bool)$recentChangeInfo['rc_bot'];
328 $vals['new'] = $recentChangeInfo['rc_type'] == RC_NEW
;
329 $vals['minor'] = (bool)$recentChangeInfo['rc_minor'];
332 /* Add sizes of each revision. (Only available on 1.10+) */
333 if ( $this->fld_sizes
) {
334 $vals['oldlen'] = intval( $recentChangeInfo['rc_old_len'] );
335 $vals['newlen'] = intval( $recentChangeInfo['rc_new_len'] );
338 /* Add the timestamp. */
339 if ( $this->fld_timestamp
) {
340 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $recentChangeInfo['rc_timestamp'] );
343 if ( $this->fld_notificationtimestamp
) {
344 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
346 : wfTimestamp( TS_ISO_8601
, $watchedItem->getNotificationTimestamp() );
349 /* Add edit summary / log summary. */
350 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
351 if ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_COMMENT
) {
352 $vals['commenthidden'] = true;
355 if ( Revision
::userCanBitfield(
356 $recentChangeInfo['rc_deleted'],
357 Revision
::DELETED_COMMENT
,
360 $comment = $this->commentStore
->getComment( $recentChangeInfo )->text
;
361 if ( $this->fld_comment
) {
362 $vals['comment'] = $comment;
365 if ( $this->fld_parsedcomment
) {
366 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
371 /* Add the patrolled flag */
372 if ( $this->fld_patrol
) {
373 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
374 $vals['unpatrolled'] = ChangesList
::isUnpatrolled( (object)$recentChangeInfo, $user );
377 if ( $this->fld_loginfo
&& $recentChangeInfo['rc_type'] == RC_LOG
) {
378 if ( $recentChangeInfo['rc_deleted'] & LogPage
::DELETED_ACTION
) {
379 $vals['actionhidden'] = true;
382 if ( LogEventsList
::userCanBitfield(
383 $recentChangeInfo['rc_deleted'],
384 LogPage
::DELETED_ACTION
,
387 $vals['logid'] = intval( $recentChangeInfo['rc_logid'] );
388 $vals['logtype'] = $recentChangeInfo['rc_log_type'];
389 $vals['logaction'] = $recentChangeInfo['rc_log_action'];
390 $vals['logparams'] = LogFormatter
::newFromRow( $recentChangeInfo )->formatParametersForApi();
394 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision
::DELETED_RESTRICTED
) ) {
395 $vals['suppressed'] = true;
398 Hooks
::run( 'ApiQueryWatchlistExtractOutputData', [
399 $this, $watchedItem, $recentChangeInfo, &$vals
405 public function getAllowedParams() {
409 ApiBase
::PARAM_TYPE
=> 'timestamp'
412 ApiBase
::PARAM_TYPE
=> 'timestamp'
415 ApiBase
::PARAM_ISMULTI
=> true,
416 ApiBase
::PARAM_TYPE
=> 'namespace'
419 ApiBase
::PARAM_TYPE
=> 'user',
422 ApiBase
::PARAM_TYPE
=> 'user',
425 ApiBase
::PARAM_DFLT
=> 'older',
426 ApiBase
::PARAM_TYPE
=> [
430 ApiHelp
::PARAM_HELP_MSG
=> 'api-help-param-direction',
433 ApiBase
::PARAM_DFLT
=> 10,
434 ApiBase
::PARAM_TYPE
=> 'limit',
435 ApiBase
::PARAM_MIN
=> 1,
436 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
437 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
440 ApiBase
::PARAM_ISMULTI
=> true,
441 ApiBase
::PARAM_DFLT
=> 'ids|title|flags',
442 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
443 ApiBase
::PARAM_TYPE
=> [
454 'notificationtimestamp',
459 ApiBase
::PARAM_ISMULTI
=> true,
460 ApiBase
::PARAM_TYPE
=> [
461 WatchedItemQueryService
::FILTER_MINOR
,
462 WatchedItemQueryService
::FILTER_NOT_MINOR
,
463 WatchedItemQueryService
::FILTER_BOT
,
464 WatchedItemQueryService
::FILTER_NOT_BOT
,
465 WatchedItemQueryService
::FILTER_ANON
,
466 WatchedItemQueryService
::FILTER_NOT_ANON
,
467 WatchedItemQueryService
::FILTER_PATROLLED
,
468 WatchedItemQueryService
::FILTER_NOT_PATROLLED
,
469 WatchedItemQueryService
::FILTER_UNREAD
,
470 WatchedItemQueryService
::FILTER_NOT_UNREAD
,
474 ApiBase
::PARAM_DFLT
=> 'edit|new|log|categorize',
475 ApiBase
::PARAM_ISMULTI
=> true,
476 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
477 ApiBase
::PARAM_TYPE
=> RecentChange
::getChangeTypes()
480 ApiBase
::PARAM_TYPE
=> 'user'
483 ApiBase
::PARAM_TYPE
=> 'string',
484 ApiBase
::PARAM_SENSITIVE
=> true,
487 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
492 protected function getExamplesMessages() {
494 'action=query&list=watchlist'
495 => 'apihelp-query+watchlist-example-simple',
496 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
497 => 'apihelp-query+watchlist-example-props',
498 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
499 => 'apihelp-query+watchlist-example-allrev',
500 'action=query&generator=watchlist&prop=info'
501 => 'apihelp-query+watchlist-example-generator',
502 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
503 => 'apihelp-query+watchlist-example-generator-rev',
504 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
505 => 'apihelp-query+watchlist-example-wlowner',
509 public function getHelpUrls() {
510 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist';