Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
index 8976626..4801267 100644 (file)
@@ -20,6 +20,8 @@
  * @file
  */
 
+use MediaWiki\Block\DatabaseBlock;
+
 /**
  * API module that facilitates the blocking of users. Requires API write mode
  * to be enabled.
@@ -28,6 +30,8 @@
  */
 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
@@ -43,25 +47,28 @@ class ApiBlock extends ApiBase {
                $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", $params['pagerestrictions'] );
+                       $pageRestrictions = implode( "\n", (array)$params['pagerestrictions'] );
+                       $namespaceRestrictions = implode( "\n", (array)$params['namespacerestrictions'] );
                }
 
                if ( $params['userid'] !== null ) {
@@ -77,7 +84,7 @@ class ApiBlock extends ApiBase {
 
                        // 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' );
@@ -119,8 +126,14 @@ class ApiBlock extends ApiBase {
                        '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 ) );
@@ -130,9 +143,9 @@ class ApiBlock extends ApiBase {
                $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
@@ -152,6 +165,7 @@ class ApiBlock extends ApiBase {
                if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
                        $res['partial'] = $params['partial'];
                        $res['pagerestrictions'] = $params['pagerestrictions'];
+                       $res['namespacerestrictions'] = $params['namespacerestrictions'];
                }
 
                $this->getResult()->addValue( null, $this->getModuleName(), $res );
@@ -196,6 +210,10 @@ class ApiBlock extends ApiBase {
                                ApiBase::PARAM_ISMULTI_LIMIT1 => 10,
                                ApiBase::PARAM_ISMULTI_LIMIT2 => 10,
                        ];
+                       $params['namespacerestrictions'] = [
+                               ApiBase::PARAM_ISMULTI => true,
+                               ApiBase::PARAM_TYPE => 'namespace',
+                       ];
                }
 
                return $params;