MediaWiki  1.34.0
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  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->requireLogin( 'specialmute-login-required' );
66  $this->loadTarget( $par );
67 
68  parent::execute( $par );
69 
70  $out = $this->getOutput();
71  $out->addModules( 'mediawiki.misc-authed-ooui' );
72  }
73 
77  public function requiresUnblock() {
78  return false;
79  }
80 
84  protected function getDisplayFormat() {
85  return 'ooui';
86  }
87 
91  public function onSuccess() {
92  $out = $this->getOutput();
93  $out->addWikiMsg( 'specialmute-success' );
94  }
95 
101  public function onSubmit( array $data, HTMLForm $form = null ) {
102  $hookData = [];
103  foreach ( $data as $userOption => $value ) {
104  $hookData[$userOption]['before'] = $this->isTargetBlacklisted( $userOption );
105  if ( $value ) {
106  $this->muteTarget( $userOption );
107  } else {
108  $this->unmuteTarget( $userOption );
109  }
110  $hookData[$userOption]['after'] = (bool)$value;
111  }
112 
113  // NOTE: this hook is temporary
114  Hooks::run( 'SpecialMuteSubmit', [ $hookData ] );
115 
116  return true;
117  }
118 
122  public function getDescription() {
123  return $this->msg( 'specialmute' )->text();
124  }
125 
131  private function unmuteTarget( $userOption ) {
132  $blacklist = $this->getBlacklist( $userOption );
133 
134  $key = array_search( $this->targetCentralId, $blacklist );
135  if ( $key !== false ) {
136  unset( $blacklist[$key] );
137  $blacklist = implode( "\n", $blacklist );
138 
139  $user = $this->getUser();
140  $user->setOption( $userOption, $blacklist );
141  $user->saveSettings();
142  }
143  }
144 
149  private function muteTarget( $userOption ) {
150  // avoid duplicates just in case
151  if ( !$this->isTargetBlacklisted( $userOption ) ) {
152  $blacklist = $this->getBlacklist( $userOption );
153 
154  $blacklist[] = $this->targetCentralId;
155  $blacklist = implode( "\n", $blacklist );
156 
157  $user = $this->getUser();
158  $user->setOption( $userOption, $blacklist );
159  $user->saveSettings();
160  }
161  }
162 
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' );
172 
173  return $form;
174  }
175 
179  protected function getFormFields() {
180  $fields = [];
181  if (
182  $this->enableUserEmailBlacklist &&
183  $this->enableUserEmail &&
184  $this->getUser()->getEmailAuthenticationTimestamp()
185  ) {
186  $fields['email-blacklist'] = [
187  'type' => 'check',
188  'label-message' => 'specialmute-label-mute-email',
189  'default' => $this->isTargetBlacklisted( 'email-blacklist' ),
190  ];
191  }
192 
193  Hooks::run( 'SpecialMuteModifyFormFields', [ $this, &$fields ] );
194 
195  if ( count( $fields ) == 0 ) {
196  throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' );
197  }
198 
199  return $fields;
200  }
201 
205  private function loadTarget( $username ) {
206  $target = User::newFromName( $username );
207  if ( !$target || !$target->getId() ) {
208  throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' );
209  } else {
210  $this->target = $target;
211  $this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target );
212  }
213  }
214 
219  public function isTargetBlacklisted( $userOption ) {
220  $blacklist = $this->getBlacklist( $userOption );
221  return in_array( $this->targetCentralId, $blacklist, true );
222  }
223 
228  private function getBlacklist( $userOption ) {
229  $blacklist = $this->getUser()->getOption( $userOption );
230  if ( !$blacklist ) {
231  return [];
232  }
233 
234  return MultiUsernameFilter::splitIds( $blacklist );
235  }
236 }
SpecialMute\muteTarget
muteTarget( $userOption)
Mute target.
Definition: SpecialMute.php:149
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
User\getId
getId()
Get the user's ID.
Definition: User.php:2203
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
SpecialMute\isTargetBlacklisted
isTargetBlacklisted( $userOption)
Definition: SpecialMute.php:219
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:122
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:515
SpecialMute\getDisplayFormat
getDisplayFormat()
Get display format for the form.See HTMLForm documentation for available values.1....
Definition: SpecialMute.php:84
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:77
SpecialMute\PAGE_NAME
const PAGE_NAME
Definition: SpecialMute.php:31
SpecialMute\unmuteTarget
unmuteTarget( $userOption)
Un-mute target.
Definition: SpecialMute.php:131
SpecialMute\onSuccess
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
Definition: SpecialMute.php:91
SpecialMute\loadTarget
loadTarget( $username)
Definition: SpecialMute.php:205
SpecialPage\getUser
getUser()
Shortcut to get the User executing this instance.
Definition: SpecialPage.php:729
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:345
SpecialMute\onSubmit
onSubmit(array $data, HTMLForm $form=null)
Definition: SpecialMute.php:101
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:179
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:431
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:228
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:51
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
SpecialMute\getForm
getForm()
Get the HTMLForm to control behavior.HTMLForm|null
Definition: SpecialMute.php:166
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131