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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
33 * API module that facilitates the blocking of users. Requires API write mode
38 class ApiBlock
extends ApiBase
{
40 public function __construct( $main, $action ) {
41 parent
::__construct( $main, $action );
45 * Blocks the user specified in the parameters for the given expiry, with the
46 * given reason, and with all other settings provided in the params. If the block
47 * succeeds, produces a result containing the details of the block and notice
48 * of success. If it fails, the result will specify the nature of the error.
50 public function execute() {
51 global $wgUser, $wgBlockAllowsUTEdit;
52 $params = $this->extractRequestParams();
54 if ( $params['gettoken'] ) {
55 $res['blocktoken'] = $wgUser->editToken();
56 $this->getResult()->addValue( null, $this->getModuleName(), $res );
60 if ( !$wgUser->isAllowed( 'block' ) ) {
61 $this->dieUsageMsg( array( 'cantblock' ) );
63 # bug 15810: blocked admins should have limited access here
64 if ( $wgUser->isBlocked() ) {
65 $status = IPBlockForm
::checkUnblockSelf( $params['user'] );
66 if ( $status !== true ) {
67 $this->dieUsageMsg( array( $status ) );
70 if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) ) {
71 $this->dieUsageMsg( array( 'canthide' ) );
73 if ( $params['noemail'] && !IPBlockForm
::canBlockEmail( $wgUser ) ) {
74 $this->dieUsageMsg( array( 'cantblock-email' ) );
77 $form = new IPBlockForm( '' );
78 $form->BlockAddress
= $params['user'];
79 $form->BlockReason
= ( is_null( $params['reason'] ) ?
'' : $params['reason'] );
80 $form->BlockReasonList
= 'other';
81 $form->BlockExpiry
= ( $params['expiry'] == 'never' ?
'infinite' : $params['expiry'] );
82 $form->BlockOther
= '';
83 $form->BlockAnonOnly
= $params['anononly'];
84 $form->BlockCreateAccount
= $params['nocreate'];
85 $form->BlockEnableAutoblock
= $params['autoblock'];
86 $form->BlockEmail
= $params['noemail'];
87 $form->BlockHideName
= $params['hidename'];
88 $form->BlockAllowUsertalk
= $params['allowusertalk'] && $wgBlockAllowsUTEdit;
89 $form->BlockReblock
= $params['reblock'];
90 $form->BlockWatchUser
= $params['watchuser'];
92 $userID = $expiry = null;
93 $retval = $form->doBlock( $userID, $expiry );
94 if ( count( $retval ) ) {
95 // We don't care about multiple errors, just report one of them
96 $this->dieUsageMsg( $retval );
99 $res['user'] = $params['user'];
100 $res['userID'] = intval( $userID );
101 $res['expiry'] = ( $expiry == Block
::infinity() ?
'infinite' : wfTimestamp( TS_ISO_8601
, $expiry ) );
102 $res['reason'] = $params['reason'];
103 if ( $params['anononly'] ) {
104 $res['anononly'] = '';
106 if ( $params['nocreate'] ) {
107 $res['nocreate'] = '';
109 if ( $params['autoblock'] ) {
110 $res['autoblock'] = '';
112 if ( $params['noemail'] ) {
113 $res['noemail'] = '';
115 if ( $params['hidename'] ) {
116 $res['hidename'] = '';
118 if ( $params['allowusertalk'] ) {
119 $res['allowusertalk'] = '';
121 if ( $params['watchuser'] ) {
122 $res['watchuser'] = '';
125 $this->getResult()->addValue( null, $this->getModuleName(), $res );
128 public function mustBePosted() {
132 public function isWriteMode() {
136 public function getAllowedParams() {
139 ApiBase
::PARAM_TYPE
=> 'string',
140 ApiBase
::PARAM_REQUIRED
=> true
148 'autoblock' => false,
151 'allowusertalk' => false,
153 'watchuser' => false,
157 public function getParamDescription() {
159 'user' => 'Username, IP address or IP range you want to block',
160 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
161 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
162 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
163 'reason' => 'Reason for block (optional)',
164 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
165 'nocreate' => 'Prevent account creation',
166 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
167 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
168 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
169 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
170 'reblock' => 'If the user is already blocked, overwrite the existing block',
171 'watchuser' => 'Watch the user/IP\'s user and talk pages',
175 public function getDescription() {
176 return 'Block a user';
179 public function getPossibleErrors() {
180 return array_merge( parent
::getPossibleErrors(), array(
181 array( 'cantblock' ),
183 array( 'cantblock-email' ),
184 array( 'ipbblocked' ),
185 array( 'ipbnounblockself' ),
189 public function needsToken() {
193 public function getTokenSalt() {
197 protected function getExamples() {
199 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
200 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
204 public function getVersion() {
205 return __CLASS__
. ': $Id$';