| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\Context\IContextSource; |
| 24 |
use MediaWiki\HTMLForm\HTMLForm; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\Status\Status; |
| 27 |
use MediaWiki\User\User; |
| 28 |
use MediaWiki\Watchlist\WatchedItem; |
| 29 |
use MediaWiki\Watchlist\WatchedItemStore; |
| 30 |
use MediaWiki\Watchlist\WatchlistManager; |
| 31 |
use MediaWiki\Xml\XmlSelect; |
| 32 |
use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class WatchAction extends FormAction { |
| 40 |
|
| 41 |
|
| 42 |
protected $watchlistExpiry; |
| 43 |
|
| 44 |
|
| 45 |
protected $expiryFormFieldName = 'expiry'; |
| 46 |
|
| 47 |
|
| 48 |
protected $watchedItem = false; |
| 49 |
|
| 50 |
private WatchlistManager $watchlistManager; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function __construct( |
| 61 |
Article $article, |
| 62 |
IContextSource $context, |
| 63 |
WatchlistManager $watchlistManager, |
| 64 |
WatchedItemStore $watchedItemStore |
| 65 |
) { |
| 66 |
parent::__construct( $article, $context ); |
| 67 |
$this->watchlistExpiry = $this->getContext()->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 68 |
if ( $this->watchlistExpiry ) { |
| 69 |
|
| 70 |
$this->watchedItem = $watchedItemStore->getWatchedItem( |
| 71 |
$this->getUser(), |
| 72 |
$this->getTitle() |
| 73 |
); |
| 74 |
} |
| 75 |
$this->watchlistManager = $watchlistManager; |
| 76 |
} |
| 77 |
|
| 78 |
public function getName() { |
| 79 |
return 'watch'; |
| 80 |
} |
| 81 |
|
| 82 |
public function requiresUnblock() { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
protected function getDescription() { |
| 87 |
return ''; |
| 88 |
} |
| 89 |
|
| 90 |
public function onSubmit( $data ) { |
| 91 |
|
| 92 |
|
| 93 |
$result = $this->watchlistManager->setWatch( |
| 94 |
true, |
| 95 |
$this->getAuthority(), |
| 96 |
$this->getTitle(), |
| 97 |
$this->getRequest()->getVal( 'wp' . $this->expiryFormFieldName ) |
| 98 |
); |
| 99 |
|
| 100 |
return Status::wrap( $result ); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
protected function checkCanExecute( User $user ) { |
| 110 |
if ( !$user->isRegistered() |
| 111 |
|| ( $user->isTemp() && !$user->isAllowed( 'editmywatchlist' ) ) |
| 112 |
) { |
| 113 |
throw new UserNotLoggedIn( 'watchlistanontext', 'watchnologin' ); |
| 114 |
} |
| 115 |
|
| 116 |
parent::checkCanExecute( $user ); |
| 117 |
} |
| 118 |
|
| 119 |
public function getRestriction() { |
| 120 |
return 'editmywatchlist'; |
| 121 |
} |
| 122 |
|
| 123 |
protected function usesOOUI() { |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
protected function getFormFields() { |
| 128 |
|
| 129 |
if ( !$this->watchlistExpiry ) { |
| 130 |
return [ |
| 131 |
'intro' => [ |
| 132 |
'type' => 'info', |
| 133 |
'raw' => true, |
| 134 |
'default' => $this->msg( 'confirm-watch-top' )->parse(), |
| 135 |
], |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
$expiryOptions = static::getExpiryOptions( $this->getContext(), $this->watchedItem ); |
| 141 |
return [ |
| 142 |
$this->expiryFormFieldName => [ |
| 143 |
'type' => 'select', |
| 144 |
'label-message' => 'confirm-watch-label', |
| 145 |
'options' => $expiryOptions['options'], |
| 146 |
'default' => $expiryOptions['default'], |
| 147 |
] |
| 148 |
]; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public static function getExpiryOptions( MessageLocalizer $msgLocalizer, $watchedItem ) { |
| 164 |
$expiryOptions = self::getExpiryOptionsFromMessage( $msgLocalizer ); |
| 165 |
$default = in_array( 'infinite', $expiryOptions ) |
| 166 |
? 'infinite' |
| 167 |
: current( $expiryOptions ); |
| 168 |
if ( $watchedItem instanceof WatchedItem && $watchedItem->getExpiry() ) { |
| 169 |
|
| 170 |
|
| 171 |
$default = $watchedItem->getExpiry( TS_ISO_8601 ); |
| 172 |
$daysLeft = $watchedItem->getExpiryInDaysText( $msgLocalizer, true ); |
| 173 |
$expiryOptions = array_merge( [ $daysLeft => $default ], $expiryOptions ); |
| 174 |
} |
| 175 |
return [ |
| 176 |
'options' => $expiryOptions, |
| 177 |
'default' => $default, |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private static function getExpiryOptionsFromMessage( |
| 190 |
MessageLocalizer $msgLocalizer, ?string $lang = null |
| 191 |
): array { |
| 192 |
$expiryOptionsMsg = $msgLocalizer->msg( 'watchlist-expiry-options' ); |
| 193 |
$optionsText = !$lang ? $expiryOptionsMsg->text() : $expiryOptionsMsg->inLanguage( $lang )->text(); |
| 194 |
$options = XmlSelect::parseOptionsMessage( |
| 195 |
$optionsText |
| 196 |
); |
| 197 |
|
| 198 |
$expiryOptions = []; |
| 199 |
foreach ( $options as $label => $value ) { |
| 200 |
if ( strtotime( $value ) || wfIsInfinity( $value ) ) { |
| 201 |
$expiryOptions[$label] = $value; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
if ( !$expiryOptions && $expiryOptionsMsg->getLanguage()->getCode() !== 'en' ) { |
| 208 |
return self::getExpiryOptionsFromMessage( $msgLocalizer, 'en' ); |
| 209 |
} |
| 210 |
|
| 211 |
return $expiryOptions; |
| 212 |
} |
| 213 |
|
| 214 |
protected function alterForm( HTMLForm $form ) { |
| 215 |
$msg = $this->watchlistExpiry && $this->watchedItem ? 'updatewatchlist' : 'addwatch'; |
| 216 |
$form->setWrapperLegendMsg( $msg ); |
| 217 |
$submitMsg = $this->watchlistExpiry ? 'confirm-watch-button-expiry' : 'confirm-watch-button'; |
| 218 |
$form->setSubmitTextMsg( $submitMsg ); |
| 219 |
$form->setTokenSalt( 'watch' ); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function onSuccess() { |
| 235 |
$msgKey = $this->getTitle()->isTalkPage() ? 'addedwatchtext-talk' : 'addedwatchtext'; |
| 236 |
$expiryLabel = null; |
| 237 |
$submittedExpiry = $this->getContext()->getRequest()->getText( 'wp' . $this->expiryFormFieldName ); |
| 238 |
if ( $submittedExpiry ) { |
| 239 |
|
| 240 |
|
| 241 |
$expiry = ExpiryDef::normalizeExpiry( $submittedExpiry, TS_ISO_8601 ); |
| 242 |
|
| 243 |
|
| 244 |
$expiryDays = WatchedItem::calculateExpiryInDays( $expiry ); |
| 245 |
$defaultLabels = static::getExpiryOptions( $this->getContext(), false )['options']; |
| 246 |
$localizedExpiry = array_search( $submittedExpiry, $defaultLabels ); |
| 247 |
$expiryLabel = $expiryDays && $localizedExpiry === false |
| 248 |
? $this->getContext()->msg( 'days', $expiryDays )->text() |
| 249 |
: $localizedExpiry; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
$isTalk = $this->getTitle()->isTalkPage(); |
| 254 |
if ( wfIsInfinity( $expiry ) ) { |
| 255 |
$msgKey = $isTalk ? 'addedwatchindefinitelytext-talk' : 'addedwatchindefinitelytext'; |
| 256 |
} elseif ( $expiryDays > 0 ) { |
| 257 |
$msgKey = $isTalk ? 'addedwatchexpirytext-talk' : 'addedwatchexpirytext'; |
| 258 |
} elseif ( $expiryDays < 1 ) { |
| 259 |
$msgKey = $isTalk ? 'addedwatchexpiryhours-talk' : 'addedwatchexpiryhours'; |
| 260 |
} |
| 261 |
} |
| 262 |
$this->getOutput()->addWikiMsg( $msgKey, $this->getTitle()->getPrefixedText(), $expiryLabel ); |
| 263 |
} |
| 264 |
|
| 265 |
public function doesWrites() { |
| 266 |
return true; |
| 267 |
} |
| 268 |
} |