Merge "docs: Fix typo"
[mediawiki.git] / includes / specials / SpecialUnblock.php
blob04a68265fca218a645c9079dc85666cf67b86a01
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Specials;
23 use LogEventsList;
24 use MediaWiki\Block\Block;
25 use MediaWiki\Block\BlockUtils;
26 use MediaWiki\Block\DatabaseBlock;
27 use MediaWiki\Block\DatabaseBlockStore;
28 use MediaWiki\Block\UnblockUserFactory;
29 use MediaWiki\HTMLForm\HTMLForm;
30 use MediaWiki\MainConfigNames;
31 use MediaWiki\Request\WebRequest;
32 use MediaWiki\SpecialPage\SpecialPage;
33 use MediaWiki\Title\Title;
34 use MediaWiki\Title\TitleValue;
35 use MediaWiki\User\UserIdentity;
36 use MediaWiki\User\UserNamePrefixSearch;
37 use MediaWiki\User\UserNameUtils;
38 use MediaWiki\Watchlist\WatchlistManager;
39 use Wikimedia\IPUtils;
41 /**
42 * A special page for unblocking users
44 * @ingroup SpecialPage
46 class SpecialUnblock extends SpecialPage {
48 /** @var UserIdentity|string|null */
49 protected $target;
51 /** @var int|null Block::TYPE_ constant */
52 protected $type;
54 /** @var DatabaseBlock|null */
55 protected $block;
57 private UnblockUserFactory $unblockUserFactory;
58 private BlockUtils $blockUtils;
59 private DatabaseBlockStore $blockStore;
60 private UserNameUtils $userNameUtils;
61 private UserNamePrefixSearch $userNamePrefixSearch;
62 private WatchlistManager $watchlistManager;
64 protected bool $useCodex = false;
66 /**
67 * @param UnblockUserFactory $unblockUserFactory
68 * @param BlockUtils $blockUtils
69 * @param DatabaseBlockStore $blockStore
70 * @param UserNameUtils $userNameUtils
71 * @param UserNamePrefixSearch $userNamePrefixSearch
72 * @param WatchlistManager $watchlistManager
74 public function __construct(
75 UnblockUserFactory $unblockUserFactory,
76 BlockUtils $blockUtils,
77 DatabaseBlockStore $blockStore,
78 UserNameUtils $userNameUtils,
79 UserNamePrefixSearch $userNamePrefixSearch,
80 WatchlistManager $watchlistManager
81 ) {
82 parent::__construct( 'Unblock', 'block' );
83 $this->unblockUserFactory = $unblockUserFactory;
84 $this->blockUtils = $blockUtils;
85 $this->blockStore = $blockStore;
86 $this->userNameUtils = $userNameUtils;
87 $this->userNamePrefixSearch = $userNamePrefixSearch;
88 $this->watchlistManager = $watchlistManager;
89 $this->useCodex = $this->getConfig()->get( MainConfigNames::UseCodexSpecialBlock ) ||
90 $this->getRequest()->getBool( 'usecodex' );
93 public function doesWrites() {
94 return true;
97 public function execute( $par ) {
98 $this->checkPermissions();
99 $this->checkReadOnly();
101 [ $this->target, $this->type ] = $this->getTargetAndType( $par, $this->getRequest() );
103 // T382539
104 if ( $this->useCodex ) {
105 // If target is null, redirect to Special:Block
106 if ( $this->target === null ) {
107 // Use 301 (Moved Permanently) as this is a deprecation
108 $this->getOutput()->redirect(
109 SpecialPage::getTitleFor( 'Block' )->getFullURL( 'redirected=1' ),
110 '301'
112 return;
116 $this->block = $this->blockStore->newFromTarget( $this->target );
117 if ( $this->target instanceof UserIdentity ) {
118 // Set the 'relevant user' in the skin, so it displays links like Contributions,
119 // User logs, UserRights, etc.
120 $this->getSkin()->setRelevantUser( $this->target );
123 $this->setHeaders();
124 $this->outputHeader();
125 $this->addHelpLink( 'Help:Blocking users' );
127 $out = $this->getOutput();
128 $out->setPageTitleMsg( $this->msg( 'unblock-target' ) );
129 $out->addModules( [ 'mediawiki.userSuggest', 'mediawiki.special.block' ] );
131 $form = HTMLForm::factory( 'ooui', $this->getFields(), $this->getContext() )
132 ->setWrapperLegendMsg( 'unblock-target' )
133 ->setSubmitCallback( function ( array $data, HTMLForm $form ) {
134 if ( $this->type != Block::TYPE_RANGE
135 && $this->type != Block::TYPE_AUTO
136 && $data['Watch']
138 $this->watchlistManager->addWatchIgnoringRights(
139 $form->getUser(),
140 Title::makeTitle( NS_USER, $this->target )
143 return $this->unblockUserFactory->newUnblockUser(
144 $data['Target'],
145 $form->getContext()->getAuthority(),
146 $data['Reason'],
147 $data['Tags'] ?? []
148 )->unblock();
150 ->setSubmitTextMsg( 'ipusubmit' )
151 ->addPreHtml( $this->msg( 'unblockiptext' )->parseAsBlock() );
153 $userPage = $this->getTargetUserTitle( $this->target );
154 if ( $userPage ) {
155 // Get relevant extracts from the block and suppression logs, if possible
156 $logExtract = '';
157 LogEventsList::showLogExtract(
158 $logExtract,
159 'block',
160 $userPage,
163 'lim' => 10,
164 'msgKey' => [
165 'unblocklog-showlog',
166 $userPage->getText(),
168 'showIfEmpty' => false
171 if ( $logExtract !== '' ) {
172 $form->addPostHtml( $logExtract );
175 // Add suppression block entries if allowed
176 if ( $this->getAuthority()->isAllowed( 'suppressionlog' ) ) {
177 $logExtract = '';
178 LogEventsList::showLogExtract(
179 $logExtract,
180 'suppress',
181 $userPage,
184 'lim' => 10,
185 'conds' => [ 'log_action' => [ 'block', 'reblock', 'unblock' ] ],
186 'msgKey' => [
187 'unblocklog-showsuppresslog',
188 $userPage->getText(),
190 'showIfEmpty' => false
193 if ( $logExtract !== '' ) {
194 $form->addPostHtml( $logExtract );
199 if ( $form->show() ) {
200 switch ( $this->type ) {
201 case Block::TYPE_IP:
202 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
203 break;
204 case Block::TYPE_USER:
205 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
206 break;
207 case Block::TYPE_RANGE:
208 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
209 break;
210 case Block::TYPE_ID:
211 case Block::TYPE_AUTO:
212 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
213 break;
219 * Get the target and type, given the request and the subpage parameter.
220 * Several parameters are handled for backwards compatability. 'wpTarget' is
221 * prioritized, since it matches the HTML form.
223 * @param string|null $par Subpage parameter
224 * @param WebRequest $request
225 * @return array [ UserIdentity|string|null, DatabaseBlock::TYPE_ constant|null ]
226 * @phan-return array{0:UserIdentity|string|null,1:int|null}
228 private function getTargetAndType( ?string $par, WebRequest $request ) {
229 $possibleTargets = [
230 $request->getVal( 'wpTarget', null ),
231 $par,
232 $request->getVal( 'ip', null ),
233 // B/C @since 1.18
234 $request->getVal( 'wpBlockAddress', null ),
236 foreach ( $possibleTargets as $possibleTarget ) {
237 $targetAndType = $this->blockUtils->parseBlockTarget( $possibleTarget );
238 // If type is not null then target is valid
239 if ( $targetAndType[ 1 ] !== null ) {
240 break;
243 return $targetAndType;
247 * Get a user page target for things like logs.
248 * This handles account and IP range targets.
249 * @param UserIdentity|string|null $target
250 * @return Title|null
252 private function getTargetUserTitle( $target ): ?Title {
253 if ( $target instanceof UserIdentity ) {
254 return Title::makeTitle( NS_USER, $target->getName() );
257 if ( is_string( $target ) && IPUtils::isIPAddress( $target ) ) {
258 return Title::makeTitle( NS_USER, $target );
261 return null;
264 protected function getFields() {
265 $fields = [
266 'Target' => [
267 'type' => 'text',
268 'label-message' => 'unblock-target-label',
269 'autofocus' => true,
270 'size' => '45',
271 'required' => true,
272 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
274 'Name' => [
275 'type' => 'info',
276 'label-message' => 'unblock-target-label',
278 'Reason' => [
279 'type' => 'text',
280 'label-message' => 'ipbreason',
284 if ( $this->block instanceof Block ) {
285 $type = $this->block->getType();
286 $targetName = $this->block->getTargetName();
288 // Autoblocks are logged as "autoblock #123 because the IP was recently used by
289 // User:Foo, and we've just got any block, auto or not, that applies to a target
290 // the user has specified. Someone could be fishing to connect IPs to autoblocks,
291 // so don't show any distinction between unblocked IPs and autoblocked IPs
292 if ( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ) {
293 $fields['Target']['default'] = $this->target;
294 unset( $fields['Name'] );
295 } else {
296 $fields['Target']['default'] = $targetName;
297 $fields['Target']['type'] = 'hidden';
298 switch ( $type ) {
299 case Block::TYPE_IP:
300 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
301 $this->getSpecialPageFactory()->getTitleForAlias( 'Contributions/' . $targetName ),
302 $targetName
304 $fields['Name']['raw'] = true;
305 break;
306 case Block::TYPE_USER:
307 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
308 new TitleValue( NS_USER, $targetName ),
309 $targetName
311 $fields['Name']['raw'] = true;
312 break;
314 case Block::TYPE_RANGE:
315 $fields['Name']['default'] = $targetName;
316 break;
318 case Block::TYPE_AUTO:
319 $fields['Name']['default'] = $this->block->getRedactedName();
320 $fields['Name']['raw'] = true;
321 // Don't expose the real target of the autoblock
322 $fields['Target']['default'] = "#{$this->target}";
323 break;
325 // Target is hidden, so the reason is the first element
326 $fields['Target']['autofocus'] = false;
327 $fields['Reason']['autofocus'] = true;
329 } else {
330 $fields['Target']['default'] = $this->target;
331 unset( $fields['Name'] );
333 // Watchlist their user page? (Only if user is logged in)
334 if ( $this->getUser()->isRegistered() ) {
335 $fields['Watch'] = [
336 'type' => 'check',
337 'label-message' => 'ipbwatchuser',
341 return $fields;
345 * Return an array of subpages beginning with $search that this special page will accept.
347 * @param string $search Prefix to search for
348 * @param int $limit Maximum number of results to return (usually 10)
349 * @param int $offset Number of results to skip (usually 0)
350 * @return string[] Matching subpages
352 public function prefixSearchSubpages( $search, $limit, $offset ) {
353 $search = $this->userNameUtils->getCanonical( $search );
354 if ( !$search ) {
355 // No prefix suggestion for invalid user
356 return [];
358 // Autocomplete subpage as user list - public to allow caching
359 return $this->userNamePrefixSearch
360 ->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset );
363 protected function getGroupName() {
364 return 'users';
369 * Retain the old class name for backwards compatibility.
370 * @deprecated since 1.41
372 class_alias( SpecialUnblock::class, 'SpecialUnblock' );