Merge "(bug 42915) make MovePage aware of whether redirects are supported."
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 4, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * API module that facilitates the blocking of users. Requires API write mode
29 * to be enabled.
30 *
31 * @ingroup API
32 */
33 class ApiBlock extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 /**
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.
44 */
45 public function execute() {
46 $user = $this->getUser();
47 $params = $this->extractRequestParams();
48
49 if ( $params['gettoken'] ) {
50 $res['blocktoken'] = $user->getEditToken();
51 $this->getResult()->addValue( null, $this->getModuleName(), $res );
52 return;
53 }
54
55 if ( !$user->isAllowed( 'block' ) ) {
56 $this->dieUsageMsg( 'cantblock' );
57 }
58 # bug 15810: blocked admins should have limited access here
59 if ( $user->isBlocked() ) {
60 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
61 if ( $status !== true ) {
62 $this->dieUsageMsg( array( $status ) );
63 }
64 }
65 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
66 $this->dieUsageMsg( 'canthide' );
67 }
68 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
69 $this->dieUsageMsg( 'cantblock-email' );
70 }
71
72 $data = array(
73 'PreviousTarget' => $params['user'],
74 'Target' => $params['user'],
75 'Reason' => array(
76 $params['reason'],
77 'other',
78 $params['reason']
79 ),
80 'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
81 'HardBlock' => !$params['anononly'],
82 'CreateAccount' => $params['nocreate'],
83 'AutoBlock' => $params['autoblock'],
84 'DisableEmail' => $params['noemail'],
85 'HideUser' => $params['hidename'],
86 'DisableUTEdit' => !$params['allowusertalk'],
87 'Reblock' => $params['reblock'],
88 'Watch' => $params['watchuser'],
89 'Confirm' => true,
90 );
91
92 $retval = SpecialBlock::processForm( $data, $this->getContext() );
93 if ( $retval !== true ) {
94 // We don't care about multiple errors, just report one of them
95 $this->dieUsageMsg( $retval );
96 }
97
98 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
99 $res['user'] = $params['user'];
100 $res['userID'] = $target instanceof User ? $target->getId() : 0;
101
102 $block = Block::newFromTarget( $target );
103 if( $block instanceof Block ){
104 $res['expiry'] = $block->mExpiry == $this->getDB()->getInfinity()
105 ? 'infinite'
106 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
107 $res['id'] = $block->getId();
108 } else {
109 # should be unreachable
110 $res['expiry'] = '';
111 $res['id'] = '';
112 }
113
114 $res['reason'] = $params['reason'];
115 if ( $params['anononly'] ) {
116 $res['anononly'] = '';
117 }
118 if ( $params['nocreate'] ) {
119 $res['nocreate'] = '';
120 }
121 if ( $params['autoblock'] ) {
122 $res['autoblock'] = '';
123 }
124 if ( $params['noemail'] ) {
125 $res['noemail'] = '';
126 }
127 if ( $params['hidename'] ) {
128 $res['hidename'] = '';
129 }
130 if ( $params['allowusertalk'] ) {
131 $res['allowusertalk'] = '';
132 }
133 if ( $params['watchuser'] ) {
134 $res['watchuser'] = '';
135 }
136
137 $this->getResult()->addValue( null, $this->getModuleName(), $res );
138 }
139
140 public function mustBePosted() {
141 return true;
142 }
143
144 public function isWriteMode() {
145 return true;
146 }
147
148 public function getAllowedParams() {
149 return array(
150 'user' => array(
151 ApiBase::PARAM_TYPE => 'string',
152 ApiBase::PARAM_REQUIRED => true
153 ),
154 'token' => null,
155 'gettoken' => array(
156 ApiBase::PARAM_DFLT => false,
157 ApiBase::PARAM_DEPRECATED => true,
158 ),
159 'expiry' => 'never',
160 'reason' => '',
161 'anononly' => false,
162 'nocreate' => false,
163 'autoblock' => false,
164 'noemail' => false,
165 'hidename' => false,
166 'allowusertalk' => false,
167 'reblock' => false,
168 'watchuser' => false,
169 );
170 }
171
172 public function getParamDescription() {
173 return array(
174 'user' => 'Username, IP address or IP range you want to block',
175 'token' => 'A block token previously obtained through prop=info',
176 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
177 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
178 'reason' => 'Reason for block',
179 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
180 'nocreate' => 'Prevent account creation',
181 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
182 'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
183 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
184 'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
185 'reblock' => 'If the user is already blocked, overwrite the existing block',
186 'watchuser' => 'Watch the user/IP\'s user and talk pages',
187 );
188 }
189
190 public function getResultProperties() {
191 return array(
192 '' => array(
193 'blocktoken' => array(
194 ApiBase::PROP_TYPE => 'string',
195 ApiBase::PROP_NULLABLE => true
196 ),
197 'user' => array(
198 ApiBase::PROP_TYPE => 'string',
199 ApiBase::PROP_NULLABLE => true
200 ),
201 'userID' => array(
202 ApiBase::PROP_TYPE => 'integer',
203 ApiBase::PROP_NULLABLE => true
204 ),
205 'expiry' => array(
206 ApiBase::PROP_TYPE => 'string',
207 ApiBase::PROP_NULLABLE => true
208 ),
209 'id' => array(
210 ApiBase::PROP_TYPE => 'integer',
211 ApiBase::PROP_NULLABLE => true
212 ),
213 'reason' => array(
214 ApiBase::PROP_TYPE => 'string',
215 ApiBase::PROP_NULLABLE => true
216 ),
217 'anononly' => 'boolean',
218 'nocreate' => 'boolean',
219 'autoblock' => 'boolean',
220 'noemail' => 'boolean',
221 'hidename' => 'boolean',
222 'allowusertalk' => 'boolean',
223 'watchuser' => 'boolean'
224 )
225 );
226 }
227
228 public function getDescription() {
229 return 'Block a user';
230 }
231
232 public function getPossibleErrors() {
233 return array_merge( parent::getPossibleErrors(), array(
234 array( 'cantblock' ),
235 array( 'canthide' ),
236 array( 'cantblock-email' ),
237 array( 'ipbblocked' ),
238 array( 'ipbnounblockself' ),
239 ) );
240 }
241
242 public function needsToken() {
243 return true;
244 }
245
246 public function getTokenSalt() {
247 return '';
248 }
249
250 public function getExamples() {
251 return array(
252 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
253 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
254 );
255 }
256
257 public function getHelpUrls() {
258 return 'https://www.mediawiki.org/wiki/API:Block';
259 }
260
261 public function getVersion() {
262 return __CLASS__ . ': $Id$';
263 }
264 }