MediaWiki REL1_34
SpecialMute.php
Go to the documentation of this file.
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 */
22
30
31 const PAGE_NAME = 'Mute';
32
34 private $target;
35
38
41
44
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
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
77 public function requiresUnblock() {
78 return false;
79 }
80
84 protected function getDisplayFormat() {
85 return 'ooui';
86 }
87
91 public function onSuccess() {
92 $out = $this->getOutput();
93 $out->addWikiMsg( 'specialmute-success' );
94 }
95
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
122 public function getDescription() {
123 return $this->msg( 'specialmute' )->text();
124 }
125
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
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
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
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
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
219 public function isTargetBlacklisted( $userOption ) {
220 $blacklist = $this->getBlacklist( $userOption );
221 return in_array( $this->targetCentralId, $blacklist, true );
222 }
223
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}
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
An error page which can definitely be safely rendered using the OutputPage.
Special page which uses an HTMLForm to handle processing.
string null $par
The sub-page of the special page.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:131
A special page that allows users to modify their notification preferences.
loadTarget( $username)
muteTarget( $userOption)
Mute target.
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
const PAGE_NAME
bool $enableUserEmailBlacklist
int $targetCentralId
unmuteTarget( $userOption)
Un-mute target.
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
execute( $par)
Entry point for special pages.
requiresUnblock()
Whether this action cannot be executed by a blocked user.bool
isTargetBlacklisted( $userOption)
getBlacklist( $userOption)
bool $enableUserEmail
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
getFormFields()
Get an HTMLForm descriptor array.array
onSubmit(array $data, HTMLForm $form=null)
CentralIdLookup $centralIdLookup
getForm()
Get the HTMLForm to control behavior.HTMLForm|null
getOutput()
Get the OutputPage being used for this instance.
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
getUser()
Shortcut to get the User executing this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
getId()
Get the user's ID.
Definition User.php:2335