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  */
22 
30 
31  private 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->enableUserEmailMuteList = $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->addHelpLink(
66  'https://meta.wikimedia.org/wiki/Community_health_initiative/User_Mute_features',
67  true
68  );
69  $this->requireLogin( 'specialmute-login-required' );
70  $this->loadTarget( $par );
71 
72  parent::execute( $par );
73 
74  $out = $this->getOutput();
75  $out->addModules( 'mediawiki.misc-authed-ooui' );
76  }
77 
81  public function requiresUnblock() {
82  return false;
83  }
84 
88  protected function getDisplayFormat() {
89  return 'ooui';
90  }
91 
95  public function onSuccess() {
96  $out = $this->getOutput();
97  $out->addWikiMsg( 'specialmute-success' );
98  }
99 
105  public function onSubmit( array $data, HTMLForm $form = null ) {
106  $hookData = [];
107  foreach ( $data as $userOption => $value ) {
108  $hookData[$userOption]['before'] = $this->isTargetMuted( $userOption );
109  if ( $value ) {
110  $this->muteTarget( $userOption );
111  } else {
112  $this->unmuteTarget( $userOption );
113  }
114  $hookData[$userOption]['after'] = (bool)$value;
115  }
116 
117  // NOTE: this hook is temporary
118  $this->getHookRunner()->onSpecialMuteSubmit( $hookData );
119 
120  return true;
121  }
122 
126  public function getDescription() {
127  return $this->msg( 'specialmute' )->text();
128  }
129 
133  public function getTarget(): ?User {
134  return $this->target;
135  }
136 
142  private function unmuteTarget( $userOption ) {
143  $muteList = $this->getMuteList( $userOption );
144 
145  $key = array_search( $this->targetCentralId, $muteList );
146  if ( $key !== false ) {
147  unset( $muteList[$key] );
148  $muteList = implode( "\n", $muteList );
149 
150  $user = $this->getUser();
151  $user->setOption( $userOption, $muteList );
152  $user->saveSettings();
153  }
154  }
155 
160  private function muteTarget( $userOption ) {
161  // avoid duplicates just in case
162  if ( !$this->isTargetMuted( $userOption ) ) {
163  $muteList = $this->getMuteList( $userOption );
164 
165  $muteList[] = $this->targetCentralId;
166  $muteList = implode( "\n", $muteList );
167 
168  $user = $this->getUser();
169  $user->setOption( $userOption, $muteList );
170  $user->saveSettings();
171  }
172  }
173 
177  protected function getForm() {
178  $form = parent::getForm();
179  $form->setId( 'mw-specialmute-form' );
180  $form->setHeaderText( $this->msg( 'specialmute-header', $this->target )->parse() );
181  $form->setSubmitTextMsg( 'specialmute-submit' );
182  $form->setSubmitID( 'save' );
183 
184  return $form;
185  }
186 
190  protected function getFormFields() {
191  $fields = [];
192  if (
193  $this->enableUserEmailMuteList &&
194  $this->enableUserEmail &&
195  $this->getUser()->getEmailAuthenticationTimestamp()
196  ) {
197  $fields['email-blacklist'] = [
198  'type' => 'check',
199  'label-message' => [
200  'specialmute-label-mute-email',
201  $this->getTarget() ? $this->getTarget()->getName() : ''
202  ],
203  'default' => $this->isTargetMuted( 'email-blacklist' ),
204  ];
205  }
206 
207  $this->getHookRunner()->onSpecialMuteModifyFormFields( $this, $fields );
208 
209  if ( count( $fields ) == 0 ) {
210  throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
211  }
212 
213  return $fields;
214  }
215 
219  private function loadTarget( $username ) {
220  $target = User::newFromName( $username );
221  if ( !$target || !$target->getId() ) {
222  throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
223  } else {
224  $this->target = $target;
225  $this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target );
226  }
227  }
228 
233  public function isTargetMuted( $userOption ) {
234  $muteList = $this->getMuteList( $userOption );
235  return in_array( $this->targetCentralId, $muteList, true );
236  }
237 
244  public function isTargetBlacklisted( $userOption ) {
245  return $this->isTargetMuted( $userOption );
246  }
247 
252  private function getMuteList( $userOption ) {
253  $muteList = $this->getUser()->getOption( $userOption );
254  if ( !$muteList ) {
255  return [];
256  }
257 
258  return MultiUsernameFilter::splitIds( $muteList );
259  }
260 }
SpecialMute\getMuteList
getMuteList( $userOption)
Definition: SpecialMute.php:252
SpecialMute\getTarget
getTarget()
Definition: SpecialMute.php:133
SpecialMute\isTargetMuted
isTargetMuted( $userOption)
Definition: SpecialMute.php:233
SpecialMute\muteTarget
muteTarget( $userOption)
Mute target.
Definition: SpecialMute.php:160
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:828
User\getId
getId()
Get the user's ID.
Definition: User.php:2025
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:744
SpecialMute\isTargetBlacklisted
isTargetBlacklisted( $userOption)
Definition: SpecialMute.php:244
SpecialMute\$centralIdLookup
CentralIdLookup $centralIdLookup
Definition: SpecialMute.php:46
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:126
FormSpecialPage
Special page which uses an HTMLForm to handle processing.
Definition: FormSpecialPage.php:31
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:539
SpecialMute\getDisplayFormat
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
Definition: SpecialMute.php:88
SpecialMute\$target
User null $target
Definition: SpecialMute.php:34
SpecialMute
A special page that allows users to modify their notification preferences.
Definition: SpecialMute.php:29
SpecialMute\requiresUnblock
requiresUnblock()
Whether this action cannot be executed by a blocked user.bool
Definition: SpecialMute.php:81
SpecialMute\PAGE_NAME
const PAGE_NAME
Definition: SpecialMute.php:31
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:864
SpecialPage\getHookRunner
getHookRunner()
Definition: SpecialPage.php:1010
SpecialMute\unmuteTarget
unmuteTarget( $userOption)
Un-mute target.
Definition: SpecialMute.php:142
SpecialMute\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialMute.php:95
SpecialMute\loadTarget
loadTarget( $username)
Definition: SpecialMute.php:219
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:754
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:373
SpecialMute\onSubmit
onSubmit(array $data, HTMLForm $form=null)
Definition: SpecialMute.php:105
SpecialMute\$enableUserEmail
bool $enableUserEmail
Definition: SpecialMute.php:43
SpecialMute\getFormFields
getFormFields()
Get an HTMLForm descriptor array.array
Definition: SpecialMute.php:190
SpecialMute\$enableUserEmailMuteList
bool $enableUserEmailMuteList
Definition: SpecialMute.php:40
SpecialMute\__construct
__construct()
Definition: SpecialMute.php:48
SpecialMute\$targetCentralId
int $targetCentralId
Definition: SpecialMute.php:37
SpecialMute\execute
execute( $par)
Entry point for special pages.
Definition: SpecialMute.php:64
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:453
MediaWiki\Preferences\MultiUsernameFilter
Definition: MultiUsernameFilter.php:25
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:177
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:135