Add rate limiter to Special:ConfirmEmail
[lhc/web/wiklou.git] / includes / specials / SpecialMute.php
1 <?php
2 /*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup SpecialPage
20 */
21 use MediaWiki\Preferences\MultiUsernameFilter;
22
23 /**
24 * A special page that allows users to modify their notification
25 * preferences
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialMute extends FormSpecialPage {
30
31 const PAGE_NAME = 'Mute';
32
33 /** @var User */
34 private $target;
35
36 /** @var int */
37 private $targetCentralId;
38
39 /** @var bool */
40 private $enableUserEmailBlacklist;
41
42 /** @var bool */
43 private $enableUserEmail;
44
45 /** @var CentralIdLookup */
46 private $centralIdLookup;
47
48 public function __construct() {
49 // TODO: inject all these dependencies once T222388 is resolved
50 $config = RequestContext::getMain()->getConfig();
51 $this->enableUserEmailBlacklist = $config->get( 'EnableUserEmailBlacklist' );
52 $this->enableUserEmail = $config->get( 'EnableUserEmail' );
53
54 $this->centralIdLookup = CentralIdLookup::factory();
55
56 parent::__construct( self::PAGE_NAME, '', false );
57 }
58
59 /**
60 * Entry point for special pages
61 *
62 * @param string $par
63 */
64 public function execute( $par ) {
65 $this->requireLogin( 'specialmute-login-required' );
66 $this->loadTarget( $par );
67
68 parent::execute( $par );
69
70 $out = $this->getOutput();
71 $out->addModules( 'mediawiki.misc-authed-ooui' );
72 }
73
74 /**
75 * @inheritDoc
76 */
77 public function requiresUnblock() {
78 return false;
79 }
80
81 /**
82 * @inheritDoc
83 */
84 protected function getDisplayFormat() {
85 return 'ooui';
86 }
87
88 /**
89 * @inheritDoc
90 */
91 public function onSuccess() {
92 $out = $this->getOutput();
93 $out->addWikiMsg( 'specialmute-success' );
94 }
95
96 /**
97 * @param array $data
98 * @param HTMLForm|null $form
99 * @return bool
100 */
101 public function onSubmit( array $data, HTMLForm $form = null ) {
102 foreach ( $data as $userOption => $value ) {
103 if ( $value ) {
104 $this->muteTarget( $userOption );
105 } else {
106 $this->unmuteTarget( $userOption );
107 }
108 }
109
110 return true;
111 }
112
113 /**
114 * @inheritDoc
115 */
116 public function getDescription() {
117 return $this->msg( 'specialmute' )->text();
118 }
119
120 /**
121 * Un-mute target
122 *
123 * @param string $userOption up_property key that holds the blacklist
124 */
125 private function unmuteTarget( $userOption ) {
126 $blacklist = $this->getBlacklist( $userOption );
127
128 $key = array_search( $this->targetCentralId, $blacklist );
129 if ( $key !== false ) {
130 unset( $blacklist[$key] );
131 $blacklist = implode( "\n", $blacklist );
132
133 $user = $this->getUser();
134 $user->setOption( $userOption, $blacklist );
135 $user->saveSettings();
136 }
137 }
138
139 /**
140 * Mute target
141 * @param string $userOption up_property key that holds the blacklist
142 */
143 private function muteTarget( $userOption ) {
144 // avoid duplicates just in case
145 if ( !$this->isTargetBlacklisted( $userOption ) ) {
146 $blacklist = $this->getBlacklist( $userOption );
147
148 $blacklist[] = $this->targetCentralId;
149 $blacklist = implode( "\n", $blacklist );
150
151 $user = $this->getUser();
152 $user->setOption( $userOption, $blacklist );
153 $user->saveSettings();
154 }
155 }
156
157 /**
158 * @inheritDoc
159 */
160 protected function getForm() {
161 $form = parent::getForm();
162 $form->setId( 'mw-specialmute-form' );
163 $form->setHeaderText( $this->msg( 'specialmute-header', $this->target )->parse() );
164 $form->setSubmitTextMsg( 'specialmute-submit' );
165 $form->setSubmitID( 'save' );
166
167 return $form;
168 }
169
170 /**
171 * @inheritDoc
172 */
173 protected function getFormFields() {
174 $fields = [];
175 if (
176 $this->enableUserEmailBlacklist &&
177 $this->enableUserEmail &&
178 $this->getUser()->getEmailAuthenticationTimestamp()
179 ) {
180 $fields['email-blacklist'] = [
181 'type' => 'check',
182 'label-message' => 'specialmute-label-mute-email',
183 'default' => $this->isTargetBlacklisted( 'email-blacklist' ),
184 ];
185 }
186
187 Hooks::run( 'SpecialMuteModifyFormFields', [ $this, &$fields ] );
188
189 if ( count( $fields ) == 0 ) {
190 throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
191 }
192
193 return $fields;
194 }
195
196 /**
197 * @param string $username
198 */
199 private function loadTarget( $username ) {
200 $target = User::newFromName( $username );
201 if ( !$target || !$target->getId() ) {
202 throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
203 } else {
204 $this->target = $target;
205 $this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target );
206 }
207 }
208
209 /**
210 * @param string $userOption
211 * @return bool
212 */
213 public function isTargetBlacklisted( $userOption ) {
214 $blacklist = $this->getBlacklist( $userOption );
215 return in_array( $this->targetCentralId, $blacklist, true );
216 }
217
218 /**
219 * @param string $userOption
220 * @return array
221 */
222 private function getBlacklist( $userOption ) {
223 $blacklist = $this->getUser()->getOption( $userOption );
224 if ( !$blacklist ) {
225 return [];
226 }
227
228 return MultiUsernameFilter::splitIds( $blacklist );
229 }
230 }