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.
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.
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
19 * @ingroup SpecialPage
21 use MediaWiki\Preferences\MultiUsernameFilter
;
24 * A special page that allows users to modify their notification
27 * @ingroup SpecialPage
29 class SpecialMute
extends FormSpecialPage
{
31 const PAGE_NAME
= 'Mute';
37 private $targetCentralId;
40 private $enableUserEmailBlacklist;
43 private $enableUserEmail;
45 /** @var CentralIdLookup */
46 private $centralIdLookup;
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' );
54 $this->centralIdLookup
= CentralIdLookup
::factory();
56 parent
::__construct( self
::PAGE_NAME
, '', false );
60 * Entry point for special pages
64 public function execute( $par ) {
65 $this->requireLogin( 'specialmute-login-required' );
66 $this->loadTarget( $par );
68 parent
::execute( $par );
70 $out = $this->getOutput();
71 $out->addModules( 'mediawiki.misc-authed-ooui' );
77 public function requiresUnblock() {
84 protected function getDisplayFormat() {
91 public function onSuccess() {
92 $out = $this->getOutput();
93 $out->addWikiMsg( 'specialmute-success' );
98 * @param HTMLForm|null $form
101 public function onSubmit( array $data, HTMLForm
$form = null ) {
102 foreach ( $data as $userOption => $value ) {
104 $this->muteTarget( $userOption );
106 $this->unmuteTarget( $userOption );
116 public function getDescription() {
117 return $this->msg( 'specialmute' )->text();
123 * @param string $userOption up_property key that holds the blacklist
125 private function unmuteTarget( $userOption ) {
126 $blacklist = $this->getBlacklist( $userOption );
128 $key = array_search( $this->targetCentralId
, $blacklist );
129 if ( $key !== false ) {
130 unset( $blacklist[$key] );
131 $blacklist = implode( "\n", $blacklist );
133 $user = $this->getUser();
134 $user->setOption( $userOption, $blacklist );
135 $user->saveSettings();
141 * @param string $userOption up_property key that holds the blacklist
143 private function muteTarget( $userOption ) {
144 // avoid duplicates just in case
145 if ( !$this->isTargetBlacklisted( $userOption ) ) {
146 $blacklist = $this->getBlacklist( $userOption );
148 $blacklist[] = $this->targetCentralId
;
149 $blacklist = implode( "\n", $blacklist );
151 $user = $this->getUser();
152 $user->setOption( $userOption, $blacklist );
153 $user->saveSettings();
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' );
173 protected function getFormFields() {
176 $this->enableUserEmailBlacklist
&&
177 $this->enableUserEmail
&&
178 $this->getUser()->getEmailAuthenticationTimestamp()
180 $fields['email-blacklist'] = [
182 'label-message' => 'specialmute-label-mute-email',
183 'default' => $this->isTargetBlacklisted( 'email-blacklist' ),
187 Hooks
::run( 'SpecialMuteModifyFormFields', [ $this, &$fields ] );
189 if ( count( $fields ) == 0 ) {
190 throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
197 * @param string $username
199 private function loadTarget( $username ) {
200 $target = User
::newFromName( $username );
201 if ( !$target ||
!$target->getId() ) {
202 throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
204 $this->target
= $target;
205 $this->targetCentralId
= $this->centralIdLookup
->centralIdFromLocalUser( $target );
210 * @param string $userOption
213 public function isTargetBlacklisted( $userOption ) {
214 $blacklist = $this->getBlacklist( $userOption );
215 return in_array( $this->targetCentralId
, $blacklist, true );
219 * @param string $userOption
222 private function getBlacklist( $userOption ) {
223 $blacklist = $this->getUser()->getOption( $userOption );
228 return MultiUsernameFilter
::splitIds( $blacklist );