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