Remove arrays from getDescription where we are only using 1 line
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
1 <?php
2
3 /**
4 * Created on Sep 4, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
8 *
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.
13 *
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.
18 *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * API module that facilitates the blocking of users. Requires API write mode
32 * to be enabled.
33 *
34 * @ingroup API
35 */
36 class ApiBlock extends ApiBase {
37
38 /**
39 * Std ctor.
40 */
41 public function __construct( $main, $action ) {
42 parent::__construct( $main, $action );
43 }
44
45 /**
46 * Blocks the user specified in the parameters for the given expiry, with the
47 * given reason, and with all other settings provided in the params. If the block
48 * succeeds, produces a result containing the details of the block and notice
49 * of success. If it fails, the result will specify the nature of the error.
50 */
51 public function execute() {
52 global $wgUser, $wgBlockAllowsUTEdit;
53 $params = $this->extractRequestParams();
54
55 if ( $params['gettoken'] ) {
56 $res['blocktoken'] = $wgUser->editToken();
57 $this->getResult()->addValue( null, $this->getModuleName(), $res );
58 return;
59 }
60
61 if ( is_null( $params['user'] ) ) {
62 $this->dieUsageMsg( array( 'missingparam', 'user' ) );
63 }
64 if ( !$wgUser->isAllowed( 'block' ) ) {
65 $this->dieUsageMsg( array( 'cantblock' ) );
66 }
67 # bug 15810: blocked admins should have limited access here
68 if ( $wgUser->isBlocked() ) {
69 $status = IPBlockForm::checkUnblockSelf( $params['user'] );
70 if ( $status !== true ) {
71 $this->dieUsageMsg( array( $status ) );
72 }
73 }
74 if ( $params['hidename'] && !$wgUser->isAllowed( 'hideuser' ) ) {
75 $this->dieUsageMsg( array( 'canthide' ) );
76 }
77 if ( $params['noemail'] && !IPBlockForm::canBlockEmail( $wgUser ) ) {
78 $this->dieUsageMsg( array( 'cantblock-email' ) );
79 }
80
81 $form = new IPBlockForm( '' );
82 $form->BlockAddress = $params['user'];
83 $form->BlockReason = ( is_null( $params['reason'] ) ? '' : $params['reason'] );
84 $form->BlockReasonList = 'other';
85 $form->BlockExpiry = ( $params['expiry'] == 'never' ? 'infinite' : $params['expiry'] );
86 $form->BlockOther = '';
87 $form->BlockAnonOnly = $params['anononly'];
88 $form->BlockCreateAccount = $params['nocreate'];
89 $form->BlockEnableAutoblock = $params['autoblock'];
90 $form->BlockEmail = $params['noemail'];
91 $form->BlockHideName = $params['hidename'];
92 $form->BlockAllowUsertalk = $params['allowusertalk'] && $wgBlockAllowsUTEdit;
93 $form->BlockReblock = $params['reblock'];
94
95 $userID = $expiry = null;
96 $retval = $form->doBlock( $userID, $expiry );
97 if ( count( $retval ) ) {
98 // We don't care about multiple errors, just report one of them
99 $this->dieUsageMsg( $retval );
100 }
101
102 $res['user'] = $params['user'];
103 $res['userID'] = intval( $userID );
104 $res['expiry'] = ( $expiry == Block::infinity() ? 'infinite' : wfTimestamp( TS_ISO_8601, $expiry ) );
105 $res['reason'] = $params['reason'];
106 if ( $params['anononly'] ) {
107 $res['anononly'] = '';
108 }
109 if ( $params['nocreate'] ) {
110 $res['nocreate'] = '';
111 }
112 if ( $params['autoblock'] ) {
113 $res['autoblock'] = '';
114 }
115 if ( $params['noemail'] ) {
116 $res['noemail'] = '';
117 }
118 if ( $params['hidename'] ) {
119 $res['hidename'] = '';
120 }
121 if ( $params['allowusertalk'] ) {
122 $res['allowusertalk'] = '';
123 }
124
125 $this->getResult()->addValue( null, $this->getModuleName(), $res );
126 }
127
128 public function mustBePosted() {
129 return true;
130 }
131
132 public function isWriteMode() {
133 return true;
134 }
135
136 public function getAllowedParams() {
137 return array(
138 'user' => null,
139 'token' => null,
140 'gettoken' => false,
141 'expiry' => 'never',
142 'reason' => null,
143 'anononly' => false,
144 'nocreate' => false,
145 'autoblock' => false,
146 'noemail' => false,
147 'hidename' => false,
148 'allowusertalk' => false,
149 'reblock' => false,
150 );
151 }
152
153 public function getParamDescription() {
154 return array(
155 'user' => 'Username, IP address or IP range you want to block',
156 'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
157 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
158 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
159 'reason' => 'Reason for block (optional)',
160 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
161 'nocreate' => 'Prevent account creation',
162 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
163 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
164 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
165 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
166 'reblock' => 'If the user is already blocked, overwrite the existing block',
167 );
168 }
169
170 public function getDescription() {
171 return 'Block a user';
172 }
173
174 public function getPossibleErrors() {
175 return array_merge( parent::getPossibleErrors(), array(
176 array( 'missingparam', 'user' ),
177 array( 'cantblock' ),
178 array( 'canthide' ),
179 array( 'cantblock-email' ),
180 array( 'ipbblocked' ),
181 array( 'ipbnounblockself' ),
182 ) );
183 }
184
185 public function getTokenSalt() {
186 return '';
187 }
188
189 protected function getExamples() {
190 return array(
191 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
192 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
193 );
194 }
195
196 public function getVersion() {
197 return __CLASS__ . ': $Id$';
198 }
199 }