Merge "Add expiry select field to action=watch"

This commit is contained in:
jenkins-bot 2020-04-09 18:56:54 +00:00 committed by Gerrit Code Review
commit 9dbb4a2876
4 changed files with 104 additions and 9 deletions

View file

@ -138,6 +138,8 @@ For notes on 1.34.x and older releases, see HISTORY.
for assistive technologies.
* (T245931) interwiki map API doesn't report foreign language if
$wgInterwikiMagic=false
* The form at ?action=watch has a new dropdown list to support expiry dates for
watchlist items (if $wgWatchlistExpiry is true).
* …
=== New developer features in 1.35 ===

View file

@ -29,6 +29,32 @@ use MediaWiki\MediaWikiServices;
*/
class WatchAction extends FormAction {
/** @var bool The value of the $wgWatchlistExpiry configuration variable. */
protected $watchlistExpiry;
/** @var string */
protected $expiryFormFieldName = 'expiry';
/** @var false|WatchedItem */
protected $watchedItem = false;
/**
* Only public since 1.21
*
* @param Page $page
* @param IContextSource|null $context
*/
public function __construct( Page $page, IContextSource $context = null ) {
parent::__construct( $page, $context );
$this->watchlistExpiry = $this->getContext()->getConfig()->get( 'WatchlistExpiry' );
if ( $this->watchlistExpiry ) {
// The watchedItem is only used in this action's form if $wgWatchlistExpiry is enabled.
$this->watchedItem = MediaWikiServices::getInstance()
->getWatchedItemStore()
->getWatchedItem( $this->getUser(), $this->getTitle() );
}
}
public function getName() {
return 'watch';
}
@ -42,7 +68,8 @@ class WatchAction extends FormAction {
}
public function onSubmit( $data ) {
return self::doWatch( $this->getTitle(), $this->getUser() );
$expiry = $this->getRequest()->getText( 'wp' . $this->expiryFormFieldName );
return self::doWatch( $this->getTitle(), $this->getUser(), User::CHECK_USER_RIGHTS, $expiry );
}
protected function checkCanExecute( User $user ) {
@ -59,19 +86,75 @@ class WatchAction extends FormAction {
}
protected function getFormFields() {
// If watchlist expiry is not enabled, return a simple confirmation message.
if ( !$this->watchlistExpiry ) {
return [
'intro' => [
'type' => 'info',
'vertical-label' => true,
'raw' => true,
'default' => $this->msg( 'confirm-watch-top' )->parse(),
],
];
}
// Otherwise, use a select-list of expiries.
$expiry = false;
if ( $this->watchedItem && $this->watchedItem->getExpiry() ) {
$expiry = $this->watchedItem->getExpiry();
}
$expiryOptions = static::getExpiryOptions( $this->getContext(), $expiry );
return [
'intro' => [
'type' => 'info',
'vertical-label' => true,
'raw' => true,
'default' => $this->msg( 'confirm-watch-top' )->parse()
$this->expiryFormFieldName => [
'type' => 'select',
'label-message' => 'confirm-watch-label',
'options' => $expiryOptions['options'],
'default' => $expiryOptions['default'],
]
];
}
/**
* Get options and default for a watchlist expiry select list. If an expiry time is provided, it
* will be added to the top of the list as 'x days left'.
*
* @todo Move this somewhere better when it's being used in more than just this action.
*
* @param IContextSource $context
* @param string $expiryTime
*
* @return mixed[] With keys `options` (string[]) and `default` (string).
*/
public static function getExpiryOptions( IContextSource $context, $expiryTime = false ) {
$expiryOptionsMsg = $context->msg( 'watchlist-expiry-options' )->text();
$expiryOptions = XmlSelect::parseOptionsMessage( $expiryOptionsMsg );
$default = 'infinite';
if ( $expiryTime ) {
// If it's already being temporarily watched,
// add the existing expiry as the default option in the dropdown.
$expiry = MWTimestamp::getInstance( $expiryTime );
$diffInSeconds = $expiry->getTimestamp() - wfTimestamp();
$diffInDays = $context
->getLanguage()
->getDurationIntervals( $diffInSeconds, [ 'days' ] )[ 'days' ];
$daysLeft = $context->msg( 'watchlist-expiry-days-left', [ $diffInDays ] )->text();
$expiryOptions = array_merge(
[ $daysLeft => $expiry->getTimestamp() ],
$expiryOptions
);
$default = $expiry->getTimestamp();
}
return [
'options' => $expiryOptions,
'default' => $default,
];
}
protected function alterForm( HTMLForm $form ) {
$form->setWrapperLegendMsg( 'addwatch' );
$form->setSubmitTextMsg( 'confirm-watch-button' );
$msg = $this->watchlistExpiry && $this->watchedItem ? 'updatewatchlist' : 'addwatch';
$form->setWrapperLegendMsg( $msg );
$submitMsg = $this->watchlistExpiry ? 'confirm-watch-button-expiry' : 'confirm-watch-button';
$form->setSubmitTextMsg( $submitMsg );
$form->setTokenSalt( 'watch' );
}

View file

@ -2352,6 +2352,7 @@
"watchlistanontext": "Please log in to view or edit items on your watchlist.",
"watchnologin": "Not logged in",
"addwatch": "Add to watchlist",
"updatewatchlist": "Update watchlist",
"addedwatchtext": "\"[[:$1]]\" and its discussion page have been added to your [[Special:Watchlist|watchlist]].",
"addedwatchtext-talk": "\"[[:$1]]\" and its associated page have been added to your [[Special:Watchlist|watchlist]].",
"addedwatchtext-short": "The page \"$1\" has been added to your watchlist.",
@ -3385,6 +3386,10 @@
"confirm-purge-bottom": "Purging a page clears the cache and forces the most current revision to appear.",
"confirm-watch-button": "OK",
"confirm-watch-top": "Add this page to your watchlist?",
"confirm-watch-label": "Watchlist time period:",
"watchlist-expiry-options": "Permanently:infinite,1 week:1 week,1 month:1 month,3 months:3 months,6 months:6 months",
"watchlist-expiry-days-left": "{{PLURAL:$1|$1 day|$1 days}} left",
"confirm-watch-button-expiry": "Watch",
"confirm-unwatch-button": "OK",
"confirm-unwatch-top": "Remove this page from your watchlist?",
"confirm-rollback-button": "OK",

View file

@ -2566,7 +2566,8 @@
"nowatchlist": "Displayed when there is no pages in the watchlist.",
"watchlistanontext": "Shown on Special:Userlogin when user tries to access their watchlist before logging in",
"watchnologin": "Used as error page title.\n\nThe error message for this title is:\n* {{msg-mw|Watchnologintext}}\n{{Identical|Not logged in}}",
"addwatch": "A confirmation message that is shown after a user clicks the watchlist star icon if the JavaScript does not or cannot load, or if they visit the \"action=\" link directly, e.g. https://translatewiki.net/w/i.php?title=MediaWiki:Addwatch/qqq&action=watch\n\nSee also:\n* {{msg-mw|Removewatch}}\n* {{msg-mw|Confirm-watch-top}}\n* {{msg-mw|Confirm-watch-button}}",
"addwatch": "A form fieldset legend that serves as a confirmation message that is shown after a user clicks the watchlist star icon if the JavaScript does not or cannot load, or if they visit the \"action=\" link directly, e.g. https://translatewiki.net/w/i.php?title=MediaWiki:Addwatch/qqq&action=watch\n\nSee also:\n* {{msg-mw|Removewatch}}\n* {{msg-mw|Confirm-watch-top}}\n* {{msg-mw|Confirm-watch-button}}\n* {{msg-mw|updatewatchlist}}",
"updatewatchlist": "A form fieldset legend used on <code>?action=watch</code> when watchlist expiries are enabled and the page being watched is already on the user's watchlist. Used instead of {{msg-mw|addwatch}} in this situation.",
"addedwatchtext": "Message shown after clicking on the {{msg-mw|Watch}} tab in a content namespace page. Parameters:\n* $1 - page title\nSee also:\n* {{msg-mw|Removedwatchtext}}\n* {{msg-mw|Addedwatchtext-talk}}",
"addedwatchtext-talk": "Message shown after clicking on the {{msg-mw|Watch}} tab in a talk namespace page. Parameters:\n* $1 - page title\nSee also:\n* {{msg-mw|Removedwatchtext-talk}}\n* {{msg-mw|Addedwatchtext}}",
"addedwatchtext-short": "Explanation shown when watching item from [[Special:UnwatchedPages]].\n\nSee also:\n* {{msg-mw|Removedwatchtext-short}}\n* {{msg-mw|Addedwatchtext}}",
@ -3600,6 +3601,10 @@
"confirm-purge-bottom": "Additional description for Purge form.",
"confirm-watch-button": "Used as Submit button text.\n{{Identical|OK}}",
"confirm-watch-top": "Used as confirmation message.",
"confirm-watch-label": "Label for select list of watchlist expiry options.",
"watchlist-expiry-options": "Options for the expiry of watchlist items.\n\n{{doc-mediawiki-options-list}}",
"watchlist-expiry-days-left": "Select-list option used to display the remaining number of days for a watchlist item that's due to expire.\n\nParemeter:\n* $1 - the integer number of days.",
"confirm-watch-button-expiry": "Used as Submit button text.",
"confirm-unwatch-button": "Used as Submit button text.\n{{Identical|OK}}",
"confirm-unwatch-top": "Used as confirmation message.",
"confirm-rollback-button": "Used as Submit button text.\n{{Identical|OK}}",