8f71b1609d1902f83f7ec5207cd4f6a9653ab36e
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
{
35 public function __construct( $main, $action ) {
36 parent
::__construct( $main, $action );
40 * Blocks the user specified in the parameters for the given expiry, with the
41 * given reason, and with all other settings provided in the params. If the block
42 * succeeds, produces a result containing the details of the block and notice
43 * of success. If it fails, the result will specify the nature of the error.
45 public function execute() {
46 $user = $this->getUser();
47 $params = $this->extractRequestParams();
49 if ( $params['gettoken'] ) {
50 $res['blocktoken'] = $user->getEditToken();
51 $this->getResult()->addValue( null, $this->getModuleName(), $res );
55 if ( !$user->isAllowed( 'block' ) ) {
56 $this->dieUsageMsg( 'cantblock' );
59 # bug 15810: blocked admins should have limited access here
60 if ( $user->isBlocked() ) {
61 $status = SpecialBlock
::checkUnblockSelf( $params['user'], $user );
62 if ( $status !== true ) {
63 $this->dieUsageMsg( array( $status ) );
67 $target = User
::newFromName( $params['user'] );
68 // Bug 38633 - if the target is a user (not an IP address), but it doesn't exist or is unusable, error.
69 if ( $target instanceof User
&& ( $target->isAnon() /* doesn't exist */ ||
!User
::isUsableName( $target->getName() ) ) ) {
70 $this->dieUsageMsg( array( 'nosuchuser', $params['user'] ) );
73 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
74 $this->dieUsageMsg( 'canthide' );
76 if ( $params['noemail'] && !SpecialBlock
::canBlockEmail( $user ) ) {
77 $this->dieUsageMsg( 'cantblock-email' );
81 'PreviousTarget' => $params['user'],
82 'Target' => $params['user'],
88 'Expiry' => $params['expiry'] == 'never' ?
'infinite' : $params['expiry'],
89 'HardBlock' => !$params['anononly'],
90 'CreateAccount' => $params['nocreate'],
91 'AutoBlock' => $params['autoblock'],
92 'DisableEmail' => $params['noemail'],
93 'HideUser' => $params['hidename'],
94 'DisableUTEdit' => !$params['allowusertalk'],
95 'Reblock' => $params['reblock'],
96 'Watch' => $params['watchuser'],
100 $retval = SpecialBlock
::processForm( $data, $this->getContext() );
101 if ( $retval !== true ) {
102 // We don't care about multiple errors, just report one of them
103 $this->dieUsageMsg( $retval );
106 list( $target, /*...*/ ) = SpecialBlock
::getTargetAndType( $params['user'] );
107 $res['user'] = $params['user'];
108 $res['userID'] = $target instanceof User ?
$target->getId() : 0;
110 $block = Block
::newFromTarget( $target );
111 if( $block instanceof Block
) {
112 $res['expiry'] = $block->mExpiry
== $this->getDB()->getInfinity()
114 : wfTimestamp( TS_ISO_8601
, $block->mExpiry
);
115 $res['id'] = $block->getId();
117 # should be unreachable
122 $res['reason'] = $params['reason'];
123 if ( $params['anononly'] ) {
124 $res['anononly'] = '';
126 if ( $params['nocreate'] ) {
127 $res['nocreate'] = '';
129 if ( $params['autoblock'] ) {
130 $res['autoblock'] = '';
132 if ( $params['noemail'] ) {
133 $res['noemail'] = '';
135 if ( $params['hidename'] ) {
136 $res['hidename'] = '';
138 if ( $params['allowusertalk'] ) {
139 $res['allowusertalk'] = '';
141 if ( $params['watchuser'] ) {
142 $res['watchuser'] = '';
145 $this->getResult()->addValue( null, $this->getModuleName(), $res );
148 public function mustBePosted() {
152 public function isWriteMode() {
156 public function getAllowedParams() {
159 ApiBase
::PARAM_TYPE
=> 'string',
160 ApiBase
::PARAM_REQUIRED
=> true
164 ApiBase
::PARAM_DFLT
=> false,
165 ApiBase
::PARAM_DEPRECATED
=> true,
171 'autoblock' => false,
174 'allowusertalk' => false,
176 'watchuser' => false,
180 public function getParamDescription() {
182 'user' => 'Username, IP address or IP range you want to block',
183 'token' => 'A block token previously obtained through prop=info',
184 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
185 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
186 'reason' => 'Reason for block',
187 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
188 'nocreate' => 'Prevent account creation',
189 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
190 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
191 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
192 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
193 'reblock' => 'If the user is already blocked, overwrite the existing block',
194 'watchuser' => 'Watch the user/IP\'s user and talk pages',
198 public function getResultProperties() {
201 'blocktoken' => array(
202 ApiBase
::PROP_TYPE
=> 'string',
203 ApiBase
::PROP_NULLABLE
=> true
206 ApiBase
::PROP_TYPE
=> 'string',
207 ApiBase
::PROP_NULLABLE
=> true
210 ApiBase
::PROP_TYPE
=> 'integer',
211 ApiBase
::PROP_NULLABLE
=> true
214 ApiBase
::PROP_TYPE
=> 'string',
215 ApiBase
::PROP_NULLABLE
=> true
218 ApiBase
::PROP_TYPE
=> 'integer',
219 ApiBase
::PROP_NULLABLE
=> true
222 ApiBase
::PROP_TYPE
=> 'string',
223 ApiBase
::PROP_NULLABLE
=> true
225 'anononly' => 'boolean',
226 'nocreate' => 'boolean',
227 'autoblock' => 'boolean',
228 'noemail' => 'boolean',
229 'hidename' => 'boolean',
230 'allowusertalk' => 'boolean',
231 'watchuser' => 'boolean'
236 public function getDescription() {
237 return 'Block a user';
240 public function getPossibleErrors() {
241 return array_merge( parent
::getPossibleErrors(), array(
242 array( 'cantblock' ),
244 array( 'cantblock-email' ),
245 array( 'ipbblocked' ),
246 array( 'ipbnounblockself' ),
250 public function needsToken() {
254 public function getTokenSalt() {
258 public function getExamples() {
260 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
261 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
265 public function getHelpUrls() {
266 return 'https://www.mediawiki.org/wiki/API:Block';