Added spaces before and removed spaces after 'array'
[lhc/web/wiklou.git] / includes / api / ApiUnblock.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 7, 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 unblocking of users. Requires API write mode
29 * to be enabled.
30 *
31 * @ingroup API
32 */
33 class ApiUnblock extends ApiBase {
34
35 /**
36 * Unblocks the specified user or provides the reason the unblock failed.
37 */
38 public function execute() {
39 $user = $this->getUser();
40 $params = $this->extractRequestParams();
41
42 if ( $params['gettoken'] ) {
43 $res['unblocktoken'] = $user->getEditToken();
44 $this->getResult()->addValue( null, $this->getModuleName(), $res );
45 return;
46 }
47
48 if ( is_null( $params['id'] ) && is_null( $params['user'] ) ) {
49 $this->dieUsageMsg( 'unblock-notarget' );
50 }
51 if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) ) {
52 $this->dieUsageMsg( 'unblock-idanduser' );
53 }
54
55 if ( !$user->isAllowed( 'block' ) ) {
56 $this->dieUsageMsg( 'cantunblock' );
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( $status );
63 }
64 }
65
66 $data = array(
67 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
68 'Reason' => $params['reason']
69 );
70 $block = Block::newFromTarget( $data['Target'] );
71 $retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
72 if ( $retval !== true ) {
73 $this->dieUsageMsg( $retval[0] );
74 }
75
76 $res['id'] = $block->getId();
77 $target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
78 $res['user'] = $target instanceof User ? $target->getName() : $target;
79 $res['userid'] = $target instanceof User ? $target->getId() : 0;
80 $res['reason'] = $params['reason'];
81 $this->getResult()->addValue( null, $this->getModuleName(), $res );
82 }
83
84 public function mustBePosted() {
85 return true;
86 }
87
88 public function isWriteMode() {
89 return true;
90 }
91
92 public function getAllowedParams() {
93 return array(
94 'id' => array(
95 ApiBase::PARAM_TYPE => 'integer',
96 ),
97 'user' => null,
98 'token' => null,
99 'gettoken' => array(
100 ApiBase::PARAM_DFLT => false,
101 ApiBase::PARAM_DEPRECATED => true,
102 ),
103 'reason' => '',
104 );
105 }
106
107 public function getParamDescription() {
108 $p = $this->getModulePrefix();
109 return array(
110 'id' => "ID of the block you want to unblock (obtained through list=blocks). Cannot be used together with {$p}user",
111 'user' => "Username, IP address or IP range you want to unblock. Cannot be used together with {$p}id",
112 'token' => "An unblock token previously obtained through prop=info",
113 'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',
114 'reason' => 'Reason for unblock',
115 );
116 }
117
118 public function getResultProperties() {
119 return array(
120 '' => array(
121 'unblocktoken' => array(
122 ApiBase::PROP_TYPE => 'string',
123 ApiBase::PROP_NULLABLE => true
124 ),
125 'id' => array(
126 ApiBase::PROP_TYPE => 'integer',
127 ApiBase::PROP_NULLABLE => true
128 ),
129 'user' => array(
130 ApiBase::PROP_TYPE => 'string',
131 ApiBase::PROP_NULLABLE => true
132 ),
133 'userid' => array(
134 ApiBase::PROP_TYPE => 'integer',
135 ApiBase::PROP_NULLABLE => true
136 ),
137 'reason' => array(
138 ApiBase::PROP_TYPE => 'string',
139 ApiBase::PROP_NULLABLE => true
140 )
141 )
142 );
143 }
144
145 public function getDescription() {
146 return 'Unblock a user';
147 }
148
149 public function getPossibleErrors() {
150 return array_merge( parent::getPossibleErrors(), array(
151 array( 'unblock-notarget' ),
152 array( 'unblock-idanduser' ),
153 array( 'cantunblock' ),
154 array( 'ipbblocked' ),
155 array( 'ipbnounblockself' ),
156 ) );
157 }
158
159 public function needsToken() {
160 return true;
161 }
162
163 public function getTokenSalt() {
164 return '';
165 }
166
167 public function getExamples() {
168 return array(
169 'api.php?action=unblock&id=105',
170 'api.php?action=unblock&user=Bob&reason=Sorry%20Bob'
171 );
172 }
173
174 public function getHelpUrls() {
175 return 'https://www.mediawiki.org/wiki/API:Block';
176 }
177 }