Oops, forgot a few.
[lhc/web/wiklou.git] / includes / api / ApiUnblock.php
1 <?php
2
3 /*
4 * Created on Sep 7, 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 ApiUnblock 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['unblocktoken'] = $wgUser->editToken();
47 $this->getResult()->addValue(null, $this->getModuleName(), $res);
48 return;
49 }
50
51 if(is_null($params['id']) && is_null($params['user']))
52 $this->dieUsage('Either the id or the user parameter must be set', 'notarget');
53 if(!is_null($params['id']) && !is_null($params['user']))
54 $this->dieUsage('The id and user parameters can\'t be used together', 'idanduser');
55 if(is_null($params['token']))
56 $this->dieUsage('The token parameter must be set', 'notoken');
57 if(!$wgUser->matchEditToken($params['token']))
58 $this->dieUsage('Invalid token', 'badtoken');
59 if(!$wgUser->isAllowed('block'))
60 $this->dieUsage('You don\'t have permission to unblock users', 'permissiondenied');
61 if(wfReadOnly())
62 $this->dieUsage('The wiki is in read-only mode', 'readonly');
63
64 $id = $params['id'];
65 $user = $params['user'];
66 $reason = $params['reason'];
67 $dbw = wfGetDb(DB_MASTER);
68 $dbw->begin();
69 $retval = IPUnblockForm::doUnblock(&$id, &$user, &$reason, &$range);
70
71 switch($retval)
72 {
73 case IPUnblockForm::UNBLOCK_SUCCESS:
74 break; // We'll deal with that later
75 case IPUnblockForm::UNBLOCK_NO_SUCH_ID:
76 $this->dieUsage("There is no block with ID ``$id''", 'nosuchid');
77 case IPUnblockForm::UNBLOCK_USER_NOT_BLOCKED:
78 $this->dieUsage("User ``$user'' is not blocked", 'notblocked');
79 case IPUnblockForm::UNBLOCK_BLOCKED_AS_RANGE:
80 $this->dieUsage("IP address ``$user'' was blocked as part of range ``$range''. You can't unblock the IP invidually, but you can unblock the range as a whole.", 'blockedasrange');
81 case IPUnblockForm::UNBLOCK_UNKNOWNERR:
82 $this->dieUsage("Unknown error", 'unknownerr');
83 default:
84 $this->dieDebug(__METHOD__, "IPBlockForm::doBlock() returned an unknown error ($retval)");
85 }
86 $dbw->commit();
87
88 $res['id'] = $id;
89 $res['user'] = $user;
90 $res['reason'] = $reason;
91 $this->getResult()->addValue(null, $this->getModuleName(), $res);
92 }
93
94 protected function getAllowedParams() {
95 return array (
96 'id' => null,
97 'user' => null,
98 'token' => null,
99 'gettoken' => false,
100 'reason' => null,
101 );
102 }
103
104 protected function getParamDescription() {
105 return array (
106 'id' => 'ID of the block you want to unblock (obtained through list=blocks). Cannot be user together with user',
107 'user' => 'Username, IP address or IP range you want to unblock. Cannot be used together with id',
108 'token' => 'An unblock token previously obtained through the gettoken parameter',
109 'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',
110 'reason' => 'Reason for unblock (optional)',
111 );
112 }
113
114 protected function getDescription() {
115 return array(
116 'Unblock a user.'
117 );
118 }
119
120 protected function getExamples() {
121 return array (
122 'api.php?action=unblock&id=105',
123 'api.php?action=unblock&user=Bob&reason=Sorry%20Bob'
124 );
125 }
126
127 public function getVersion() {
128 return __CLASS__ . ': $Id$';
129 }
130 }