MediaWiki  master
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,
74  Title $title,
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,
94  Title $title,
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 }
$watchlistMaxDuration
string $watchlistMaxDuration
Relative maximum expiry.
Definition: ApiWatchlistTrait.php:23
getExpiryFromParams
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
Definition: ApiWatchlistTrait.php:135
Wikimedia\ParamValidator\TypeDef\ExpiryDef
Type definition for expiry timestamps.
Definition: ExpiryDef.php:16
setWatch
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.
Definition: ApiWatchlistTrait.php:72
$title
$title
Definition: testCompression.php:38
getWatchlistValue
getWatchlistValue(string $watchlist, Title $title, User $user, ?string $userOption=null)
Return true if we're to watch the page, false if not.
Definition: ApiWatchlistTrait.php:92
WatchAction\doWatchOrUnwatch
static doWatchOrUnwatch( $watch, Title $title, User $user, string $expiry=null)
Watch or unwatch a page.
Definition: WatchAction.php:211
ApiWatchlistTrait
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...
Definition: ApiWatchlistTrait.php:17
Title
Represents a title within MediaWiki.
Definition: Title.php:41
getWatchlistParams
getWatchlistParams(array $watchOptions=[])
Get additional allow params specific to watchlisting.
Definition: ApiWatchlistTrait.php:35
ApiResult\formatExpiry
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
Definition: ApiResult.php:1193
User\IGNORE_USER_RIGHTS
const IGNORE_USER_RIGHTS
Definition: User.php:89
User\isWatched
isWatched( $title, $checkRights=self::CHECK_USER_RIGHTS)
Check the watched status of an article.
Definition: User.php:3113
User\getBoolOption
getBoolOption( $oname)
Get the user's current setting for a given option, as a boolean value.
Definition: User.php:2590
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42