MediaWiki master
ApiWatchlistTrait.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Api;
4
14
27
29 private $watchlistExpiryEnabled;
30
32 private $watchlistMaxDuration;
33
34 private WatchlistManager $watchlistManager;
35 private UserOptionsLookup $userOptionsLookup;
36
37 private function initServices() {
38 // @phan-suppress-next-line PhanRedundantCondition Phan trusts the type hints too much
39 if ( isset( $this->watchlistManager ) && isset( $this->userOptionsLookup ) ) {
40 return;
41 }
42 // This trait is used outside of core and therefor fallback to global state - T263904
44 $this->watchlistManager ??= $services->getWatchlistManager();
45 $this->userOptionsLookup ??= $services->getUserOptionsLookup();
46 }
47
58 protected function getWatchlistParams( array $watchOptions = [] ): array {
59 if ( !$watchOptions ) {
60 $watchOptions = [
61 'watch',
62 'unwatch',
63 'preferences',
64 'nochange',
65 ];
66 }
67
68 $result = [
69 'watchlist' => [
70 ParamValidator::PARAM_DEFAULT => 'preferences',
71 ParamValidator::PARAM_TYPE => $watchOptions,
72 ],
73 ];
74
75 if ( $this->watchlistExpiryEnabled ) {
76 $result['watchlistexpiry'] = [
77 ParamValidator::PARAM_TYPE => 'expiry',
78 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
79 ExpiryDef::PARAM_USE_MAX => true,
80 ];
81 }
82
83 return $result;
84 }
85
95 protected function setWatch(
96 string $watch,
97 PageIdentity $page,
98 User $user,
99 ?string $userOption = null,
100 ?string $expiry = null
101 ): void {
102 $value = $this->getWatchlistValue( $watch, $page, $user, $userOption );
103 $this->watchlistManager->setWatch( $value, $user, $page, $expiry );
104 }
105
115 protected function getWatchlistValue(
116 string $watchlist,
117 PageIdentity $page,
118 User $user,
119 ?string $userOption = null
120 ): bool {
121 $this->initServices();
122 $userWatching = $this->watchlistManager->isWatchedIgnoringRights( $user, $page );
123
124 switch ( $watchlist ) {
125 case 'watch':
126 return true;
127
128 case 'unwatch':
129 return false;
130
131 case 'preferences':
132 // If the user is already watching, don't bother checking
133 if ( $userWatching ) {
134 return true;
135 }
136 // If the user is a bot, act as 'nochange' to avoid big watchlists on single users
137 if ( $user->isBot() ) {
138 return $userWatching;
139 }
140 // If no user option was passed, use watchdefault and watchcreations
141 if ( $userOption === null ) {
142 return $this->userOptionsLookup->getBoolOption( $user, 'watchdefault' ) ||
143 ( $this->userOptionsLookup->getBoolOption( $user, 'watchcreations' ) && !$page->exists() );
144 }
145
146 // Watch the article based on the user preference
147 return $this->userOptionsLookup->getBoolOption( $user, $userOption );
148
149 // case 'nochange':
150 default:
151 return $userWatching;
152 }
153 }
154
160 protected function getExpiryFromParams( array $params ): ?string {
161 $watchlistExpiry = null;
162 if ( $this->watchlistExpiryEnabled && isset( $params['watchlistexpiry'] ) ) {
163 $watchlistExpiry = ApiResult::formatExpiry( $params['watchlistexpiry'] );
164 }
165
166 return $watchlistExpiry;
167 }
168
177 protected function getWatchlistExpiry(
179 PageIdentity $page,
180 UserIdentity $user
181 ): ?string {
182 $watchedItem = $store->getWatchedItem( $user, $page );
183
184 if ( $watchedItem ) {
185 $expiry = $watchedItem->getExpiry();
186
187 if ( $expiry !== null ) {
188 return ApiResult::formatExpiry( $expiry );
189 }
190 }
191
192 return null;
193 }
194}
195
197class_alias( ApiWatchlistTrait::class, 'ApiWatchlistTrait' );
array $params
The job parameters.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Provides access to user options.
internal since 1.36
Definition User.php:93
Service for formatting and validating API parameters.
Type definition for expiry timestamps.
Definition ExpiryDef.php:17
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...
Interface for objects (potentially) representing an editable wiki page.
exists()
Checks if the page currently exists.
Interface for objects representing user identity.
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
setWatch(string $watch, PageIdentity $page, User $user, ?string $userOption=null, ?string $expiry=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
getWatchlistValue(string $watchlist, PageIdentity $page, User $user, ?string $userOption=null)
Return true if we're to watch the page, false if not.
getWatchlistParams(array $watchOptions=[])
Get additional allow params specific to watchlisting.
getWatchlistExpiry(WatchedItemStoreInterface $store, PageIdentity $page, UserIdentity $user)
Get existing expiry from the database.