| 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 LogEventsList; |
| 24 |
use MediaWiki\Block\Block; |
| 25 |
use MediaWiki\Block\BlockUtils; |
| 26 |
use MediaWiki\Block\DatabaseBlock; |
| 27 |
use MediaWiki\Block\DatabaseBlockStore; |
| 28 |
use MediaWiki\Block\UnblockUserFactory; |
| 29 |
use MediaWiki\HTMLForm\HTMLForm; |
| 30 |
use MediaWiki\Request\WebRequest; |
| 31 |
use MediaWiki\SpecialPage\SpecialPage; |
| 32 |
use MediaWiki\Title\Title; |
| 33 |
use MediaWiki\Title\TitleValue; |
| 34 |
use MediaWiki\User\UserIdentity; |
| 35 |
use MediaWiki\User\UserNamePrefixSearch; |
| 36 |
use MediaWiki\User\UserNameUtils; |
| 37 |
use MediaWiki\Watchlist\WatchlistManager; |
| 38 |
use Wikimedia\IPUtils; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class SpecialUnblock extends SpecialPage { |
| 46 |
|
| 47 |
|
| 48 |
protected $target; |
| 49 |
|
| 50 |
|
| 51 |
protected $type; |
| 52 |
|
| 53 |
|
| 54 |
protected $block; |
| 55 |
|
| 56 |
private UnblockUserFactory $unblockUserFactory; |
| 57 |
private BlockUtils $blockUtils; |
| 58 |
private DatabaseBlockStore $blockStore; |
| 59 |
private UserNameUtils $userNameUtils; |
| 60 |
private UserNamePrefixSearch $userNamePrefixSearch; |
| 61 |
private WatchlistManager $watchlistManager; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function __construct( |
| 72 |
UnblockUserFactory $unblockUserFactory, |
| 73 |
BlockUtils $blockUtils, |
| 74 |
DatabaseBlockStore $blockStore, |
| 75 |
UserNameUtils $userNameUtils, |
| 76 |
UserNamePrefixSearch $userNamePrefixSearch, |
| 77 |
WatchlistManager $watchlistManager |
| 78 |
) { |
| 79 |
parent::__construct( 'Unblock', 'block' ); |
| 80 |
$this->unblockUserFactory = $unblockUserFactory; |
| 81 |
$this->blockUtils = $blockUtils; |
| 82 |
$this->blockStore = $blockStore; |
| 83 |
$this->userNameUtils = $userNameUtils; |
| 84 |
$this->userNamePrefixSearch = $userNamePrefixSearch; |
| 85 |
$this->watchlistManager = $watchlistManager; |
| 86 |
} |
| 87 |
|
| 88 |
public function doesWrites() { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
public function execute( $par ) { |
| 93 |
$this->checkPermissions(); |
| 94 |
$this->checkReadOnly(); |
| 95 |
|
| 96 |
[ $this->target, $this->type ] = $this->getTargetAndType( $par, $this->getRequest() ); |
| 97 |
$this->block = $this->blockStore->newFromTarget( $this->target ); |
| 98 |
if ( $this->target instanceof UserIdentity ) { |
| 99 |
|
| 100 |
|
| 101 |
$this->getSkin()->setRelevantUser( $this->target ); |
| 102 |
} |
| 103 |
|
| 104 |
$this->setHeaders(); |
| 105 |
$this->outputHeader(); |
| 106 |
$this->addHelpLink( 'Help:Blocking users' ); |
| 107 |
|
| 108 |
$out = $this->getOutput(); |
| 109 |
$out->setPageTitleMsg( $this->msg( 'unblockip' ) ); |
| 110 |
$out->addModules( [ 'mediawiki.userSuggest', 'mediawiki.special.block' ] ); |
| 111 |
|
| 112 |
$form = HTMLForm::factory( 'ooui', $this->getFields(), $this->getContext() ) |
| 113 |
->setWrapperLegendMsg( 'unblockip' ) |
| 114 |
->setSubmitCallback( function ( array $data, HTMLForm $form ) { |
| 115 |
if ( $this->type != Block::TYPE_RANGE |
| 116 |
&& $this->type != Block::TYPE_AUTO |
| 117 |
&& $data['Watch'] |
| 118 |
) { |
| 119 |
$this->watchlistManager->addWatchIgnoringRights( |
| 120 |
$form->getUser(), |
| 121 |
Title::makeTitle( NS_USER, $this->target ) |
| 122 |
); |
| 123 |
} |
| 124 |
return $this->unblockUserFactory->newUnblockUser( |
| 125 |
$data['Target'], |
| 126 |
$form->getContext()->getAuthority(), |
| 127 |
$data['Reason'], |
| 128 |
$data['Tags'] ?? [] |
| 129 |
)->unblock(); |
| 130 |
} ) |
| 131 |
->setSubmitTextMsg( 'ipusubmit' ) |
| 132 |
->addPreHtml( $this->msg( 'unblockiptext' )->parseAsBlock() ); |
| 133 |
|
| 134 |
$userPage = $this->getTargetUserTitle( $this->target ); |
| 135 |
if ( $userPage ) { |
| 136 |
|
| 137 |
$logExtract = ''; |
| 138 |
LogEventsList::showLogExtract( |
| 139 |
$logExtract, |
| 140 |
'block', |
| 141 |
$userPage, |
| 142 |
'', |
| 143 |
[ |
| 144 |
'lim' => 10, |
| 145 |
'msgKey' => [ |
| 146 |
'unblocklog-showlog', |
| 147 |
$userPage->getText(), |
| 148 |
], |
| 149 |
'showIfEmpty' => false |
| 150 |
] |
| 151 |
); |
| 152 |
if ( $logExtract !== '' ) { |
| 153 |
$form->addPostHtml( $logExtract ); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
if ( $this->getAuthority()->isAllowed( 'suppressionlog' ) ) { |
| 158 |
$logExtract = ''; |
| 159 |
LogEventsList::showLogExtract( |
| 160 |
$logExtract, |
| 161 |
'suppress', |
| 162 |
$userPage, |
| 163 |
'', |
| 164 |
[ |
| 165 |
'lim' => 10, |
| 166 |
'conds' => [ 'log_action' => [ 'block', 'reblock', 'unblock' ] ], |
| 167 |
'msgKey' => [ |
| 168 |
'unblocklog-showsuppresslog', |
| 169 |
$userPage->getText(), |
| 170 |
], |
| 171 |
'showIfEmpty' => false |
| 172 |
] |
| 173 |
); |
| 174 |
if ( $logExtract !== '' ) { |
| 175 |
$form->addPostHtml( $logExtract ); |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if ( $form->show() ) { |
| 181 |
switch ( $this->type ) { |
| 182 |
case Block::TYPE_IP: |
| 183 |
$out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) ); |
| 184 |
break; |
| 185 |
case Block::TYPE_USER: |
| 186 |
$out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) ); |
| 187 |
break; |
| 188 |
case Block::TYPE_RANGE: |
| 189 |
$out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) ); |
| 190 |
break; |
| 191 |
case Block::TYPE_ID: |
| 192 |
case Block::TYPE_AUTO: |
| 193 |
$out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) ); |
| 194 |
break; |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private function getTargetAndType( ?string $par, WebRequest $request ) { |
| 210 |
$possibleTargets = [ |
| 211 |
$request->getVal( 'wpTarget', null ), |
| 212 |
$par, |
| 213 |
$request->getVal( 'ip', null ), |
| 214 |
|
| 215 |
$request->getVal( 'wpBlockAddress', null ), |
| 216 |
]; |
| 217 |
foreach ( $possibleTargets as $possibleTarget ) { |
| 218 |
$targetAndType = $this->blockUtils->parseBlockTarget( $possibleTarget ); |
| 219 |
|
| 220 |
if ( $targetAndType[ 1 ] !== null ) { |
| 221 |
break; |
| 222 |
} |
| 223 |
} |
| 224 |
return $targetAndType; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
private function getTargetUserTitle( $target ): ?Title { |
| 234 |
if ( $target instanceof UserIdentity ) { |
| 235 |
return Title::makeTitle( NS_USER, $target->getName() ); |
| 236 |
} |
| 237 |
|
| 238 |
if ( is_string( $target ) && IPUtils::isIPAddress( $target ) ) { |
| 239 |
return Title::makeTitle( NS_USER, $target ); |
| 240 |
} |
| 241 |
|
| 242 |
return null; |
| 243 |
} |
| 244 |
|
| 245 |
protected function getFields() { |
| 246 |
$fields = [ |
| 247 |
'Target' => [ |
| 248 |
'type' => 'text', |
| 249 |
'label-message' => 'ipaddressorusername', |
| 250 |
'autofocus' => true, |
| 251 |
'size' => '45', |
| 252 |
'required' => true, |
| 253 |
'cssclass' => 'mw-autocomplete-user', |
| 254 |
], |
| 255 |
'Name' => [ |
| 256 |
'type' => 'info', |
| 257 |
'label-message' => 'ipaddressorusername', |
| 258 |
], |
| 259 |
'Reason' => [ |
| 260 |
'type' => 'text', |
| 261 |
'label-message' => 'ipbreason', |
| 262 |
] |
| 263 |
]; |
| 264 |
|
| 265 |
if ( $this->block instanceof Block ) { |
| 266 |
$type = $this->block->getType(); |
| 267 |
$targetName = $this->block->getTargetName(); |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if ( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ) { |
| 274 |
$fields['Target']['default'] = $this->target; |
| 275 |
unset( $fields['Name'] ); |
| 276 |
} else { |
| 277 |
$fields['Target']['default'] = $targetName; |
| 278 |
$fields['Target']['type'] = 'hidden'; |
| 279 |
switch ( $type ) { |
| 280 |
case Block::TYPE_IP: |
| 281 |
$fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink( |
| 282 |
$this->getSpecialPageFactory()->getTitleForAlias( 'Contributions/' . $targetName ), |
| 283 |
$targetName |
| 284 |
); |
| 285 |
$fields['Name']['raw'] = true; |
| 286 |
break; |
| 287 |
case Block::TYPE_USER: |
| 288 |
$fields['Name']['default'] = $this->getLinkRenderer()->makeLink( |
| 289 |
new TitleValue( NS_USER, $targetName ), |
| 290 |
$targetName |
| 291 |
); |
| 292 |
$fields['Name']['raw'] = true; |
| 293 |
break; |
| 294 |
|
| 295 |
case Block::TYPE_RANGE: |
| 296 |
$fields['Name']['default'] = $targetName; |
| 297 |
break; |
| 298 |
|
| 299 |
case Block::TYPE_AUTO: |
| 300 |
$fields['Name']['default'] = $this->block->getRedactedName(); |
| 301 |
$fields['Name']['raw'] = true; |
| 302 |
|
| 303 |
$fields['Target']['default'] = "#{$this->target}"; |
| 304 |
break; |
| 305 |
} |
| 306 |
|
| 307 |
$fields['Target']['autofocus'] = false; |
| 308 |
$fields['Reason']['autofocus'] = true; |
| 309 |
} |
| 310 |
} else { |
| 311 |
$fields['Target']['default'] = $this->target; |
| 312 |
unset( $fields['Name'] ); |
| 313 |
} |
| 314 |
|
| 315 |
if ( $this->getUser()->isRegistered() ) { |
| 316 |
$fields['Watch'] = [ |
| 317 |
'type' => 'check', |
| 318 |
'label-message' => 'ipbwatchuser', |
| 319 |
]; |
| 320 |
} |
| 321 |
|
| 322 |
return $fields; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function prefixSearchSubpages( $search, $limit, $offset ) { |
| 334 |
$search = $this->userNameUtils->getCanonical( $search ); |
| 335 |
if ( !$search ) { |
| 336 |
|
| 337 |
return []; |
| 338 |
} |
| 339 |
|
| 340 |
return $this->userNamePrefixSearch |
| 341 |
->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset ); |
| 342 |
} |
| 343 |
|
| 344 |
protected function getGroupName() { |
| 345 |
return 'users'; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
class_alias( SpecialUnblock::class, 'SpecialUnblock' ); |