| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use ErrorPageError; |
| 24 |
use MediaWiki\HTMLForm\HTMLForm; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\Preferences\MultiUsernameFilter; |
| 27 |
use MediaWiki\SpecialPage\FormSpecialPage; |
| 28 |
use MediaWiki\User\CentralId\CentralIdLookup; |
| 29 |
use MediaWiki\User\Options\UserOptionsManager; |
| 30 |
use MediaWiki\User\User; |
| 31 |
use MediaWiki\User\UserIdentity; |
| 32 |
use MediaWiki\User\UserIdentityLookup; |
| 33 |
use MediaWiki\User\UserIdentityUtils; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class SpecialMute extends FormSpecialPage { |
| 41 |
|
| 42 |
private const PAGE_NAME = 'Mute'; |
| 43 |
|
| 44 |
|
| 45 |
private $target; |
| 46 |
|
| 47 |
|
| 48 |
private $targetCentralId; |
| 49 |
|
| 50 |
private CentralIdLookup $centralIdLookup; |
| 51 |
private UserOptionsManager $userOptionsManager; |
| 52 |
private UserIdentityLookup $userIdentityLookup; |
| 53 |
private UserIdentityUtils $userIdentityUtils; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function __construct( |
| 62 |
CentralIdLookup $centralIdLookup, |
| 63 |
UserOptionsManager $userOptionsManager, |
| 64 |
UserIdentityLookup $userIdentityLookup, |
| 65 |
UserIdentityUtils $userIdentityUtils |
| 66 |
) { |
| 67 |
parent::__construct( self::PAGE_NAME, '', false ); |
| 68 |
$this->centralIdLookup = $centralIdLookup; |
| 69 |
$this->userOptionsManager = $userOptionsManager; |
| 70 |
$this->userIdentityLookup = $userIdentityLookup; |
| 71 |
$this->userIdentityUtils = $userIdentityUtils; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function execute( $par ) { |
| 80 |
$this->addHelpLink( |
| 81 |
'https://meta.wikimedia.org/wiki/Community_health_initiative/User_Mute_features', |
| 82 |
true |
| 83 |
); |
| 84 |
$this->requireNamedUser( 'specialmute-login-required' ); |
| 85 |
$this->loadTarget( $par ); |
| 86 |
|
| 87 |
parent::execute( $par ); |
| 88 |
|
| 89 |
$out = $this->getOutput(); |
| 90 |
$out->addModules( 'mediawiki.misc-authed-ooui' ); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public function requiresUnblock() { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
protected function getDisplayFormat() { |
| 104 |
return 'ooui'; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function onSuccess() { |
| 111 |
$out = $this->getOutput(); |
| 112 |
$out->addWikiMsg( 'specialmute-success' ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function onSubmit( array $data, ?HTMLForm $form = null ) { |
| 121 |
foreach ( $data as $userOption => $value ) { |
| 122 |
if ( $value ) { |
| 123 |
$this->muteTarget( $userOption ); |
| 124 |
} else { |
| 125 |
$this->unmuteTarget( $userOption ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function getDescription() { |
| 136 |
return $this->msg( 'specialmute' ); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private function getTarget(): ?UserIdentity { |
| 143 |
return $this->target; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private function muteTarget( $userOption ) { |
| 170 |
|
| 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 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
protected function getForm() { |
| 187 |
$target = $this->getTarget(); |
| 188 |
$form = parent::getForm(); |
| 189 |
$form->setId( 'mw-specialmute-form' ); |
| 190 |
$form->setHeaderHtml( $this->msg( 'specialmute-header', $target ? $target->getName() : '' )->parse() ); |
| 191 |
$form->setSubmitTextMsg( 'specialmute-submit' ); |
| 192 |
$form->setSubmitID( 'save' ); |
| 193 |
|
| 194 |
return $form; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
protected function getFormFields() { |
| 201 |
$config = $this->getConfig(); |
| 202 |
$fields = []; |
| 203 |
|
| 204 |
if ( !$config->get( MainConfigNames::EnableUserEmail ) ) { |
| 205 |
throw new ErrorPageError( 'specialmute', 'specialmute-error-email-disabled' ); |
| 206 |
} |
| 207 |
|
| 208 |
if ( !$config->get( MainConfigNames::EnableUserEmailMuteList ) ) { |
| 209 |
throw new ErrorPageError( 'specialmute', 'specialmute-error-mutelist-disabled' ); |
| 210 |
} |
| 211 |
|
| 212 |
if ( !$this->getUser()->isEmailConfirmed() ) { |
| 213 |
throw new ErrorPageError( 'specialmute', 'specialmute-error-no-email-set' ); |
| 214 |
} |
| 215 |
|
| 216 |
$target = $this->getTarget(); |
| 217 |
|
| 218 |
if ( $target && $this->userIdentityUtils->isNamed( $target ) ) { |
| 219 |
$fields['email-blacklist'] = [ |
| 220 |
'type' => 'check', |
| 221 |
'label-message' => [ |
| 222 |
'specialmute-label-mute-email', |
| 223 |
$target->getName() |
| 224 |
], |
| 225 |
'default' => $this->isTargetMuted( 'email-blacklist' ), |
| 226 |
]; |
| 227 |
} |
| 228 |
|
| 229 |
$legacyUser = $target ? User::newFromIdentity( $target ) : null; |
| 230 |
$this->getHookRunner()->onSpecialMuteModifyFormFields( $legacyUser, $this->getUser(), $fields ); |
| 231 |
|
| 232 |
if ( count( $fields ) == 0 ) { |
| 233 |
throw new ErrorPageError( 'specialmute', 'specialmute-error-no-options' ); |
| 234 |
} |
| 235 |
|
| 236 |
return $fields; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
private function loadTarget( $username ) { |
| 243 |
$target = null; |
| 244 |
if ( $username !== null ) { |
| 245 |
$target = $this->userIdentityLookup->getUserIdentityByName( $username ); |
| 246 |
} |
| 247 |
if ( !$target || !$target->isRegistered() ) { |
| 248 |
throw new ErrorPageError( 'specialmute', 'specialmute-error-invalid-user' ); |
| 249 |
} else { |
| 250 |
$this->target = $target; |
| 251 |
$this->targetCentralId = $this->centralIdLookup->centralIdFromLocalUser( $target ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function isTargetMuted( $userOption ) { |
| 260 |
$muteList = $this->getMuteList( $userOption ); |
| 261 |
return in_array( $this->targetCentralId, $muteList, true ); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 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 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
class_alias( SpecialMute::class, 'SpecialMute' ); |