* of success. If it fails, the result will specify the nature of the error.
*/
public function execute() {
- global $wgContLang;
+ $this->checkUserRightsAny( 'block' );
$user = $this->getUser();
$params = $this->extractRequestParams();
- if ( !$user->isAllowed( 'block' ) ) {
- $this->dieUsageMsg( 'cantblock' );
- }
+ $this->requireOnlyOneParameter( $params, 'user', 'userid' );
# bug 15810: blocked admins should have limited access here
if ( $user->isBlocked() ) {
$status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
if ( $status !== true ) {
- $msg = $this->parseMsg( $status );
- $this->dieUsage(
- $msg['info'],
- $msg['code'],
- 0,
+ $this->dieWithError(
+ $status,
+ null,
[ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
);
}
}
- $target = User::newFromName( $params['user'] );
- // Bug 38633 - if the target is a user (not an IP address), but it
- // doesn't exist or is unusable, error.
- if ( $target instanceof User &&
- ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
- ) {
- $this->dieUsageMsg( [ 'nosuchuser', $params['user'] ] );
+ if ( $params['userid'] !== null ) {
+ $username = User::whoIs( $params['userid'] );
+
+ if ( $username === false ) {
+ $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
+ } else {
+ $params['user'] = $username;
+ }
+ } else {
+ $target = User::newFromName( $params['user'] );
+
+ // Bug 38633 - if the target is a user (not an IP address), but it
+ // doesn't exist or is unusable, error.
+ if ( $target instanceof User &&
+ ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
+ ) {
+ $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
+ }
+ }
+
+ if ( $params['tags'] ) {
+ $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
+ if ( !$ableToTag->isOK() ) {
+ $this->dieStatus( $ableToTag );
+ }
}
if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
- $this->dieUsageMsg( 'canthide' );
+ $this->dieWithError( 'apierror-canthide' );
}
if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
- $this->dieUsageMsg( 'cantblock-email' );
+ $this->dieWithError( 'apierror-cantblock-email' );
}
$data = [
'Reblock' => $params['reblock'],
'Watch' => $params['watchuser'],
'Confirm' => true,
+ 'Tags' => $params['tags'],
];
$retval = SpecialBlock::processForm( $data, $this->getContext() );
if ( $retval !== true ) {
- // We don't care about multiple errors, just report one of them
- $this->dieUsageMsg( $retval );
+ $this->dieStatus( $this->errorArrayToStatus( $retval ) );
}
list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
$block = Block::newFromTarget( $target, null, true );
if ( $block instanceof Block ) {
- $res['expiry'] = $wgContLang->formatExpiry( $block->mExpiry, TS_ISO_8601, 'infinite' );
+ $res['expiry'] = ApiResult::formatExpiry( $block->mExpiry, 'infinite' );
$res['id'] = $block->getId();
} else {
# should be unreachable
return [
'user' => [
ApiBase::PARAM_TYPE => 'user',
- ApiBase::PARAM_REQUIRED => true
+ ],
+ 'userid' => [
+ ApiBase::PARAM_TYPE => 'integer',
],
'expiry' => 'never',
'reason' => '',
'allowusertalk' => false,
'reblock' => false,
'watchuser' => false,
+ 'tags' => [
+ ApiBase::PARAM_TYPE => 'tags',
+ ApiBase::PARAM_ISMULTI => true,
+ ],
];
}
protected function getExamplesMessages() {
// @codingStandardsIgnoreStart Generic.Files.LineLength
- return array(
+ return [
'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
=> 'apihelp-block-example-ip-simple',
'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
=> 'apihelp-block-example-user-complex',
- );
+ ];
// @codingStandardsIgnoreEnd
}