MediaWiki  master
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  */
21 
24 
32 
33  private const PAGE_NAME = 'Mute';
34 
36  private $target;
37 
40 
43 
46 
51  parent::__construct( self::PAGE_NAME, '', false );
52  $this->centralIdLookup = CentralIdLookup::factory();
53  $this->userOptionsManager = $userOptionsManager;
54  }
55 
61  public function execute( $par ) {
62  $this->addHelpLink(
63  'https://meta.wikimedia.org/wiki/Community_health_initiative/User_Mute_features',
64  true
65  );
66  $this->requireLogin( 'specialmute-login-required' );
67  $this->loadTarget( $par );
68 
69  parent::execute( $par );
70 
71  $out = $this->getOutput();
72  $out->addModules( 'mediawiki.misc-authed-ooui' );
73  }
74 
78  public function requiresUnblock() {
79  return false;
80  }
81 
85  protected function getDisplayFormat() {
86  return 'ooui';
87  }
88 
92  public function onSuccess() {
93  $out = $this->getOutput();
94  $out->addWikiMsg( 'specialmute-success' );
95  }
96 
102  public function onSubmit( array $data, HTMLForm $form = null ) {
103  $hookData = [];
104  foreach ( $data as $userOption => $value ) {
105  $hookData[$userOption]['before'] = $this->isTargetMuted( $userOption );
106  if ( $value ) {
107  $this->muteTarget( $userOption );
108  } else {
109  $this->unmuteTarget( $userOption );
110  }
111  $hookData[$userOption]['after'] = (bool)$value;
112  }
113 
114  // NOTE: this hook is temporary
115  $this->getHookRunner()->onSpecialMuteSubmit( $hookData );
116 
117  return true;
118  }
119 
123  public function getDescription() {
124  return $this->msg( 'specialmute' )->text();
125  }
126 
130  public function getTarget(): ?User {
131  return $this->target;
132  }
133 
139  private function unmuteTarget( $userOption ) {
140  $muteList = $this->getMuteList( $userOption );
141 
142  $key = array_search( $this->targetCentralId, $muteList );
143  if ( $key !== false ) {
144  unset( $muteList[$key] );
145  $muteList = implode( "\n", $muteList );
146 
147  $user = $this->getUser();
148  $this->userOptionsManager->setOption( $user, $userOption, $muteList );
149  $user->saveSettings();
150  }
151  }
152 
157  private function muteTarget( $userOption ) {
158  // avoid duplicates just in case
159  if ( !$this->isTargetMuted( $userOption ) ) {
160  $muteList = $this->getMuteList( $userOption );
161 
162  $muteList[] = $this->targetCentralId;
163  $muteList = implode( "\n", $muteList );
164 
165  $user = $this->getUser();
166  $this->userOptionsManager->setOption( $user, $userOption, $muteList );
167  $user->saveSettings();
168  }
169  }
170 
174  protected function getForm() {
175  $form = parent::getForm();
176  $form->setId( 'mw-specialmute-form' );
177  $form->setHeaderText( $this->msg( 'specialmute-header', $this->target )->parse() );
178  $form->setSubmitTextMsg( 'specialmute-submit' );
179  $form->setSubmitID( 'save' );
180 
181  return $form;
182  }
183 
187  protected function getFormFields() {
188  $config = $this->getConfig();
189  $fields = [];
190  if (
191  $config->get( 'EnableUserEmailBlacklist' ) &&
192  $config->get( 'EnableUserEmail' ) &&
193  $this->getUser()->getEmailAuthenticationTimestamp()
194  ) {
195  $fields['email-blacklist'] = [
196  'type' => 'check',
197  'label-message' => [
198  'specialmute-label-mute-email',
199  $this->getTarget() ? $this->getTarget()->getName() : ''
200  ],
201  'default' => $this->isTargetMuted( 'email-blacklist' ),
202  ];
203  }
204 
205  $this->getHookRunner()->onSpecialMuteModifyFormFields( $this->getTarget(), $this->getUser(), $fields );
206 
207  if ( count( $fields ) == 0 ) {
208  throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
209  }
210 
211  return $fields;
212  }
213 
217  private function loadTarget( $username ) {
218  $target = User::newFromName( $username );
219  if ( !$target || !$target->getId() ) {
220  throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
221  } else {
222  $this->target = $target;
223  $this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target );
224  }
225  }
226 
231  public function isTargetMuted( $userOption ) {
232  $muteList = $this->getMuteList( $userOption );
233  return in_array( $this->targetCentralId, $muteList, true );
234  }
235 
242  public function isTargetBlacklisted( $userOption ) {
243  return $this->isTargetMuted( $userOption );
244  }
245 
250  private function getMuteList( $userOption ) {
251  $muteList = $this->userOptionsManager->getOption( $this->getUser(), $userOption );
252  if ( !$muteList ) {
253  return [];
254  }
255 
256  return MultiUsernameFilter::splitIds( $muteList );
257  }
258 }
SpecialMute\getMuteList
getMuteList( $userOption)
Definition: SpecialMute.php:250
SpecialMute\getTarget
getTarget()
Definition: SpecialMute.php:130
SpecialMute\isTargetMuted
isTargetMuted( $userOption)
Definition: SpecialMute.php:231
SpecialMute\muteTarget
muteTarget( $userOption)
Mute target.
Definition: SpecialMute.php:157
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:900
User\getId
getId()
Get the user's ID.
Definition: User.php:2024
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:788
SpecialMute\isTargetBlacklisted
isTargetBlacklisted( $userOption)
Definition: SpecialMute.php:242
SpecialMute\$centralIdLookup
CentralIdLookup $centralIdLookup
Definition: SpecialMute.php:42
SpecialMute\getDescription
getDescription()
Returns the name that goes in the <h1> in the special page itself, and also the name that will be l...
Definition: SpecialMute.php:123
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:545
SpecialMute\getDisplayFormat
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
Definition: SpecialMute.php:85
SpecialMute\$target
User null $target
Definition: SpecialMute.php:36
SpecialMute
A special page that allows users to modify their notification preferences.
Definition: SpecialMute.php:31
SpecialMute\requiresUnblock
requiresUnblock()
Whether this action cannot be executed by a blocked user.bool
Definition: SpecialMute.php:78
SpecialMute\PAGE_NAME
const PAGE_NAME
Definition: SpecialMute.php:33
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:936
SpecialPage\getHookRunner
getHookRunner()
Definition: SpecialPage.php:1083
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:866
SpecialMute\__construct
__construct(UserOptionsManager $userOptionsManager)
Definition: SpecialMute.php:50
SpecialMute\unmuteTarget
unmuteTarget( $userOption)
Un-mute target.
Definition: SpecialMute.php:139
SpecialMute\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialMute.php:92
SpecialMute\loadTarget
loadTarget( $username)
Definition: SpecialMute.php:217
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:798
FormSpecialPage\$par
string null $par
The sub-page of the special page.
Definition: FormSpecialPage.php:36
SpecialPage\requireLogin
requireLogin( $reasonMsg='exception-nologin-text', $titleMsg='exception-nologin')
If the user is not logged in, throws UserNotLoggedIn error.
Definition: SpecialPage.php:386
SpecialMute\onSubmit
onSubmit(array $data, HTMLForm $form=null)
Definition: SpecialMute.php:102
SpecialMute\getFormFields
getFormFields()
Get an HTMLForm descriptor array.array
Definition: SpecialMute.php:187
SpecialMute\$targetCentralId
int $targetCentralId
Definition: SpecialMute.php:39
SpecialMute\execute
execute( $par)
Entry point for special pages.
Definition: SpecialMute.php:61
MediaWiki\Preferences\MultiUsernameFilter
Definition: MultiUsernameFilter.php:25
MediaWiki\User\UserOptionsManager
A service class to control user options.
Definition: UserOptionsManager.php:44
CentralIdLookup
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Definition: CentralIdLookup.php:31
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:30
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:47
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
SpecialMute\getForm
getForm()
Get the HTMLForm to control behavior.HTMLForm|null
Definition: SpecialMute.php:174
SpecialMute\$userOptionsManager
UserOptionsManager $userOptionsManager
Definition: SpecialMute.php:45
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:135