Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 64
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 / 64
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 / 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
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 Article;
6use ErrorPageError;
7use FormAction;
8use HTMLForm;
9use IContextSource;
10use MediaWiki\Extension\DiscussionTools\SubscriptionStore;
11use MediaWiki\Html\Html;
12use MediaWiki\SpecialPage\SpecialPage;
13use MediaWiki\Title\Title;
14use MediaWiki\User\User;
15use UserNotLoggedIn;
16
17class SubscribeAction extends FormAction {
18
19    protected SubscriptionStore $subscriptionStore;
20    protected ?Title $subscriptionTitle = null;
21    protected ?string $subscriptionName = 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     * @inheritDoc
34     */
35    protected function getPageTitle() {
36        if ( $this->subscriptionTitle &&
37            $this->subscriptionName !== null && !str_starts_with( $this->subscriptionName, 'p-topics-' )
38        ) {
39            $title = $this->subscriptionTitle;
40            return $this->msg( 'discussiontools-topicsubscription-action-title' )
41                ->plaintextParams( $title->getPrefixedText(), $title->getFragment() );
42        } else {
43            return parent::getPageTitle();
44        }
45    }
46
47    /**
48     * @inheritDoc
49     */
50    public function show() {
51        $commentName = $this->getRequest()->getVal( 'commentname' );
52        $section = $this->getRequest()->getVal( 'section', '' );
53
54        if ( $commentName !== null ) {
55            $this->subscriptionTitle = $this->getTitle()->createFragmentTarget( $section );
56            $this->subscriptionName = $commentName;
57        }
58
59        parent::show();
60    }
61
62    /**
63     * @inheritDoc
64     */
65    public function getName() {
66        return 'dtsubscribe';
67    }
68
69    /**
70     * @inheritDoc
71     */
72    public function requiresUnblock() {
73        return false;
74    }
75
76    /**
77     * @inheritDoc
78     */
79    protected function getDescription() {
80        return '';
81    }
82
83    /**
84     * @inheritDoc
85     */
86    protected function getFormFields() {
87        if ( $this->subscriptionTitle ) {
88            return [
89                'commentname' => [
90                    'name' => 'commentname',
91                    'type' => 'hidden',
92                    'default' => $this->getRequest()->getVal( 'commentname' ),
93                ],
94                'section' => [
95                    'name' => 'section',
96                    'type' => 'hidden',
97                    'default' => $this->getRequest()->getVal( 'section', '' ),
98                ],
99                'intro' => [
100                    'type' => 'info',
101                    'raw' => true,
102                    'default' => $this->msg( str_starts_with( $this->subscriptionName, 'p-topics-' ) ?
103                        'discussiontools-topicsubscription-action-subscribe-prompt-newtopics' :
104                        'discussiontools-topicsubscription-action-subscribe-prompt' )->parse(),
105                ],
106            ];
107        } else {
108            return [];
109        }
110    }
111
112    /**
113     * @inheritDoc
114     */
115    protected function alterForm( HTMLForm $form ) {
116        $form->setSubmitTextMsg( 'discussiontools-topicsubscription-action-subscribe-button' );
117    }
118
119    /**
120     * @inheritDoc
121     */
122    public function onSubmit( $data ) {
123        return $this->subscriptionStore->addSubscriptionForUser(
124            $this->getUser(),
125            $this->subscriptionTitle,
126            $this->subscriptionName
127        );
128    }
129
130    /**
131     * @inheritDoc
132     */
133    public function onSuccess() {
134        $this->getOutput()->addHTML(
135            Html::element(
136                'p',
137                [],
138                $this->msg( str_starts_with( $this->subscriptionName, 'p-topics-' ) ?
139                    'discussiontools-newtopicssubscription-notify-subscribed-body' :
140                    'discussiontools-topicsubscription-notify-subscribed-body' )->text()
141            )
142        );
143        $this->getOutput()->addReturnTo( $this->subscriptionTitle );
144        $this->getOutput()->addReturnTo( SpecialPage::getTitleFor( 'TopicSubscriptions' ) );
145    }
146
147    /**
148     * @inheritDoc
149     */
150    protected function usesOOUI() {
151        return true;
152    }
153
154    /**
155     * @inheritDoc
156     * @throws ErrorPageError
157     */
158    protected function checkCanExecute( User $user ) {
159        // Must be logged in
160        if ( !$user->isNamed() ) {
161            throw new UserNotLoggedIn();
162        }
163
164        if ( !$this->subscriptionTitle ) {
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}