MediaWiki  master
WatchAction.php
Go to the documentation of this file.
1 <?php
25 
31 class WatchAction extends FormAction {
32 
34  protected $watchlistExpiry;
35 
37  protected $expiryFormFieldName = 'expiry';
38 
40  protected $watchedItem = false;
41 
44 
53  public function __construct(
54  Page $page,
57  WatchedItemStore $watchedItemStore
58  ) {
59  parent::__construct( $page, $context );
60  $this->watchlistExpiry = $this->getContext()->getConfig()->get( 'WatchlistExpiry' );
61  if ( $this->watchlistExpiry ) {
62  // The watchedItem is only used in this action's form if $wgWatchlistExpiry is enabled.
63  $this->watchedItem = $watchedItemStore->getWatchedItem(
64  $this->getUser(),
65  $this->getTitle()
66  );
67  }
68  $this->watchlistManager = $watchlistManager;
69  }
70 
71  public function getName() {
72  return 'watch';
73  }
74 
75  public function requiresUnblock() {
76  return false;
77  }
78 
79  protected function getDescription() {
80  return '';
81  }
82 
83  public function onSubmit( $data ) {
84  // Even though we're never unwatching here, use WatchlistManager::setWatch()
85  // because it also checks for changed expiry.
86  $result = $this->watchlistManager->setWatch(
87  true,
88  $this->getContext()->getAuthority(),
89  $this->getTitle(),
90  $this->getRequest()->getVal( 'wp' . $this->expiryFormFieldName )
91  );
92 
93  return Status::wrap( $result );
94  }
95 
96  protected function checkCanExecute( User $user ) {
97  // Must be logged in
98  if ( $user->isAnon() ) {
99  throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' );
100  }
101 
102  parent::checkCanExecute( $user );
103  }
104 
105  protected function usesOOUI() {
106  return true;
107  }
108 
109  protected function getFormFields() {
110  // If watchlist expiry is not enabled, return a simple confirmation message.
111  if ( !$this->watchlistExpiry ) {
112  return [
113  'intro' => [
114  'type' => 'info',
115  'raw' => true,
116  'default' => $this->msg( 'confirm-watch-top' )->parse(),
117  ],
118  ];
119  }
120 
121  // Otherwise, use a select-list of expiries.
122  $expiryOptions = static::getExpiryOptions( $this->getContext(), $this->watchedItem );
123  return [
124  $this->expiryFormFieldName => [
125  'type' => 'select',
126  'label-message' => 'confirm-watch-label',
127  'options' => $expiryOptions['options'],
128  'default' => $expiryOptions['default'],
129  ]
130  ];
131  }
132 
145  public static function getExpiryOptions( MessageLocalizer $msgLocalizer, $watchedItem ) {
146  $expiryOptions = self::getExpiryOptionsFromMessage( $msgLocalizer );
147  $default = in_array( 'infinite', $expiryOptions )
148  ? 'infinite'
149  : current( $expiryOptions );
150  if ( $watchedItem instanceof WatchedItem && $watchedItem->getExpiry() ) {
151  // If it's already being temporarily watched,
152  // add the existing expiry as the default option in the dropdown.
153  $default = $watchedItem->getExpiry( TS_ISO_8601 );
154  $daysLeft = $watchedItem->getExpiryInDaysText( $msgLocalizer, true );
155  $expiryOptions = array_merge( [ $daysLeft => $default ], $expiryOptions );
156  }
157  return [
158  'options' => $expiryOptions,
159  'default' => $default,
160  ];
161  }
162 
171  private static function getExpiryOptionsFromMessage(
172  MessageLocalizer $msgLocalizer, ?string $lang = null
173  ): array {
174  $expiryOptionsMsg = $msgLocalizer->msg( 'watchlist-expiry-options' );
175  $optionsText = !$lang ? $expiryOptionsMsg->text() : $expiryOptionsMsg->inLanguage( $lang )->text();
177  $optionsText
178  );
179 
180  $expiryOptions = [];
181  foreach ( $options as $label => $value ) {
182  if ( strtotime( $value ) || wfIsInfinity( $value ) ) {
183  $expiryOptions[$label] = $value;
184  }
185  }
186 
187  // If message options is invalid try to recover by returning
188  // english options (T267611)
189  if ( !$expiryOptions && $expiryOptionsMsg->getLanguage()->getCode() !== 'en' ) {
190  return self::getExpiryOptionsFromMessage( $msgLocalizer, 'en' );
191  }
192 
193  return $expiryOptions;
194  }
195 
196  protected function alterForm( HTMLForm $form ) {
197  $msg = $this->watchlistExpiry && $this->watchedItem ? 'updatewatchlist' : 'addwatch';
198  $form->setWrapperLegendMsg( $msg );
199  $submitMsg = $this->watchlistExpiry ? 'confirm-watch-button-expiry' : 'confirm-watch-button';
200  $form->setSubmitTextMsg( $submitMsg );
201  $form->setTokenSalt( 'watch' );
202  }
203 
216  public function onSuccess() {
217  $msgKey = $this->getTitle()->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext';
218  $expiryLabel = null;
219  $submittedExpiry = $this->getContext()->getRequest()->getText( 'wp' . $this->expiryFormFieldName );
220  if ( $submittedExpiry ) {
221  // We can't use $this->watcheditem to get the expiry because it's not been saved at this
222  // point in the request and so its values are those from before saving.
223  $expiry = ExpiryDef::normalizeExpiry( $submittedExpiry, TS_ISO_8601 );
224 
225  // If the expiry label isn't one of the predefined ones in the dropdown, calculate 'x days'.
226  $expiryDays = WatchedItem::calculateExpiryInDays( $expiry );
227  $defaultLabels = static::getExpiryOptions( $this->getContext(), null )['options'];
228  $localizedExpiry = array_search( $submittedExpiry, $defaultLabels );
229  $expiryLabel = $expiryDays && $localizedExpiry === false
230  ? $this->getContext()->msg( 'days', $expiryDays )->text()
231  : $localizedExpiry;
232 
233  // Determine which message to use, depending on whether this is a talk page or not
234  // and whether an expiry was selected.
235  $isTalk = $this->getTitle()->isTalkPage();
236  if ( wfIsInfinity( $expiry ) ) {
237  $msgKey = $isTalk ? 'addedwatchindefinitelytext-talk' : 'addedwatchindefinitelytext';
238  } elseif ( $expiryDays > 0 ) {
239  $msgKey = $isTalk ? 'addedwatchexpirytext-talk' : 'addedwatchexpirytext';
240  } elseif ( $expiryDays < 1 ) {
241  $msgKey = $isTalk ? 'addedwatchexpiryhours-talk' : 'addedwatchexpiryhours';
242  }
243  }
244  $this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText(), $expiryLabel );
245  }
246 
247  public function doesWrites() {
248  return true;
249  }
250 }
WatchAction\$watchlistManager
WatchlistManager $watchlistManager
Definition: WatchAction.php:43
Page
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:29
User\isAnon
isAnon()
Get whether the user is anonymous.
Definition: User.php:2538
WatchAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: WatchAction.php:196
WatchAction\onSuccess
onSuccess()
Show one of 8 possible success messages.
Definition: WatchAction.php:216
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:146
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
WatchAction\requiresUnblock
requiresUnblock()
Whether this action can still be executed by a blocked user.
Definition: WatchAction.php:75
UserNotLoggedIn
Redirect a user to the login page.
Definition: UserNotLoggedIn.php:54
WatchAction\$expiryFormFieldName
string $expiryFormFieldName
Definition: WatchAction.php:37
WatchAction\getExpiryOptions
static getExpiryOptions(MessageLocalizer $msgLocalizer, $watchedItem)
Get options and default for a watchlist expiry select list.
Definition: WatchAction.php:145
HTMLForm\setTokenSalt
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1214
getAuthority
getAuthority()
FormAction
An action which shows a form and does something based on the input from the form.
Definition: FormAction.php:30
WatchAction\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: WatchAction.php:79
WatchAction
Page addition to a user's watchlist.
Definition: WatchAction.php:31
XmlSelect\parseOptionsMessage
static parseOptionsMessage(string $msg)
Parse labels and values out of a comma- and colon-separated list of options, such as is used for expi...
Definition: XmlSelect.php:146
WatchAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: WatchAction.php:83
MessageLocalizer
Interface for localizing messages in MediaWiki.
Definition: MessageLocalizer.php:29
WatchAction\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: WatchAction.php:109
Action\getContext
getContext()
Get the IContextSource in use here.
Definition: Action.php:132
WatchedItem\getExpiry
getExpiry(?int $style=TS_MW)
When the watched item will expire.
Definition: WatchedItem.php:150
Wikimedia\ParamValidator\TypeDef\ExpiryDef
Type definition for expiry timestamps.
Definition: ExpiryDef.php:17
WatchAction\doesWrites
doesWrites()
Definition: WatchAction.php:247
MessageLocalizer\msg
msg( $key,... $params)
This is the method for getting translated interface messages.
WatchAction\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: WatchAction.php:96
MediaWiki\Watchlist\WatchlistManager
WatchlistManager service.
Definition: WatchlistManager.php:52
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:62
WatchedItem\calculateExpiryInDays
static calculateExpiryInDays(?string $expiry)
Get the number of days remaining until the given expiry time.
Definition: WatchedItem.php:192
WatchAction\getExpiryOptionsFromMessage
static getExpiryOptionsFromMessage(MessageLocalizer $msgLocalizer, ?string $lang=null)
Parse expiry options message.
Definition: WatchAction.php:171
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:166
Action\$context
IContextSource $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:66
WatchAction\$watchlistExpiry
bool $watchlistExpiry
The value of the $wgWatchlistExpiry configuration variable.
Definition: WatchAction.php:34
wfIsInfinity
wfIsInfinity( $str)
Determine input string is represents as infinity.
Definition: GlobalFunctions.php:2514
WatchedItem
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:36
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:216
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
WatchAction\getName
getName()
Return the name of the action this object responds to.
Definition: WatchAction.php:71
WatchedItemStore
Storage layer class for WatchedItems.
Definition: WatchedItemStore.php:26
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1577
WatchedItem\getExpiryInDaysText
getExpiryInDaysText(MessageLocalizer $msgLocalizer, $isDropdownOption=false)
Get days remaining until a watched item expires as a text.
Definition: WatchedItem.php:218
Action\$page
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition: Action.php:53
WatchAction\__construct
__construct(Page $page, IContextSource $context, WatchlistManager $watchlistManager, WatchedItemStore $watchedItemStore)
Only public since 1.21.
Definition: WatchAction.php:53
Action\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:228
HTMLForm\setWrapperLegendMsg
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1782
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:156
WatchedItemStore\getWatchedItem
getWatchedItem(UserIdentity $user, $target)
Definition: WatchedItemStore.php:742
WatchAction\$watchedItem
false WatchedItem $watchedItem
Definition: WatchAction.php:40
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:67
WatchAction\usesOOUI
usesOOUI()
Whether the form should use OOUI.
Definition: WatchAction.php:105
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:149