| 1 |
<?php |
| 2 |
|
| 3 |
> |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 25 |
use MediaWiki\Block\Block; |
| 26 |
use MediaWiki\Block\BlockPermissionCheckerFactory; |
| 27 |
use MediaWiki\Block\UnblockUserFactory; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\ParamValidator\TypeDef\UserDef; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 32 |
use MediaWiki\User\UserIdentityLookup; |
| 33 |
use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 34 |
use MediaWiki\Watchlist\WatchlistManager; |
| 35 |
use Wikimedia\ParamValidator\ParamValidator; |
| 36 |
use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ApiUnblock extends ApiBase { |
| 45 |
|
| 46 |
use ApiBlockInfoTrait; |
| 47 |
use ApiWatchlistTrait; |
| 48 |
|
| 49 |
private BlockPermissionCheckerFactory $permissionCheckerFactory; |
| 50 |
private UnblockUserFactory $unblockUserFactory; |
| 51 |
private UserIdentityLookup $userIdentityLookup; |
| 52 |
private WatchedItemStoreInterface $watchedItemStore; |
| 53 |
|
| 54 |
public function __construct( |
| 55 |
ApiMain $main, |
| 56 |
string $action, |
| 57 |
BlockPermissionCheckerFactory $permissionCheckerFactory, |
| 58 |
UnblockUserFactory $unblockUserFactory, |
| 59 |
UserIdentityLookup $userIdentityLookup, |
| 60 |
WatchedItemStoreInterface $watchedItemStore, |
| 61 |
WatchlistManager $watchlistManager, |
| 62 |
UserOptionsLookup $userOptionsLookup |
| 63 |
) { |
| 64 |
parent::__construct( $main, $action ); |
| 65 |
|
| 66 |
$this->permissionCheckerFactory = $permissionCheckerFactory; |
| 67 |
$this->unblockUserFactory = $unblockUserFactory; |
| 68 |
$this->userIdentityLookup = $userIdentityLookup; |
| 69 |
$this->watchedItemStore = $watchedItemStore; |
| 70 |
|
| 71 |
|
| 72 |
$this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 73 |
$this->watchlistMaxDuration = |
| 74 |
$this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 75 |
$this->watchlistManager = $watchlistManager; |
| 76 |
$this->userOptionsLookup = $userOptionsLookup; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function execute() { |
| 83 |
$performer = $this->getUser(); |
| 84 |
$params = $this->extractRequestParams(); |
| 85 |
|
| 86 |
$this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' ); |
| 87 |
|
| 88 |
if ( !$this->getAuthority()->isAllowed( 'block' ) ) { |
| 89 |
$this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( $params['userid'] !== null ) { |
| 93 |
$identity = $this->userIdentityLookup->getUserIdentityByUserId( $params['userid'] ); |
| 94 |
if ( !$identity ) { |
| 95 |
$this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' ); |
| 96 |
} |
| 97 |
$params['user'] = $identity->getName(); |
| 98 |
} |
| 99 |
|
| 100 |
$target = $params['id'] === null ? $params['user'] : "#{$params['id']}"; |
| 101 |
|
| 102 |
|
| 103 |
$status = $this->permissionCheckerFactory |
| 104 |
->newBlockPermissionChecker( |
| 105 |
$target, |
| 106 |
$this->getAuthority() |
| 107 |
)->checkBlockPermissions(); |
| 108 |
if ( $status !== true ) { |
| 109 |
$this->dieWithError( |
| 110 |
$status, |
| 111 |
null, |
| 112 |
|
| 113 |
[ 'blockinfo' => $this->getBlockDetails( $performer->getBlock() ) ] |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
$status = $this->unblockUserFactory->newUnblockUser( |
| 118 |
$target, |
| 119 |
$this->getAuthority(), |
| 120 |
$params['reason'], |
| 121 |
$params['tags'] ?? [] |
| 122 |
)->unblock(); |
| 123 |
|
| 124 |
if ( !$status->isOK() ) { |
| 125 |
$this->dieStatus( $status ); |
| 126 |
} |
| 127 |
|
| 128 |
$block = $status->getValue(); |
| 129 |
$targetType = $block->getType(); |
| 130 |
$targetName = $targetType === Block::TYPE_AUTO ? '' : $block->getTargetName(); |
| 131 |
$targetUserId = $block->getTargetUserIdentity() ? $block->getTargetUserIdentity()->getId() : 0; |
| 132 |
|
| 133 |
$watchlistExpiry = $this->getExpiryFromParams( $params ); |
| 134 |
$watchuser = $params['watchuser']; |
| 135 |
$userPage = Title::makeTitle( NS_USER, $targetName ); |
| 136 |
if ( $watchuser && $targetType !== Block::TYPE_RANGE && $targetType !== Block::TYPE_AUTO ) { |
| 137 |
$this->setWatch( 'watch', $userPage, $this->getUser(), null, $watchlistExpiry ); |
| 138 |
} else { |
| 139 |
$watchuser = false; |
| 140 |
$watchlistExpiry = null; |
| 141 |
} |
| 142 |
|
| 143 |
$res = [ |
| 144 |
'id' => $block->getId(), |
| 145 |
'user' => $targetName, |
| 146 |
'userid' => $targetUserId, |
| 147 |
'reason' => $params['reason'], |
| 148 |
'watchuser' => $watchuser, |
| 149 |
]; |
| 150 |
if ( $watchlistExpiry !== null ) { |
| 151 |
$res['watchlistexpiry'] = $this->getWatchlistExpiry( |
| 152 |
$this->watchedItemStore, |
| 153 |
$userPage, |
| 154 |
$this->getUser() |
| 155 |
); |
| 156 |
} |
| 157 |
$this->getResult()->addValue( null, $this->getModuleName(), $res ); |
| 158 |
} |
| 159 |
|
| 160 |
public function mustBePosted() { |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
public function isWriteMode() { |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
public function getAllowedParams() { |
| 169 |
$params = [ |
| 170 |
'id' => [ |
| 171 |
ParamValidator::PARAM_TYPE => 'integer', |
| 172 |
], |
| 173 |
'user' => [ |
| 174 |
ParamValidator::PARAM_TYPE => 'user', |
| 175 |
UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'cidr', 'id' ], |
| 176 |
], |
| 177 |
'userid' => [ |
| 178 |
ParamValidator::PARAM_TYPE => 'integer', |
| 179 |
ParamValidator::PARAM_DEPRECATED => true, |
| 180 |
], |
| 181 |
'reason' => '', |
| 182 |
'tags' => [ |
| 183 |
ParamValidator::PARAM_TYPE => 'tags', |
| 184 |
ParamValidator::PARAM_ISMULTI => true, |
| 185 |
], |
| 186 |
'watchuser' => false, |
| 187 |
]; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
if ( $this->watchlistExpiryEnabled ) { |
| 193 |
$params += [ |
| 194 |
'watchlistexpiry' => [ |
| 195 |
ParamValidator::PARAM_TYPE => 'expiry', |
| 196 |
ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration, |
| 197 |
ExpiryDef::PARAM_USE_MAX => true, |
| 198 |
] |
| 199 |
]; |
| 200 |
} |
| 201 |
|
| 202 |
return $params; |
| 203 |
} |
| 204 |
|
| 205 |
public function needsToken() { |
| 206 |
return 'csrf'; |
| 207 |
} |
| 208 |
|
| 209 |
protected function getExamplesMessages() { |
| 210 |
return [ |
| 211 |
'action=unblock&id=105' |
| 212 |
=> 'apihelp-unblock-example-id', |
| 213 |
'action=unblock&user=Bob&reason=Sorry%20Bob' |
| 214 |
=> 'apihelp-unblock-example-user', |
| 215 |
]; |
| 216 |
} |
| 217 |
|
| 218 |
public function getHelpUrls() { |
| 219 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block'; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
class_alias( ApiUnblock::class, 'ApiUnblock' ); |