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() {
44 $this->checkUserRightsAny( 'block' );
46 $user = $this->getUser();
47 $params = $this->extractRequestParams();
49 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
51 # bug 15810: blocked admins should have limited access here
52 if ( $user->isBlocked() ) {
53 $status = SpecialBlock
::checkUnblockSelf( $params['user'], $user );
54 if ( $status !== true ) {
58 [ 'blockinfo' => ApiQueryUserInfo
::getBlockInfo( $user->getBlock() ) ]
63 if ( $params['userid'] !== null ) {
64 $username = User
::whoIs( $params['userid'] );
66 if ( $username === false ) {
67 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
69 $params['user'] = $username;
72 $target = User
::newFromName( $params['user'] );
74 // Bug 38633 - if the target is a user (not an IP address), but it
75 // doesn't exist or is unusable, error.
76 if ( $target instanceof User
&&
77 ( $target->isAnon() /* doesn't exist */ ||
!User
::isUsableName( $target->getName() ) )
79 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
83 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
84 $this->dieWithError( 'apierror-canthide' );
86 if ( $params['noemail'] && !SpecialBlock
::canBlockEmail( $user ) ) {
87 $this->dieWithError( 'apierror-cantblock-email' );
91 'PreviousTarget' => $params['user'],
92 'Target' => $params['user'],
98 'Expiry' => $params['expiry'],
99 'HardBlock' => !$params['anononly'],
100 'CreateAccount' => $params['nocreate'],
101 'AutoBlock' => $params['autoblock'],
102 'DisableEmail' => $params['noemail'],
103 'HideUser' => $params['hidename'],
104 'DisableUTEdit' => !$params['allowusertalk'],
105 'Reblock' => $params['reblock'],
106 'Watch' => $params['watchuser'],
110 $retval = SpecialBlock
::processForm( $data, $this->getContext() );
111 if ( $retval !== true ) {
112 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
115 list( $target, /*...*/ ) = SpecialBlock
::getTargetAndType( $params['user'] );
116 $res['user'] = $params['user'];
117 $res['userID'] = $target instanceof User ?
$target->getId() : 0;
119 $block = Block
::newFromTarget( $target, null, true );
120 if ( $block instanceof Block
) {
121 $res['expiry'] = $wgContLang->formatExpiry( $block->mExpiry
, TS_ISO_8601
, 'infinite' );
122 $res['id'] = $block->getId();
124 # should be unreachable
129 $res['reason'] = $params['reason'];
130 $res['anononly'] = $params['anononly'];
131 $res['nocreate'] = $params['nocreate'];
132 $res['autoblock'] = $params['autoblock'];
133 $res['noemail'] = $params['noemail'];
134 $res['hidename'] = $params['hidename'];
135 $res['allowusertalk'] = $params['allowusertalk'];
136 $res['watchuser'] = $params['watchuser'];
138 $this->getResult()->addValue( null, $this->getModuleName(), $res );
141 public function mustBePosted() {
145 public function isWriteMode() {
149 public function getAllowedParams() {
152 ApiBase
::PARAM_TYPE
=> 'user',
155 ApiBase
::PARAM_TYPE
=> 'integer',
161 'autoblock' => false,
164 'allowusertalk' => false,
166 'watchuser' => false,
170 public function needsToken() {
174 protected function getExamplesMessages() {
175 // @codingStandardsIgnoreStart Generic.Files.LineLength
177 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
178 => 'apihelp-block-example-ip-simple',
179 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
180 => 'apihelp-block-example-user-complex',
182 // @codingStandardsIgnoreEnd
185 public function getHelpUrls() {
186 return 'https://www.mediawiki.org/wiki/API:Block';