Merge "Set namespaces for dtp"
[mediawiki.git] / includes / specials / SpecialUnblock.php
blob603a44293bbd0a52b7a092e04c02d4a429f0dc93
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\Request\WebRequest;
31 use MediaWiki\SpecialPage\SpecialPage;
32 use MediaWiki\Title\Title;
33 use MediaWiki\Title\TitleValue;
34 use MediaWiki\User\UserIdentity;
35 use MediaWiki\User\UserNamePrefixSearch;
36 use MediaWiki\User\UserNameUtils;
37 use MediaWiki\Watchlist\WatchlistManager;
38 use Wikimedia\IPUtils;
40 /**
41 * A special page for unblocking users
43 * @ingroup SpecialPage
45 class SpecialUnblock extends SpecialPage {
47 /** @var UserIdentity|string|null */
48 protected $target;
50 /** @var int|null Block::TYPE_ constant */
51 protected $type;
53 /** @var DatabaseBlock|null */
54 protected $block;
56 private UnblockUserFactory $unblockUserFactory;
57 private BlockUtils $blockUtils;
58 private DatabaseBlockStore $blockStore;
59 private UserNameUtils $userNameUtils;
60 private UserNamePrefixSearch $userNamePrefixSearch;
61 private WatchlistManager $watchlistManager;
63 /**
64 * @param UnblockUserFactory $unblockUserFactory
65 * @param BlockUtils $blockUtils
66 * @param DatabaseBlockStore $blockStore
67 * @param UserNameUtils $userNameUtils
68 * @param UserNamePrefixSearch $userNamePrefixSearch
69 * @param WatchlistManager $watchlistManager
71 public function __construct(
72 UnblockUserFactory $unblockUserFactory,
73 BlockUtils $blockUtils,
74 DatabaseBlockStore $blockStore,
75 UserNameUtils $userNameUtils,
76 UserNamePrefixSearch $userNamePrefixSearch,
77 WatchlistManager $watchlistManager
78 ) {
79 parent::__construct( 'Unblock', 'block' );
80 $this->unblockUserFactory = $unblockUserFactory;
81 $this->blockUtils = $blockUtils;
82 $this->blockStore = $blockStore;
83 $this->userNameUtils = $userNameUtils;
84 $this->userNamePrefixSearch = $userNamePrefixSearch;
85 $this->watchlistManager = $watchlistManager;
88 public function doesWrites() {
89 return true;
92 public function execute( $par ) {
93 $this->checkPermissions();
94 $this->checkReadOnly();
96 [ $this->target, $this->type ] = $this->getTargetAndType( $par, $this->getRequest() );
97 $this->block = $this->blockStore->newFromTarget( $this->target );
98 if ( $this->target instanceof UserIdentity ) {
99 // Set the 'relevant user' in the skin, so it displays links like Contributions,
100 // User logs, UserRights, etc.
101 $this->getSkin()->setRelevantUser( $this->target );
104 $this->setHeaders();
105 $this->outputHeader();
106 $this->addHelpLink( 'Help:Blocking users' );
108 $out = $this->getOutput();
109 $out->setPageTitleMsg( $this->msg( 'unblockip' ) );
110 $out->addModules( [ 'mediawiki.userSuggest', 'mediawiki.special.block' ] );
112 $form = HTMLForm::factory( 'ooui', $this->getFields(), $this->getContext() )
113 ->setWrapperLegendMsg( 'unblockip' )
114 ->setSubmitCallback( function ( array $data, HTMLForm $form ) {
115 if ( $this->type != Block::TYPE_RANGE
116 && $this->type != Block::TYPE_AUTO
117 && $data['Watch']
119 $this->watchlistManager->addWatchIgnoringRights(
120 $form->getUser(),
121 Title::makeTitle( NS_USER, $this->target )
124 return $this->unblockUserFactory->newUnblockUser(
125 $data['Target'],
126 $form->getContext()->getAuthority(),
127 $data['Reason'],
128 $data['Tags'] ?? []
129 )->unblock();
131 ->setSubmitTextMsg( 'ipusubmit' )
132 ->addPreHtml( $this->msg( 'unblockiptext' )->parseAsBlock() );
134 $userPage = $this->getTargetUserTitle( $this->target );
135 if ( $userPage ) {
136 // Get relevant extracts from the block and suppression logs, if possible
137 $logExtract = '';
138 LogEventsList::showLogExtract(
139 $logExtract,
140 'block',
141 $userPage,
144 'lim' => 10,
145 'msgKey' => [
146 'unblocklog-showlog',
147 $userPage->getText(),
149 'showIfEmpty' => false
152 if ( $logExtract !== '' ) {
153 $form->addPostHtml( $logExtract );
156 // Add suppression block entries if allowed
157 if ( $this->getAuthority()->isAllowed( 'suppressionlog' ) ) {
158 $logExtract = '';
159 LogEventsList::showLogExtract(
160 $logExtract,
161 'suppress',
162 $userPage,
165 'lim' => 10,
166 'conds' => [ 'log_action' => [ 'block', 'reblock', 'unblock' ] ],
167 'msgKey' => [
168 'unblocklog-showsuppresslog',
169 $userPage->getText(),
171 'showIfEmpty' => false
174 if ( $logExtract !== '' ) {
175 $form->addPostHtml( $logExtract );
180 if ( $form->show() ) {
181 switch ( $this->type ) {
182 case Block::TYPE_IP:
183 $out->addWikiMsg( 'unblocked-ip', wfEscapeWikiText( $this->target ) );
184 break;
185 case Block::TYPE_USER:
186 $out->addWikiMsg( 'unblocked', wfEscapeWikiText( $this->target ) );
187 break;
188 case Block::TYPE_RANGE:
189 $out->addWikiMsg( 'unblocked-range', wfEscapeWikiText( $this->target ) );
190 break;
191 case Block::TYPE_ID:
192 case Block::TYPE_AUTO:
193 $out->addWikiMsg( 'unblocked-id', wfEscapeWikiText( $this->target ) );
194 break;
200 * Get the target and type, given the request and the subpage parameter.
201 * Several parameters are handled for backwards compatability. 'wpTarget' is
202 * prioritized, since it matches the HTML form.
204 * @param string|null $par Subpage parameter
205 * @param WebRequest $request
206 * @return array [ UserIdentity|string|null, DatabaseBlock::TYPE_ constant|null ]
207 * @phan-return array{0:UserIdentity|string|null,1:int|null}
209 private function getTargetAndType( ?string $par, WebRequest $request ) {
210 $possibleTargets = [
211 $request->getVal( 'wpTarget', null ),
212 $par,
213 $request->getVal( 'ip', null ),
214 // B/C @since 1.18
215 $request->getVal( 'wpBlockAddress', null ),
217 foreach ( $possibleTargets as $possibleTarget ) {
218 $targetAndType = $this->blockUtils->parseBlockTarget( $possibleTarget );
219 // If type is not null then target is valid
220 if ( $targetAndType[ 1 ] !== null ) {
221 break;
224 return $targetAndType;
228 * Get a user page target for things like logs.
229 * This handles account and IP range targets.
230 * @param UserIdentity|string|null $target
231 * @return Title|null
233 private function getTargetUserTitle( $target ): ?Title {
234 if ( $target instanceof UserIdentity ) {
235 return Title::makeTitle( NS_USER, $target->getName() );
238 if ( is_string( $target ) && IPUtils::isIPAddress( $target ) ) {
239 return Title::makeTitle( NS_USER, $target );
242 return null;
245 protected function getFields() {
246 $fields = [
247 'Target' => [
248 'type' => 'text',
249 'label-message' => 'ipaddressorusername',
250 'autofocus' => true,
251 'size' => '45',
252 'required' => true,
253 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
255 'Name' => [
256 'type' => 'info',
257 'label-message' => 'ipaddressorusername',
259 'Reason' => [
260 'type' => 'text',
261 'label-message' => 'ipbreason',
265 if ( $this->block instanceof Block ) {
266 $type = $this->block->getType();
267 $targetName = $this->block->getTargetName();
269 // Autoblocks are logged as "autoblock #123 because the IP was recently used by
270 // User:Foo, and we've just got any block, auto or not, that applies to a target
271 // the user has specified. Someone could be fishing to connect IPs to autoblocks,
272 // so don't show any distinction between unblocked IPs and autoblocked IPs
273 if ( $type == Block::TYPE_AUTO && $this->type == Block::TYPE_IP ) {
274 $fields['Target']['default'] = $this->target;
275 unset( $fields['Name'] );
276 } else {
277 $fields['Target']['default'] = $targetName;
278 $fields['Target']['type'] = 'hidden';
279 switch ( $type ) {
280 case Block::TYPE_IP:
281 $fields['Name']['default'] = $this->getLinkRenderer()->makeKnownLink(
282 $this->getSpecialPageFactory()->getTitleForAlias( 'Contributions/' . $targetName ),
283 $targetName
285 $fields['Name']['raw'] = true;
286 break;
287 case Block::TYPE_USER:
288 $fields['Name']['default'] = $this->getLinkRenderer()->makeLink(
289 new TitleValue( NS_USER, $targetName ),
290 $targetName
292 $fields['Name']['raw'] = true;
293 break;
295 case Block::TYPE_RANGE:
296 $fields['Name']['default'] = $targetName;
297 break;
299 case Block::TYPE_AUTO:
300 $fields['Name']['default'] = $this->block->getRedactedName();
301 $fields['Name']['raw'] = true;
302 // Don't expose the real target of the autoblock
303 $fields['Target']['default'] = "#{$this->target}";
304 break;
306 // Target is hidden, so the reason is the first element
307 $fields['Target']['autofocus'] = false;
308 $fields['Reason']['autofocus'] = true;
310 } else {
311 $fields['Target']['default'] = $this->target;
312 unset( $fields['Name'] );
314 // Watchlist their user page? (Only if user is logged in)
315 if ( $this->getUser()->isRegistered() ) {
316 $fields['Watch'] = [
317 'type' => 'check',
318 'label-message' => 'ipbwatchuser',
322 return $fields;
326 * Return an array of subpages beginning with $search that this special page will accept.
328 * @param string $search Prefix to search for
329 * @param int $limit Maximum number of results to return (usually 10)
330 * @param int $offset Number of results to skip (usually 0)
331 * @return string[] Matching subpages
333 public function prefixSearchSubpages( $search, $limit, $offset ) {
334 $search = $this->userNameUtils->getCanonical( $search );
335 if ( !$search ) {
336 // No prefix suggestion for invalid user
337 return [];
339 // Autocomplete subpage as user list - public to allow caching
340 return $this->userNamePrefixSearch
341 ->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset );
344 protected function getGroupName() {
345 return 'users';
350 * Retain the old class name for backwards compatibility.
351 * @deprecated since 1.41
353 class_alias( SpecialUnblock::class, 'SpecialUnblock' );