Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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 $hookData = [];
103 foreach ( $data as $userOption => $value ) {
104 $hookData[$userOption]['before'] = $this->isTargetBlacklisted( $userOption );
105 if ( $value ) {
106 $this->muteTarget( $userOption );
107 } else {
108 $this->unmuteTarget( $userOption );
109 }
110 $hookData[$userOption]['after'] = (bool)$value;
111 }
112
113 // NOTE: this hook is temporary
114 Hooks::run( 'SpecialMuteSubmit', [ $hookData ] );
115
116 return true;
117 }
118
119 /**
120 * @inheritDoc
121 */
122 public function getDescription() {
123 return $this->msg( 'specialmute' )->text();
124 }
125
126 /**
127 * Un-mute target
128 *
129 * @param string $userOption up_property key that holds the blacklist
130 */
131 private function unmuteTarget( $userOption ) {
132 $blacklist = $this->getBlacklist( $userOption );
133
134 $key = array_search( $this->targetCentralId, $blacklist );
135 if ( $key !== false ) {
136 unset( $blacklist[$key] );
137 $blacklist = implode( "\n", $blacklist );
138
139 $user = $this->getUser();
140 $user->setOption( $userOption, $blacklist );
141 $user->saveSettings();
142 }
143 }
144
145 /**
146 * Mute target
147 * @param string $userOption up_property key that holds the blacklist
148 */
149 private function muteTarget( $userOption ) {
150 // avoid duplicates just in case
151 if ( !$this->isTargetBlacklisted( $userOption ) ) {
152 $blacklist = $this->getBlacklist( $userOption );
153
154 $blacklist[] = $this->targetCentralId;
155 $blacklist = implode( "\n", $blacklist );
156
157 $user = $this->getUser();
158 $user->setOption( $userOption, $blacklist );
159 $user->saveSettings();
160 }
161 }
162
163 /**
164 * @inheritDoc
165 */
166 protected function getForm() {
167 $form = parent::getForm();
168 $form->setId( 'mw-specialmute-form' );
169 $form->setHeaderText( $this->msg( 'specialmute-header', $this->target )->parse() );
170 $form->setSubmitTextMsg( 'specialmute-submit' );
171 $form->setSubmitID( 'save' );
172
173 return $form;
174 }
175
176 /**
177 * @inheritDoc
178 */
179 protected function getFormFields() {
180 $fields = [];
181 if (
182 $this->enableUserEmailBlacklist &&
183 $this->enableUserEmail &&
184 $this->getUser()->getEmailAuthenticationTimestamp()
185 ) {
186 $fields['email-blacklist'] = [
187 'type' => 'check',
188 'label-message' => 'specialmute-label-mute-email',
189 'default' => $this->isTargetBlacklisted( 'email-blacklist' ),
190 ];
191 }
192
193 Hooks::run( 'SpecialMuteModifyFormFields', [ $this, &$fields ] );
194
195 if ( count( $fields ) == 0 ) {
196 throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
197 }
198
199 return $fields;
200 }
201
202 /**
203 * @param string $username
204 */
205 private function loadTarget( $username ) {
206 $target = User::newFromName( $username );
207 if ( !$target || !$target->getId() ) {
208 throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
209 } else {
210 $this->target = $target;
211 $this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target );
212 }
213 }
214
215 /**
216 * @param string $userOption
217 * @return bool
218 */
219 public function isTargetBlacklisted( $userOption ) {
220 $blacklist = $this->getBlacklist( $userOption );
221 return in_array( $this->targetCentralId, $blacklist, true );
222 }
223
224 /**
225 * @param string $userOption
226 * @return array
227 */
228 private function getBlacklist( $userOption ) {
229 $blacklist = $this->getUser()->getOption( $userOption );
230 if ( !$blacklist ) {
231 return [];
232 }
233
234 return MultiUsernameFilter::splitIds( $blacklist );
235 }
236 }