Switch some HTMLForms in special pages to OOUI
[lhc/web/wiklou.git] / includes / specials / SpecialUnblock.php
1 <?php
2 /**
3 * Implements Special:Unblock
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * A special page for unblocking users
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialUnblock extends SpecialPage {
30
31 protected $target;
32 protected $type;
33 protected $block;
34
35 public function __construct() {
36 parent::__construct( 'Unblock', 'block' );
37 }
38
39 public function execute( $par ) {
40 $this->checkPermissions();
41 $this->checkReadOnly();
42
43 list( $this->target, $this->type ) = SpecialBlock::getTargetAndType( $par, $this->getRequest() );
44 $this->block = Block::newFromTarget( $this->target );
45 if ( $this->target instanceof User ) {
46 # Set the 'relevant user' in the skin, so it displays links like Contributions,
47 # User logs, UserRights, etc.
48 $this->getSkin()->setRelevantUser( $this->target );
49 }
50
51 $this->setHeaders();
52 $this->outputHeader();
53
54 $out = $this->getOutput();
55 $out->setPageTitle( $this->msg( 'unblockip' ) );
56 $out->addModules( array( 'mediawiki.special', 'mediawiki.userSuggest' ) );
57
58 $form = new HTMLForm( $this->getFields(), $this->getContext() );
59 $form->setWrapperLegendMsg( 'unblockip' );
60 $form->setSubmitCallback( array( __CLASS__, 'processUIUnblock' ) );
61 $form->setSubmitTextMsg( 'ipusubmit' );
62 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
63
64 if ( $form->show() ) {
65 switch ( $this->type ) {
66 case Block::TYPE_IP:
67 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
68 break;
69 case Block::TYPE_USER:
70 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
71 break;
72 case Block::TYPE_RANGE:
73 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
74 break;
75 case Block::TYPE_ID:
76 case Block::TYPE_AUTO:
77 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
78 break;
79 }
80 }
81 }
82
83 protected function getFields() {
84 $fields = array(
85 'Target' => array(
86 'type' => 'text',
87 'label-message' => 'ipaddressorusername',
88 'autofocus' => true,
89 'size' => '45',
90 'required' => true,
91 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
92 ),
93 'Name' => array(
94 'type' => 'info',
95 'label-message' => 'ipaddressorusername',
96 ),
97 'Reason' => array(
98 'type' => 'text',
99 'label-message' => 'ipbreason',
100 )
101 );
102
103 if ( $this->block instanceof Block ) {
104 list( $target, $type ) = $this->block->getTargetAndType();
105
106 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
107 # User:Foo, and we've just got any block, auto or not, that applies to a target
108 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
109 # so don't show any distinction between unblocked IPs and autoblocked IPs
110 if ( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ) {
111 $fields['Target']['default'] = $this->target;
112 unset( $fields['Name'] );
113 } else {
114 $fields['Target']['default'] = $target;
115 $fields['Target']['type'] = 'hidden';
116 switch ( $type ) {
117 case Block::TYPE_IP:
118 $fields['Name']['default'] = Linker::linkKnown(
119 SpecialPage::getTitleFor( 'Contributions', $target->getName() ),
120 $target->getName()
121 );
122 $fields['Name']['raw'] = true;
123 break;
124 case Block::TYPE_USER:
125 $fields['Name']['default'] = Linker::link(
126 $target->getUserPage(),
127 $target->getName()
128 );
129 $fields['Name']['raw'] = true;
130 break;
131
132 case Block::TYPE_RANGE:
133 $fields['Name']['default'] = $target;
134 break;
135
136 case Block::TYPE_AUTO:
137 $fields['Name']['default'] = $this->block->getRedactedName();
138 $fields['Name']['raw'] = true;
139 # Don't expose the real target of the autoblock
140 $fields['Target']['default'] = "#{$this->target}";
141 break;
142 }
143 // target is hidden, so the reason is the first element
144 $fields['Target']['autofocus'] = false;
145 $fields['Reason']['autofocus'] = true;
146 }
147 } else {
148 $fields['Target']['default'] = $this->target;
149 unset( $fields['Name'] );
150 }
151
152 return $fields;
153 }
154
155 /**
156 * Submit callback for an HTMLForm object
157 * @param array $data
158 * @param HTMLForm $form
159 * @return array|bool Array(message key, parameters)
160 */
161 public static function processUIUnblock( array $data, HTMLForm $form ) {
162 return self::processUnblock( $data, $form->getContext() );
163 }
164
165 /**
166 * Process the form
167 *
168 * @param array $data
169 * @param IContextSource $context
170 * @throws ErrorPageError
171 * @return array|bool Array(message key, parameters) on failure, True on success
172 */
173 public static function processUnblock( array $data, IContextSource $context ) {
174 $performer = $context->getUser();
175 $target = $data['Target'];
176 $block = Block::newFromTarget( $data['Target'] );
177
178 if ( !$block instanceof Block ) {
179 return array( array( 'ipb_cant_unblock', $target ) );
180 }
181
182 # bug 15810: blocked admins should have limited access here. This
183 # won't allow sysops to remove autoblocks on themselves, but they
184 # should have ipblock-exempt anyway
185 $status = SpecialBlock::checkUnblockSelf( $target, $performer );
186 if ( $status !== true ) {
187 throw new ErrorPageError( 'badaccess', $status );
188 }
189
190 # If the specified IP is a single address, and the block is a range block, don't
191 # unblock the whole range.
192 list( $target, $type ) = SpecialBlock::getTargetAndType( $target );
193 if ( $block->getType() == Block::TYPE_RANGE && $type == Block::TYPE_IP ) {
194 $range = $block->getTarget();
195
196 return array( array( 'ipb_blocked_as_range', $target, $range ) );
197 }
198
199 # If the name was hidden and the blocking user cannot hide
200 # names, then don't allow any block removals...
201 if ( !$performer->isAllowed( 'hideuser' ) && $block->mHideName ) {
202 return array( 'unblock-hideuser' );
203 }
204
205 # Delete block
206 if ( !$block->delete() ) {
207 return array( 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) );
208 }
209
210 # Unset _deleted fields as needed
211 if ( $block->mHideName ) {
212 # Something is deeply FUBAR if this is not a User object, but who knows?
213 $id = $block->getTarget() instanceof User
214 ? $block->getTarget()->getID()
215 : User::idFromName( $block->getTarget() );
216
217 RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
218 }
219
220 # Redact the name (IP address) for autoblocks
221 if ( $block->getType() == Block::TYPE_AUTO ) {
222 $page = Title::makeTitle( NS_USER, '#' . $block->getId() );
223 } else {
224 $page = $block->getTarget() instanceof User
225 ? $block->getTarget()->getUserpage()
226 : Title::makeTitle( NS_USER, $block->getTarget() );
227 }
228
229 # Make log entry
230 $logEntry = new ManualLogEntry( 'block', 'unblock' );
231 $logEntry->setTarget( $page );
232 $logEntry->setComment( $data['Reason'] );
233 $logEntry->setPerformer( $performer );
234 $logId = $logEntry->insert();
235 $logEntry->publish( $logId );
236
237 return true;
238 }
239
240 protected function getGroupName() {
241 return 'users';
242 }
243 }