API: Various docu and clean-up.
[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 (C) 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 * @addtogroup 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;
53 $this->getMain()->requestWriteMode();
54 $params = $this->extractRequestParams();
55
56 if($params['gettoken'])
57 {
58 $res['blocktoken'] = $wgUser->editToken();
59 $this->getResult()->addValue(null, $this->getModuleName(), $res);
60 return;
61 }
62
63 if(is_null($params['user']))
64 $this->dieUsage('The user parameter must be set', 'nouser');
65 if(is_null($params['token']))
66 $this->dieUsage('The token parameter must be set', 'notoken');
67 if(!$wgUser->matchEditToken($params['token']))
68 $this->dieUsage('Invalid token', 'badtoken');
69 if(!$wgUser->isAllowed('block'))
70 $this->dieUsage('You don\'t have permission to block users', 'permissiondenied');
71 if($params['hidename'] && !$wgUser->isAllowed('hideuser'))
72 $this->dieUsage('You don\'t have permission to hide user names from the block log', 'nohide');
73 if(wfReadOnly())
74 $this->dieUsage('The wiki is in read-only mode', 'readonly');
75
76 $form = new IPBlockForm('');
77 $form->BlockAddress = $params['user'];
78 $form->BlockReason = $params['reason'];
79 $form->BlockReasonList = 'other';
80 $form->BlockExpiry = ($params['expiry'] == 'never' ? 'infinite' : $params['expiry']);
81 $form->BlockOther = '';
82 $form->BlockAnonOnly = $params['anononly'];
83 $form->BlockCreateAccount = $params['nocreate'];
84 $form->BlockEnableAutoBlock = $params['autoblock'];
85 $form->BlockEmail = $params['noemail'];
86 $form->BlockHideName = $params['hidename'];
87
88 $dbw = wfGetDb(DB_MASTER);
89 $dbw->begin();
90 $retval = $form->doBlock($userID, $expiry);
91 switch($retval)
92 {
93 case IPBlockForm::BLOCK_SUCCESS:
94 break; // We'll deal with that later
95 case IPBlockForm::BLOCK_RANGE_INVALID:
96 $this->dieUsage("Invalid IP range ``{$params['user']}''", 'invalidrange');
97 case IPBlockForm::BLOCK_RANGE_DISABLED:
98 $this->dieUsage('Blocking IP ranges has been disabled', 'rangedisabled');
99 case IPBlockForm::BLOCK_NONEXISTENT_USER:
100 $this->dieUsage("User ``{$params['user']}'' doesn't exist", 'nosuchuser');
101 case IPBlockForm::BLOCK_IP_INVALID:
102 $this->dieUsage("Invaild IP address ``{$params['user']}''", 'invalidip');
103 case IPBlockForm::BLOCK_EXPIRY_INVALID:
104 $this->dieUsage("Invalid expiry time ``{$params['expiry']}''", 'invalidexpiry');
105 case IPBlockForm::BLOCK_ALREADY_BLOCKED:
106 $this->dieUsage("User ``{$params['user']}'' is already blocked", 'alreadyblocked');
107 default:
108 $this->dieDebug(__METHOD__, "IPBlockForm::doBlock() returned an unknown error ($retval)");
109 }
110 $dbw->commit();
111
112 $res['user'] = $params['user'];
113 $res['userID'] = $userID;
114 $res['expiry'] = ($expiry == Block::infinity() ? 'infinite' : $expiry);
115 $res['reason'] = $params['reason'];
116 if($params['anononly'])
117 $res['anononly'] = '';
118 if($params['nocreate'])
119 $res['nocreate'] = '';
120 if($params['autoblock'])
121 $res['autoblock'] = '';
122 if($params['noemail'])
123 $res['noemail'] = '';
124 if($params['hidename'])
125 $res['hidename'] = '';
126
127 $this->getResult()->addValue(null, $this->getModuleName(), $res);
128 }
129
130 protected function getAllowedParams() {
131 return array (
132 'user' => null,
133 'token' => null,
134 'gettoken' => false,
135 'expiry' => 'never',
136 'reason' => null,
137 'anononly' => false,
138 'nocreate' => false,
139 'autoblock' => false,
140 'noemail' => false,
141 'hidename' => false,
142 );
143 }
144
145 protected function getParamDescription() {
146 return array (
147 'user' => 'Username, IP address or IP range you want to block',
148 'token' => 'A block token previously obtained through the gettoken parameter',
149 'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
150 'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
151 'reason' => 'Reason for block (optional)',
152 'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
153 'nocreate' => 'Prevent account creation',
154 'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
155 'noemail' => 'Prevent user from sending e-mail through the wiki',
156 'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)'
157 );
158 }
159
160 protected function getDescription() {
161 return array(
162 'Block a user.'
163 );
164 }
165
166 protected function getExamples() {
167 return array (
168 'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
169 'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'
170 );
171 }
172
173 public function getVersion() {
174 return __CLASS__ . ': $Id$';
175 }
176 }