MediaWiki  1.34.0
SpecialUnblock.php
Go to the documentation of this file.
1 <?php
26 
32 class SpecialUnblock extends SpecialPage {
33 
34  protected $target;
35  protected $type;
36  protected $block;
37 
38  public function __construct() {
39  parent::__construct( 'Unblock', 'block' );
40  }
41 
42  public function doesWrites() {
43  return true;
44  }
45 
46  public function execute( $par ) {
47  $this->checkPermissions();
48  $this->checkReadOnly();
49 
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 );
56  }
57 
58  $this->setHeaders();
59  $this->outputHeader();
60  $this->addHelpLink( 'Help:Blocking users' );
61 
62  $out = $this->getOutput();
63  $out->setPageTitle( $this->msg( 'unblockip' ) );
64  $out->addModules( [ 'mediawiki.userSuggest' ] );
65 
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() );
71 
72  if ( $form->show() ) {
73  switch ( $this->type ) {
74  case DatabaseBlock::TYPE_IP:
75  $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
76  break;
77  case DatabaseBlock::TYPE_USER:
78  $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
79  break;
80  case DatabaseBlock::TYPE_RANGE:
81  $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
82  break;
83  case DatabaseBlock::TYPE_ID:
84  case DatabaseBlock::TYPE_AUTO:
85  $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
86  break;
87  }
88  }
89  }
90 
91  protected function getFields() {
92  $fields = [
93  'Target' => [
94  'type' => 'text',
95  'label-message' => 'ipaddressorusername',
96  'autofocus' => true,
97  'size' => '45',
98  'required' => true,
99  'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
100  ],
101  'Name' => [
102  'type' => 'info',
103  'label-message' => 'ipaddressorusername',
104  ],
105  'Reason' => [
106  'type' => 'text',
107  'label-message' => 'ipbreason',
108  ]
109  ];
110 
111  if ( $this->block instanceof DatabaseBlock ) {
112  list( $target, $type ) = $this->block->getTargetAndType();
113 
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'] );
121  } else {
122  $fields['Target']['default'] = $target;
123  $fields['Target']['type'] = 'hidden';
124  switch ( $type ) {
125  case DatabaseBlock::TYPE_IP:
126  $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
127  SpecialPage::getTitleFor( 'Contributions', $target->getName() ),
128  $target->getName()
129  );
130  $fields['Name']['raw'] = true;
131  break;
132  case DatabaseBlock::TYPE_USER:
133  $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
134  $target->getUserPage(),
135  $target->getName()
136  );
137  $fields['Name']['raw'] = true;
138  break;
139 
140  case DatabaseBlock::TYPE_RANGE:
141  $fields['Name']['default'] = $target;
142  break;
143 
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}";
149  break;
150  }
151  // target is hidden, so the reason is the first element
152  $fields['Target']['autofocus'] = false;
153  $fields['Reason']['autofocus'] = true;
154  }
155  } else {
156  $fields['Target']['default'] = $this->target;
157  unset( $fields['Name'] );
158  }
159 
160  return $fields;
161  }
162 
169  public static function processUIUnblock( array $data, HTMLForm $form ) {
170  return self::processUnblock( $data, $form->getContext() );
171  }
172 
184  public static function processUnblock( array $data, IContextSource $context ) {
185  $performer = $context->getUser();
186  $target = $data['Target'];
187  $block = DatabaseBlock::newFromTarget( $data['Target'] );
188 
189  if ( !$block instanceof DatabaseBlock ) {
190  return [ [ 'ipb_cant_unblock', $target ] ];
191  }
192 
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
197  if ( $status !== true ) {
198  throw new ErrorPageError( 'badaccess', $status );
199  }
200 
201  # If the specified IP is a single address, and the block is a range block, don't
202  # unblock the whole range.
204  if ( $block->getType() == DatabaseBlock::TYPE_RANGE && $type == DatabaseBlock::TYPE_IP ) {
205  $range = $block->getTarget();
206 
207  return [ [ 'ipb_blocked_as_range', $target, $range ] ];
208  }
209 
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()
214  ->userHasRight( $performer, 'hideuser' ) && $block->getHideName()
215  ) {
216  return [ 'unblock-hideuser' ];
217  }
218 
219  $reason = [ 'hookaborted' ];
220  if ( !Hooks::run( 'UnblockUser', [ &$block, &$performer, &$reason ] ) ) {
221  return $reason;
222  }
223 
224  # Delete block
225  if ( !$block->delete() ) {
226  return [ [ 'ipb_cant_unblock', htmlspecialchars( $block->getTarget() ) ] ];
227  }
228 
229  Hooks::run( 'UnblockUserComplete', [ $block, $performer ] );
230 
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() );
237 
238  RevisionDeleteUser::unsuppressUserName( $block->getTarget(), $id );
239  }
240 
241  # Redact the name (IP address) for autoblocks
242  if ( $block->getType() == DatabaseBlock::TYPE_AUTO ) {
243  $page = Title::makeTitle( NS_USER, '#' . $block->getId() );
244  } else {
245  $page = $block->getTarget() instanceof User
246  ? $block->getTarget()->getUserPage()
247  : Title::makeTitle( NS_USER, $block->getTarget() );
248  }
249 
250  # Make log entry
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'] );
257  }
258  $logEntry->setRelations( [ 'ipb_id' => $block->getId() ] );
259  $logId = $logEntry->insert();
260  $logEntry->publish( $logId );
261 
262  return true;
263  }
264 
273  public function prefixSearchSubpages( $search, $limit, $offset ) {
274  $user = User::newFromName( $search );
275  if ( !$user ) {
276  // No prefix suggestion for invalid user
277  return [];
278  }
279  // Autocomplete subpage as user list - public to allow caching
280  return UserNamePrefixSearch::search( 'public', $search, $limit, $offset );
281  }
282 
283  protected function getGroupName() {
284  return 'users';
285  }
286 }
SpecialUnblock\processUnblock
static processUnblock(array $data, IContextSource $context)
Process the form.
Definition: SpecialUnblock.php:184
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:792
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:40
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:719
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
SpecialBlock\checkUnblockSelf
static checkUnblockSelf( $target, User $performer)
T17810: blocked admins should not be able to block/unblock others, and probably shouldn't be able to ...
Definition: SpecialBlock.php:1148
UserNamePrefixSearch\search
static search( $audience, $search, $limit, $offset=0)
Do a prefix search of user names and return a list of matching user names.
Definition: UserNamePrefixSearch.php:41
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:315
SpecialUnblock\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialUnblock.php:283
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
SpecialPage\getTitleFor
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
Definition: SpecialPage.php:83
SpecialPage\getSkin
getSkin()
Shortcut to get the skin being used for this instance.
Definition: SpecialPage.php:739
RevisionDeleteUser\unsuppressUserName
static unsuppressUserName( $name, $userId, IDatabase $dbw=null)
Definition: RevisionDeleteUser.php:168
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:54
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:828
getPermissionManager
getPermissionManager()
SpecialUnblock\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialUnblock.php:273
SpecialUnblock\getFields
getFields()
Definition: SpecialUnblock.php:91
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:537
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
SpecialUnblock\$type
$type
Definition: SpecialUnblock.php:35
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:692
SpecialUnblock\processUIUnblock
static processUIUnblock(array $data, HTMLForm $form)
Submit callback for an HTMLForm object.
Definition: SpecialUnblock.php:169
SpecialUnblock\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialUnblock.php:46
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:37
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:709
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1551
SpecialUnblock\$block
$block
Definition: SpecialUnblock.php:36
SpecialUnblock\$target
$target
Definition: SpecialUnblock.php:34
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
$context
$context
Definition: load.php:45
SpecialUnblock\__construct
__construct()
Definition: SpecialUnblock.php:38
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:904
$status
return $status
Definition: SyntaxHighlight.php:347
User\idFromName
static idFromName( $name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition: User.php:829
NS_USER
const NS_USER
Definition: Defines.php:62
SpecialUnblock\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialUnblock.php:42
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:37
SpecialBlock\getTargetAndType
static getTargetAndType( $par, WebRequest $request=null)
Determine the target of the block, and the type of target.
Definition: SpecialBlock.php:636
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:328
SpecialUnblock
A special page for unblocking users.
Definition: SpecialUnblock.php:32
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:27
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:51
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:303
SpecialPage\outputHeader
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
Definition: SpecialPage.php:639
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:131