2021-04-08 13:02:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Actions;
|
|
|
|
|
2022-11-23 11:12:24 +00:00
|
|
|
use Article;
|
2021-04-08 13:02:45 +00:00
|
|
|
use ErrorPageError;
|
|
|
|
use FormAction;
|
|
|
|
use Html;
|
|
|
|
use HTMLForm;
|
|
|
|
use IContextSource;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\SubscriptionItem;
|
|
|
|
use MediaWiki\Extension\DiscussionTools\SubscriptionStore;
|
2022-12-08 16:15:02 +00:00
|
|
|
use SpecialPage;
|
2021-04-08 13:02:45 +00:00
|
|
|
use Title;
|
|
|
|
use User;
|
|
|
|
use UserNotLoggedIn;
|
|
|
|
|
|
|
|
class UnsubscribeAction extends FormAction {
|
|
|
|
|
2022-10-21 19:34:18 +00:00
|
|
|
protected SubscriptionStore $subscriptionStore;
|
|
|
|
protected ?SubscriptionItem $subscriptionItem = null;
|
2021-04-08 13:02:45 +00:00
|
|
|
|
2021-08-02 02:55:33 +00:00
|
|
|
public function __construct(
|
2022-11-23 11:12:24 +00:00
|
|
|
Article $page,
|
2021-08-02 02:55:33 +00:00
|
|
|
IContextSource $context,
|
|
|
|
SubscriptionStore $subscriptionStore
|
|
|
|
) {
|
2021-04-08 13:02:45 +00:00
|
|
|
parent::__construct( $page, $context );
|
2021-08-02 02:55:33 +00:00
|
|
|
$this->subscriptionStore = $subscriptionStore;
|
2021-04-08 13:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function getPageTitle() {
|
2022-12-08 16:15:02 +00:00
|
|
|
if ( $this->subscriptionItem && !str_starts_with( $this->subscriptionItem->getItemName(), 'p-topics-' ) ) {
|
2021-04-08 13:02:45 +00:00
|
|
|
$title = Title::newFromLinkTarget( $this->subscriptionItem->getLinkTarget() );
|
|
|
|
return htmlspecialchars( $title->getPrefixedText() ) .
|
|
|
|
$this->msg( 'pipe-separator' )->escaped() .
|
|
|
|
htmlspecialchars( $title->getFragment() );
|
|
|
|
} else {
|
|
|
|
return parent::getPageTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function show() {
|
|
|
|
$commentName = $this->getRequest()->getVal( 'commentname' );
|
|
|
|
|
|
|
|
if ( $commentName ) {
|
|
|
|
$subscriptionItems = $this->subscriptionStore->getSubscriptionItemsForUser(
|
|
|
|
$this->getUser(),
|
|
|
|
[ $commentName ]
|
|
|
|
// We could check the user is still subscribed, but then we'd need more error messages
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( count( $subscriptionItems ) > 0 ) {
|
|
|
|
$this->subscriptionItem = $subscriptionItems[ 0 ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::show();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return 'dtunsubscribe';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function requiresUnblock() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function getDescription() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function getFormFields() {
|
|
|
|
if ( $this->subscriptionItem ) {
|
|
|
|
return [
|
|
|
|
'commentname' => [
|
|
|
|
'name' => 'commentname',
|
|
|
|
'type' => 'hidden',
|
|
|
|
'default' => $this->getRequest()->getVal( 'commentname' ),
|
|
|
|
],
|
|
|
|
'intro' => [
|
|
|
|
'type' => 'info',
|
|
|
|
'raw' => true,
|
2022-12-08 16:15:02 +00:00
|
|
|
'default' => $this->msg( str_starts_with( $this->subscriptionItem->getItemName(), 'p-topics-' ) ?
|
|
|
|
'discussiontools-topicsubscription-action-unsubscribe-prompt-newtopics' :
|
|
|
|
'discussiontools-topicsubscription-action-unsubscribe-prompt' )->parse(),
|
2021-04-08 13:02:45 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function alterForm( HTMLForm $form ) {
|
|
|
|
$form->setSubmitTextMsg( 'discussiontools-topicsubscription-action-unsubscribe-button' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function onSubmit( $data ) {
|
|
|
|
$commentName = $this->getRequest()->getVal( 'commentname' );
|
|
|
|
|
|
|
|
return $this->subscriptionStore->removeSubscriptionForUser(
|
|
|
|
$this->getUser(),
|
|
|
|
$commentName
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function onSuccess() {
|
|
|
|
$this->getOutput()->addHTML(
|
|
|
|
Html::element(
|
|
|
|
'p',
|
|
|
|
[],
|
2022-12-08 16:15:02 +00:00
|
|
|
$this->msg( str_starts_with( $this->subscriptionItem->getItemName(), 'p-topics-' ) ?
|
|
|
|
'discussiontools-newtopicssubscription-notify-unsubscribed-body' :
|
|
|
|
'discussiontools-topicsubscription-notify-unsubscribed-body' )->text()
|
2021-04-08 13:02:45 +00:00
|
|
|
)
|
|
|
|
);
|
2022-12-08 16:15:02 +00:00
|
|
|
$this->getOutput()->addReturnTo( $this->subscriptionItem->getLinkTarget() );
|
|
|
|
$this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'TopicSubscriptions' ) );
|
2021-04-08 13:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function usesOOUI() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
2022-10-28 18:24:02 +00:00
|
|
|
* @throws ErrorPageError
|
2021-04-08 13:02:45 +00:00
|
|
|
*/
|
|
|
|
protected function checkCanExecute( User $user ) {
|
|
|
|
// Must be logged in
|
2023-06-02 19:11:36 +00:00
|
|
|
if ( !$user->isNamed() ) {
|
2021-04-08 13:02:45 +00:00
|
|
|
throw new UserNotLoggedIn();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !$this->subscriptionItem ) {
|
|
|
|
throw new ErrorPageError(
|
|
|
|
'discussiontools-topicsubscription-error-not-found-title',
|
|
|
|
'discussiontools-topicsubscription-error-not-found-body'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::checkCanExecute( $user );
|
|
|
|
}
|
|
|
|
}
|