MediaWiki  master
SpecialUnblock.php
Go to the documentation of this file.
1 <?php
30 
36 class SpecialUnblock extends SpecialPage {
37 
38  protected $target;
39  protected $type;
40  protected $block;
41 
44 
46  private $blockUtils;
47 
49  private $userNameUtils;
50 
53 
60  public function __construct(
65  ) {
66  parent::__construct( 'Unblock', 'block' );
67  $this->unblockUserFactory = $unblockUserFactory;
68  $this->blockUtils = $blockUtils;
69  $this->userNameUtils = $userNameUtils;
70  $this->userNamePrefixSearch = $userNamePrefixSearch;
71  }
72 
73  public function doesWrites() {
74  return true;
75  }
76 
77  public function execute( $par ) {
78  $this->checkPermissions();
79  $this->checkReadOnly();
80 
81  list( $this->target, $this->type ) = $this->getTargetAndType( $par, $this->getRequest() );
82  $this->block = DatabaseBlock::newFromTarget( $this->target );
83  if ( $this->target instanceof User ) {
84  # Set the 'relevant user' in the skin, so it displays links like Contributions,
85  # User logs, UserRights, etc.
86  $this->getSkin()->setRelevantUser( $this->target );
87  }
88 
89  $this->setHeaders();
90  $this->outputHeader();
91  $this->addHelpLink( 'Help:Blocking users' );
92 
93  $out = $this->getOutput();
94  $out->setPageTitle( $this->msg( 'unblockip' ) );
95  $out->addModules( [ 'mediawiki.userSuggest' ] );
96 
97  $form = HTMLForm::factory( 'ooui', $this->getFields(), $this->getContext() )
98  ->setWrapperLegendMsg( 'unblockip' )
99  ->setSubmitCallback( function ( array $data, HTMLForm $form ) {
100  return $this->unblockUserFactory->newUnblockUser(
101  $data['Target'],
102  $form->getContext()->getUser(),
103  $data['Reason'],
104  $data['Tags'] ?? []
105  )->unblock();
106  } )
107  ->setSubmitTextMsg( 'ipusubmit' )
108  ->addPreText( $this->msg( 'unblockiptext' )->parseAsBlock() );
109 
110  if ( $form->show() ) {
111  switch ( $this->type ) {
112  case DatabaseBlock::TYPE_IP:
113  $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
114  break;
115  case DatabaseBlock::TYPE_USER:
116  $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
117  break;
118  case DatabaseBlock::TYPE_RANGE:
119  $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
120  break;
121  case DatabaseBlock::TYPE_ID:
122  case DatabaseBlock::TYPE_AUTO:
123  $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
124  break;
125  }
126  }
127  }
128 
139  private function getTargetAndType( ?string $par, WebRequest $request ) {
140  $possibleTargets = [
141  $request->getVal( 'wpTarget', null ),
142  $par,
143  $request->getVal( 'ip', null ),
144  // B/C @since 1.18
145  $request->getVal( 'wpBlockAddress', null ),
146  ];
147  foreach ( $possibleTargets as $possibleTarget ) {
148  $targetAndType = $this->blockUtils->parseBlockTarget( $possibleTarget );
149  // If type is not null then target is valid
150  if ( $targetAndType[ 1 ] !== null ) {
151  break;
152  }
153  }
154  return $targetAndType;
155  }
156 
157  protected function getFields() {
158  $fields = [
159  'Target' => [
160  'type' => 'text',
161  'label-message' => 'ipaddressorusername',
162  'autofocus' => true,
163  'size' => '45',
164  'required' => true,
165  'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
166  ],
167  'Name' => [
168  'type' => 'info',
169  'label-message' => 'ipaddressorusername',
170  ],
171  'Reason' => [
172  'type' => 'text',
173  'label-message' => 'ipbreason',
174  ]
175  ];
176 
177  if ( $this->block instanceof DatabaseBlock ) {
178  list( $target, $type ) = $this->block->getTargetAndType();
179 
180  # Autoblocks are logged as "autoblock #123 because the IP was recently used by
181  # User:Foo, and we've just got any block, auto or not, that applies to a target
182  # the user has specified. Someone could be fishing to connect IPs to autoblocks,
183  # so don't show any distinction between unblocked IPs and autoblocked IPs
184  if ( $type == DatabaseBlock::TYPE_AUTO && $this->type == DatabaseBlock::TYPE_IP ) {
185  $fields['Target']['default'] = $this->target;
186  unset( $fields['Name'] );
187  } else {
188  $fields['Target']['default'] = $target;
189  $fields['Target']['type'] = 'hidden';
190  switch ( $type ) {
191  case DatabaseBlock::TYPE_IP:
192  $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
193  $this->getSpecialPageFactory()->getTitleForAlias( 'Contributions/' . $target->getName() ),
194  $target->getName()
195  );
196  $fields['Name']['raw'] = true;
197  break;
198  case DatabaseBlock::TYPE_USER:
199  $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
200  $target->getUserPage(),
201  $target->getName()
202  );
203  $fields['Name']['raw'] = true;
204  break;
205 
206  case DatabaseBlock::TYPE_RANGE:
207  $fields['Name']['default'] = $target;
208  break;
209 
210  case DatabaseBlock::TYPE_AUTO:
211  $fields['Name']['default'] = $this->block->getRedactedName();
212  $fields['Name']['raw'] = true;
213  # Don't expose the real target of the autoblock
214  $fields['Target']['default'] = "#{$this->target}";
215  break;
216  }
217  // target is hidden, so the reason is the first element
218  $fields['Target']['autofocus'] = false;
219  $fields['Reason']['autofocus'] = true;
220  }
221  } else {
222  $fields['Target']['default'] = $this->target;
223  unset( $fields['Name'] );
224  }
225 
226  return $fields;
227  }
228 
237  public static function processUnblock( array $data, IContextSource $context ) {
238  wfDeprecated( __METHOD__, '1.36' );
239 
240  if ( !isset( $data['Tags'] ) ) {
241  $data['Tags'] = [];
242  }
243 
244  $unblockUser = MediaWikiServices::getInstance()->getUnblockUserFactory()->newUnblockUser(
245  $data['Target'],
246  $context->getUser(),
247  $data['Reason'],
248  $data['Tags']
249  );
250 
251  $status = $unblockUser->unblock();
252  if ( !$status->isOK() ) {
253  return $status->getErrorsArray();
254  }
255 
256  return true;
257  }
258 
267  public function prefixSearchSubpages( $search, $limit, $offset ) {
268  $search = $this->userNameUtils->getCanonical( $search );
269  if ( !$search ) {
270  // No prefix suggestion for invalid user
271  return [];
272  }
273  // Autocomplete subpage as user list - public to allow caching
274  return $this->userNamePrefixSearch
275  ->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset );
276  }
277 
278  protected function getGroupName() {
279  return 'users';
280  }
281 }
SpecialUnblock\processUnblock
static processUnblock(array $data, IContextSource $context)
Process the form.
Definition: SpecialUnblock.php:237
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:900
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:46
SpecialUnblock\getTargetAndType
getTargetAndType(?string $par, WebRequest $request)
Get the target and type, given the request and the subpage parameter.
Definition: SpecialUnblock.php:139
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:788
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
MediaWiki\Block\BlockUtils
Backend class for blocking utils.
Definition: BlockUtils.php:44
SpecialPage\checkPermissions
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
Definition: SpecialPage.php:356
HTMLForm\show
show()
The here's-one-I-made-earlier option: do the submission if posted, or display the form with or withou...
Definition: HTMLForm.php:616
SpecialUnblock\getGroupName
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialUnblock.php:278
SpecialPage\getSkin
getSkin()
Shortcut to get the skin being used for this instance.
Definition: SpecialPage.php:808
SpecialUnblock\$userNamePrefixSearch
UserNamePrefixSearch $userNamePrefixSearch
Definition: SpecialUnblock.php:52
MediaWiki\Block\DatabaseBlock
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
Definition: DatabaseBlock.php:50
SpecialPage\addHelpLink
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Definition: SpecialPage.php:936
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1033
SpecialUnblock\prefixSearchSubpages
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
Definition: SpecialUnblock.php:267
MediaWiki\User\UserNamePrefixSearch
Handles searching prefixes of user names.
Definition: UserNamePrefixSearch.php:39
MediaWiki\Block\UnblockUserFactory
Definition: UnblockUserFactory.php:29
SpecialUnblock\$userNameUtils
UserNameUtils $userNameUtils
Definition: SpecialUnblock.php:49
SpecialUnblock\getFields
getFields()
Definition: SpecialUnblock.php:157
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!...
Definition: SpecialPage.php:616
SpecialUnblock\$type
$type
Definition: SpecialUnblock.php:39
SpecialPage\getContext
getContext()
Gets the context this SpecialPage is executed in.
Definition: SpecialPage.php:762
SpecialUnblock\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialUnblock.php:77
SpecialPage
Parent class for all special pages.
Definition: SpecialPage.php:42
SpecialPage\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: SpecialPage.php:778
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1504
SpecialUnblock\$block
$block
Definition: SpecialUnblock.php:40
IContextSource\getUser
getUser()
SpecialUnblock\$target
$target
Definition: SpecialUnblock.php:38
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:57
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:42
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1016
WebRequest\getVal
getVal( $name, $default=null)
Fetch a scalar from the input or return $default if it's not set.
Definition: WebRequest.php:504
MediaWiki\User\UserNameUtils
UserNameUtils service.
Definition: UserNameUtils.php:42
SpecialPage\getSpecialPageFactory
getSpecialPageFactory()
Definition: SpecialPage.php:1103
SpecialUnblock\doesWrites
doesWrites()
Indicates whether this special page may perform database writes.
Definition: SpecialUnblock.php:73
SpecialUnblock\$blockUtils
BlockUtils $blockUtils
Definition: SpecialUnblock.php:46
SpecialPage\checkReadOnly
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
Definition: SpecialPage.php:369
SpecialUnblock
A special page for unblocking users.
Definition: SpecialUnblock.php:36
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:63
HTMLForm\factory
static factory( $displayFormat,... $arguments)
Construct a HTMLForm object for given display type.
Definition: HTMLForm.php:321
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:707
SpecialUnblock\__construct
__construct(UnblockUserFactory $unblockUserFactory, BlockUtils $blockUtils, UserNameUtils $userNameUtils, UserNamePrefixSearch $userNamePrefixSearch)
Definition: SpecialUnblock.php:60
SpecialUnblock\$unblockUserFactory
UnblockUserFactory $unblockUserFactory
Definition: SpecialUnblock.php:43
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:140