Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
SubscribeAction
0.00% covered (danger)
0.00%
0 / 63
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 / 6
0.00% covered (danger)
0.00%
0 / 1
20
 show
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 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 / 21
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\SubscriptionStore;
10use MediaWiki\Html\Html;
11use MediaWiki\HTMLForm\HTMLForm;
12use MediaWiki\Page\Article;
13use MediaWiki\SpecialPage\SpecialPage;
14use MediaWiki\Title\Title;
15use MediaWiki\User\User;
16
17class SubscribeAction extends FormAction {
18
19    protected ?Title $subscriptionTitle = null;
20    protected ?string $subscriptionName = 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->subscriptionTitle &&
35            $this->subscriptionName !== null && !str_starts_with( $this->subscriptionName, 'p-topics-' )
36        ) {
37            $title = $this->subscriptionTitle;
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     * @inheritDoc
47     */
48    public function show() {
49        $commentName = $this->getRequest()->getVal( 'commentname' );
50        $section = $this->getRequest()->getVal( 'section', '' );
51
52        if ( $commentName !== null ) {
53            $this->subscriptionTitle = $this->getTitle()->createFragmentTarget( $section );
54            $this->subscriptionName = $commentName;
55        }
56
57        parent::show();
58    }
59
60    /**
61     * @inheritDoc
62     */
63    public function getName() {
64        return 'dtsubscribe';
65    }
66
67    /**
68     * @inheritDoc
69     */
70    public function requiresUnblock() {
71        return false;
72    }
73
74    /**
75     * @inheritDoc
76     */
77    protected function getDescription() {
78        return '';
79    }
80
81    /**
82     * @inheritDoc
83     */
84    protected function getFormFields() {
85        if ( $this->subscriptionTitle ) {
86            return [
87                'commentname' => [
88                    'name' => 'commentname',
89                    'type' => 'hidden',
90                    'default' => $this->getRequest()->getVal( 'commentname' ),
91                ],
92                'section' => [
93                    'name' => 'section',
94                    'type' => 'hidden',
95                    'default' => $this->getRequest()->getVal( 'section', '' ),
96                ],
97                'intro' => [
98                    'type' => 'info',
99                    'raw' => true,
100                    'default' => $this->msg( str_starts_with( $this->subscriptionName, 'p-topics-' ) ?
101                        'discussiontools-topicsubscription-action-subscribe-prompt-newtopics' :
102                        'discussiontools-topicsubscription-action-subscribe-prompt' )->parse(),
103                ],
104            ];
105        } else {
106            return [];
107        }
108    }
109
110    /**
111     * @inheritDoc
112     */
113    protected function alterForm( HTMLForm $form ) {
114        $form->setSubmitTextMsg( 'discussiontools-topicsubscription-action-subscribe-button' );
115    }
116
117    /**
118     * @inheritDoc
119     */
120    public function onSubmit( $data ) {
121        return $this->subscriptionStore->addSubscriptionForUser(
122            $this->getUser(),
123            $this->subscriptionTitle,
124            $this->subscriptionName
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->subscriptionName, 'p-topics-' ) ?
137                    'discussiontools-newtopicssubscription-notify-subscribed-body' :
138                    'discussiontools-topicsubscription-notify-subscribed-body' )->text()
139            )
140        );
141        $this->getOutput()->addReturnTo( $this->subscriptionTitle );
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->subscriptionTitle ) {
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}