71 parent::__construct( $main, $action );
73 $this->blockPermissionCheckerFactory = $blockPermissionCheckerFactory;
74 $this->blockUserFactory = $blockUserFactory;
75 $this->titleFactory = $titleFactory;
76 $this->userIdentityLookup = $userIdentityLookup;
77 $this->watchedItemStore = $watchedItemStore;
78 $this->blockTargetFactory = $blockTargetFactory;
79 $this->blockActionInfo = $blockActionInfo;
80 $this->blockStore = $blockStore;
84 $this->watchlistMaxDuration =
86 $this->watchlistManager = $watchlistManager;
87 $this->userOptionsLookup = $userOptionsLookup;
102 'id', [
'newblock',
'reblock' ] );
104 if ( $params[
'id'] !==
null ) {
105 $block = $this->blockStore->newFromID( $params[
'id'],
true );
108 [
'apierror-nosuchblockid', $params[
'id'] ],
111 if ( $block->getType() === AbstractBlock::TYPE_AUTO ) {
114 $status = $this->updateBlock( $block, $params );
116 if ( $params[
'user'] !==
null ) {
117 $target = $this->blockTargetFactory->newFromUser( $params[
'user'] );
119 $targetUser = $this->userIdentityLookup->getUserIdentityByUserId( $params[
'userid'] );
120 if ( !$targetUser ) {
121 $this->
dieWithError( [
'apierror-nosuchuserid', $params[
'userid'] ],
'nosuchuserid' );
123 $target = $this->blockTargetFactory->newUserBlockTarget( $targetUser );
125 if ( $params[
'newblock'] ) {
126 $status = $this->insertBlock( $target, $params );
128 $blocks = $this->blockStore->newListFromTarget(
129 $target,
null,
false, DatabaseBlockStore::AUTO_NONE );
130 if ( count( $blocks ) === 0 ) {
131 $status = $this->insertBlock( $target, $params );
132 } elseif ( count( $blocks ) === 1 ) {
133 if ( $params[
'reblock'] ) {
134 $status = $this->updateBlock( $blocks[0], $params );
136 $status = Status::newFatal(
'ipb_already_blocked', $blocks[0]->getTargetName() );
139 $this->
dieWithError(
'apierror-ambiguous-block',
'ambiguous-block' );
144 if ( !$status->isOK() ) {
148 $block = $status->value;
150 throw new RuntimeException(
"Unexpected block class" );
156 if ( $params[
'watchuser'] && $block->getType() !== AbstractBlock::TYPE_RANGE ) {
157 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
162 $res[
'user'] = $block->getTargetName();
164 $blockedUser = $block->getTargetUserIdentity();
165 $res[
'userID'] = $blockedUser ? $blockedUser->getId() : 0;
168 $res[
'id'] = $block->getId();
170 $res[
'reason'] = $params[
'reason'];
171 $res[
'anononly'] = $params[
'anononly'];
172 $res[
'nocreate'] = $params[
'nocreate'];
173 $res[
'autoblock'] = $params[
'autoblock'];
174 $res[
'noemail'] = $params[
'noemail'];
175 $res[
'hidename'] = $block->getHideName();
176 $res[
'allowusertalk'] = $params[
'allowusertalk'];
177 $res[
'watchuser'] = $params[
'watchuser'];
178 if ( $watchlistExpiry ) {
180 $this->watchedItemStore,
184 $res[
'watchlistexpiry'] = $expiry;
186 $res[
'partial'] = $params[
'partial'];
187 $res[
'pagerestrictions'] = $params[
'pagerestrictions'];
188 $res[
'namespacerestrictions'] = $params[
'namespacerestrictions'];
189 $res[
'actionrestrictions'] = $params[
'actionrestrictions'];
200 private function getBlockOptions( $params ) {
202 'isCreateAccountBlocked' => $params[
'nocreate'],
203 'isEmailBlocked' => $params[
'noemail'],
204 'isHardBlock' => !$params[
'anononly'],
205 'isAutoblocking' => $params[
'autoblock'],
206 'isUserTalkEditBlocked' => !$params[
'allowusertalk'],
207 'isHideUser' => $params[
'hidename'],
208 'isPartial' => $params[
'partial'],
217 private function getRestrictions( $params ) {
219 if ( $params[
'partial'] ) {
220 $pageRestrictions = array_map(
221 PageRestriction::newFromTitle( ... ),
222 (array)$params[
'pagerestrictions']
225 $namespaceRestrictions = array_map(
static function ( $id ) {
226 return new NamespaceRestriction( 0, $id );
227 }, (array)$params[
'namespacerestrictions'] );
228 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
230 $actionRestrictions = array_map(
function ( $action ) {
231 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
232 }, (array)$params[
'actionrestrictions'] );
233 $restrictions = array_merge( $restrictions, $actionRestrictions );
235 return $restrictions;
243 private function checkEmailPermissions( $params ) {
245 $params[
'noemail'] &&
246 !$this->blockPermissionCheckerFactory
248 ->checkEmailPermissions()
261 private function updateBlock( DatabaseBlock $block, $params ) {
262 $this->checkEmailPermissions( $params );
263 return $this->blockUserFactory->newUpdateBlock(
268 $this->getBlockOptions( $params ),
269 $this->getRestrictions( $params ),
281 private function insertBlock( $target, $params ) {
282 $this->checkEmailPermissions( $params );
283 return $this->blockUserFactory->newBlockUser(
288 $this->getBlockOptions( $params ),
289 $this->getRestrictions( $params ),
307 'id' => [ ParamValidator::PARAM_TYPE =>
'integer' ],
309 ParamValidator::PARAM_TYPE =>
'user',
310 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr',
'id' ],
311 UserDef::PARAM_RETURN_OBJECT =>
true,
314 ParamValidator::PARAM_TYPE =>
'integer',
315 ParamValidator::PARAM_DEPRECATED =>
true,
321 'autoblock' =>
false,
324 'allowusertalk' =>
false,
327 'watchuser' =>
false,
332 if ( $this->watchlistExpiryEnabled ) {
334 'watchlistexpiry' => [
335 ParamValidator::PARAM_TYPE =>
'expiry',
336 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
337 ExpiryDef::PARAM_USE_MAX =>
true,
346 ParamValidator::PARAM_TYPE =>
'tags',
347 ParamValidator::PARAM_ISMULTI =>
true,
350 'pagerestrictions' => [
351 ParamValidator::PARAM_TYPE =>
'title',
352 TitleDef::PARAM_MUST_EXIST =>
true,
360 ParamValidator::PARAM_ISMULTI =>
true,
361 ParamValidator::PARAM_ISMULTI_LIMIT1 => $pageLimit,
362 ParamValidator::PARAM_ISMULTI_LIMIT2 => $pageLimit,
364 'namespacerestrictions' => [
365 ParamValidator::PARAM_ISMULTI =>
true,
366 ParamValidator::PARAM_TYPE =>
'namespace',
368 'actionrestrictions' => [
369 ParamValidator::PARAM_ISMULTI =>
true,
370 ParamValidator::PARAM_TYPE => array_keys(
371 $this->blockActionInfo->getAllBlockActions()
388 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
389 =>
'apihelp-block-example-ip-simple',
390 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
391 =>
'apihelp-block-example-user-complex',
398 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
403class_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...