5 * Created on Sep 4, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API module that facilitates the blocking of users. Requires API write mode
33 class ApiBlock
extends ApiBase
{
36 * Blocks the user specified in the parameters for the given expiry, with the
37 * given reason, and with all other settings provided in the params. If the block
38 * succeeds, produces a result containing the details of the block and notice
39 * of success. If it fails, the result will specify the nature of the error.
41 public function execute() {
42 $this->checkUserRightsAny( 'block' );
44 $user = $this->getUser();
45 $params = $this->extractRequestParams();
47 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
49 # bug 15810: blocked admins should have limited access here
50 if ( $user->isBlocked() ) {
51 $status = SpecialBlock
::checkUnblockSelf( $params['user'], $user );
52 if ( $status !== true ) {
56 [ 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $user->getBlock() ) ]
61 if ( $params['userid'] !== null ) {
62 $username = User
::whoIs( $params['userid'] );
64 if ( $username === false ) {
65 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
67 $params['user'] = $username;
70 $target = User
::newFromName( $params['user'] );
72 // Bug 38633 - if the target is a user (not an IP address), but it
73 // doesn't exist or is unusable, error.
74 if ( $target instanceof User
&&
75 ( $target->isAnon() /* doesn't exist */ ||
!User
::isUsableName( $target->getName() ) )
77 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
81 if ( $params['tags'] ) {
82 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
83 if ( !$ableToTag->isOK() ) {
84 $this->dieStatus( $ableToTag );
88 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
89 $this->dieWithError( 'apierror-canthide' );
91 if ( $params['noemail'] && !SpecialBlock
::canBlockEmail( $user ) ) {
92 $this->dieWithError( 'apierror-cantblock-email' );
96 'PreviousTarget' => $params['user'],
97 'Target' => $params['user'],
103 'Expiry' => $params['expiry'],
104 'HardBlock' => !$params['anononly'],
105 'CreateAccount' => $params['nocreate'],
106 'AutoBlock' => $params['autoblock'],
107 'DisableEmail' => $params['noemail'],
108 'HideUser' => $params['hidename'],
109 'DisableUTEdit' => !$params['allowusertalk'],
110 'Reblock' => $params['reblock'],
111 'Watch' => $params['watchuser'],
113 'Tags' => $params['tags'],
116 $retval = SpecialBlock
::processForm( $data, $this->getContext() );
117 if ( $retval !== true ) {
118 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
121 list( $target, /*...*/ ) = SpecialBlock
::getTargetAndType( $params['user'] );
122 $res['user'] = $params['user'];
123 $res['userID'] = $target instanceof User ?
$target->getId() : 0;
125 $block = Block
::newFromTarget( $target, null, true );
126 if ( $block instanceof Block
) {
127 $res['expiry'] = ApiResult
::formatExpiry( $block->mExpiry
, 'infinite' );
128 $res['id'] = $block->getId();
130 # should be unreachable
135 $res['reason'] = $params['reason'];
136 $res['anononly'] = $params['anononly'];
137 $res['nocreate'] = $params['nocreate'];
138 $res['autoblock'] = $params['autoblock'];
139 $res['noemail'] = $params['noemail'];
140 $res['hidename'] = $params['hidename'];
141 $res['allowusertalk'] = $params['allowusertalk'];
142 $res['watchuser'] = $params['watchuser'];
144 $this->getResult()->addValue( null, $this->getModuleName(), $res );
147 public function mustBePosted() {
151 public function isWriteMode() {
155 public function getAllowedParams() {
158 ApiBase
::PARAM_TYPE
=> 'user',
161 ApiBase
::PARAM_TYPE
=> 'integer',
167 'autoblock' => false,
170 'allowusertalk' => false,
172 'watchuser' => false,
174 ApiBase
::PARAM_TYPE
=> 'tags',
175 ApiBase
::PARAM_ISMULTI
=> true,
180 public function needsToken() {
184 protected function getExamplesMessages() {
185 // @codingStandardsIgnoreStart Generic.Files.LineLength
187 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
188 => 'apihelp-block-example-ip-simple',
189 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
190 => 'apihelp-block-example-user-complex',
192 // @codingStandardsIgnoreEnd
195 public function getHelpUrls() {
196 return 'https://www.mediawiki.org/wiki/API:Block';