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