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
21 namespace MediaWiki\Specials
;
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
;
42 * A special page for unblocking users
44 * @ingroup SpecialPage
46 class SpecialUnblock
extends SpecialPage
{
48 /** @var UserIdentity|string|null */
51 /** @var int|null Block::TYPE_ constant */
54 /** @var DatabaseBlock|null */
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;
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
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() {
97 public function execute( $par ) {
98 $this->checkPermissions();
99 $this->checkReadOnly();
101 [ $this->target
, $this->type
] = $this->getTargetAndType( $par, $this->getRequest() );
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' ),
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
);
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
138 $this->watchlistManager
->addWatchIgnoringRights(
140 Title
::makeTitle( NS_USER
, $this->target
)
143 return $this->unblockUserFactory
->newUnblockUser(
145 $form->getContext()->getAuthority(),
150 ->setSubmitTextMsg( 'ipusubmit' )
151 ->addPreHtml( $this->msg( 'unblockiptext' )->parseAsBlock() );
153 $userPage = $this->getTargetUserTitle( $this->target
);
155 // Get relevant extracts from the block and suppression logs, if possible
157 LogEventsList
::showLogExtract(
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' ) ) {
178 LogEventsList
::showLogExtract(
185 'conds' => [ 'log_action' => [ 'block', 'reblock', 'unblock' ] ],
187 'unblocklog-showsuppresslog',
188 $userPage->getText(),
190 'showIfEmpty' => false
193 if ( $logExtract !== '' ) {
194 $form->addPostHtml( $logExtract );
199 if ( $form->show() ) {
200 switch ( $this->type
) {
202 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target
) );
204 case Block
::TYPE_USER
:
205 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target
) );
207 case Block
::TYPE_RANGE
:
208 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target
) );
211 case Block
::TYPE_AUTO
:
212 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target
) );
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 ) {
230 $request->getVal( 'wpTarget', null ),
232 $request->getVal( 'ip', null ),
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 ) {
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
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 );
264 protected function getFields() {
268 'label-message' => 'unblock-target-label',
272 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
276 'label-message' => 'unblock-target-label',
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'] );
296 $fields['Target']['default'] = $targetName;
297 $fields['Target']['type'] = 'hidden';
300 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
301 $this->getSpecialPageFactory()->getTitleForAlias( 'Contributions/' . $targetName ),
304 $fields['Name']['raw'] = true;
306 case Block
::TYPE_USER
:
307 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
308 new TitleValue( NS_USER
, $targetName ),
311 $fields['Name']['raw'] = true;
314 case Block
::TYPE_RANGE
:
315 $fields['Name']['default'] = $targetName;
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}";
325 // Target is hidden, so the reason is the first element
326 $fields['Target']['autofocus'] = false;
327 $fields['Reason']['autofocus'] = true;
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() ) {
337 'label-message' => 'ipbwatchuser',
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 );
355 // No prefix suggestion for invalid user
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() {
369 * Retain the old class name for backwards compatibility.
370 * @deprecated since 1.41
372 class_alias( SpecialUnblock
::class, 'SpecialUnblock' );