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();
59 $this->addHelpLink( 'Help:Blocking users' );
61 $out = $this->getOutput();
62 $out->setPageTitle( $this->msg( 'unblockip' ) );
63 $out->addModules( [ 'mediawiki.userSuggest' ] );
65 $form = HTMLForm
::factory( 'ooui', $this->getFields(), $this->getContext() );
66 $form->setWrapperLegendMsg( 'unblockip' );
67 $form->setSubmitCallback( [ __CLASS__
, 'processUIUnblock' ] );
68 $form->setSubmitTextMsg( 'ipusubmit' );
69 $form->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
71 if ( $form->show() ) {
72 switch ( $this->type
) {
73 case DatabaseBlock
::TYPE_IP
:
74 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target
) );
76 case DatabaseBlock
::TYPE_USER
:
77 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target
) );
79 case DatabaseBlock
::TYPE_RANGE
:
80 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target
) );
82 case DatabaseBlock
::TYPE_ID
:
83 case DatabaseBlock
::TYPE_AUTO
:
84 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target
) );
90 protected function getFields() {
94 'label-message' => 'ipaddressorusername',
98 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
102 'label-message' => 'ipaddressorusername',
106 'label-message' => 'ipbreason',
110 if ( $this->block
instanceof DatabaseBlock
) {
111 list( $target, $type ) = $this->block
->getTargetAndType();
113 # Autoblocks are logged as "autoblock #123 because the IP was recently used by
114 # User:Foo, and we've just got any block, auto or not, that applies to a target
115 # the user has specified. Someone could be fishing to connect IPs to autoblocks,
116 # so don't show any distinction between unblocked IPs and autoblocked IPs
117 if ( $type == DatabaseBlock
::TYPE_AUTO
&& $this->type
== DatabaseBlock
::TYPE_IP
) {
118 $fields['Target']['default'] = $this->target
;
119 unset( $fields['Name'] );
121 $fields['Target']['default'] = $target;
122 $fields['Target']['type'] = 'hidden';
124 case DatabaseBlock
::TYPE_IP
:
125 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
126 SpecialPage
::getTitleFor( 'Contributions', $target->getName() ),
129 $fields['Name']['raw'] = true;
131 case DatabaseBlock
::TYPE_USER
:
132 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
133 $target->getUserPage(),
136 $fields['Name']['raw'] = true;
139 case DatabaseBlock
::TYPE_RANGE
:
140 $fields['Name']['default'] = $target;
143 case DatabaseBlock
::TYPE_AUTO
:
144 $fields['Name']['default'] = $this->block
->getRedactedName();
145 $fields['Name']['raw'] = true;
146 # Don't expose the real target of the autoblock
147 $fields['Target']['default'] = "#{$this->target}";
150 // target is hidden, so the reason is the first element
151 $fields['Target']['autofocus'] = false;
152 $fields['Reason']['autofocus'] = true;
155 $fields['Target']['default'] = $this->target
;
156 unset( $fields['Name'] );
163 * Submit callback for an HTMLForm object
165 * @param HTMLForm $form
166 * @return array|bool [ message key, parameters ]
168 public static function processUIUnblock( array $data, HTMLForm
$form ) {
169 return self
::processUnblock( $data, $form->getContext() );
175 * Change tags can be provided via $data['Tags'], but the calling function
176 * must check if the tags can be added by the user prior to this function.
179 * @param IContextSource $context
180 * @throws ErrorPageError
181 * @return array|bool [ [ message key, parameters ] ] on failure, True on success
183 public static function processUnblock( array $data, IContextSource
$context ) {
184 $performer = $context->getUser();
185 $target = $data['Target'];
186 $block = DatabaseBlock
::newFromTarget( $data['Target'] );
188 if ( !$block instanceof DatabaseBlock
) {
189 return [ [ 'ipb_cant_unblock', $target ] ];
192 # T17810: blocked admins should have limited access here. This
193 # won't allow sysops to remove autoblocks on themselves, but they
194 # should have ipblock-exempt anyway
195 $status = SpecialBlock
::checkUnblockSelf( $target, $performer );
196 if ( $status !== true ) {
197 throw new ErrorPageError( 'badaccess', $status );
200 # If the specified IP is a single address, and the block is a range block, don't
201 # unblock the whole range.
202 list( $target, $type ) = SpecialBlock
::getTargetAndType( $target );
203 if ( $block->getType() == DatabaseBlock
::TYPE_RANGE
&& $type == DatabaseBlock
::TYPE_IP
) {
204 $range = $block->getTarget();
206 return [ [ 'ipb_blocked_as_range', $target, $range ] ];
209 # If the name was hidden and the blocking user cannot hide
210 # names, then don't allow any block removals...
211 if ( !$performer->isAllowed( 'hideuser' ) && $block->getHideName() ) {
212 return [ 'unblock-hideuser' ];
215 $reason = [ 'hookaborted' ];
216 if ( !Hooks
::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
221 if ( !$block->delete() ) {
222 return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
225 Hooks
::run( 'UnblockUserComplete', [ $block, $performer ] );
227 # Unset _deleted fields as needed
228 if ( $block->getHideName() ) {
229 # Something is deeply FUBAR if this is not a User object, but who knows?
230 $id = $block->getTarget() instanceof User
231 ?
$block->getTarget()->getId()
232 : User
::idFromName( $block->getTarget() );
234 RevisionDeleteUser
::unsuppressUserName( $block->getTarget(), $id );
237 # Redact the name (IP address) for autoblocks
238 if ( $block->getType() == DatabaseBlock
::TYPE_AUTO
) {
239 $page = Title
::makeTitle( NS_USER
, '#' . $block->getId() );
241 $page = $block->getTarget() instanceof User
242 ?
$block->getTarget()->getUserPage()
243 : Title
::makeTitle( NS_USER
, $block->getTarget() );
247 $logEntry = new ManualLogEntry( 'block', 'unblock' );
248 $logEntry->setTarget( $page );
249 $logEntry->setComment( $data['Reason'] );
250 $logEntry->setPerformer( $performer );
251 if ( isset( $data['Tags'] ) ) {
252 $logEntry->addTags( $data['Tags'] );
254 $logEntry->setRelations( [ 'ipb_id' => $block->getId() ] );
255 $logId = $logEntry->insert();
256 $logEntry->publish( $logId );
262 * Return an array of subpages beginning with $search that this special page will accept.
264 * @param string $search Prefix to search for
265 * @param int $limit Maximum number of results to return (usually 10)
266 * @param int $offset Number of results to skip (usually 0)
267 * @return string[] Matching subpages
269 public function prefixSearchSubpages( $search, $limit, $offset ) {
270 $user = User
::newFromName( $search );
272 // No prefix suggestion for invalid user
275 // Autocomplete subpage as user list - public to allow caching
276 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
279 protected function getGroupName() {