85 parent::__construct( $main, $action );
87 $this->blockPermissionCheckerFactory = $blockPermissionCheckerFactory;
88 $this->blockUserFactory = $blockUserFactory;
89 $this->titleFactory = $titleFactory;
90 $this->userIdentityLookup = $userIdentityLookup;
91 $this->watchedItemStore = $watchedItemStore;
92 $this->blockTargetFactory = $blockTargetFactory;
93 $this->blockActionInfo = $blockActionInfo;
94 $this->blockStore = $blockStore;
98 $this->watchlistMaxDuration =
100 $this->watchlistManager = $watchlistManager;
101 $this->userOptionsLookup = $userOptionsLookup;
115 'id', [
'newblock',
'reblock' ] );
117 if ( $params[
'id'] !==
null ) {
118 $block = $this->blockStore->newFromID( $params[
'id'],
true );
121 [
'apierror-nosuchblockid', $params[
'id'] ],
124 if ( $block->getType() === AbstractBlock::TYPE_AUTO ) {
127 $status = $this->updateBlock( $block, $params );
129 if ( $params[
'user'] !==
null ) {
130 $target = $this->blockTargetFactory->newFromUser( $params[
'user'] );
132 $targetUser = $this->userIdentityLookup->getUserIdentityByUserId( $params[
'userid'] );
133 if ( !$targetUser ) {
134 $this->
dieWithError( [
'apierror-nosuchuserid', $params[
'userid'] ],
'nosuchuserid' );
136 $target = $this->blockTargetFactory->newUserBlockTarget( $targetUser );
138 if ( $params[
'newblock'] ) {
139 $status = $this->insertBlock( $target, $params );
141 $blocks = $this->blockStore->newListFromTarget(
142 $target,
null,
false, DatabaseBlockStore::AUTO_NONE );
143 if ( count( $blocks ) === 0 ) {
144 $status = $this->insertBlock( $target, $params );
145 } elseif ( count( $blocks ) === 1 ) {
146 if ( $params[
'reblock'] ) {
147 $status = $this->updateBlock( $blocks[0], $params );
149 $status = Status::newFatal(
'ipb_already_blocked', $blocks[0]->getTargetName() );
152 $this->
dieWithError(
'apierror-ambiguous-block',
'ambiguous-block' );
157 if ( !$status->isOK() ) {
161 $block = $status->value;
163 throw new RuntimeException(
"Unexpected block class" );
167 $userPage = Title::makeTitle(
NS_USER, $block->getTargetName() );
169 if ( $params[
'watchuser'] && $block->getType() !== AbstractBlock::TYPE_RANGE ) {
170 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
175 $res[
'user'] = $block->getTargetName();
177 $blockedUser = $block->getTargetUserIdentity();
178 $res[
'userID'] = $blockedUser ? $blockedUser->getId() : 0;
181 $res[
'id'] = $block->getId();
183 $res[
'reason'] = $params[
'reason'];
184 $res[
'anononly'] = $params[
'anononly'];
185 $res[
'nocreate'] = $params[
'nocreate'];
186 $res[
'autoblock'] = $params[
'autoblock'];
187 $res[
'noemail'] = $params[
'noemail'];
188 $res[
'hidename'] = $block->getHideName();
189 $res[
'allowusertalk'] = $params[
'allowusertalk'];
190 $res[
'watchuser'] = $params[
'watchuser'];
191 if ( $watchlistExpiry ) {
193 $this->watchedItemStore,
197 $res[
'watchlistexpiry'] = $expiry;
199 $res[
'partial'] = $params[
'partial'];
200 $res[
'pagerestrictions'] = $params[
'pagerestrictions'];
201 $res[
'namespacerestrictions'] = $params[
'namespacerestrictions'];
203 $res[
'actionrestrictions'] = $params[
'actionrestrictions'];
215 private function getBlockOptions( $params ) {
217 'isCreateAccountBlocked' => $params[
'nocreate'],
218 'isEmailBlocked' => $params[
'noemail'],
219 'isHardBlock' => !$params[
'anononly'],
220 'isAutoblocking' => $params[
'autoblock'],
221 'isUserTalkEditBlocked' => !$params[
'allowusertalk'],
222 'isHideUser' => $params[
'hidename'],
223 'isPartial' => $params[
'partial'],
232 private function getRestrictions( $params ) {
234 if ( $params[
'partial'] ) {
235 $pageRestrictions = array_map(
236 [ PageRestriction::class,
'newFromTitle' ],
237 (array)$params[
'pagerestrictions']
240 $namespaceRestrictions = array_map(
static function ( $id ) {
241 return new NamespaceRestriction( 0, $id );
242 }, (array)$params[
'namespacerestrictions'] );
243 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
246 $actionRestrictions = array_map(
function ( $action ) {
247 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
248 }, (array)$params[
'actionrestrictions'] );
249 $restrictions = array_merge( $restrictions, $actionRestrictions );
252 return $restrictions;
260 private function checkEmailPermissions( $params ) {
262 $params[
'noemail'] &&
263 !$this->blockPermissionCheckerFactory
265 ->checkEmailPermissions()
278 private function updateBlock( DatabaseBlock $block, $params ) {
279 $this->checkEmailPermissions( $params );
280 return $this->blockUserFactory->newUpdateBlock(
285 $this->getBlockOptions( $params ),
286 $this->getRestrictions( $params ),
298 private function insertBlock( $target, $params ) {
299 $this->checkEmailPermissions( $params );
300 return $this->blockUserFactory->newBlockUser(
305 $this->getBlockOptions( $params ),
306 $this->getRestrictions( $params ),
321 'id' => [ ParamValidator::PARAM_TYPE =>
'integer' ],
323 ParamValidator::PARAM_TYPE =>
'user',
324 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr',
'id' ],
325 UserDef::PARAM_RETURN_OBJECT =>
true,
328 ParamValidator::PARAM_TYPE =>
'integer',
329 ParamValidator::PARAM_DEPRECATED =>
true,
335 'autoblock' =>
false,
338 'allowusertalk' =>
false,
341 'watchuser' =>
false,
346 if ( $this->watchlistExpiryEnabled ) {
348 'watchlistexpiry' => [
349 ParamValidator::PARAM_TYPE =>
'expiry',
350 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
351 ExpiryDef::PARAM_USE_MAX =>
true,
360 ParamValidator::PARAM_TYPE =>
'tags',
361 ParamValidator::PARAM_ISMULTI =>
true,
364 'pagerestrictions' => [
365 ParamValidator::PARAM_TYPE =>
'title',
366 TitleDef::PARAM_MUST_EXIST =>
true,
374 ParamValidator::PARAM_ISMULTI =>
true,
375 ParamValidator::PARAM_ISMULTI_LIMIT1 => $pageLimit,
376 ParamValidator::PARAM_ISMULTI_LIMIT2 => $pageLimit,
378 'namespacerestrictions' => [
379 ParamValidator::PARAM_ISMULTI =>
true,
380 ParamValidator::PARAM_TYPE =>
'namespace',
386 'actionrestrictions' => [
387 ParamValidator::PARAM_ISMULTI =>
true,
388 ParamValidator::PARAM_TYPE => array_keys(
389 $this->blockActionInfo->getAllBlockActions()
405 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
406 =>
'apihelp-block-example-ip-simple',
407 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
408 =>
'apihelp-block-example-user-complex',
414 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
419class_alias( ApiBlock::class,
'ApiBlock' );
This is the main API class, used for both external and internal processing.
A class containing constants representing the names of configuration variables.
const WatchlistExpiry
Name constant for the WatchlistExpiry setting, for use with Config::get()
const EnableMultiBlocks
Name constant for the EnableMultiBlocks setting, for use with Config::get()
const WatchlistExpiryMaxDuration
Name constant for the WatchlistExpiryMaxDuration setting, for use with Config::get()
const EnablePartialActionBlocks
Name constant for the EnablePartialActionBlocks setting, for use with Config::get()
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...