MediaWiki master
WatchAction.php
Go to the documentation of this file.
1<?php
34
40class WatchAction extends FormAction {
41
44
46 protected $expiryFormFieldName = 'expiry';
47
49 protected $watchedItem = false;
50
51 private WatchlistManager $watchlistManager;
52
61 public function __construct(
62 Article $article,
63 IContextSource $context,
64 WatchlistManager $watchlistManager,
65 WatchedItemStore $watchedItemStore
66 ) {
67 parent::__construct( $article, $context );
68 $this->watchlistExpiry = $this->getContext()->getConfig()->get( MainConfigNames::WatchlistExpiry );
69 if ( $this->watchlistExpiry ) {
70 // The watchedItem is only used in this action's form if $wgWatchlistExpiry is enabled.
71 $this->watchedItem = $watchedItemStore->getWatchedItem(
72 $this->getUser(),
73 $this->getTitle()
74 );
75 }
76 $this->watchlistManager = $watchlistManager;
77 }
78
79 public function getName() {
80 return 'watch';
81 }
82
83 public function requiresUnblock() {
84 return false;
85 }
86
87 protected function getDescription() {
88 return '';
89 }
90
91 public function onSubmit( $data ) {
92 // Even though we're never unwatching here, use WatchlistManager::setWatch()
93 // because it also checks for changed expiry.
94 $result = $this->watchlistManager->setWatch(
95 true,
96 $this->getAuthority(),
97 $this->getTitle(),
98 $this->getRequest()->getVal( 'wp' . $this->expiryFormFieldName )
99 );
100
101 return Status::wrap( $result );
102 }
103
110 protected function checkCanExecute( User $user ) {
111 if ( !$user->isRegistered()
112 || ( $user->isTemp() && !$user->isAllowed( 'editmywatchlist' ) )
113 ) {
114 throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
115 }
116
117 parent::checkCanExecute( $user );
118 }
119
120 public function getRestriction() {
121 return 'editmywatchlist';
122 }
123
124 protected function usesOOUI() {
125 return true;
126 }
127
128 protected function getFormFields() {
129 // If watchlist expiry is not enabled, return a simple confirmation message.
130 if ( !$this->watchlistExpiry ) {
131 return [
132 'intro' => [
133 'type' => 'info',
134 'raw' => true,
135 'default' => $this->msg( 'confirm-watch-top' )->parse(),
136 ],
137 ];
138 }
139
140 // Otherwise, use a select-list of expiries.
141 $expiryOptions = static::getExpiryOptions( $this->getContext(), $this->watchedItem );
142 return [
143 $this->expiryFormFieldName => [
144 'type' => 'select',
145 'label-message' => 'confirm-watch-label',
146 'options' => $expiryOptions['options'],
147 'default' => $expiryOptions['default'],
148 ]
149 ];
150 }
151
164 public static function getExpiryOptions( MessageLocalizer $msgLocalizer, $watchedItem ) {
165 $expiryOptions = self::getExpiryOptionsFromMessage( $msgLocalizer );
166 $default = in_array( 'infinite', $expiryOptions )
167 ? 'infinite'
168 : current( $expiryOptions );
169 if ( $watchedItem instanceof WatchedItem && $watchedItem->getExpiry() ) {
170 // If it's already being temporarily watched,
171 // add the existing expiry as the default option in the dropdown.
172 $default = $watchedItem->getExpiry( TS_ISO_8601 );
173 $daysLeft = $watchedItem->getExpiryInDaysText( $msgLocalizer, true );
174 $expiryOptions = array_merge( [ $daysLeft => $default ], $expiryOptions );
175 }
176 return [
177 'options' => $expiryOptions,
178 'default' => $default,
179 ];
180 }
181
190 private static function getExpiryOptionsFromMessage(
191 MessageLocalizer $msgLocalizer, ?string $lang = null
192 ): array {
193 $expiryOptionsMsg = $msgLocalizer->msg( 'watchlist-expiry-options' );
194 $optionsText = !$lang ? $expiryOptionsMsg->text() : $expiryOptionsMsg->inLanguage( $lang )->text();
195 $options = XmlSelect::parseOptionsMessage(
196 $optionsText
197 );
198
199 $expiryOptions = [];
200 foreach ( $options as $label => $value ) {
201 if ( strtotime( $value ) || wfIsInfinity( $value ) ) {
202 $expiryOptions[$label] = $value;
203 }
204 }
205
206 // If message options is invalid try to recover by returning
207 // english options (T267611)
208 if ( !$expiryOptions && $expiryOptionsMsg->getLanguage()->getCode() !== 'en' ) {
209 return self::getExpiryOptionsFromMessage( $msgLocalizer, 'en' );
210 }
211
212 return $expiryOptions;
213 }
214
215 protected function alterForm( HTMLForm $form ) {
216 $msg = $this->watchlistExpiry && $this->watchedItem ? 'updatewatchlist' : 'addwatch';
217 $form->setWrapperLegendMsg( $msg );
218 $submitMsg = $this->watchlistExpiry ? 'confirm-watch-button-expiry' : 'confirm-watch-button';
219 $form->setSubmitTextMsg( $submitMsg );
220 $form->setTokenSalt( 'watch' );
221 }
222
235 public function onSuccess() {
236 $submittedExpiry = $this->getContext()->getRequest()->getText( 'wp' . $this->expiryFormFieldName );
237 $this->getOutput()->addWikiMsg( $this->makeSuccessMessage( $submittedExpiry ) );
238 }
239
240 protected function makeSuccessMessage( string $submittedExpiry ): MessageValue {
241 $msgKey = $this->getTitle()->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext';
242 $params = [];
243 if ( $submittedExpiry ) {
244 // We can't use $this->watcheditem to get the expiry because it's not been saved at this
245 // point in the request and so its values are those from before saving.
246 $expiry = ExpiryDef::normalizeExpiry( $submittedExpiry, TS_ISO_8601 );
247
248 // If the expiry label isn't one of the predefined ones in the dropdown, calculate 'x days'.
249 $expiryDays = WatchedItem::calculateExpiryInDays( $expiry );
250 $defaultLabels = static::getExpiryOptions( $this->getContext(), false )['options'];
251 $localizedExpiry = array_search( $submittedExpiry, $defaultLabels );
252
253 // Determine which message to use, depending on whether this is a talk page or not
254 // and whether an expiry was selected.
255 $isTalk = $this->getTitle()->isTalkPage();
256 if ( wfIsInfinity( $expiry ) ) {
257 $msgKey = $isTalk ? 'addedwatchindefinitelytext-talk' : 'addedwatchindefinitelytext';
258 } elseif ( $expiryDays >= 1 ) {
259 $msgKey = $isTalk ? 'addedwatchexpirytext-talk' : 'addedwatchexpirytext';
260 $params[] = $localizedExpiry === false
261 ? $this->getContext()->msg( 'days', $expiryDays )->text()
262 : $localizedExpiry;
263 } else {
264 $msgKey = $isTalk ? 'addedwatchexpiryhours-talk' : 'addedwatchexpiryhours';
265 }
266 }
267 return MessageValue::new( $msgKey )->params( $this->getTitle()->getPrefixedText(), ...$params );
268 }
269
270 public function doesWrites() {
271 return true;
272 }
273}
wfIsInfinity( $str)
Determine input string is represents as infinity.
array $params
The job parameters.
getContext()
Get the IContextSource in use here.
Definition Action.php:120
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:154
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:228
getAuthority()
Shortcut to get the Authority executing this instance.
Definition Action.php:164
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:134
Legacy class representing an editable page and handling UI for some page actions.
Definition Article.php:71
An action which shows a form and does something based on the input from the form.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
setTokenSalt( $salt)
Set the salt for the edit token.
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
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
User class for the MediaWiki software.
Definition User.php:119
isAllowed(string $permission, ?PermissionStatus $status=null)
Checks whether this authority has the given permission in general.
Definition User.php:2196
isTemp()
Is the user an autocreated temporary user?
Definition User.php:3398
isRegistered()
Get whether the user is registered.
Definition User.php:2139
Storage layer class for WatchedItems.
getWatchedItem(UserIdentity $user, $target)
Representation of a pair of user and title for watchlist entries.
getExpiry(?int $style=TS_MW)
When the watched item will expire.
getExpiryInDaysText(MessageLocalizer $msgLocalizer, $isDropdownOption=false)
Get days remaining until a watched item expires as a text.
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:30
Redirect a user to the login page or account creation 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.
makeSuccessMessage(string $submittedExpiry)
Value object representing a message for i18n.
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.