3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * API module that facilitates the blocking of users. Requires API write mode
29 class ApiBlock
extends ApiBase
{
32 * Blocks the user specified in the parameters for the given expiry, with the
33 * given reason, and with all other settings provided in the params. If the block
34 * succeeds, produces a result containing the details of the block and notice
35 * of success. If it fails, the result will specify the nature of the error.
37 public function execute() {
38 $this->checkUserRightsAny( 'block' );
40 $user = $this->getUser();
41 $params = $this->extractRequestParams();
43 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
45 # T17810: blocked admins should have limited access here
46 if ( $user->isBlocked() ) {
47 $status = SpecialBlock
::checkUnblockSelf( $params['user'], $user );
48 if ( $status !== true ) {
52 [ 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $user->getBlock() ) ]
57 if ( $params['userid'] !== null ) {
58 $username = User
::whoIs( $params['userid'] );
60 if ( $username === false ) {
61 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
63 $params['user'] = $username;
66 list( $target, $type ) = SpecialBlock
::getTargetAndType( $params['user'] );
68 // T40633 - if the target is a user (not an IP address), but it
69 // doesn't exist or is unusable, error.
70 if ( $type === Block
::TYPE_USER
&&
71 ( $target->isAnon() /* doesn't exist */ ||
!User
::isUsableName( $params['user'] ) )
73 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
77 if ( $params['tags'] ) {
78 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
79 if ( !$ableToTag->isOK() ) {
80 $this->dieStatus( $ableToTag );
84 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
85 $this->dieWithError( 'apierror-canthide' );
87 if ( $params['noemail'] && !SpecialBlock
::canBlockEmail( $user ) ) {
88 $this->dieWithError( 'apierror-cantblock-email' );
92 'PreviousTarget' => $params['user'],
93 'Target' => $params['user'],
99 'Expiry' => $params['expiry'],
100 'HardBlock' => !$params['anononly'],
101 'CreateAccount' => $params['nocreate'],
102 'AutoBlock' => $params['autoblock'],
103 'DisableEmail' => $params['noemail'],
104 'HideUser' => $params['hidename'],
105 'DisableUTEdit' => !$params['allowusertalk'],
106 'Reblock' => $params['reblock'],
107 'Watch' => $params['watchuser'],
109 'Tags' => $params['tags'],
112 $retval = SpecialBlock
::processForm( $data, $this->getContext() );
113 if ( $retval !== true ) {
114 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
117 list( $target, /*...*/ ) = SpecialBlock
::getTargetAndType( $params['user'] );
118 $res['user'] = $params['user'];
119 $res['userID'] = $target instanceof User ?
$target->getId() : 0;
121 $block = Block
::newFromTarget( $target, null, true );
122 if ( $block instanceof Block
) {
123 $res['expiry'] = ApiResult
::formatExpiry( $block->mExpiry
, 'infinite' );
124 $res['id'] = $block->getId();
126 # should be unreachable
127 $res['expiry'] = ''; // @codeCoverageIgnore
128 $res['id'] = ''; // @codeCoverageIgnore
131 $res['reason'] = $params['reason'];
132 $res['anononly'] = $params['anononly'];
133 $res['nocreate'] = $params['nocreate'];
134 $res['autoblock'] = $params['autoblock'];
135 $res['noemail'] = $params['noemail'];
136 $res['hidename'] = $params['hidename'];
137 $res['allowusertalk'] = $params['allowusertalk'];
138 $res['watchuser'] = $params['watchuser'];
140 $this->getResult()->addValue( null, $this->getModuleName(), $res );
143 public function mustBePosted() {
147 public function isWriteMode() {
151 public function getAllowedParams() {
154 ApiBase
::PARAM_TYPE
=> 'user',
157 ApiBase
::PARAM_TYPE
=> 'integer',
163 'autoblock' => false,
166 'allowusertalk' => false,
168 'watchuser' => false,
170 ApiBase
::PARAM_TYPE
=> 'tags',
171 ApiBase
::PARAM_ISMULTI
=> true,
176 public function needsToken() {
180 protected function getExamplesMessages() {
181 // phpcs:disable Generic.Files.LineLength
183 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
184 => 'apihelp-block-example-ip-simple',
185 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
186 => 'apihelp-block-example-user-complex',
191 public function getHelpUrls() {
192 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';