Special:Upload should not crash on failing previews
[mediawiki.git] / includes / specials / SpecialUnblock.php
blob0d42e3fc7022ebe9561293063e9865f946bab98b
1 <?php
2 /**
3 * Implements Special:Unblock
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.
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.
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * A special page for unblocking users
27 * @ingroup SpecialPage
29 class SpecialUnblock extends SpecialPage {
31 protected $target;
32 protected $type;
33 protected $block;
35 public function __construct() {
36 parent::__construct( 'Unblock', 'block' );
39 public function doesWrites() {
40 return true;
43 public function execute( $par ) {
44 $this->checkPermissions();
45 $this->checkReadOnly();
47 list( $this->target, $this->type ) = SpecialBlock::getTargetAndType( $par, $this->getRequest() );
48 $this->block = Block::newFromTarget( $this->target );
49 if ( $this->target instanceof User ) {
50 # Set the 'relevant user' in the skin, so it displays links like Contributions,
51 # User logs, UserRights, etc.
52 $this->getSkin()->setRelevantUser( $this->target );
55 $this->setHeaders();
56 $this->outputHeader();
58 $out = $this->getOutput();
59 $out->setPageTitle( $this->msg( 'unblockip' ) );
60 $out->addModules( [ 'mediawiki.special', 'mediawiki.userSuggest' ] );
62 $form = new HTMLForm( $this->getFields(), $this->getContext() );
63 $form->setWrapperLegendMsg( 'unblockip' );
64 $form->setSubmitCallback( [ __CLASS__, 'processUIUnblock' ] );
65 $form->setSubmitTextMsg( 'ipusubmit' );
66 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
68 if ( $form->show() ) {
69 switch ( $this->type ) {
70 case Block::TYPE_IP:
71 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
72 break;
73 case Block::TYPE_USER:
74 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
75 break;
76 case Block::TYPE_RANGE:
77 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
78 break;
79 case Block::TYPE_ID:
80 case Block::TYPE_AUTO:
81 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
82 break;
87 protected function getFields() {
88 $fields = [
89 'Target' => [
90 'type' => 'text',
91 'label-message' => 'ipaddressorusername',
92 'autofocus' => true,
93 'size' => '45',
94 'required' => true,
95 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
97 'Name' => [
98 'type' => 'info',
99 'label-message' => 'ipaddressorusername',
101 'Reason' => [
102 'type' => 'text',
103 'label-message' => 'ipbreason',
107 if ( $this->block instanceof Block ) {
108 list( $target, $type ) = $this->block->getTargetAndType();
110 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
111 # User:Foo, and we've just got any block, auto or not, that applies to a target
112 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
113 # so don't show any distinction between unblocked IPs and autoblocked IPs
114 if ( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ) {
115 $fields['Target']['default'] = $this->target;
116 unset( $fields['Name'] );
117 } else {
118 $fields['Target']['default'] = $target;
119 $fields['Target']['type'] = 'hidden';
120 switch ( $type ) {
121 case Block::TYPE_IP:
122 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
123 SpecialPage::getTitleFor( 'Contributions', $target->getName() ),
124 $target->getName()
126 $fields['Name']['raw'] = true;
127 break;
128 case Block::TYPE_USER:
129 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
130 $target->getUserPage(),
131 $target->getName()
133 $fields['Name']['raw'] = true;
134 break;
136 case Block::TYPE_RANGE:
137 $fields['Name']['default'] = $target;
138 break;
140 case Block::TYPE_AUTO:
141 $fields['Name']['default'] = $this->block->getRedactedName();
142 $fields['Name']['raw'] = true;
143 # Don't expose the real target of the autoblock
144 $fields['Target']['default'] = "#{$this->target}";
145 break;
147 // target is hidden, so the reason is the first element
148 $fields['Target']['autofocus'] = false;
149 $fields['Reason']['autofocus'] = true;
151 } else {
152 $fields['Target']['default'] = $this->target;
153 unset( $fields['Name'] );
156 return $fields;
160 * Submit callback for an HTMLForm object
161 * @param array $data
162 * @param HTMLForm $form
163 * @return array|bool Array(message key, parameters)
165 public static function processUIUnblock( array $data, HTMLForm $form ) {
166 return self::processUnblock( $data, $form->getContext() );
170 * Process the form
172 * Change tags can be provided via $data['Tags'], but the calling function
173 * must check if the tags can be added by the user prior to this function.
175 * @param array $data
176 * @param IContextSource $context
177 * @throws ErrorPageError
178 * @return array|bool Array( Array( message key, parameters ) ) on failure, True on success
180 public static function processUnblock( array $data, IContextSource $context ) {
181 $performer = $context->getUser();
182 $target = $data['Target'];
183 $block = Block::newFromTarget( $data['Target'] );
185 if ( !$block instanceof Block ) {
186 return [ [ 'ipb_cant_unblock', $target ] ];
189 # bug 15810: blocked admins should have limited access here. This
190 # won't allow sysops to remove autoblocks on themselves, but they
191 # should have ipblock-exempt anyway
192 $status = SpecialBlock::checkUnblockSelf( $target, $performer );
193 if ( $status !== true ) {
194 throw new ErrorPageError( 'badaccess', $status );
197 # If the specified IP is a single address, and the block is a range block, don't
198 # unblock the whole range.
199 list( $target, $type ) = SpecialBlock::getTargetAndType( $target );
200 if ( $block->getType() == Block::TYPE_RANGE && $type == Block::TYPE_IP ) {
201 $range = $block->getTarget();
203 return [ [ 'ipb_blocked_as_range', $target, $range ] ];
206 # If the name was hidden and the blocking user cannot hide
207 # names, then don't allow any block removals...
208 if ( !$performer->isAllowed( 'hideuser' ) && $block->mHideName ) {
209 return [ 'unblock-hideuser' ];
212 $reason = [ 'hookaborted' ];
213 if ( !Hooks::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
214 return $reason;
217 # Delete block
218 if ( !$block->delete() ) {
219 return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
222 Hooks::run( 'UnblockUserComplete', [ $block, $performer ] );
224 # Unset _deleted fields as needed
225 if ( $block->mHideName ) {
226 # Something is deeply FUBAR if this is not a User object, but who knows?
227 $id = $block->getTarget() instanceof User
228 ? $block->getTarget()->getId()
229 : User::idFromName( $block->getTarget() );
231 RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
234 # Redact the name (IP address) for autoblocks
235 if ( $block->getType() == Block::TYPE_AUTO ) {
236 $page = Title::makeTitle( NS_USER, '#' . $block->getId() );
237 } else {
238 $page = $block->getTarget() instanceof User
239 ? $block->getTarget()->getUserPage()
240 : Title::makeTitle( NS_USER, $block->getTarget() );
243 # Make log entry
244 $logEntry = new ManualLogEntry( 'block', 'unblock' );
245 $logEntry->setTarget( $page );
246 $logEntry->setComment( $data['Reason'] );
247 $logEntry->setPerformer( $performer );
248 if ( isset( $data['Tags'] ) ) {
249 $logEntry->setTags( $data['Tags'] );
251 $logId = $logEntry->insert();
252 $logEntry->publish( $logId );
254 return true;
258 * Return an array of subpages beginning with $search that this special page will accept.
260 * @param string $search Prefix to search for
261 * @param int $limit Maximum number of results to return (usually 10)
262 * @param int $offset Number of results to skip (usually 0)
263 * @return string[] Matching subpages
265 public function prefixSearchSubpages( $search, $limit, $offset ) {
266 $user = User::newFromName( $search );
267 if ( !$user ) {
268 // No prefix suggestion for invalid user
269 return [];
271 // Autocomplete subpage as user list - public to allow caching
272 return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
275 protected function getGroupName() {
276 return 'users';