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
;
27 * A special page for unblocking users
29 * @ingroup SpecialPage
31 class SpecialUnblock
extends SpecialPage
{
37 public function __construct() {
38 parent
::__construct( 'Unblock', 'block' );
41 public function doesWrites() {
45 public function execute( $par ) {
46 $this->checkPermissions();
47 $this->checkReadOnly();
49 list( $this->target
, $this->type
) = SpecialBlock
::getTargetAndType( $par, $this->getRequest() );
50 $this->block
= DatabaseBlock
::newFromTarget( $this->target
);
51 if ( $this->target
instanceof User
) {
52 # Set the 'relevant user' in the skin, so it displays links like Contributions,
53 # User logs, UserRights, etc.
54 $this->getSkin()->setRelevantUser( $this->target
);
58 $this->outputHeader();
60 $out = $this->getOutput();
61 $out->setPageTitle( $this->msg( 'unblockip' ) );
62 $out->addModules( [ 'mediawiki.userSuggest' ] );
64 $form = HTMLForm
::factory( 'ooui', $this->getFields(), $this->getContext() );
65 $form->setWrapperLegendMsg( 'unblockip' );
66 $form->setSubmitCallback( [ __CLASS__
, 'processUIUnblock' ] );
67 $form->setSubmitTextMsg( 'ipusubmit' );
68 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
70 if ( $form->show() ) {
71 switch ( $this->type
) {
72 case DatabaseBlock
::TYPE_IP
:
73 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target
) );
75 case DatabaseBlock
::TYPE_USER
:
76 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target
) );
78 case DatabaseBlock
::TYPE_RANGE
:
79 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target
) );
81 case DatabaseBlock
::TYPE_ID
:
82 case DatabaseBlock
::TYPE_AUTO
:
83 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target
) );
89 protected function getFields() {
93 'label-message' => 'ipaddressorusername',
97 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
101 'label-message' => 'ipaddressorusername',
105 'label-message' => 'ipbreason',
109 if ( $this->block
instanceof DatabaseBlock
) {
110 list( $target, $type ) = $this->block
->getTargetAndType();
112 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
113 # User:Foo, and we've just got any block, auto or not, that applies to a target
114 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
115 # so don't show any distinction between unblocked IPs and autoblocked IPs
116 if ( $type == DatabaseBlock
::TYPE_AUTO
&& $this->type
== DatabaseBlock
::TYPE_IP
) {
117 $fields['Target']['default'] = $this->target
;
118 unset( $fields['Name'] );
120 $fields['Target']['default'] = $target;
121 $fields['Target']['type'] = 'hidden';
123 case DatabaseBlock
::TYPE_IP
:
124 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
125 SpecialPage
::getTitleFor( 'Contributions', $target->getName() ),
128 $fields['Name']['raw'] = true;
130 case DatabaseBlock
::TYPE_USER
:
131 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
132 $target->getUserPage(),
135 $fields['Name']['raw'] = true;
138 case DatabaseBlock
::TYPE_RANGE
:
139 $fields['Name']['default'] = $target;
142 case DatabaseBlock
::TYPE_AUTO
:
143 $fields['Name']['default'] = $this->block
->getRedactedName();
144 $fields['Name']['raw'] = true;
145 # Don't expose the real target of the autoblock
146 $fields['Target']['default'] = "#{$this->target}";
149 // target is hidden, so the reason is the first element
150 $fields['Target']['autofocus'] = false;
151 $fields['Reason']['autofocus'] = true;
154 $fields['Target']['default'] = $this->target
;
155 unset( $fields['Name'] );
162 * Submit callback for an HTMLForm object
164 * @param HTMLForm $form
165 * @return array|bool Array(message key, parameters)
167 public static function processUIUnblock( array $data, HTMLForm
$form ) {
168 return self
::processUnblock( $data, $form->getContext() );
174 * Change tags can be provided via $data['Tags'], but the calling function
175 * must check if the tags can be added by the user prior to this function.
178 * @param IContextSource $context
179 * @throws ErrorPageError
180 * @return array|bool Array( Array( message key, parameters ) ) on failure, True on success
182 public static function processUnblock( array $data, IContextSource
$context ) {
183 $performer = $context->getUser();
184 $target = $data['Target'];
185 $block = DatabaseBlock
::newFromTarget( $data['Target'] );
187 if ( !$block instanceof DatabaseBlock
) {
188 return [ [ 'ipb_cant_unblock', $target ] ];
191 # T17810: blocked admins should have limited access here. This
192 # won't allow sysops to remove autoblocks on themselves, but they
193 # should have ipblock-exempt anyway
194 $status = SpecialBlock
::checkUnblockSelf( $target, $performer );
195 if ( $status !== true ) {
196 throw new ErrorPageError( 'badaccess', $status );
199 # If the specified IP is a single address, and the block is a range block, don't
200 # unblock the whole range.
201 list( $target, $type ) = SpecialBlock
::getTargetAndType( $target );
202 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
&& $type == DatabaseBlock
::TYPE_IP
) {
203 $range = $block->getTarget();
205 return [ [ 'ipb_blocked_as_range', $target, $range ] ];
208 # If the name was hidden and the blocking user cannot hide
209 # names, then don't allow any block removals...
210 if ( !$performer->isAllowed( 'hideuser' ) && $block->getHideName() ) {
211 return [ 'unblock-hideuser' ];
214 $reason = [ 'hookaborted' ];
215 if ( !Hooks
::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
220 if ( !$block->delete() ) {
221 return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
224 Hooks
::run( 'UnblockUserComplete', [ $block, $performer ] );
226 # Unset _deleted fields as needed
227 if ( $block->getHideName() ) {
228 # Something is deeply FUBAR if this is not a User object, but who knows?
229 $id = $block->getTarget() instanceof User
230 ?
$block->getTarget()->getId()
231 : User
::idFromName( $block->getTarget() );
233 RevisionDeleteUser
::unsuppressUserName( $block->getTarget(), $id );
236 # Redact the name (IP address) for autoblocks
237 if ( $block->getType() == DatabaseBlock
::TYPE_AUTO
) {
238 $page = Title
::makeTitle( NS_USER
, '#' . $block->getId() );
240 $page = $block->getTarget() instanceof User
241 ?
$block->getTarget()->getUserPage()
242 : Title
::makeTitle( NS_USER
, $block->getTarget() );
246 $logEntry = new ManualLogEntry( 'block', 'unblock' );
247 $logEntry->setTarget( $page );
248 $logEntry->setComment( $data['Reason'] );
249 $logEntry->setPerformer( $performer );
250 if ( isset( $data['Tags'] ) ) {
251 $logEntry->setTags( $data['Tags'] );
253 $logEntry->setRelations( [ 'ipb_id' => $block->getId() ] );
254 $logId = $logEntry->insert();
255 $logEntry->publish( $logId );
261 * Return an array of subpages beginning with $search that this special page will accept.
263 * @param string $search Prefix to search for
264 * @param int $limit Maximum number of results to return (usually 10)
265 * @param int $offset Number of results to skip (usually 0)
266 * @return string[] Matching subpages
268 public function prefixSearchSubpages( $search, $limit, $offset ) {
269 $user = User
::newFromName( $search );
271 // No prefix suggestion for invalid user
274 // Autocomplete subpage as user list - public to allow caching
275 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
278 protected function getGroupName() {