MediaWiki REL1_39
ApiWatchlistTrait.php
Go to the documentation of this file.
1<?php
2
9
22
24 private $watchlistExpiryEnabled;
25
27 private $watchlistMaxDuration;
28
30 private $watchlistManager;
31
33 private $userOptionsLookup;
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 default:
153 return $userWatching;
154 }
155 }
156
162 protected function getExpiryFromParams( array $params ): ?string {
163 $watchlistExpiry = null;
164 if ( $this->watchlistExpiryEnabled && isset( $params['watchlistexpiry'] ) ) {
165 $watchlistExpiry = ApiResult::formatExpiry( $params['watchlistexpiry'] );
166 }
167
168 return $watchlistExpiry;
169 }
170
179 protected function getWatchlistExpiry(
182 UserIdentity $user
183 ): ?string {
184 $watchedItem = $store->getWatchedItem( $user, $title );
185
186 if ( $watchedItem ) {
187 $expiry = $watchedItem->getExpiry();
188
189 if ( $expiry !== null ) {
190 return ApiResult::formatExpiry( $expiry );
191 }
192 }
193
194 return null;
195 }
196}
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.
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:82
Service locator for MediaWiki core services.
Provides access to user options.
Represents a title within MediaWiki.
Definition Title.php:49
internal since 1.36
Definition User.php:70
isBot()
Definition User.php:2327
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.