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->blockUtils = $blockUtils;
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 ) {
132 $target = $this->userIdentityLookup->getUserIdentityByUserId(
$params[
'userid'] );
138 $status = $this->insertBlock( $target,
$params );
140 $blocks = $this->blockStore->newListFromTarget( $target );
141 if ( count( $blocks ) === 0 ) {
142 $status = $this->insertBlock( $target,
$params );
143 } elseif ( count( $blocks ) === 1 ) {
145 $status = $this->updateBlock( $blocks[0],
$params );
147 $status = Status::newFatal(
'ipb_already_blocked', $blocks[0]->getTargetName() );
150 $this->
dieWithError(
'apierror-ambiguous-block',
'ambiguous-block' );
155 if ( !$status->isOK() ) {
159 $block = $status->value;
161 throw new RuntimeException(
"Unexpected block class" );
165 $userPage = Title::makeTitle(
NS_USER, $block->getTargetName() );
167 if (
$params[
'watchuser'] && $block->getType() !== AbstractBlock::TYPE_RANGE ) {
168 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
173 $res[
'user'] = $block->getTargetName();
175 $blockedUser = $block->getTargetUserIdentity();
176 $res[
'userID'] = $blockedUser ? $blockedUser->getId() : 0;
179 $res[
'id'] = $block->getId();
181 $res[
'reason'] =
$params[
'reason'];
182 $res[
'anononly'] =
$params[
'anononly'];
183 $res[
'nocreate'] =
$params[
'nocreate'];
184 $res[
'autoblock'] =
$params[
'autoblock'];
185 $res[
'noemail'] =
$params[
'noemail'];
186 $res[
'hidename'] = $block->getHideName();
187 $res[
'allowusertalk'] =
$params[
'allowusertalk'];
188 $res[
'watchuser'] =
$params[
'watchuser'];
189 if ( $watchlistExpiry ) {
191 $this->watchedItemStore,
195 $res[
'watchlistexpiry'] = $expiry;
197 $res[
'partial'] =
$params[
'partial'];
198 $res[
'pagerestrictions'] =
$params[
'pagerestrictions'];
199 $res[
'namespacerestrictions'] =
$params[
'namespacerestrictions'];
201 $res[
'actionrestrictions'] =
$params[
'actionrestrictions'];
213 private function getBlockOptions(
$params ) {
215 'isCreateAccountBlocked' =>
$params[
'nocreate'],
216 'isEmailBlocked' =>
$params[
'noemail'],
217 'isHardBlock' => !
$params[
'anononly'],
218 'isAutoblocking' =>
$params[
'autoblock'],
219 'isUserTalkEditBlocked' => !
$params[
'allowusertalk'],
220 'isHideUser' =>
$params[
'hidename'],
221 'isPartial' =>
$params[
'partial'],
230 private function getRestrictions(
$params ) {
233 $pageRestrictions = array_map(
234 [ PageRestriction::class,
'newFromTitle' ],
235 (array)
$params[
'pagerestrictions']
238 $namespaceRestrictions = array_map(
static function ( $id ) {
239 return new NamespaceRestriction( 0, $id );
240 }, (array)
$params[
'namespacerestrictions'] );
241 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
244 $actionRestrictions = array_map(
function ( $action ) {
245 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
246 }, (array)
$params[
'actionrestrictions'] );
247 $restrictions = array_merge( $restrictions, $actionRestrictions );
250 return $restrictions;
258 private function checkEmailPermissions(
$params ) {
261 !$this->blockPermissionCheckerFactory
262 ->newBlockPermissionChecker(
null, $this->
getAuthority() )
263 ->checkEmailPermissions()
276 private function updateBlock( DatabaseBlock $block,
$params ) {
277 $this->checkEmailPermissions(
$params );
278 return $this->blockUserFactory->newUpdateBlock(
283 $this->getBlockOptions(
$params ),
284 $this->getRestrictions(
$params ),
296 private function insertBlock( $target,
$params ) {
297 $this->checkEmailPermissions(
$params );
298 return $this->blockUserFactory->newBlockUser(
303 $this->getBlockOptions(
$params ),
304 $this->getRestrictions(
$params ),
319 'id' => [ ParamValidator::PARAM_TYPE =>
'integer' ],
321 ParamValidator::PARAM_TYPE =>
'user',
322 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr',
'id' ],
325 ParamValidator::PARAM_TYPE =>
'integer',
326 ParamValidator::PARAM_DEPRECATED =>
true,
332 'autoblock' =>
false,
335 'allowusertalk' =>
false,
338 'watchuser' =>
false,
343 if ( $this->watchlistExpiryEnabled ) {
345 'watchlistexpiry' => [
346 ParamValidator::PARAM_TYPE =>
'expiry',
347 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
348 ExpiryDef::PARAM_USE_MAX =>
true,
355 ParamValidator::PARAM_TYPE =>
'tags',
356 ParamValidator::PARAM_ISMULTI =>
true,
359 'pagerestrictions' => [
360 ParamValidator::PARAM_TYPE =>
'title',
361 TitleDef::PARAM_MUST_EXIST =>
true,
369 ParamValidator::PARAM_ISMULTI =>
true,
370 ParamValidator::PARAM_ISMULTI_LIMIT1 => 10,
371 ParamValidator::PARAM_ISMULTI_LIMIT2 => 10,
373 'namespacerestrictions' => [
374 ParamValidator::PARAM_ISMULTI =>
true,
375 ParamValidator::PARAM_TYPE =>
'namespace',
381 'actionrestrictions' => [
382 ParamValidator::PARAM_ISMULTI =>
true,
383 ParamValidator::PARAM_TYPE => array_keys(
384 $this->blockActionInfo->getAllBlockActions()
400 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
401 =>
'apihelp-block-example-ip-simple',
402 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
403 =>
'apihelp-block-example-user-complex',
409 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
414class_alias( ApiBlock::class,
'ApiBlock' );
array $params
The job parameters.
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 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...