90 parent::__construct( $main, $action );
92 $this->blockPermissionCheckerFactory = $blockPermissionCheckerFactory;
93 $this->blockUserFactory = $blockUserFactory;
94 $this->titleFactory = $titleFactory;
95 $this->userIdentityLookup = $userIdentityLookup;
96 $this->watchedItemStore = $watchedItemStore;
97 $this->blockUtils = $blockUtils;
98 $this->blockActionInfo = $blockActionInfo;
101 $this->watchlistExpiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
102 $this->watchlistMaxDuration =
103 $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
104 $this->watchlistManager = $watchlistManager;
105 $this->userOptionsLookup = $userOptionsLookup;
120 if (
$params[
'user'] !==
null ) {
123 $target = $this->userIdentityLookup->getUserIdentityByUserId(
$params[
'userid'] );
128 [ $target, $targetType ] = $this->blockUtils->parseBlockTarget( $target );
132 !$this->blockPermissionCheckerFactory
133 ->newBlockPermissionChecker(
137 ->checkEmailPermissions()
144 $pageRestrictions = array_map(
145 [ PageRestriction::class,
'newFromTitle' ],
146 (array)
$params[
'pagerestrictions']
149 $namespaceRestrictions = array_map(
static function ( $id ) {
151 }, (array)
$params[
'namespacerestrictions'] );
152 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
154 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
155 $actionRestrictions = array_map(
function ( $action ) {
156 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
157 }, (array)
$params[
'actionrestrictions'] );
158 $restrictions = array_merge( $restrictions, $actionRestrictions );
162 $status = $this->blockUserFactory->newBlockUser(
168 'isCreateAccountBlocked' =>
$params[
'nocreate'],
169 'isEmailBlocked' =>
$params[
'noemail'],
170 'isHardBlock' => !
$params[
'anononly'],
171 'isAutoblocking' =>
$params[
'autoblock'],
172 'isUserTalkEditBlocked' => !
$params[
'allowusertalk'],
173 'isHideUser' =>
$params[
'hidename'],
174 'isPartial' =>
$params[
'partial'],
178 )->placeBlock(
$params[
'reblock'] );
180 if ( !$status->isOK() ) {
184 $block = $status->value;
187 $userPage = Title::makeTitle(
NS_USER, $block->getTargetName() );
189 if (
$params[
'watchuser'] && $targetType !== AbstractBlock::TYPE_RANGE ) {
190 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
195 $res[
'user'] = $block->getTargetName();
199 $res[
'expiry'] = ApiResult::formatExpiry( $block->getExpiry(),
'infinite' );
200 $res[
'id'] = $block->getId();
202 # should be unreachable
207 $res[
'reason'] =
$params[
'reason'];
208 $res[
'anononly'] =
$params[
'anononly'];
209 $res[
'nocreate'] =
$params[
'nocreate'];
210 $res[
'autoblock'] =
$params[
'autoblock'];
211 $res[
'noemail'] =
$params[
'noemail'];
212 $res[
'hidename'] = $block->getHideName();
213 $res[
'allowusertalk'] =
$params[
'allowusertalk'];
214 $res[
'watchuser'] =
$params[
'watchuser'];
215 if ( $watchlistExpiry ) {
217 $this->watchedItemStore,
221 $res[
'watchlistexpiry'] = $expiry;
223 $res[
'partial'] =
$params[
'partial'];
224 $res[
'pagerestrictions'] =
$params[
'pagerestrictions'];
225 $res[
'namespacerestrictions'] =
$params[
'namespacerestrictions'];
226 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
227 $res[
'actionrestrictions'] =
$params[
'actionrestrictions'];
244 ParamValidator::PARAM_TYPE =>
'user',
245 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'cidr',
'id' ],
248 ParamValidator::PARAM_TYPE =>
'integer',
249 ParamValidator::PARAM_DEPRECATED =>
true,
255 'autoblock' =>
false,
258 'allowusertalk' =>
false,
260 'watchuser' =>
false,
266 if ( $this->watchlistExpiryEnabled ) {
268 'watchlistexpiry' => [
269 ParamValidator::PARAM_TYPE =>
'expiry',
270 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
271 ExpiryDef::PARAM_USE_MAX =>
true,
278 ParamValidator::PARAM_TYPE =>
'tags',
279 ParamValidator::PARAM_ISMULTI =>
true,
282 'pagerestrictions' => [
283 ParamValidator::PARAM_TYPE =>
'title',
284 TitleDef::PARAM_MUST_EXIST =>
true,
292 ParamValidator::PARAM_ISMULTI =>
true,
293 ParamValidator::PARAM_ISMULTI_LIMIT1 => 10,
294 ParamValidator::PARAM_ISMULTI_LIMIT2 => 10,
296 'namespacerestrictions' => [
297 ParamValidator::PARAM_ISMULTI =>
true,
298 ParamValidator::PARAM_TYPE =>
'namespace',
302 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
304 'actionrestrictions' => [
305 ParamValidator::PARAM_ISMULTI =>
true,
306 ParamValidator::PARAM_TYPE => array_keys(
307 $this->blockActionInfo->getAllBlockActions()
323 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
324 =>
'apihelp-block-example-ip-simple',
325 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
326 =>
'apihelp-block-example-user-complex',
332 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, PageIdentity $page, User $user, ?string $userOption=null, ?string $expiry=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
getWatchlistExpiry(WatchedItemStoreInterface $store, PageIdentity $page, UserIdentity $user)
Get existing expiry from the database.
array $params
The job parameters.
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)
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.
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...