* @file
*/
+use MediaWiki\Block\DatabaseBlock;
+
/**
* API module that facilitates the blocking of users. Requires API write mode
* to be enabled.
*/
class ApiBlock extends ApiBase {
+ use ApiBlockInfoTrait;
+
/**
* Blocks the user specified in the parameters for the given expiry, with the
* given reason, and with all other settings provided in the params. If the block
$this->requireOnlyOneParameter( $params, 'user', 'userid' );
# T17810: blocked admins should have limited access here
- if ( $user->isBlocked() ) {
+ $block = $user->getBlock();
+ if ( $block ) {
$status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
if ( $status !== true ) {
$this->dieWithError(
$status,
null,
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
+ [ 'blockinfo' => $this->getBlockDetails( $block ) ]
);
}
}
+ $editingRestriction = 'sitewide';
+ $pageRestrictions = '';
+ $namespaceRestrictions = '';
+ if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
+ if ( $params['partial'] ) {
+ $editingRestriction = 'partial';
+ }
+
+ $pageRestrictions = implode( "\n", (array)$params['pagerestrictions'] );
+ $namespaceRestrictions = implode( "\n", (array)$params['namespacerestrictions'] );
+ }
+
if ( $params['userid'] !== null ) {
$username = User::whoIs( $params['userid'] );
// T40633 - if the target is a user (not an IP address), but it
// doesn't exist or is unusable, error.
- if ( $type === Block::TYPE_USER &&
+ if ( $type === DatabaseBlock::TYPE_USER &&
( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $params['user'] ) )
) {
$this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
'Watch' => $params['watchuser'],
'Confirm' => true,
'Tags' => $params['tags'],
+ 'EditingRestriction' => $editingRestriction,
+ 'PageRestrictions' => $pageRestrictions,
+ 'NamespaceRestrictions' => $namespaceRestrictions,
];
+ $status = SpecialBlock::validateTarget( $params['user'], $user );
+ if ( !$status->isOK() ) {
+ $this->dieStatus( $status );
+ }
+
$retval = SpecialBlock::processForm( $data, $this->getContext() );
if ( $retval !== true ) {
$this->dieStatus( $this->errorArrayToStatus( $retval ) );
$res['user'] = $params['user'];
$res['userID'] = $target instanceof User ? $target->getId() : 0;
- $block = Block::newFromTarget( $target, null, true );
- if ( $block instanceof Block ) {
- $res['expiry'] = ApiResult::formatExpiry( $block->mExpiry, 'infinite' );
+ $block = DatabaseBlock::newFromTarget( $target, null, true );
+ if ( $block instanceof DatabaseBlock ) {
+ $res['expiry'] = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
$res['id'] = $block->getId();
} else {
# should be unreachable
- $res['expiry'] = '';
- $res['id'] = '';
+ $res['expiry'] = ''; // @codeCoverageIgnore
+ $res['id'] = ''; // @codeCoverageIgnore
}
$res['reason'] = $params['reason'];
$res['allowusertalk'] = $params['allowusertalk'];
$res['watchuser'] = $params['watchuser'];
+ if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
+ $res['partial'] = $params['partial'];
+ $res['pagerestrictions'] = $params['pagerestrictions'];
+ $res['namespacerestrictions'] = $params['namespacerestrictions'];
+ }
+
$this->getResult()->addValue( null, $this->getModuleName(), $res );
}
}
public function getAllowedParams() {
- return [
+ $params = [
'user' => [
ApiBase::PARAM_TYPE => 'user',
],
ApiBase::PARAM_ISMULTI => true,
],
];
+
+ if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
+ $params['partial'] = false;
+ $params['pagerestrictions'] = [
+ ApiBase::PARAM_ISMULTI => true,
+ ApiBase::PARAM_ISMULTI_LIMIT1 => 10,
+ ApiBase::PARAM_ISMULTI_LIMIT2 => 10,
+ ];
+ $params['namespacerestrictions'] = [
+ ApiBase::PARAM_ISMULTI => true,
+ ApiBase::PARAM_TYPE => 'namespace',
+ ];
+ }
+
+ return $params;
}
public function needsToken() {