56c6289581e8da64cac3ad78c97f90222e847cb9
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 $user = $this->getUser();
52 $params = $this->extractRequestParams();
54 if ( $params['gettoken'] ) {
55 $res['blocktoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
56 $this->getResult()->addValue( null, $this->getModuleName(), $res );
60 if ( !$user->isAllowed( 'block' ) ) {
61 $this->dieUsageMsg( 'cantblock' );
63 # bug 15810: blocked admins should have limited access here
64 if ( $user->isBlocked() ) {
65 $status = SpecialBlock
::checkUnblockSelf( $params['user'] );
66 if ( $status !== true ) {
67 $this->dieUsageMsg( array( $status ) );
70 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
71 $this->dieUsageMsg( 'canthide' );
73 if ( $params['noemail'] && !SpecialBlock
::canBlockEmail( $user ) ) {
74 $this->dieUsageMsg( 'cantblock-email' );
78 'Target' => $params['user'],
80 is_null( $params['reason'] ) ?
'' : $params['reason'],
82 is_null( $params['reason'] ) ?
'' : $params['reason']
84 'Expiry' => $params['expiry'] == 'never' ?
'infinite' : $params['expiry'],
85 'HardBlock' => !$params['anononly'],
86 'CreateAccount' => $params['nocreate'],
87 'AutoBlock' => $params['autoblock'],
88 'DisableEmail' => $params['noemail'],
89 'HideUser' => $params['hidename'],
90 'DisableUTEdit' => !$params['allowusertalk'],
91 'AlreadyBlocked' => $params['reblock'],
92 'Watch' => $params['watchuser'],
96 $retval = SpecialBlock
::processForm( $data );
97 if ( $retval !== true ) {
98 // We don't care about multiple errors, just report one of them
99 $this->dieUsageMsg( $retval );
102 list( $target, /*...*/ ) = SpecialBlock
::getTargetAndType( $params['user'] );
103 $res['user'] = $params['user'];
104 $res['userID'] = $target instanceof User ?
$target->getId() : 0;
106 $block = Block
::newFromTarget( $target );
107 if( $block instanceof Block
){
108 $res['expiry'] = $block->mExpiry
== wfGetDB( DB_SLAVE
)->getInfinity()
110 : wfTimestamp( TS_ISO_8601
, $block->mExpiry
);
112 # should be unreachable
116 $res['reason'] = $params['reason'];
117 if ( $params['anononly'] ) {
118 $res['anononly'] = '';
120 if ( $params['nocreate'] ) {
121 $res['nocreate'] = '';
123 if ( $params['autoblock'] ) {
124 $res['autoblock'] = '';
126 if ( $params['noemail'] ) {
127 $res['noemail'] = '';
129 if ( $params['hidename'] ) {
130 $res['hidename'] = '';
132 if ( $params['allowusertalk'] ) {
133 $res['allowusertalk'] = '';
135 if ( $params['watchuser'] ) {
136 $res['watchuser'] = '';
139 $this->getResult()->addValue( null, $this->getModuleName(), $res );
142 public function mustBePosted() {
146 public function isWriteMode() {
150 public function getAllowedParams() {
153 ApiBase
::PARAM_TYPE
=> 'string',
154 ApiBase
::PARAM_REQUIRED
=> true
162 'autoblock' => false,
165 'allowusertalk' => false,
167 'watchuser' => false,
171 public function getParamDescription() {
173 'user' => 'Username, IP address or IP range you want to block',
174 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
175 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
176 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
177 'reason' => 'Reason for block (optional)',
178 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
179 'nocreate' => 'Prevent account creation',
180 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
181 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
182 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
183 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
184 'reblock' => 'If the user is already blocked, overwrite the existing block',
185 'watchuser' => 'Watch the user/IP\'s user and talk pages',
189 public function getDescription() {
190 return 'Block a user';
193 public function getPossibleErrors() {
194 return array_merge( parent
::getPossibleErrors(), array(
195 array( 'cantblock' ),
197 array( 'cantblock-email' ),
198 array( 'ipbblocked' ),
199 array( 'ipbnounblockself' ),
203 public function needsToken() {
207 public function getTokenSalt() {
211 public function getExamples() {
213 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
214 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
218 public function getHelpUrls() {
219 return 'http://www.mediawiki.org/wiki/API:Block';
222 public function getVersion() {
223 return __CLASS__
. ': $Id$';