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 
26 
34 
35  private const PAGE_NAME = 'Mute';
36 
38  private $target;
39 
42 
45 
48 
51 
57  public function __construct(
61  ) {
62  parent::__construct( self::PAGE_NAME, '', false );
63  $this->centralIdLookup = $centralIdLookup;
64  $this->userOptionsManager = $userOptionsManager;
65  $this->userIdentityLookup = $userIdentityLookup;
66  }
67 
73  public function execute( $par ) {
74  $this->addHelpLink(
75  'https://meta.wikimedia.org/wiki/Community_health_initiative/User_Mute_features',
76  true
77  );
78  $this->requireLogin( 'specialmute-login-required' );
79  $this->loadTarget( $par );
80 
81  parent::execute( $par );
82 
83  $out = $this->getOutput();
84  $out->addModules( 'mediawiki.misc-authed-ooui' );
85  }
86 
90  public function requiresUnblock() {
91  return false;
92  }
93 
97  protected function getDisplayFormat() {
98  return 'ooui';
99  }
100 
104  public function onSuccess() {
105  $out = $this->getOutput();
106  $out->addWikiMsg( 'specialmute-success' );
107  }
108 
114  public function onSubmit( array $data, HTMLForm $form = null ) {
115  $hookData = [];
116  foreach ( $data as $userOption => $value ) {
117  $hookData[$userOption]['before'] = $this->isTargetMuted( $userOption );
118  if ( $value ) {
119  $this->muteTarget( $userOption );
120  } else {
121  $this->unmuteTarget( $userOption );
122  }
123  $hookData[$userOption]['after'] = (bool)$value;
124  }
125 
126  // NOTE: this hook is temporary
127  $this->getHookRunner()->onSpecialMuteSubmit( $hookData );
128 
129  return true;
130  }
131 
135  public function getDescription() {
136  return $this->msg( 'specialmute' )->text();
137  }
138 
142  private function getTarget(): ?UserIdentity {
143  return $this->target;
144  }
145 
151  private function unmuteTarget( $userOption ) {
152  $muteList = $this->getMuteList( $userOption );
153 
154  $key = array_search( $this->targetCentralId, $muteList );
155  if ( $key !== false ) {
156  unset( $muteList[$key] );
157  $muteList = implode( "\n", $muteList );
158 
159  $user = $this->getUser();
160  $this->userOptionsManager->setOption( $user, $userOption, $muteList );
161  $user->saveSettings();
162  }
163  }
164 
169  private function muteTarget( $userOption ) {
170  // avoid duplicates just in case
171  if ( !$this->isTargetMuted( $userOption ) ) {
172  $muteList = $this->getMuteList( $userOption );
173 
174  $muteList[] = $this->targetCentralId;
175  $muteList = implode( "\n", $muteList );
176 
177  $user = $this->getUser();
178  $this->userOptionsManager->setOption( $user, $userOption, $muteList );
179  $user->saveSettings();
180  }
181  }
182 
186  protected function getForm() {
187  $target = $this->getTarget();
188  $form = parent::getForm();
189  $form->setId( 'mw-specialmute-form' );
190  $form->setHeaderText( $this->msg( 'specialmute-header', $target ? $target->getName() : '' )->parse() );
191  $form->setSubmitTextMsg( 'specialmute-submit' );
192  $form->setSubmitID( 'save' );
193 
194  return $form;
195  }
196 
200  protected function getFormFields() {
201  $config = $this->getConfig();
202  $fields = [];
203  if (
204  $config->get( 'EnableUserEmailMuteList' ) &&
205  $config->get( 'EnableUserEmail' ) &&
206  $this->getUser()->getEmailAuthenticationTimestamp()
207  ) {
208  $fields['email-blacklist'] = [
209  'type' => 'check',
210  'label-message' => [
211  'specialmute-label-mute-email',
212  $this->getTarget() ? $this->getTarget()->getName() : ''
213  ],
214  'default' => $this->isTargetMuted( 'email-blacklist' ),
215  ];
216  }
217 
218  $legacyUser = $this->getTarget() ? User::newFromIdentity( $this->getTarget() ) : null;
219  $this->getHookRunner()->onSpecialMuteModifyFormFields( $legacyUser, $this->getUser(), $fields );
220 
221  if ( count( $fields ) == 0 ) {
222  throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
223  }
224 
225  return $fields;
226  }
227 
231  private function loadTarget( $username ) {
232  $target = null;
233  if ( $username !== null ) {
234  $target = $this->userIdentityLookup->getUserIdentityByName( $username );
235  }
236  if ( !$target || !$target->isRegistered() ) {
237  throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
238  } else {
239  $this->target = $target;
240  $this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target );
241  }
242  }
243 
248  public function isTargetMuted( $userOption ) {
249  $muteList = $this->getMuteList( $userOption );
250  return in_array( $this->targetCentralId, $muteList, true );
251  }
252 
259  public function isTargetBlacklisted( $userOption ) {
260  wfDeprecated( __METHOD__, '1.35' );
261  return $this->isTargetMuted( $userOption );
262  }
263 
268  private function getMuteList( $userOption ) {
269  $muteList = $this->userOptionsManager->getOption( $this->getUser(), $userOption );
270  if ( !$muteList ) {
271  return [];
272  }
273 
274  return MultiUsernameFilter::splitIds( $muteList );
275  }
276 }
SpecialMute\getMuteList
getMuteList( $userOption)
Definition: SpecialMute.php:268
SpecialMute\getTarget
getTarget()
Definition: SpecialMute.php:142
SpecialMute\isTargetMuted
isTargetMuted( $userOption)
Definition: SpecialMute.php:248
SpecialMute\muteTarget
muteTarget( $userOption)
Mute target.
Definition: SpecialMute.php:169
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:912
SpecialMute\$userIdentityLookup
UserIdentityLookup $userIdentityLookup
Definition: SpecialMute.php:50
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:790
SpecialMute\isTargetBlacklisted
isTargetBlacklisted( $userOption)
Definition: SpecialMute.php:259
SpecialMute\$centralIdLookup
CentralIdLookup $centralIdLookup
Definition: SpecialMute.php:44
SpecialMute\__construct
__construct(CentralIdLookup $centralIdLookup, UserOptionsManager $userOptionsManager, UserIdentityLookup $userIdentityLookup)
Definition: SpecialMute.php:57
SpecialMute\$target
UserIdentity null $target
Definition: SpecialMute.php:38
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:135
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:683
SpecialMute\getDisplayFormat
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
Definition: SpecialMute.php:97
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
SpecialMute
A special page that allows users to modify their notification preferences.
Definition: SpecialMute.php:33
SpecialMute\requiresUnblock
requiresUnblock()
Whether this action cannot be executed by a blocked user.bool
Definition: SpecialMute.php:90
SpecialMute\PAGE_NAME
const PAGE_NAME
Definition: SpecialMute.php:35
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:948
SpecialPage\getHookRunner
getHookRunner()
Definition: SpecialPage.php:1095
SpecialPage\getConfig
getConfig()
Shortcut to get main config object.
Definition: SpecialPage.php:878
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:989
MediaWiki\User\UserIdentity\isRegistered
isRegistered()
SpecialMute\unmuteTarget
unmuteTarget( $userOption)
Un-mute target.
Definition: SpecialMute.php:151
SpecialMute\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialMute.php:104
SpecialMute\loadTarget
loadTarget( $username)
Definition: SpecialMute.php:231
MediaWiki\User\UserIdentity\getName
getName()
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:800
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:388
MediaWiki\User\UserIdentityLookup
Definition: UserIdentityLookup.php:33
SpecialMute\onSubmit
onSubmit(array $data, HTMLForm $form=null)
Definition: SpecialMute.php:114
SpecialMute\getFormFields
getFormFields()
Get an HTMLForm descriptor array.array
Definition: SpecialMute.php:200
SpecialMute\$targetCentralId
int $targetCentralId
Definition: SpecialMute.php:41
SpecialMute\execute
execute( $par)
Entry point for special pages.
Definition: SpecialMute.php:73
MediaWiki\Preferences\MultiUsernameFilter
Definition: MultiUsernameFilter.php:27
MediaWiki\User\UserOptionsManager
A service class to control user options.
Definition: UserOptionsManager.php:43
CentralIdLookup
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Definition: CentralIdLookup.php:35
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:30
SpecialMute\getForm
getForm()
Get the HTMLForm to control behavior.HTMLForm|null
Definition: SpecialMute.php:186
SpecialMute\$userOptionsManager
UserOptionsManager $userOptionsManager
Definition: SpecialMute.php:47
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:143