MediaWiki REL1_35
ApiWatchlistTrait.php
Go to the documentation of this file.
1<?php
2
5
18
20 private $watchlistExpiryEnabled;
21
24
35 protected function getWatchlistParams( array $watchOptions = [] ): array {
36 if ( !$watchOptions ) {
37 $watchOptions = [
38 'watch',
39 'unwatch',
40 'preferences',
41 'nochange',
42 ];
43 }
44
45 $result = [
46 'watchlist' => [
47 ParamValidator::PARAM_DEFAULT => 'preferences',
48 ParamValidator::PARAM_TYPE => $watchOptions,
49 ],
50 ];
51
52 if ( $this->watchlistExpiryEnabled ) {
53 $result['watchlistexpiry'] = [
54 ParamValidator::PARAM_TYPE => 'expiry',
55 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
56 ExpiryDef::PARAM_USE_MAX => true,
57 ];
58 }
59
60 return $result;
61 }
62
72 protected function setWatch(
73 string $watch,
75 User $user,
76 ?string $userOption = null,
77 ?string $expiry = null
78 ): void {
79 $value = $this->getWatchlistValue( $watch, $title, $user, $userOption );
80 WatchAction::doWatchOrUnwatch( $value, $title, $user, $expiry );
81 }
82
92 protected function getWatchlistValue(
93 string $watchlist,
95 User $user,
96 ?string $userOption = null
97 ): bool {
98 $userWatching = $user->isWatched( $title, User::IGNORE_USER_RIGHTS );
99
100 switch ( $watchlist ) {
101 case 'watch':
102 return true;
103
104 case 'unwatch':
105 return false;
106
107 case 'preferences':
108 // If the user is already watching, don't bother checking
109 if ( $userWatching ) {
110 return true;
111 }
112 // If no user option was passed, use watchdefault and watchcreations
113 if ( $userOption === null ) {
114 return $user->getBoolOption( 'watchdefault' ) ||
115 $user->getBoolOption( 'watchcreations' ) &&
116 !$title->exists();
117 }
118
119 // Watch the article based on the user preference
120 return $user->getBoolOption( $userOption );
121
122 case 'nochange':
123 return $userWatching;
124
125 default:
126 return $userWatching;
127 }
128 }
129
135 protected function getExpiryFromParams( array $params ): ?string {
136 $watchlistExpiry = null;
137 if ( $this->watchlistExpiryEnabled && isset( $params['watchlistexpiry'] ) ) {
138 $watchlistExpiry = ApiResult::formatExpiry( $params['watchlistexpiry'] );
139 }
140
141 return $watchlistExpiry;
142 }
143
152 protected function getWatchlistExpiry(
155 User $user
156 ): ?string {
157 $watchedItem = $store->getWatchedItem( $user, $title );
158
159 if ( $watchedItem ) {
160 $expiry = $watchedItem->getExpiry();
161
162 if ( $expiry !== null ) {
163 return ApiResult::formatExpiry( $expiry );
164 }
165 }
166
167 return null;
168 }
169}
getWatchlistExpiry(WatchedItemStoreInterface $store, Title $title, User $user)
Get existing expiry from the database.
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.
getWatchlistParams(array $watchOptions=[])
Get additional allow params specific to watchlisting.
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:85
Represents a title within MediaWiki.
Definition Title.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
getBoolOption( $oname)
Get the user's current setting for a given option, as a boolean value.
Definition User.php:2697
static doWatchOrUnwatch( $watch, Title $title, User $user, string $expiry=null)
Watch or unwatch a page.
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...