MediaWiki REL1_37
SpecialUnblock.php
Go to the documentation of this file.
1<?php
31
38
40 protected $target;
41
43 protected $type;
44
45 protected $block;
46
49
51 private $blockUtils;
52
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}
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
getContext()
Get the base IContextSource object.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:143
show()
The here's-one-I-made-earlier option: do the submission if posted, or display the form with or withou...
Definition HTMLForm.php:621
Backend class for blocking utils.
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
MediaWikiServices is the service locator for the application scope of MediaWiki.
Handles searching prefixes of user names.
UserNameUtils service.
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getSkin()
Shortcut to get the skin being used for this instance.
checkPermissions()
Checks if userCanExecute, and if not throws a PermissionsError.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getRequest()
Get the WebRequest being used for this instance.
checkReadOnly()
If the wiki is currently in readonly mode, throws a ReadOnlyError.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
A special page for unblocking users.
getTargetAndType(?string $par, WebRequest $request)
Get the target and type, given the request and the subpage parameter.
UserIdentity string null $target
doesWrites()
Indicates whether this special page may perform database writes.
execute( $par)
Default execute method Checks user permissions.
UserNameUtils $userNameUtils
__construct(UnblockUserFactory $unblockUserFactory, BlockUtils $blockUtils, UserNameUtils $userNameUtils, UserNamePrefixSearch $userNamePrefixSearch)
BlockUtils $blockUtils
UnblockUserFactory $unblockUserFactory
UserNamePrefixSearch $userNamePrefixSearch
int null $type
DatabaseBlock::TYPE_ constant.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
getVal( $name, $default=null)
Fetch a text string and partially normalized it.
Interface for objects representing user identity.