MediaWiki master
WatchAction.php
Go to the documentation of this file.
1<?php
29
35class WatchAction extends FormAction {
36
39
41 protected $expiryFormFieldName = 'expiry';
42
44 protected $watchedItem = false;
45
46 private WatchlistManager $watchlistManager;
47
56 public function __construct(
57 Article $article,
58 IContextSource $context,
59 WatchlistManager $watchlistManager,
60 WatchedItemStore $watchedItemStore
61 ) {
62 parent::__construct( $article, $context );
63 $this->watchlistExpiry = $this->getContext()->getConfig()->get( MainConfigNames::WatchlistExpiry );
64 if ( $this->watchlistExpiry ) {
65 // The watchedItem is only used in this action's form if $wgWatchlistExpiry is enabled.
66 $this->watchedItem = $watchedItemStore->getWatchedItem(
67 $this->getUser(),
68 $this->getTitle()
69 );
70 }
71 $this->watchlistManager = $watchlistManager;
72 }
73
74 public function getName() {
75 return 'watch';
76 }
77
78 public function requiresUnblock() {
79 return false;
80 }
81
82 protected function getDescription() {
83 return '';
84 }
85
86 public function onSubmit( $data ) {
87 // Even though we're never unwatching here, use WatchlistManager::setWatch()
88 // because it also checks for changed expiry.
89 $result = $this->watchlistManager->setWatch(
90 true,
91 $this->getAuthority(),
92 $this->getTitle(),
93 $this->getRequest()->getVal( 'wp' . $this->expiryFormFieldName )
94 );
95
96 return Status::wrap( $result );
97 }
98
105 protected function checkCanExecute( User $user ) {
106 if ( !$user->isRegistered()
107 || ( $user->isTemp() && !$user->isAllowed( 'editmywatchlist' ) )
108 ) {
109 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
110 }
111
112 parent::checkCanExecute( $user );
113 }
114
115 public function getRestriction() {
116 return 'editmywatchlist';
117 }
118
119 protected function usesOOUI() {
120 return true;
121 }
122
123 protected function getFormFields() {
124 // If watchlist expiry is not enabled, return a simple confirmation message.
125 if ( !$this->watchlistExpiry ) {
126 return [
127 'intro' => [
128 'type' => 'info',
129 'raw' => true,
130 'default' => $this->msg( 'confirm-watch-top' )->parse(),
131 ],
132 ];
133 }
134
135 // Otherwise, use a select-list of expiries.
136 $expiryOptions = static::getExpiryOptions( $this->getContext(), $this->watchedItem );
137 return [
138 $this->expiryFormFieldName => [
139 'type' => 'select',
140 'label-message' => 'confirm-watch-label',
141 'options' => $expiryOptions['options'],
142 'default' => $expiryOptions['default'],
143 ]
144 ];
145 }
146
159 public static function getExpiryOptions( MessageLocalizer $msgLocalizer, $watchedItem ) {
160 $expiryOptions = self::getExpiryOptionsFromMessage( $msgLocalizer );
161 $default = in_array( 'infinite', $expiryOptions )
162 ? 'infinite'
163 : current( $expiryOptions );
164 if ( $watchedItem instanceof WatchedItem && $watchedItem->getExpiry() ) {
165 // If it's already being temporarily watched,
166 // add the existing expiry as the default option in the dropdown.
167 $default = $watchedItem->getExpiry( TS_ISO_8601 );
168 $daysLeft = $watchedItem->getExpiryInDaysText( $msgLocalizer, true );
169 $expiryOptions = array_merge( [ $daysLeft => $default ], $expiryOptions );
170 }
171 return [
172 'options' => $expiryOptions,
173 'default' => $default,
174 ];
175 }
176
185 private static function getExpiryOptionsFromMessage(
186 MessageLocalizer $msgLocalizer, ?string $lang = null
187 ): array {
188 $expiryOptionsMsg = $msgLocalizer->msg( 'watchlist-expiry-options' );
189 $optionsText = !$lang ? $expiryOptionsMsg->text() : $expiryOptionsMsg->inLanguage( $lang )->text();
190 $options = XmlSelect::parseOptionsMessage(
191 $optionsText
192 );
193
194 $expiryOptions = [];
195 foreach ( $options as $label => $value ) {
196 if ( strtotime( $value ) || wfIsInfinity( $value ) ) {
197 $expiryOptions[$label] = $value;
198 }
199 }
200
201 // If message options is invalid try to recover by returning
202 // english options (T267611)
203 if ( !$expiryOptions && $expiryOptionsMsg->getLanguage()->getCode() !== 'en' ) {
204 return self::getExpiryOptionsFromMessage( $msgLocalizer, 'en' );
205 }
206
207 return $expiryOptions;
208 }
209
210 protected function alterForm( HTMLForm $form ) {
211 $msg = $this->watchlistExpiry && $this->watchedItem ? 'updatewatchlist' : 'addwatch';
212 $form->setWrapperLegendMsg( $msg );
213 $submitMsg = $this->watchlistExpiry ? 'confirm-watch-button-expiry' : 'confirm-watch-button';
214 $form->setSubmitTextMsg( $submitMsg );
215 $form->setTokenSalt( 'watch' );
216 }
217
230 public function onSuccess() {
231 $msgKey = $this->getTitle()->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext';
232 $expiryLabel = null;
233 $submittedExpiry = $this->getContext()->getRequest()->getText( 'wp' . $this->expiryFormFieldName );
234 if ( $submittedExpiry ) {
235 // We can't use $this->watcheditem to get the expiry because it's not been saved at this
236 // point in the request and so its values are those from before saving.
237 $expiry = ExpiryDef::normalizeExpiry( $submittedExpiry, TS_ISO_8601 );
238
239 // If the expiry label isn't one of the predefined ones in the dropdown, calculate 'x days'.
240 $expiryDays = WatchedItem::calculateExpiryInDays( $expiry );
241 $defaultLabels = static::getExpiryOptions( $this->getContext(), false )['options'];
242 $localizedExpiry = array_search( $submittedExpiry, $defaultLabels );
243 $expiryLabel = $expiryDays && $localizedExpiry === false
244 ? $this->getContext()->msg( 'days', $expiryDays )->text()
245 : $localizedExpiry;
246
247 // Determine which message to use, depending on whether this is a talk page or not
248 // and whether an expiry was selected.
249 $isTalk = $this->getTitle()->isTalkPage();
250 if ( wfIsInfinity( $expiry ) ) {
251 $msgKey = $isTalk ? 'addedwatchindefinitelytext-talk' : 'addedwatchindefinitelytext';
252 } elseif ( $expiryDays > 0 ) {
253 $msgKey = $isTalk ? 'addedwatchexpirytext-talk' : 'addedwatchexpirytext';
254 } elseif ( $expiryDays < 1 ) {
255 $msgKey = $isTalk ? 'addedwatchexpiryhours-talk' : 'addedwatchexpiryhours';
256 }
257 }
258 $this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText(), $expiryLabel );
259 }
260
261 public function doesWrites() {
262 return true;
263 }
264}
getUser()
getRequest()
getAuthority()
wfIsInfinity( $str)
Determine input string is represents as infinity.
getContext()
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:223
Legacy class representing an editable page and handling UI for some page actions.
Definition Article.php:67
An action which shows a form and does something based on the input from the form.
A class containing constants representing the names of configuration variables.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
internal since 1.36
Definition User.php:93
isTemp()
Is the user an autocreated temporary user?
Definition User.php:3535
isAllowed(string $permission, PermissionStatus $status=null)
Checks whether this authority has the given permission in general.
Definition User.php:2291
isRegistered()
Get whether the user is registered.
Definition User.php:2234
Redirect a user to the login page.
Page addition to a user's watchlist.
getRestriction()
Get the permission required to perform this action.
static getExpiryOptions(MessageLocalizer $msgLocalizer, $watchedItem)
Get options and default for a watchlist expiry select list.
getDescription()
Returns the description that goes below the <h1> element.
string $expiryFormFieldName
bool $watchlistExpiry
The value of the $wgWatchlistExpiry configuration variable.
requiresUnblock()
Whether this action can still be executed by a blocked user.
getFormFields()
Get an HTMLForm descriptor array.
usesOOUI()
Whether the form should use OOUI.
__construct(Article $article, IContextSource $context, WatchlistManager $watchlistManager, WatchedItemStore $watchedItemStore)
Only public since 1.21.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
false WatchedItem $watchedItem
checkCanExecute(User $user)
onSuccess()
Show one of 8 possible success messages.
onSubmit( $data)
Process the form on POST submission.
getName()
Return the name of the action this object responds to.
Storage layer class for WatchedItems.
getWatchedItem(UserIdentity $user, $target)
Representation of a pair of user and title for watchlist entries.
getExpiryInDaysText(MessageLocalizer $msgLocalizer, $isDropdownOption=false)
Get days remaining until a watched item expires as a text.
getExpiry(?int $style=TS_MW)
When the watched item will expire.
Type definition for expiry timestamps.
Definition ExpiryDef.php:17
Interface for objects which can provide a MediaWiki context on request.
Interface for localizing messages in MediaWiki.