54 private $blockPermissionCheckerFactory;
57 private $blockUserFactory;
60 private $titleFactory;
63 private $userIdentityLookup;
66 private $watchedItemStore;
72 private $blockActionInfo;
100 parent::__construct( $main, $action );
102 $this->blockPermissionCheckerFactory = $blockPermissionCheckerFactory;
103 $this->blockUserFactory = $blockUserFactory;
104 $this->titleFactory = $titleFactory;
105 $this->userIdentityLookup = $userIdentityLookup;
106 $this->watchedItemStore = $watchedItemStore;
107 $this->blockUtils = $blockUtils;
108 $this->blockActionInfo = $blockActionInfo;
111 $this->watchlistExpiryEnabled = $this->
getConfig()->get( MainConfigNames::WatchlistExpiry );
112 $this->watchlistMaxDuration =
113 $this->
getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
114 $this->watchlistManager = $watchlistManager;
115 $this->userOptionsLookup = $userOptionsLookup;
130 if ( $params[
'user'] !==
null ) {
131 $target = $params[
'user'];
133 $target = $this->userIdentityLookup->getUserIdentityByUserId( $params[
'userid'] );
135 $this->
dieWithError( [
'apierror-nosuchuserid', $params[
'userid'] ],
'nosuchuserid' );
138 list( $target, $targetType ) = $this->blockUtils->parseBlockTarget( $target );
141 $params[
'noemail'] &&
142 !$this->blockPermissionCheckerFactory
143 ->newBlockPermissionChecker(
147 ->checkEmailPermissions()
153 if ( $params[
'partial'] ) {
154 $pageRestrictions = array_map(
155 [ PageRestriction::class,
'newFromTitle' ],
156 (array)$params[
'pagerestrictions']
159 $namespaceRestrictions = array_map(
static function ( $id ) {
161 }, (array)$params[
'namespacerestrictions'] );
162 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
164 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
165 $actionRestrictions = array_map(
function ( $action ) {
166 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
167 }, (array)$params[
'actionrestrictions'] );
168 $restrictions = array_merge( $restrictions, $actionRestrictions );
172 $status = $this->blockUserFactory->newBlockUser(
178 'isCreateAccountBlocked' => $params[
'nocreate'],
179 'isEmailBlocked' => $params[
'noemail'],
180 'isHardBlock' => !$params[
'anononly'],
181 'isAutoblocking' => $params[
'autoblock'],
182 'isUserTalkEditBlocked' => !$params[
'allowusertalk'],
183 'isHideUser' => $params[
'hidename'],
184 'isPartial' => $params[
'partial'],
188 )->placeBlock( $params[
'reblock'] );
190 if ( !$status->isOK() ) {
194 $block = $status->value;
197 $userPage = Title::makeTitle(
NS_USER, $block->getTargetName() );
199 if ( $params[
'watchuser'] && $targetType !== AbstractBlock::TYPE_RANGE ) {
200 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
205 $res[
'user'] = $block->getTargetName();
209 $res[
'expiry'] = ApiResult::formatExpiry( $block->getExpiry(),
'infinite' );
210 $res[
'id'] = $block->getId();
212 # should be unreachable
217 $res[
'reason'] = $params[
'reason'];
218 $res[
'anononly'] = $params[
'anononly'];
219 $res[
'nocreate'] = $params[
'nocreate'];
220 $res[
'autoblock'] = $params[
'autoblock'];
221 $res[
'noemail'] = $params[
'noemail'];
222 $res[
'hidename'] = $params[
'hidename'];
223 $res[
'allowusertalk'] = $params[
'allowusertalk'];
224 $res[
'watchuser'] = $params[
'watchuser'];
225 if ( $watchlistExpiry ) {
227 $this->watchedItemStore,
231 $res[
'watchlistexpiry'] = $expiry;
233 $res[
'partial'] = $params[
'partial'];
234 $res[
'pagerestrictions'] = $params[
'pagerestrictions'];
235 $res[
'namespacerestrictions'] = $params[
'namespacerestrictions'];
236 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
237 $res[
'actionrestrictions'] = $params[
'actionrestrictions'];
254 ParamValidator::PARAM_TYPE =>
'user',
255 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'cidr',
'id' ],
258 ParamValidator::PARAM_TYPE =>
'integer',
259 ParamValidator::PARAM_DEPRECATED =>
true,
265 'autoblock' =>
false,
268 'allowusertalk' =>
false,
270 'watchuser' =>
false,
276 if ( $this->watchlistExpiryEnabled ) {
278 'watchlistexpiry' => [
279 ParamValidator::PARAM_TYPE =>
'expiry',
280 ExpiryDef::PARAM_MAX => $this->watchlistMaxDuration,
281 ExpiryDef::PARAM_USE_MAX =>
true,
288 ParamValidator::PARAM_TYPE =>
'tags',
289 ParamValidator::PARAM_ISMULTI =>
true,
292 'pagerestrictions' => [
293 ParamValidator::PARAM_TYPE =>
'title',
294 TitleDef::PARAM_MUST_EXIST =>
true,
302 ParamValidator::PARAM_ISMULTI =>
true,
303 ParamValidator::PARAM_ISMULTI_LIMIT1 => 10,
304 ParamValidator::PARAM_ISMULTI_LIMIT2 => 10,
306 'namespacerestrictions' => [
307 ParamValidator::PARAM_ISMULTI =>
true,
308 ParamValidator::PARAM_TYPE =>
'namespace',
312 if ( $this->
getConfig()->
get( MainConfigNames::EnablePartialActionBlocks ) ) {
314 'actionrestrictions' => [
315 ParamValidator::PARAM_ISMULTI =>
true,
316 ParamValidator::PARAM_TYPE => array_keys(
317 $this->blockActionInfo->getAllBlockActions()
333 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
334 =>
'apihelp-block-example-ip-simple',
335 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
336 =>
'apihelp-block-example-user-complex',
342 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...