MediaWiki REL1_37
ApiWatchlistTrait.php
Go to the documentation of this file.
1<?php
2
9
22
24 private $watchlistExpiryEnabled;
25
28
31
34
35 private function initServices() {
36 if ( $this->watchlistManager !== null && $this->userOptionsLookup !== null ) {
37 return;
38 }
39 // This trait is used outside of core and therefor fallback to global state - T263904
40 $services = MediaWikiServices::getInstance();
41 if ( $this->watchlistManager === null ) {
42 $this->watchlistManager = $services->getWatchlistManager();
43 }
44 if ( $this->userOptionsLookup === null ) {
45 $this->userOptionsLookup = $services->getUserOptionsLookup();
46 }
47 }
48
59 protected function getWatchlistParams( array $watchOptions = [] ): array {
60 if ( !$watchOptions ) {
61 $watchOptions = [
62 'watch',
63 'unwatch',
64 'preferences',
65 'nochange',
66 ];
67 }
68
69 $result = [
70 'watchlist' => [
71 ParamValidator::PARAM_DEFAULT => 'preferences',
72 ParamValidator::PARAM_TYPE => $watchOptions,
73 ],
74 ];
75
76 if ( $this->watchlistExpiryEnabled ) {
77 $result['watchlistexpiry'] = [
78 ParamValidator::PARAM_TYPE => 'expiry',
79 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
80 ExpiryDef::PARAM_USE_MAX => true,
81 ];
82 }
83
84 return $result;
85 }
86
96 protected function setWatch(
97 string $watch,
99 User $user,
100 ?string $userOption = null,
101 ?string $expiry = null
102 ): void {
103 $value = $this->getWatchlistValue( $watch, $title, $user, $userOption );
104 $this->watchlistManager->setWatch( $value, $user, $title, $expiry );
105 }
106
116 protected function getWatchlistValue(
117 string $watchlist,
119 User $user,
120 ?string $userOption = null
121 ): bool {
122 $this->initServices();
123 $userWatching = $this->watchlistManager->isWatchedIgnoringRights( $user, $title );
124
125 switch ( $watchlist ) {
126 case 'watch':
127 return true;
128
129 case 'unwatch':
130 return false;
131
132 case 'preferences':
133 // If the user is already watching, don't bother checking
134 if ( $userWatching ) {
135 return true;
136 }
137 // If the user is a bot, act as 'nochange' to avoid big watchlists on single users
138 if ( $user->isBot() ) {
139 return $userWatching;
140 }
141 // If no user option was passed, use watchdefault and watchcreations
142 if ( $userOption === null ) {
143 return $this->userOptionsLookup->getBoolOption( $user, 'watchdefault' ) ||
144 $this->userOptionsLookup->getBoolOption( $user, 'watchcreations' ) &&
145 !$title->exists();
146 }
147
148 // Watch the article based on the user preference
149 return $this->userOptionsLookup->getBoolOption( $user, $userOption );
150
151 case 'nochange':
152 return $userWatching;
153
154 default:
155 return $userWatching;
156 }
157 }
158
164 protected function getExpiryFromParams( array $params ): ?string {
165 $watchlistExpiry = null;
166 if ( $this->watchlistExpiryEnabled && isset( $params['watchlistexpiry'] ) ) {
167 $watchlistExpiry = ApiResult::formatExpiry( $params['watchlistexpiry'] );
168 }
169
170 return $watchlistExpiry;
171 }
172
181 protected function getWatchlistExpiry(
184 UserIdentity $user
185 ): ?string {
186 $watchedItem = $store->getWatchedItem( $user, $title );
187
188 if ( $watchedItem ) {
189 $expiry = $watchedItem->getExpiry();
190
191 if ( $expiry !== null ) {
192 return ApiResult::formatExpiry( $expiry );
193 }
194 }
195
196 return null;
197 }
198}
getWatchlistValue(string $watchlist, Title $title, User $user, ?string $userOption=null)
Return true if we're to watch the page, false if not.
string $watchlistMaxDuration
Relative maximum expiry.
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.
WatchlistManager $watchlistManager
UserOptionsLookup $userOptionsLookup
initServices()
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
MediaWikiServices is the service locator for the application scope of MediaWiki.
Provides access to user options.
Represents a title within MediaWiki.
Definition Title.php:48
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:69
isBot()
Definition User.php:2994
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.