Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
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 / 35
0.00% covered (danger)
0.00%
0 / 12
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getPageTitle
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 show
0.00% covered (danger)
0.00%
0 / 7
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 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 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 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 onSuccess
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 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 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools\Actions;
4
5use Article;
6use ErrorPageError;
7use FormAction;
8use Html;
9use HTMLForm;
10use IContextSource;
11use MediaWiki\Extension\DiscussionTools\SubscriptionItem;
12use MediaWiki\Extension\DiscussionTools\SubscriptionStore;
13use Title;
14use User;
15use UserNotLoggedIn;
16
17class UnsubscribeAction extends FormAction {
18
19    protected SubscriptionStore $subscriptionStore;
20    protected ?SubscriptionItem $subscriptionItem = null;
21
22    public function __construct(
23        Article $page,
24        IContextSource $context,
25        SubscriptionStore $subscriptionStore
26    ) {
27        parent::__construct( $page, $context );
28        $this->subscriptionStore = $subscriptionStore;
29    }
30
31    /**
32     * @inheritDoc
33     */
34    protected function getPageTitle() {
35        if ( $this->subscriptionItem ) {
36            $title = Title::newFromLinkTarget( $this->subscriptionItem->getLinkTarget() );
37            return htmlspecialchars( $title->getPrefixedText() ) .
38                $this->msg( 'pipe-separator' )->escaped() .
39                htmlspecialchars( $title->getFragment() );
40        } else {
41            return parent::getPageTitle();
42        }
43    }
44
45    /**
46     * @inheritDoc
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                // We could check the user is still subscribed, but then we'd need more error messages
56            );
57
58            if ( count( $subscriptionItems ) > 0 ) {
59                $this->subscriptionItem = $subscriptionItems[ 0 ];
60            }
61        }
62
63        parent::show();
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function getName() {
70        return 'dtunsubscribe';
71    }
72
73    /**
74     * @inheritDoc
75     */
76    public function requiresUnblock() {
77        return false;
78    }
79
80    /**
81     * @inheritDoc
82     */
83    protected function getDescription() {
84        return '';
85    }
86
87    /**
88     * @inheritDoc
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( '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( 'discussiontools-topicsubscription-notify-unsubscribed-body' )->text()
137            )
138        );
139    }
140
141    /**
142     * @inheritDoc
143     */
144    protected function usesOOUI() {
145        return true;
146    }
147
148    /**
149     * @inheritDoc
150     */
151    protected function checkCanExecute( User $user ) {
152        // Must be logged in
153        if ( $user->isAnon() ) {
154            throw new UserNotLoggedIn();
155        }
156
157        if ( !$this->subscriptionItem ) {
158            throw new ErrorPageError(
159                'discussiontools-topicsubscription-error-not-found-title',
160                'discussiontools-topicsubscription-error-not-found-body'
161            );
162        }
163
164        parent::checkCanExecute( $user );
165    }
166}