Debian Codegraph @

extensions/DiscussionTools/includes/Actions/SubscribeAction.php

raw symbols SCIP
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\SubscriptionStore;
10 use MediaWiki\Html\Html;
11 use MediaWiki\HTMLForm\HTMLForm;
12 use MediaWiki\SpecialPage\SpecialPage;
13 use MediaWiki\Title\Title;
14 use MediaWiki\User\User;
15 use UserNotLoggedIn;
16
17 class 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 }