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