56 private $blockPermissionCheckerFactory;
59 private $blockUserFactory;
62 private $titleFactory;
65 private $userIdentityLookup;
68 private $watchedItemStore;
74 private $blockActionInfo;
102 parent::__construct( $main, $action );
104 $this->blockPermissionCheckerFactory = $blockPermissionCheckerFactory;
105 $this->blockUserFactory = $blockUserFactory;
106 $this->titleFactory = $titleFactory;
107 $this->userIdentityLookup = $userIdentityLookup;
108 $this->watchedItemStore = $watchedItemStore;
109 $this->blockUtils = $blockUtils;
110 $this->blockActionInfo = $blockActionInfo;
113 $this->watchlistExpiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
114 $this->watchlistMaxDuration =
115 $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
116 $this->watchlistManager = $watchlistManager;
117 $this->userOptionsLookup = $userOptionsLookup;
132 if ( $params[
'user'] !==
null ) {
133 $target = $params[
'user'];
135 $target = $this->userIdentityLookup->getUserIdentityByUserId( $params[
'userid'] );
137 $this->
dieWithError( [
'apierror-nosuchuserid', $params[
'userid'] ],
'nosuchuserid' );
140 [ $target, $targetType ] = $this->blockUtils->parseBlockTarget( $target );
143 $params[
'noemail'] &&
144 !$this->blockPermissionCheckerFactory
145 ->newBlockPermissionChecker(
149 ->checkEmailPermissions()
155 if ( $params[
'partial'] ) {
156 $pageRestrictions = array_map(
157 [ PageRestriction::class,
'newFromTitle' ],
158 (array)$params[
'pagerestrictions']
161 $namespaceRestrictions = array_map(
static function ( $id ) {
163 }, (array)$params[
'namespacerestrictions'] );
164 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
166 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
167 $actionRestrictions = array_map(
function ( $action ) {
168 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
169 }, (array)$params[
'actionrestrictions'] );
170 $restrictions = array_merge( $restrictions, $actionRestrictions );
174 $status = $this->blockUserFactory->newBlockUser(
180 'isCreateAccountBlocked' => $params[
'nocreate'],
181 'isEmailBlocked' => $params[
'noemail'],
182 'isHardBlock' => !$params[
'anononly'],
183 'isAutoblocking' => $params[
'autoblock'],
184 'isUserTalkEditBlocked' => !$params[
'allowusertalk'],
185 'isHideUser' => $params[
'hidename'],
186 'isPartial' => $params[
'partial'],
190 )->placeBlock( $params[
'reblock'] );
192 if ( !$status->isOK() ) {
196 $block = $status->value;
199 $userPage = Title::makeTitle(
NS_USER, $block->getTargetName() );
201 if ( $params[
'watchuser'] && $targetType !== AbstractBlock::TYPE_RANGE ) {
202 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
207 $res[
'user'] = $block->getTargetName();
211 $res[
'expiry'] = ApiResult::formatExpiry( $block->getExpiry(),
'infinite' );
212 $res[
'id'] = $block->getId();
214 # should be unreachable
219 $res[
'reason'] = $params[
'reason'];
220 $res[
'anononly'] = $params[
'anononly'];
221 $res[
'nocreate'] = $params[
'nocreate'];
222 $res[
'autoblock'] = $params[
'autoblock'];
223 $res[
'noemail'] = $params[
'noemail'];
224 $res[
'hidename'] = $params[
'hidename'];
225 $res[
'allowusertalk'] = $params[
'allowusertalk'];
226 $res[
'watchuser'] = $params[
'watchuser'];
227 if ( $watchlistExpiry ) {
229 $this->watchedItemStore,
233 $res[
'watchlistexpiry'] = $expiry;
235 $res[
'partial'] = $params[
'partial'];
236 $res[
'pagerestrictions'] = $params[
'pagerestrictions'];
237 $res[
'namespacerestrictions'] = $params[
'namespacerestrictions'];
238 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
239 $res[
'actionrestrictions'] = $params[
'actionrestrictions'];
256 ParamValidator::PARAM_TYPE =>
'user',
257 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'cidr',
'id' ],
260 ParamValidator::PARAM_TYPE =>
'integer',
261 ParamValidator::PARAM_DEPRECATED =>
true,
267 'autoblock' =>
false,
270 'allowusertalk' =>
false,
272 'watchuser' =>
false,
278 if ( $this->watchlistExpiryEnabled ) {
280 'watchlistexpiry' => [
281 ParamValidator::PARAM_TYPE =>
'expiry',
282 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
283 ExpiryDef::PARAM_USE_MAX =>
true,
290 ParamValidator::PARAM_TYPE =>
'tags',
291 ParamValidator::PARAM_ISMULTI =>
true,
294 'pagerestrictions' => [
295 ParamValidator::PARAM_TYPE =>
'title',
296 TitleDef::PARAM_MUST_EXIST =>
true,
304 ParamValidator::PARAM_ISMULTI =>
true,
305 ParamValidator::PARAM_ISMULTI_LIMIT1 => 10,
306 ParamValidator::PARAM_ISMULTI_LIMIT2 => 10,
308 'namespacerestrictions' => [
309 ParamValidator::PARAM_ISMULTI =>
true,
310 ParamValidator::PARAM_TYPE =>
'namespace',
314 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
316 'actionrestrictions' => [
317 ParamValidator::PARAM_ISMULTI =>
true,
318 ParamValidator::PARAM_TYPE => array_keys(
319 $this->blockActionInfo->getAllBlockActions()
335 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
336 =>
'apihelp-block-example-ip-simple',
337 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
338 =>
'apihelp-block-example-user-complex',
344 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
getExpiryFromParams(array $params)
Get formatted expiry from the given parameters, or null if no expiry was provided.
setWatch(string $watch, Title $title, User $user, ?string $userOption=null, ?string $expiry=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
getWatchlistExpiry(WatchedItemStoreInterface $store, Title $title, UserIdentity $user)
Get existing expiry from the database.
This abstract class implements many basic API functions, and is the base of all API classes.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
checkUserRightsAny( $rights, $user=null)
Helper function for permission-denied errors.
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
getResult()
Get the result object.
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
getModuleName()
Get the name of the module being executed by this instance.
dieStatus(StatusValue $status)
Throw an ApiUsageException based on the Status object.
API module that facilitates the blocking of users.
needsToken()
Returns the token type this module requires in order to execute.
getExamplesMessages()
Returns usage examples for this module.
isWriteMode()
Indicates whether this module requires write mode.
__construct(ApiMain $main, $action, BlockPermissionCheckerFactory $blockPermissionCheckerFactory, BlockUserFactory $blockUserFactory, TitleFactory $titleFactory, UserIdentityLookup $userIdentityLookup, WatchedItemStoreInterface $watchedItemStore, BlockUtils $blockUtils, BlockActionInfo $blockActionInfo, WatchlistManager $watchlistManager, UserOptionsLookup $userOptionsLookup)
mustBePosted()
Indicates whether this module must be called with a POST request.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
execute()
Blocks the user specified in the parameters for the given expiry, with the given reason,...
getHelpUrls()
Return links to more detailed help pages about the module.
This is the main API class, used for both external and internal processing.
A class containing constants representing the names of configuration variables.
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...