89 parent::__construct( $main, $action );
91 $this->blockPermissionCheckerFactory = $blockPermissionCheckerFactory;
92 $this->blockUserFactory = $blockUserFactory;
93 $this->titleFactory = $titleFactory;
94 $this->userIdentityLookup = $userIdentityLookup;
95 $this->watchedItemStore = $watchedItemStore;
96 $this->blockUtils = $blockUtils;
97 $this->blockActionInfo = $blockActionInfo;
100 $this->watchlistExpiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
101 $this->watchlistMaxDuration =
102 $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
103 $this->watchlistManager = $watchlistManager;
104 $this->userOptionsLookup = $userOptionsLookup;
119 if ( $params[
'user'] !==
null ) {
120 $target = $params[
'user'];
122 $target = $this->userIdentityLookup->getUserIdentityByUserId( $params[
'userid'] );
124 $this->
dieWithError( [
'apierror-nosuchuserid', $params[
'userid'] ],
'nosuchuserid' );
127 [ $target, $targetType ] = $this->blockUtils->parseBlockTarget( $target );
130 $params[
'noemail'] &&
131 !$this->blockPermissionCheckerFactory
132 ->newBlockPermissionChecker(
136 ->checkEmailPermissions()
142 if ( $params[
'partial'] ) {
143 $pageRestrictions = array_map(
144 [ PageRestriction::class,
'newFromTitle' ],
145 (array)$params[
'pagerestrictions']
148 $namespaceRestrictions = array_map(
static function ( $id ) {
150 }, (array)$params[
'namespacerestrictions'] );
151 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
153 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
154 $actionRestrictions = array_map(
function ( $action ) {
155 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
156 }, (array)$params[
'actionrestrictions'] );
157 $restrictions = array_merge( $restrictions, $actionRestrictions );
161 $status = $this->blockUserFactory->newBlockUser(
167 'isCreateAccountBlocked' => $params[
'nocreate'],
168 'isEmailBlocked' => $params[
'noemail'],
169 'isHardBlock' => !$params[
'anononly'],
170 'isAutoblocking' => $params[
'autoblock'],
171 'isUserTalkEditBlocked' => !$params[
'allowusertalk'],
172 'isHideUser' => $params[
'hidename'],
173 'isPartial' => $params[
'partial'],
177 )->placeBlock( $params[
'reblock'] );
179 if ( !$status->isOK() ) {
183 $block = $status->value;
186 $userPage = Title::makeTitle(
NS_USER, $block->getTargetName() );
188 if ( $params[
'watchuser'] && $targetType !== AbstractBlock::TYPE_RANGE ) {
189 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
194 $res[
'user'] = $block->getTargetName();
199 $res[
'id'] = $block->getId();
201 # should be unreachable
206 $res[
'reason'] = $params[
'reason'];
207 $res[
'anononly'] = $params[
'anononly'];
208 $res[
'nocreate'] = $params[
'nocreate'];
209 $res[
'autoblock'] = $params[
'autoblock'];
210 $res[
'noemail'] = $params[
'noemail'];
211 $res[
'hidename'] = $block->getHideName();
212 $res[
'allowusertalk'] = $params[
'allowusertalk'];
213 $res[
'watchuser'] = $params[
'watchuser'];
214 if ( $watchlistExpiry ) {
216 $this->watchedItemStore,
220 $res[
'watchlistexpiry'] = $expiry;
222 $res[
'partial'] = $params[
'partial'];
223 $res[
'pagerestrictions'] = $params[
'pagerestrictions'];
224 $res[
'namespacerestrictions'] = $params[
'namespacerestrictions'];
225 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
226 $res[
'actionrestrictions'] = $params[
'actionrestrictions'];
243 ParamValidator::PARAM_TYPE =>
'user',
244 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'cidr',
'id' ],
247 ParamValidator::PARAM_TYPE =>
'integer',
248 ParamValidator::PARAM_DEPRECATED =>
true,
254 'autoblock' =>
false,
257 'allowusertalk' =>
false,
259 'watchuser' =>
false,
265 if ( $this->watchlistExpiryEnabled ) {
267 'watchlistexpiry' => [
268 ParamValidator::PARAM_TYPE =>
'expiry',
269 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
270 ExpiryDef::PARAM_USE_MAX =>
true,
277 ParamValidator::PARAM_TYPE =>
'tags',
278 ParamValidator::PARAM_ISMULTI =>
true,
281 'pagerestrictions' => [
282 ParamValidator::PARAM_TYPE =>
'title',
283 TitleDef::PARAM_MUST_EXIST =>
true,
291 ParamValidator::PARAM_ISMULTI =>
true,
292 ParamValidator::PARAM_ISMULTI_LIMIT1 => 10,
293 ParamValidator::PARAM_ISMULTI_LIMIT2 => 10,
295 'namespacerestrictions' => [
296 ParamValidator::PARAM_ISMULTI =>
true,
297 ParamValidator::PARAM_TYPE =>
'namespace',
301 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
303 'actionrestrictions' => [
304 ParamValidator::PARAM_ISMULTI =>
true,
305 ParamValidator::PARAM_TYPE => array_keys(
306 $this->blockActionInfo->getAllBlockActions()
322 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
323 =>
'apihelp-block-example-ip-simple',
324 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
325 =>
'apihelp-block-example-user-complex',
331 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 0 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.
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
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...