Merge "multi dbms support in api"
[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 'Target' => $params['user'],
74 'Reason' => array(
75 is_null( $params['reason'] ) ? '' : $params['reason'],
76 'other',
77 is_null( $params['reason'] ) ? '' : $params['reason']
78 ),
79 'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
80 'HardBlock' => !$params['anononly'],
81 'CreateAccount' => $params['nocreate'],
82 'AutoBlock' => $params['autoblock'],
83 'DisableEmail' => $params['noemail'],
84 'HideUser' => $params['hidename'],
85 'DisableUTEdit' => !$params['allowusertalk'],
86 'AlreadyBlocked' => $params['reblock'],
87 'Watch' => $params['watchuser'],
88 'Confirm' => true,
89 );
90
91 $retval = SpecialBlock::processForm( $data, $this->getContext() );
92 if ( $retval !== true ) {
93 // We don't care about multiple errors, just report one of them
94 $this->dieUsageMsg( $retval );
95 }
96
97 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
98 $res['user'] = $params['user'];
99 $res['userID'] = $target instanceof User ? $target->getId() : 0;
100
101 $block = Block::newFromTarget( $target );
102 if( $block instanceof Block ){
103 $res['expiry'] = $block->mExpiry == $this->getDB()->getInfinity()
104 ? 'infinite'
105 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
106 $res['id'] = $block->getId();
107 } else {
108 # should be unreachable
109 $res['expiry'] = '';
110 $res['id'] = '';
111 }
112
113 $res['reason'] = $params['reason'];
114 if ( $params['anononly'] ) {
115 $res['anononly'] = '';
116 }
117 if ( $params['nocreate'] ) {
118 $res['nocreate'] = '';
119 }
120 if ( $params['autoblock'] ) {
121 $res['autoblock'] = '';
122 }
123 if ( $params['noemail'] ) {
124 $res['noemail'] = '';
125 }
126 if ( $params['hidename'] ) {
127 $res['hidename'] = '';
128 }
129 if ( $params['allowusertalk'] ) {
130 $res['allowusertalk'] = '';
131 }
132 if ( $params['watchuser'] ) {
133 $res['watchuser'] = '';
134 }
135
136 $this->getResult()->addValue( null, $this->getModuleName(), $res );
137 }
138
139 public function mustBePosted() {
140 return true;
141 }
142
143 public function isWriteMode() {
144 return true;
145 }
146
147 public function getAllowedParams() {
148 return array(
149 'user' => array(
150 ApiBase::PARAM_TYPE => 'string',
151 ApiBase::PARAM_REQUIRED => true
152 ),
153 'token' => null,
154 'gettoken' => array(
155 ApiBase::PARAM_DFLT => false,
156 ApiBase::PARAM_DEPRECATED => true,
157 ),
158 'expiry' => 'never',
159 'reason' => null,
160 'anononly' => false,
161 'nocreate' => false,
162 'autoblock' => false,
163 'noemail' => false,
164 'hidename' => false,
165 'allowusertalk' => false,
166 'reblock' => false,
167 'watchuser' => false,
168 );
169 }
170
171 public function getParamDescription() {
172 return array(
173 'user' => 'Username, IP address or IP range you want to block',
174 'token' => 'A block token previously obtained through 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',
186 );
187 }
188
189 public function getDescription() {
190 return 'Block a user';
191 }
192
193 public function getPossibleErrors() {
194 return array_merge( parent::getPossibleErrors(), array(
195 array( 'cantblock' ),
196 array( 'canthide' ),
197 array( 'cantblock-email' ),
198 array( 'ipbblocked' ),
199 array( 'ipbnounblockself' ),
200 ) );
201 }
202
203 public function needsToken() {
204 return true;
205 }
206
207 public function getTokenSalt() {
208 return '';
209 }
210
211 public function getExamples() {
212 return array(
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='
215 );
216 }
217
218 public function getHelpUrls() {
219 return 'https://www.mediawiki.org/wiki/API:Block';
220 }
221
222 public function getVersion() {
223 return __CLASS__ . ': $Id$';
224 }
225 }