98 parent::__construct( $main, $action );
109 $this->watchlistExpiryEnabled = $this->
getConfig()->get(
'WatchlistExpiry' );
110 $this->watchlistMaxDuration = $this->
getConfig()->get(
'WatchlistExpiryMaxDuration' );
127 if ( $params[
'user'] !==
null ) {
128 $target = $params[
'user'];
130 $target = $this->userIdentityLookup->getUserIdentityByUserId( $params[
'userid'] );
132 $this->
dieWithError( [
'apierror-nosuchuserid', $params[
'userid'] ],
'nosuchuserid' );
135 list( $target, $targetType ) = $this->blockUtils->parseBlockTarget( $target );
138 $params[
'noemail'] &&
139 !$this->blockPermissionCheckerFactory
140 ->newBlockPermissionChecker(
144 ->checkEmailPermissions()
150 if ( $params[
'partial'] ) {
151 $pageRestrictions = array_map(
static function (
$title ) {
152 return PageRestriction::newFromTitle(
$title );
153 }, (array)$params[
'pagerestrictions'] );
155 $namespaceRestrictions = array_map(
static function ( $id ) {
157 }, (array)$params[
'namespacerestrictions'] );
158 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions );
160 if ( $this->
getConfig()->
get(
'EnablePartialActionBlocks' ) ) {
161 $actionRestrictions = array_map(
function ( $action ) {
162 return new ActionRestriction( 0, $this->blockActionInfo->getIdFromAction( $action ) );
163 }, (array)$params[
'actionrestrictions'] );
164 $restrictions = array_merge( $restrictions, $actionRestrictions );
168 $status = $this->blockUserFactory->newBlockUser(
174 'isCreateAccountBlocked' => $params[
'nocreate'],
175 'isEmailBlocked' => $params[
'noemail'],
176 'isHardBlock' => !$params[
'anononly'],
177 'isAutoblocking' => $params[
'autoblock'],
178 'isUserTalkEditBlocked' => !$params[
'allowusertalk'],
179 'isHideUser' => $params[
'hidename'],
180 'isPartial' => $params[
'partial'],
184 )->placeBlock( $params[
'reblock'] );
186 if ( !$status->isOK() ) {
190 $block = $status->value;
193 $userPage = Title::makeTitle(
NS_USER, $block->getTargetName() );
195 if ( $params[
'watchuser'] && $targetType !== AbstractBlock::TYPE_RANGE ) {
196 $this->
setWatch(
'watch', $userPage, $this->
getUser(),
null, $watchlistExpiry );
201 $res[
'user'] = $block->getTargetName();
205 $res[
'expiry'] = ApiResult::formatExpiry( $block->getExpiry(),
'infinite' );
206 $res[
'id'] = $block->getId();
208 # should be unreachable
213 $res[
'reason'] = $params[
'reason'];
214 $res[
'anononly'] = $params[
'anononly'];
215 $res[
'nocreate'] = $params[
'nocreate'];
216 $res[
'autoblock'] = $params[
'autoblock'];
217 $res[
'noemail'] = $params[
'noemail'];
218 $res[
'hidename'] = $params[
'hidename'];
219 $res[
'allowusertalk'] = $params[
'allowusertalk'];
220 $res[
'watchuser'] = $params[
'watchuser'];
221 if ( $watchlistExpiry ) {
223 $this->watchedItemStore,
227 $res[
'watchlistexpiry'] = $expiry;
229 $res[
'partial'] = $params[
'partial'];
230 $res[
'pagerestrictions'] = $params[
'pagerestrictions'];
231 $res[
'namespacerestrictions'] = $params[
'namespacerestrictions'];
232 if ( $this->
getConfig()->
get(
'EnablePartialActionBlocks' ) ) {
233 $res[
'actionrestrictions'] = $params[
'actionrestrictions'];
251 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'cidr',
'id' ],
261 'autoblock' =>
false,
264 'allowusertalk' =>
false,
266 'watchuser' =>
false,
272 if ( $this->watchlistExpiryEnabled ) {
274 'watchlistexpiry' => [
277 ExpiryDef::PARAM_USE_MAX =>
true,
288 'pagerestrictions' => [
290 TitleDef::PARAM_MUST_EXIST =>
true,
302 'namespacerestrictions' => [
308 if ( $this->
getConfig()->
get(
'EnablePartialActionBlocks' ) ) {
310 'actionrestrictions' => [
313 $this->blockActionInfo->getAllBlockActions()
329 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
330 =>
'apihelp-block-example-ip-simple',
331 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
332 =>
'apihelp-block-example-user-complex',
338 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
string $watchlistMaxDuration
Relative maximum expiry.
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.
WatchlistManager $watchlistManager
UserOptionsLookup $userOptionsLookup
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.
const PARAM_ISMULTI_LIMIT1
requireOnlyOneParameter( $params,... $required)
Die if none or more than one of a certain set of parameters is set and not false.
const PARAM_ISMULTI_LIMIT2
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.
BlockUserFactory $blockUserFactory
isWriteMode()
Indicates whether this module requires write mode.
BlockPermissionCheckerFactory $blockPermissionCheckerFactory
__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.
WatchedItemStoreInterface $watchedItemStore
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,...
UserIdentityLookup $userIdentityLookup
getHelpUrls()
Return links to more detailed help pages about the module.
TitleFactory $titleFactory
BlockActionInfo $blockActionInfo
This is the main API class, used for both external and internal processing.
trait ApiWatchlistTrait
An ApiWatchlistTrait adds class properties and convenience methods for APIs that allow you to watch a...