'ApiAuthManagerHelper' => __DIR__ . '/includes/api/ApiAuthManagerHelper.php',
'ApiBase' => __DIR__ . '/includes/api/ApiBase.php',
'ApiBlock' => __DIR__ . '/includes/api/ApiBlock.php',
+ 'ApiBlockInfoTrait' => __DIR__ . '/includes/api/ApiBlockInfoTrait.php',
'ApiCSPReport' => __DIR__ . '/includes/api/ApiCSPReport.php',
'ApiChangeAuthenticationData' => __DIR__ . '/includes/api/ApiChangeAuthenticationData.php',
'ApiCheckToken' => __DIR__ . '/includes/api/ApiCheckToken.php',
*/
abstract class ApiBase extends ContextSource {
+ use ApiBlockInfoTrait;
+
/**
* @name Constants for ::getAllowedParams() arrays
* These constants are keys in the arrays returned by ::getAllowedParams()
if ( is_string( $error[0] ) && isset( self::$blockMsgMap[$error[0]] ) && $user->getBlock() ) {
list( $msg, $code ) = self::$blockMsgMap[$error[0]];
$status->fatal( ApiMessage::create( $msg, $code,
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
+ [ 'blockinfo' => $this->getBlockInfo( $user->getBlock() ) ]
) );
} else {
$status->fatal( ...$error );
foreach ( self::$blockMsgMap as $msg => list( $apiMsg, $code ) ) {
if ( $status->hasMessage( $msg ) && $user->getBlock() ) {
$status->replaceMessage( $msg, ApiMessage::create( $apiMsg, $code,
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
+ [ 'blockinfo' => $this->getBlockInfo( $user->getBlock() ) ]
) );
}
}
$this->dieWithError(
'apierror-autoblocked',
'autoblocked',
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
+ [ 'blockinfo' => $this->getBlockInfo( $block ) ]
);
} elseif ( !$block->isSitewide() ) {
$this->dieWithError(
'apierror-blocked-partial',
'blocked',
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
+ [ 'blockinfo' => $this->getBlockInfo( $block ) ]
);
} else {
$this->dieWithError(
'apierror-blocked',
'blocked',
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
+ [ 'blockinfo' => $this->getBlockInfo( $block ) ]
);
}
}
*/
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->dieWithError(
$status,
null,
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
+ [ 'blockinfo' => $this->getBlockInfo( $block ) ]
);
}
}
--- /dev/null
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+/**
+ * @ingroup API
+ */
+trait ApiBlockInfoTrait {
+
+ /**
+ * Get basic info about a given block
+ * @param Block $block
+ * @return array Array containing several keys:
+ * - blockid - ID of the block
+ * - blockedby - username of the blocker
+ * - blockedbyid - user ID of the blocker
+ * - blockreason - reason provided for the block
+ * - blockedtimestamp - timestamp for when the block was placed/modified
+ * - blockexpiry - expiry time of the block
+ * - systemblocktype - system block type, if any
+ */
+ private function getBlockInfo( Block $block ) {
+ $vals = [];
+ $vals['blockid'] = $block->getId();
+ $vals['blockedby'] = $block->getByName();
+ $vals['blockedbyid'] = $block->getBy();
+ $vals['blockreason'] = $block->getReason();
+ $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->getTimestamp() );
+ $vals['blockexpiry'] = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
+ $vals['blockpartial'] = !$block->isSitewide();
+ if ( $block->getSystemBlockType() !== null ) {
+ $vals['systemblocktype'] = $block->getSystemBlockType();
+ }
+ return $vals;
+ }
+
+}
*/
class ApiQueryUserInfo extends ApiQueryBase {
+ use ApiBlockInfoTrait;
+
const WL_UNREAD_LIMIT = 1000;
private $params = [];
$result->addValue( 'query', $this->getModuleName(), $r );
}
- /**
- * Get basic info about a given block
- * @param Block $block
- * @return array Array containing several keys:
- * - blockid - ID of the block
- * - blockedby - username of the blocker
- * - blockedbyid - user ID of the blocker
- * - blockreason - reason provided for the block
- * - blockedtimestamp - timestamp for when the block was placed/modified
- * - blockexpiry - expiry time of the block
- * - systemblocktype - system block type, if any
- */
- public static function getBlockInfo( Block $block ) {
- $vals = [];
- $vals['blockid'] = $block->getId();
- $vals['blockedby'] = $block->getByName();
- $vals['blockedbyid'] = $block->getBy();
- $vals['blockreason'] = $block->getReason();
- $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->getTimestamp() );
- $vals['blockexpiry'] = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
- $vals['blockpartial'] = !$block->isSitewide();
- if ( $block->getSystemBlockType() !== null ) {
- $vals['systemblocktype'] = $block->getSystemBlockType();
- }
- return $vals;
- }
-
/**
* Get central user info
* @param Config $config
if ( isset( $this->prop['blockinfo'] ) ) {
$block = $user->getBlock();
if ( $block ) {
- $vals = array_merge( $vals, self::getBlockInfo( $block ) );
+ $vals = array_merge( $vals, $this->getBlockInfo( $block ) );
}
}
*/
class ApiUnblock extends ApiBase {
+ use ApiBlockInfoTrait;
+
/**
* Unblocks the specified user or provides the reason the unblock failed.
*/
$this->dieWithError(
$status,
null,
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
+ [ 'blockinfo' => $this->getBlockInfo( $block ) ]
);
}
}
'expiry' => time() + 100500,
] );
$block->insert();
- $blockinfo = [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ];
+ $userInfoTrait = TestingAccessWrapper::newFromObject(
+ $this->getMockForTrait( ApiBlockInfoTrait::class )
+ );
+ $blockinfo = [ 'blockinfo' => $userInfoTrait->getBlockInfo( $block ) ];
$expect = Status::newGood();
$expect->fatal( ApiMessage::create( 'apierror-blocked', 'blocked', $blockinfo ) );
'expiry' => time() + 100500,
] );
$block->insert();
- $blockinfo = [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ];
+ $userInfoTrait = TestingAccessWrapper::newFromObject(
+ $this->getObjectForTrait( ApiBlockInfoTrait::class )
+ );
+ $blockinfo = [ 'blockinfo' => $userInfoTrait->getBlockInfo( $block ) ];
$expect = Status::newGood();
$expect->fatal( ApiMessage::create( 'apierror-blocked', 'blocked', $blockinfo ) );
--- /dev/null
+<?php
+
+use Wikimedia\TestingAccessWrapper;
+
+/**
+ * @covers ApiBlockInfoTrait
+ */
+class ApiBlockInfoTraitTest extends MediaWikiTestCase {
+
+ public function testGetBlockInfo() {
+ $block = new Block();
+ $mock = $this->getMockForTrait( ApiBlockInfoTrait::class );
+ $info = TestingAccessWrapper::newFromObject( $mock )->getBlockInfo( $block );
+ $subset = [
+ 'blockid' => null,
+ 'blockedby' => '',
+ 'blockedbyid' => 0,
+ 'blockreason' => '',
+ 'blockexpiry' => 'infinite',
+ 'blockpartial' => false,
+ ];
+ $this->assertArraySubset( $subset, $info );
+ }
+
+ public function testGetBlockInfoPartial() {
+ $mock = $this->getMockForTrait( ApiBlockInfoTrait::class );
+
+ $block = new Block( [
+ 'sitewide' => false,
+ ] );
+ $info = TestingAccessWrapper::newFromObject( $mock )->getBlockInfo( $block );
+ $subset = [
+ 'blockid' => null,
+ 'blockedby' => '',
+ 'blockedbyid' => 0,
+ 'blockreason' => '',
+ 'blockexpiry' => 'infinite',
+ 'blockpartial' => true,
+ ];
+ $this->assertArraySubset( $subset, $info );
+ }
+
+}
+++ /dev/null
-<?php
-
-/**
- * @group medium
- * @covers ApiQueryUserInfo
- */
-class ApiQueryUserInfoTest extends ApiTestCase {
- public function testGetBlockInfo() {
- $apiQueryUserInfo = new ApiQueryUserInfo(
- new ApiQuery( new ApiMain( $this->apiContext ), 'userinfo' ),
- 'userinfo'
- );
-
- $block = new Block();
- $info = $apiQueryUserInfo->getBlockInfo( $block );
- $subset = [
- 'blockid' => null,
- 'blockedby' => '',
- 'blockedbyid' => 0,
- 'blockreason' => '',
- 'blockexpiry' => 'infinite',
- 'blockpartial' => false,
- ];
- $this->assertArraySubset( $subset, $info );
- }
-
- public function testGetBlockInfoPartial() {
- $apiQueryUserInfo = new ApiQueryUserInfo(
- new ApiQuery( new ApiMain( $this->apiContext ), 'userinfo' ),
- 'userinfo'
- );
-
- $block = new Block( [
- 'sitewide' => false,
- ] );
- $info = $apiQueryUserInfo->getBlockInfo( $block );
- $subset = [
- 'blockid' => null,
- 'blockedby' => '',
- 'blockedbyid' => 0,
- 'blockreason' => '',
- 'blockexpiry' => 'infinite',
- 'blockpartial' => true,
- ];
- $this->assertArraySubset( $subset, $info );
- }
-}