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->enableUserEmailBlacklist = $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->isTargetBlacklisted( $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 
135  private function unmuteTarget( $userOption ) {
136  $blacklist = $this->getBlacklist( $userOption );
137 
138  $key = array_search( $this->targetCentralId, $blacklist );
139  if ( $key !== false ) {
140  unset( $blacklist[$key] );
141  $blacklist = implode( "\n", $blacklist );
142 
143  $user = $this->getUser();
144  $user->setOption( $userOption, $blacklist );
145  $user->saveSettings();
146  }
147  }
148 
153  private function muteTarget( $userOption ) {
154  // avoid duplicates just in case
155  if ( !$this->isTargetBlacklisted( $userOption ) ) {
156  $blacklist = $this->getBlacklist( $userOption );
157 
158  $blacklist[] = $this->targetCentralId;
159  $blacklist = implode( "\n", $blacklist );
160 
161  $user = $this->getUser();
162  $user->setOption( $userOption, $blacklist );
163  $user->saveSettings();
164  }
165  }
166 
170  protected function getForm() {
171  $form = parent::getForm();
172  $form->setId( 'mw-specialmute-form' );
173  $form->setHeaderText( $this->msg( 'specialmute-header', $this->target )->parse() );
174  $form->setSubmitTextMsg( 'specialmute-submit' );
175  $form->setSubmitID( 'save' );
176 
177  return $form;
178  }
179 
183  protected function getFormFields() {
184  $fields = [];
185  if (
186  $this->enableUserEmailBlacklist &&
187  $this->enableUserEmail &&
188  $this->getUser()->getEmailAuthenticationTimestamp()
189  ) {
190  $fields['email-blacklist'] = [
191  'type' => 'check',
192  'label-message' => 'specialmute-label-mute-email',
193  'default' => $this->isTargetBlacklisted( 'email-blacklist' ),
194  ];
195  }
196 
197  $this->getHookRunner()->onSpecialMuteModifyFormFields( $this, $fields );
198 
199  if ( count( $fields ) == 0 ) {
200  throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
201  }
202 
203  return $fields;
204  }
205 
209  private function loadTarget( $username ) {
210  $target = User::newFromName( $username );
211  if ( !$target || !$target->getId() ) {
212  throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
213  } else {
214  $this->target = $target;
215  $this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target );
216  }
217  }
218 
223  public function isTargetBlacklisted( $userOption ) {
224  $blacklist = $this->getBlacklist( $userOption );
225  return in_array( $this->targetCentralId, $blacklist, true );
226  }
227 
232  private function getBlacklist( $userOption ) {
233  $blacklist = $this->getUser()->getOption( $userOption );
234  if ( !$blacklist ) {
235  return [];
236  }
237 
238  return MultiUsernameFilter::splitIds( $blacklist );
239  }
240 }
SpecialMute\muteTarget
muteTarget( $userOption)
Mute target.
Definition: SpecialMute.php:153
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:800
User\getId
getId()
Get the user's ID.
Definition: User.php:2108
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:716
SpecialMute\isTargetBlacklisted
isTargetBlacklisted( $userOption)
Definition: SpecialMute.php:223
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:535
SpecialMute\getDisplayFormat
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
Definition: SpecialMute.php:88
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:836
SpecialPage\getHookRunner
getHookRunner()
Definition: SpecialPage.php:974
SpecialMute\unmuteTarget
unmuteTarget( $userOption)
Un-mute target.
Definition: SpecialMute.php:135
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:209
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:726
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:358
SpecialMute\onSubmit
onSubmit(array $data, HTMLForm $form=null)
Definition: SpecialMute.php:105
SpecialMute\$enableUserEmail
bool $enableUserEmail
Definition: SpecialMute.php:43
SpecialMute\$target
User $target
Definition: SpecialMute.php:34
SpecialMute\getFormFields
getFormFields()
Get an HTMLForm descriptor array.array
Definition: SpecialMute.php:183
SpecialMute\__construct
__construct()
Definition: SpecialMute.php:48
SpecialMute\$targetCentralId
int $targetCentralId
Definition: SpecialMute.php:37
SpecialMute\$enableUserEmailBlacklist
bool $enableUserEmailBlacklist
Definition: SpecialMute.php:40
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:451
MediaWiki\Preferences\MultiUsernameFilter
Definition: MultiUsernameFilter.php:25
CentralIdLookup
The CentralIdLookup service allows for connecting local users with cluster-wide IDs.
Definition: CentralIdLookup.php:30
SpecialMute\getBlacklist
getBlacklist( $userOption)
Definition: SpecialMute.php:232
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:27
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:46
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
SpecialMute\getForm
getForm()
Get the HTMLForm to control behavior.HTMLForm|null
Definition: SpecialMute.php:170
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:133