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