* @file
*/
+use MediaWiki\Block\AbstractBlock;
+use MediaWiki\Block\DatabaseBlock;
+use MediaWiki\Linker\LinkTarget;
+use MediaWiki\MediaWikiServices;
+use MediaWiki\Permissions\PermissionManager;
use Wikimedia\Rdbms\IDatabase;
/**
*/
abstract class ApiBase extends ContextSource {
+ use ApiBlockInfoTrait;
+
/**
* @name Constants for ::getAllowedParams() arrays
* These constants are keys in the arrays returned by ::getAllowedParams()
$this->getMain()->setContinuationManager( $manager );
}
+ /**
+ * Obtain a PermissionManager instance that subclasses may use in their authorization checks.
+ *
+ * @since 1.34
+ * @return PermissionManager
+ */
+ protected function getPermissionManager(): PermissionManager {
+ return MediaWikiServices::getInstance()->getPermissionManager();
+ }
+
/**@}*/
/************************************************************************//**
$provided = $this->getMain()->getCheck( $encParamName );
if ( isset( $value ) && $type == 'namespace' ) {
- $type = MWNamespace::getValidNamespaces();
+ $type = MediaWikiServices::getInstance()->getNamespaceInfo()->
+ getValidNamespaces();
if ( isset( $paramSettings[self::PARAM_EXTRA_NAMESPACES] ) &&
is_array( $paramSettings[self::PARAM_EXTRA_NAMESPACES] )
) {
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->getBlockDetails( $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->getBlockDetails( $user->getBlock() ) ]
) );
}
}
* error handler and die with an error message including block info.
*
* @since 1.27
- * @param Block $block The block used to generate the ApiUsageException
+ * @param AbstractBlock $block The block used to generate the ApiUsageException
* @throws ApiUsageException always
*/
- public function dieBlocked( Block $block ) {
+ public function dieBlocked( AbstractBlock $block ) {
// Die using the appropriate message depending on block type
- if ( $block->getType() == Block::TYPE_AUTO ) {
+ if ( $block->getType() == DatabaseBlock::TYPE_AUTO ) {
$this->dieWithError(
'apierror-autoblocked',
'autoblocked',
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
+ [ 'blockinfo' => $this->getBlockDetails( $block ) ]
);
} elseif ( !$block->isSitewide() ) {
$this->dieWithError(
'apierror-blocked-partial',
'blocked',
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
+ [ 'blockinfo' => $this->getBlockDetails( $block ) ]
);
} else {
$this->dieWithError(
'apierror-blocked',
'blocked',
- [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
+ [ 'blockinfo' => $this->getBlockDetails( $block ) ]
);
}
}
/**
* Helper function for permission-denied errors
- * @since 1.29
- * @since 1.33 Changed the third parameter from $user to $options.
- * @param Title $title
+ *
+ * @param LinkTarget $linkTarget
* @param string|string[] $actions
* @param array $options Additional options
* - user: (User) User to use rather than $this->getUser()
* - autoblock: (bool, default false) Whether to spread autoblocks
* For compatibility, passing a User object is treated as the value for the 'user' option.
* @throws ApiUsageException if the user doesn't have all of the rights.
+ *
+ * @since 1.29
+ * @since 1.33 Changed the third parameter from $user to $options.
*/
- public function checkTitleUserPermissions( Title $title, $actions, $options = [] ) {
+ public function checkTitleUserPermissions( LinkTarget $linkTarget, $actions, $options = [] ) {
if ( !is_array( $options ) ) {
wfDeprecated( '$user as the third parameter to ' . __METHOD__, '1.33' );
$options = [ 'user' => $options ];
$errors = [];
foreach ( (array)$actions as $action ) {
- $errors = array_merge( $errors, $title->getUserPermissionsErrors( $action, $user ) );
+ $errors = array_merge(
+ $errors,
+ $this->getPermissionManager()->getPermissionErrors( $action, $user, $linkTarget )
+ );
}
if ( $errors ) {
] ];
}
- /**
- * Truncate an array to a certain length.
- * @deprecated since 1.32, no replacement
- * @param array &$arr Array to truncate
- * @param int $limit Maximum length
- * @return bool True if the array was truncated, false otherwise
- */
- public static function truncateArray( &$arr, $limit ) {
- wfDeprecated( __METHOD__, '1.32' );
- $modified = false;
- while ( count( $arr ) > $limit ) {
- array_pop( $arr );
- $modified = true;
- }
-
- return $modified;
- }
-
/**@}*/
}