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
21 * @ingroup SpecialPage
24 use MediaWiki\Block\DatabaseBlock
;
25 use MediaWiki\MediaWikiServices
;
28 * A special page for unblocking users
30 * @ingroup SpecialPage
32 class SpecialUnblock
extends SpecialPage
{
38 public function __construct() {
39 parent
::__construct( 'Unblock', 'block' );
42 public function doesWrites() {
46 public function execute( $par ) {
47 $this->checkPermissions();
48 $this->checkReadOnly();
50 list( $this->target
, $this->type
) = SpecialBlock
::getTargetAndType( $par, $this->getRequest() );
51 $this->block
= DatabaseBlock
::newFromTarget( $this->target
);
52 if ( $this->target
instanceof User
) {
53 # Set the 'relevant user' in the skin, so it displays links like Contributions,
54 # User logs, UserRights, etc.
55 $this->getSkin()->setRelevantUser( $this->target
);
59 $this->outputHeader();
60 $this->addHelpLink( 'Help:Blocking users' );
62 $out = $this->getOutput();
63 $out->setPageTitle( $this->msg( 'unblockip' ) );
64 $out->addModules( [ 'mediawiki.userSuggest' ] );
66 $form = HTMLForm
::factory( 'ooui', $this->getFields(), $this->getContext() );
67 $form->setWrapperLegendMsg( 'unblockip' );
68 $form->setSubmitCallback( [ __CLASS__
, 'processUIUnblock' ] );
69 $form->setSubmitTextMsg( 'ipusubmit' );
70 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
72 if ( $form->show() ) {
73 switch ( $this->type
) {
74 case DatabaseBlock
::TYPE_IP
:
75 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target
) );
77 case DatabaseBlock
::TYPE_USER
:
78 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target
) );
80 case DatabaseBlock
::TYPE_RANGE
:
81 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target
) );
83 case DatabaseBlock
::TYPE_ID
:
84 case DatabaseBlock
::TYPE_AUTO
:
85 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target
) );
91 protected function getFields() {
95 'label-message' => 'ipaddressorusername',
99 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
103 'label-message' => 'ipaddressorusername',
107 'label-message' => 'ipbreason',
111 if ( $this->block
instanceof DatabaseBlock
) {
112 list( $target, $type ) = $this->block
->getTargetAndType();
114 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
115 # User:Foo, and we've just got any block, auto or not, that applies to a target
116 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
117 # so don't show any distinction between unblocked IPs and autoblocked IPs
118 if ( $type == DatabaseBlock
::TYPE_AUTO
&& $this->type
== DatabaseBlock
::TYPE_IP
) {
119 $fields['Target']['default'] = $this->target
;
120 unset( $fields['Name'] );
122 $fields['Target']['default'] = $target;
123 $fields['Target']['type'] = 'hidden';
125 case DatabaseBlock
::TYPE_IP
:
126 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
127 SpecialPage
::getTitleFor( 'Contributions', $target->getName() ),
130 $fields['Name']['raw'] = true;
132 case DatabaseBlock
::TYPE_USER
:
133 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
134 $target->getUserPage(),
137 $fields['Name']['raw'] = true;
140 case DatabaseBlock
::TYPE_RANGE
:
141 $fields['Name']['default'] = $target;
144 case DatabaseBlock
::TYPE_AUTO
:
145 $fields['Name']['default'] = $this->block
->getRedactedName();
146 $fields['Name']['raw'] = true;
147 # Don't expose the real target of the autoblock
148 $fields['Target']['default'] = "#{$this->target}";
151 // target is hidden, so the reason is the first element
152 $fields['Target']['autofocus'] = false;
153 $fields['Reason']['autofocus'] = true;
156 $fields['Target']['default'] = $this->target
;
157 unset( $fields['Name'] );
164 * Submit callback for an HTMLForm object
166 * @param HTMLForm $form
167 * @return array|bool [ message key, parameters ]
169 public static function processUIUnblock( array $data, HTMLForm
$form ) {
170 return self
::processUnblock( $data, $form->getContext() );
176 * Change tags can be provided via $data['Tags'], but the calling function
177 * must check if the tags can be added by the user prior to this function.
180 * @param IContextSource $context
181 * @throws ErrorPageError
182 * @return array|bool [ [ message key, parameters ] ] on failure, True on success
184 public static function processUnblock( array $data, IContextSource
$context ) {
185 $performer = $context->getUser();
186 $target = $data['Target'];
187 $block = DatabaseBlock
::newFromTarget( $data['Target'] );
189 if ( !$block instanceof DatabaseBlock
) {
190 return [ [ 'ipb_cant_unblock', $target ] ];
193 # T17810: blocked admins should have limited access here. This
194 # won't allow sysops to remove autoblocks on themselves, but they
195 # should have ipblock-exempt anyway
196 $status = SpecialBlock
::checkUnblockSelf( $target, $performer );
197 if ( $status !== true ) {
198 throw new ErrorPageError( 'badaccess', $status );
201 # If the specified IP is a single address, and the block is a range block, don't
202 # unblock the whole range.
203 list( $target, $type ) = SpecialBlock
::getTargetAndType( $target );
204 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
&& $type == DatabaseBlock
::TYPE_IP
) {
205 $range = $block->getTarget();
207 return [ [ 'ipb_blocked_as_range', $target, $range ] ];
210 # If the name was hidden and the blocking user cannot hide
211 # names, then don't allow any block removals...
212 if ( !MediaWikiServices
::getInstance()
213 ->getPermissionManager()
214 ->userHasRight( $performer, 'hideuser' ) && $block->getHideName()
216 return [ 'unblock-hideuser' ];
219 $reason = [ 'hookaborted' ];
220 if ( !Hooks
::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
225 if ( !$block->delete() ) {
226 return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
229 Hooks
::run( 'UnblockUserComplete', [ $block, $performer ] );
231 # Unset _deleted fields as needed
232 if ( $block->getHideName() ) {
233 # Something is deeply FUBAR if this is not a User object, but who knows?
234 $id = $block->getTarget() instanceof User
235 ?
$block->getTarget()->getId()
236 : User
::idFromName( $block->getTarget() );
238 RevisionDeleteUser
::unsuppressUserName( $block->getTarget(), $id );
241 # Redact the name (IP address) for autoblocks
242 if ( $block->getType() == DatabaseBlock
::TYPE_AUTO
) {
243 $page = Title
::makeTitle( NS_USER
, '#' . $block->getId() );
245 $page = $block->getTarget() instanceof User
246 ?
$block->getTarget()->getUserPage()
247 : Title
::makeTitle( NS_USER
, $block->getTarget() );
251 $logEntry = new ManualLogEntry( 'block', 'unblock' );
252 $logEntry->setTarget( $page );
253 $logEntry->setComment( $data['Reason'] );
254 $logEntry->setPerformer( $performer );
255 if ( isset( $data['Tags'] ) ) {
256 $logEntry->addTags( $data['Tags'] );
258 $logEntry->setRelations( [ 'ipb_id' => $block->getId() ] );
259 $logId = $logEntry->insert();
260 $logEntry->publish( $logId );
266 * Return an array of subpages beginning with $search that this special page will accept.
268 * @param string $search Prefix to search for
269 * @param int $limit Maximum number of results to return (usually 10)
270 * @param int $offset Number of results to skip (usually 0)
271 * @return string[] Matching subpages
273 public function prefixSearchSubpages( $search, $limit, $offset ) {
274 $user = User
::newFromName( $search );
276 // No prefix suggestion for invalid user
279 // Autocomplete subpage as user list - public to allow caching
280 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
283 protected function getGroupName() {