60 parent::__construct( $main, $action );
62 $this->watchedItemStore = $watchedItemStore;
66 $this->watchlistMaxDuration =
68 $this->watchlistManager = $watchlistManager;
69 $this->userOptionsLookup = $userOptionsLookup;
84 'id', [
'newblock',
'reblock' ] );
86 $additionalBlocksStatuses = [];
87 if ( $params[
'id'] !==
null ) {
88 $block = $this->blockStore->newFromID( $params[
'id'],
true );
91 [
'apierror-nosuchblockid', $params[
'id'] ],
94 if ( $block->getType() === AbstractBlock::TYPE_AUTO ) {
97 $status = $this->updateBlock( $block, $params );
99 if ( $params[
'user'] !==
null ) {
100 $target = $this->blockTargetFactory->newFromUser( $params[
'user'] );
102 $targetUser = $this->userIdentityLookup->getUserIdentityByUserId( $params[
'userid'] );
103 if ( !$targetUser ) {
104 $this->
dieWithError( [
'apierror-nosuchuserid', $params[
'userid'] ],
'nosuchuserid' );
106 $target = $this->blockTargetFactory->newUserBlockTarget( $targetUser );
108 if ( $params[
'newblock'] ) {
112 if ( !$status->isOK() ) {
118 $targetUserIdentity = $this->userIdentityLookup->getUserIdentityByName( $target->toString() );
119 if ( $targetUserIdentity ) {
125 $additionalBlocksStatuses
130 $blocks = $this->blockStore->newListFromTarget(
131 $target,
null,
false, DatabaseBlockStore::AUTO_NONE );
132 if ( count( $blocks ) === 0 ) {
134 } elseif ( count( $blocks ) === 1 ) {
135 if ( $params[
'reblock'] ) {
136 $status = $this->updateBlock( $blocks[0], $params );
138 $status = Status::newFatal(
'ipb_already_blocked', $blocks[0]->getTargetName() );
141 $this->
dieWithError(
'apierror-ambiguous-block',
'ambiguous-block' );
146 if ( !$status->isOK() ) {
150 $block = $status->value;
152 throw new RuntimeException(
"Unexpected block class" );
158 if ( $params[
'watchuser'] && $block->getType() !== AbstractBlock::TYPE_RANGE ) {
159 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
164 $res[
'user'] = $block->getTargetName();
166 $blockedUser = $block->getTargetUserIdentity();
167 $res[
'userID'] = $blockedUser ? $blockedUser->getId() : 0;
170 $res[
'id'] = $block->getId();
172 $res[
'reason'] = $params[
'reason'];
173 $res[
'anononly'] = $params[
'anononly'];
174 $res[
'nocreate'] = $params[
'nocreate'];
175 $res[
'autoblock'] = $params[
'autoblock'];
176 $res[
'noemail'] = $params[
'noemail'];
177 $res[
'hidename'] = $block->getHideName();
178 $res[
'allowusertalk'] = $params[
'allowusertalk'];
179 $res[
'watchuser'] = $params[
'watchuser'];
180 if ( $watchlistExpiry ) {
182 $this->watchedItemStore,
186 $res[
'watchlistexpiry'] = $expiry;
188 $res[
'partial'] = $params[
'partial'];
189 $res[
'pagerestrictions'] = $params[
'pagerestrictions'];
190 $res[
'namespacerestrictions'] = $params[
'namespacerestrictions'];
191 $res[
'actionrestrictions'] = $params[
'actionrestrictions'];
192 $res[
'additionalBlocksStatuses'] = $additionalBlocksStatuses;
203 private function getBlockOptions( $params ) {
205 'isCreateAccountBlocked' => $params[
'nocreate'],
206 'isEmailBlocked' => $params[
'noemail'],
207 'isHardBlock' => !$params[
'anononly'],
208 'isAutoblocking' => $params[
'autoblock'],
209 'isUserTalkEditBlocked' => !$params[
'allowusertalk'],
210 'isHideUser' => $params[
'hidename'],
211 'isPartial' => $params[
'partial'],
220 private function getRestrictions( $params ) {
222 if ( $params[
'partial'] ) {
223 $pageRestrictions = array_map(
224 PageRestriction::newFromTitle( ... ),
225 (array)$params[
'pagerestrictions']
228 $namespaceRestrictions = array_map(
static function ( $id ) {
229 return new NamespaceRestriction( 0, $id );
230 }, (array)$params[
'namespacerestrictions'] );
231 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
233 $actionRestrictions = array_map(
function ( $action ) {
234 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
235 }, (array)$params[
'actionrestrictions'] );
236 $restrictions = array_merge( $restrictions, $actionRestrictions );
238 return $restrictions;
246 private function checkEmailPermissions( $params ) {
248 $params[
'noemail'] &&
249 !$this->blockPermissionCheckerFactory
251 ->checkEmailPermissions()
264 private function updateBlock( DatabaseBlock $block, $params ) {
265 $this->checkEmailPermissions( $params );
266 return $this->blockUserFactory->newUpdateBlock(
271 $this->getBlockOptions( $params ),
272 $this->getRestrictions( $params ),
285 $this->checkEmailPermissions( $params );
286 return $this->blockUserFactory->newBlockUser(
291 $this->getBlockOptions( $params ),
292 $this->getRestrictions( $params ),
310 'id' => [ ParamValidator::PARAM_TYPE =>
'integer' ],
312 ParamValidator::PARAM_TYPE =>
'user',
313 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr',
'id' ],
314 UserDef::PARAM_RETURN_OBJECT =>
true,
317 ParamValidator::PARAM_TYPE =>
'integer',
318 ParamValidator::PARAM_DEPRECATED =>
true,
324 'autoblock' =>
false,
327 'allowusertalk' =>
false,
330 'watchuser' =>
false,
335 if ( $this->watchlistExpiryEnabled ) {
337 'watchlistexpiry' => [
338 ParamValidator::PARAM_TYPE =>
'expiry',
339 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
340 ExpiryDef::PARAM_USE_MAX =>
true,
349 ParamValidator::PARAM_TYPE =>
'tags',
350 ParamValidator::PARAM_ISMULTI =>
true,
353 'pagerestrictions' => [
354 ParamValidator::PARAM_TYPE =>
'title',
355 TitleDef::PARAM_MUST_EXIST =>
true,
363 ParamValidator::PARAM_ISMULTI =>
true,
364 ParamValidator::PARAM_ISMULTI_LIMIT1 => $pageLimit,
365 ParamValidator::PARAM_ISMULTI_LIMIT2 => $pageLimit,
367 'namespacerestrictions' => [
368 ParamValidator::PARAM_ISMULTI =>
true,
369 ParamValidator::PARAM_TYPE =>
'namespace',
371 'actionrestrictions' => [
372 ParamValidator::PARAM_ISMULTI =>
true,
373 ParamValidator::PARAM_TYPE => array_keys(
374 $this->blockActionInfo->getAllBlockActions()
391 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
392 =>
'apihelp-block-example-ip-simple',
393 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
394 =>
'apihelp-block-example-user-complex',
401 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
406class_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()
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...