| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools\Actions; |
| 4 |
|
| 5 |
use Article; |
| 6 |
use ErrorPageError; |
| 7 |
use FormAction; |
| 8 |
use MediaWiki\Context\IContextSource; |
| 9 |
use MediaWiki\Extension\DiscussionTools\SubscriptionItem; |
| 10 |
use MediaWiki\Extension\DiscussionTools\SubscriptionStore; |
| 11 |
use MediaWiki\Html\Html; |
| 12 |
use MediaWiki\HTMLForm\HTMLForm; |
| 13 |
use MediaWiki\SpecialPage\SpecialPage; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use MediaWiki\User\User; |
| 16 |
use UserNotLoggedIn; |
| 17 |
|
| 18 |
class UnsubscribeAction extends FormAction { |
| 19 |
|
| 20 |
protected SubscriptionStore $subscriptionStore; |
| 21 |
protected ?SubscriptionItem $subscriptionItem = null; |
| 22 |
|
| 23 |
public function __construct( |
| 24 |
Article $page, |
| 25 |
IContextSource $context, |
| 26 |
SubscriptionStore $subscriptionStore |
| 27 |
) { |
| 28 |
parent::__construct( $page, $context ); |
| 29 |
$this->subscriptionStore = $subscriptionStore; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected function getPageTitle() { |
| 36 |
if ( $this->subscriptionItem && !str_starts_with( $this->subscriptionItem->getItemName(), 'p-topics-' ) ) { |
| 37 |
$title = Title::newFromLinkTarget( $this->subscriptionItem->getLinkTarget() ); |
| 38 |
return $this->msg( 'discussiontools-topicsubscription-action-title' ) |
| 39 |
->plaintextParams( $title->getPrefixedText(), $title->getFragment() ); |
| 40 |
} else { |
| 41 |
return parent::getPageTitle(); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function show() { |
| 49 |
$commentName = $this->getRequest()->getVal( 'commentname' ); |
| 50 |
|
| 51 |
if ( $commentName ) { |
| 52 |
$subscriptionItems = $this->subscriptionStore->getSubscriptionItemsForUser( |
| 53 |
$this->getUser(), |
| 54 |
[ $commentName ] |
| 55 |
|
| 56 |
); |
| 57 |
|
| 58 |
if ( count( $subscriptionItems ) > 0 ) { |
| 59 |
$this->subscriptionItem = $subscriptionItems[ 0 ]; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
parent::show(); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function getName() { |
| 70 |
return 'dtunsubscribe'; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function requiresUnblock() { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected function getDescription() { |
| 84 |
return ''; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
protected function getFormFields() { |
| 91 |
if ( $this->subscriptionItem ) { |
| 92 |
return [ |
| 93 |
'commentname' => [ |
| 94 |
'name' => 'commentname', |
| 95 |
'type' => 'hidden', |
| 96 |
'default' => $this->getRequest()->getVal( 'commentname' ), |
| 97 |
], |
| 98 |
'intro' => [ |
| 99 |
'type' => 'info', |
| 100 |
'raw' => true, |
| 101 |
'default' => $this->msg( str_starts_with( $this->subscriptionItem->getItemName(), 'p-topics-' ) ? |
| 102 |
'discussiontools-topicsubscription-action-unsubscribe-prompt-newtopics' : |
| 103 |
'discussiontools-topicsubscription-action-unsubscribe-prompt' )->parse(), |
| 104 |
], |
| 105 |
]; |
| 106 |
} else { |
| 107 |
return []; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
protected function alterForm( HTMLForm $form ) { |
| 115 |
$form->setSubmitTextMsg( 'discussiontools-topicsubscription-action-unsubscribe-button' ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public function onSubmit( $data ) { |
| 122 |
$commentName = $this->getRequest()->getVal( 'commentname' ); |
| 123 |
|
| 124 |
return $this->subscriptionStore->removeSubscriptionForUser( |
| 125 |
$this->getUser(), |
| 126 |
$commentName |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function onSuccess() { |
| 134 |
$this->getOutput()->addHTML( |
| 135 |
Html::element( |
| 136 |
'p', |
| 137 |
[], |
| 138 |
$this->msg( str_starts_with( $this->subscriptionItem->getItemName(), 'p-topics-' ) ? |
| 139 |
'discussiontools-newtopicssubscription-notify-unsubscribed-body' : |
| 140 |
'discussiontools-topicsubscription-notify-unsubscribed-body' )->text() |
| 141 |
) |
| 142 |
); |
| 143 |
$this->getOutput()->addReturnTo( $this->subscriptionItem->getLinkTarget() ); |
| 144 |
$this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'TopicSubscriptions' ) ); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
protected function usesOOUI() { |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
protected function checkCanExecute( User $user ) { |
| 159 |
|
| 160 |
if ( !$user->isNamed() ) { |
| 161 |
throw new UserNotLoggedIn(); |
| 162 |
} |
| 163 |
|
| 164 |
if ( !$this->subscriptionItem ) { |
| 165 |
throw new ErrorPageError( |
| 166 |
'discussiontools-topicsubscription-error-not-found-title', |
| 167 |
'discussiontools-topicsubscription-error-not-found-body' |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
parent::checkCanExecute( $user ); |
| 172 |
} |
| 173 |
} |