MediaWiki master
ApiWatchlistTrait.php
Go to the documentation of this file.
1<?php
2
11
24
26 private $watchlistExpiryEnabled;
27
29 private $watchlistMaxDuration;
30
31 private WatchlistManager $watchlistManager;
32 private UserOptionsLookup $userOptionsLookup;
33
34 private function initServices() {
35 // @phan-suppress-next-line PhanRedundantCondition Phan trusts the type hints too much
36 if ( isset( $this->watchlistManager ) && isset( $this->userOptionsLookup ) ) {
37 return;
38 }
39 // This trait is used outside of core and therefor fallback to global state - T263904
40 $services = MediaWikiServices::getInstance();
41 $this->watchlistManager ??= $services->getWatchlistManager();
42 $this->userOptionsLookup ??= $services->getUserOptionsLookup();
43 }
44
55 protected function getWatchlistParams( array $watchOptions = [] ): array {
56 if ( !$watchOptions ) {
57 $watchOptions = [
58 'watch',
59 'unwatch',
60 'preferences',
61 'nochange',
62 ];
63 }
64
65 $result = [
66 'watchlist' => [
67 ParamValidator::PARAM_DEFAULT => 'preferences',
68 ParamValidator::PARAM_TYPE => $watchOptions,
69 ],
70 ];
71
72 if ( $this->watchlistExpiryEnabled ) {
73 $result['watchlistexpiry'] = [
74 ParamValidator::PARAM_TYPE => 'expiry',
75 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
76 ExpiryDef::PARAM_USE_MAX => true,
77 ];
78 }
79
80 return $result;
81 }
82
92 protected function setWatch(
93 string $watch,
94 Title $title,
95 User $user,
96 ?string $userOption = null,
97 ?string $expiry = null
98 ): void {
99 $value = $this->getWatchlistValue( $watch, $title, $user, $userOption );
100 $this->watchlistManager->setWatch( $value, $user, $title, $expiry );
101 }
102
112 protected function getWatchlistValue(
113 string $watchlist,
114 Title $title,
115 User $user,
116 ?string $userOption = null
117 ): bool {
118 $this->initServices();
119 $userWatching = $this->watchlistManager->isWatchedIgnoringRights( $user, $title );
120
121 switch ( $watchlist ) {
122 case 'watch':
123 return true;
124
125 case 'unwatch':
126 return false;
127
128 case 'preferences':
129 // If the user is already watching, don't bother checking
130 if ( $userWatching ) {
131 return true;
132 }
133 // If the user is a bot, act as 'nochange' to avoid big watchlists on single users
134 if ( $user->isBot() ) {
135 return $userWatching;
136 }
137 // If no user option was passed, use watchdefault and watchcreations
138 if ( $userOption === null ) {
139 return $this->userOptionsLookup->getBoolOption( $user, 'watchdefault' ) ||
140 ( $this->userOptionsLookup->getBoolOption( $user, 'watchcreations' ) && !$title->exists() );
141 }
142
143 // Watch the article based on the user preference
144 return $this->userOptionsLookup->getBoolOption( $user, $userOption );
145
146 // case 'nochange':
147 default:
148 return $userWatching;
149 }
150 }
151
157 protected function getExpiryFromParams( array $params ): ?string {
158 $watchlistExpiry = null;
159 if ( $this->watchlistExpiryEnabled && isset( $params['watchlistexpiry'] ) ) {
160 $watchlistExpiry = ApiResult::formatExpiry( $params['watchlistexpiry'] );
161 }
162
163 return $watchlistExpiry;
164 }
165
174 protected function getWatchlistExpiry(
176 Title $title,
177 UserIdentity $user
178 ): ?string {
179 $watchedItem = $store->getWatchedItem( $user, $title );
180
181 if ( $watchedItem ) {
182 $expiry = $watchedItem->getExpiry();
183
184 if ( $expiry !== null ) {
185 return ApiResult::formatExpiry( $expiry );
186 }
187 }
188
189 return null;
190 }
191}
getWatchlistValue(string $watchlist, Title $title, User $user, ?string $userOption=null)
Return true if we're to watch the page, false if not.
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
setWatch(string $watch, Title $title, User $user, ?string $userOption=null, ?string $expiry=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
getWatchlistExpiry(WatchedItemStoreInterface $store, Title $title, UserIdentity $user)
Get existing expiry from the database.
getWatchlistParams(array $watchOptions=[])
Get additional allow params specific to watchlisting.
array $params
The job parameters.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:78
exists( $flags=0)
Check if page exists.
Definition Title.php:3201
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 representing user identity.