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 ) {
103 foreach ( $data as $userOption => $value ) {
104 $hookData[$userOption]['before'] = $this->isTargetBlacklisted( $userOption );
106 $this->muteTarget( $userOption );
108 $this->unmuteTarget( $userOption );
110 $hookData[$userOption]['after'] = (bool)$value;
113 // NOTE: this hook is temporary
114 Hooks
::run( 'SpecialMuteSubmit', [ $hookData ] );
122 public function getDescription() {
123 return $this->msg( 'specialmute' )->text();
129 * @param string $userOption up_property key that holds the blacklist
131 private function unmuteTarget( $userOption ) {
132 $blacklist = $this->getBlacklist( $userOption );
134 $key = array_search( $this->targetCentralId
, $blacklist );
135 if ( $key !== false ) {
136 unset( $blacklist[$key] );
137 $blacklist = implode( "\n", $blacklist );
139 $user = $this->getUser();
140 $user->setOption( $userOption, $blacklist );
141 $user->saveSettings();
147 * @param string $userOption up_property key that holds the blacklist
149 private function muteTarget( $userOption ) {
150 // avoid duplicates just in case
151 if ( !$this->isTargetBlacklisted( $userOption ) ) {
152 $blacklist = $this->getBlacklist( $userOption );
154 $blacklist[] = $this->targetCentralId
;
155 $blacklist = implode( "\n", $blacklist );
157 $user = $this->getUser();
158 $user->setOption( $userOption, $blacklist );
159 $user->saveSettings();
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' );
179 protected function getFormFields() {
182 $this->enableUserEmailBlacklist
&&
183 $this->enableUserEmail
&&
184 $this->getUser()->getEmailAuthenticationTimestamp()
186 $fields['email-blacklist'] = [
188 'label-message' => 'specialmute-label-mute-email',
189 'default' => $this->isTargetBlacklisted( 'email-blacklist' ),
193 Hooks
::run( 'SpecialMuteModifyFormFields', [ $this, &$fields ] );
195 if ( count( $fields ) == 0 ) {
196 throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
203 * @param string $username
205 private function loadTarget( $username ) {
206 $target = User
::newFromName( $username );
207 if ( !$target ||
!$target->getId() ) {
208 throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
210 $this->target
= $target;
211 $this->targetCentralId
= $this->centralIdLookup
->centralIdFromLocalUser( $target );
216 * @param string $userOption
219 public function isTargetBlacklisted( $userOption ) {
220 $blacklist = $this->getBlacklist( $userOption );
221 return in_array( $this->targetCentralId
, $blacklist, true );
225 * @param string $userOption
228 private function getBlacklist( $userOption ) {
229 $blacklist = $this->getUser()->getOption( $userOption );
234 return MultiUsernameFilter
::splitIds( $blacklist );