MediaWiki master
SpecialBlock.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
24use HtmlArmor;
25use Language;
59use OOUI\FieldLayout;
60use OOUI\HtmlSnippet;
61use OOUI\LabelWidget;
62use OOUI\Widget;
63use Wikimedia\IPUtils;
64
72
73 private BlockUtils $blockUtils;
74 private BlockPermissionCheckerFactory $blockPermissionCheckerFactory;
75 private BlockUserFactory $blockUserFactory;
76 private DatabaseBlockStore $blockStore;
77 private UserNameUtils $userNameUtils;
78 private UserNamePrefixSearch $userNamePrefixSearch;
79 private BlockActionInfo $blockActionInfo;
80 private TitleFormatter $titleFormatter;
81
85 protected $target;
86
88 protected $type;
89
91 protected $previousTarget;
92
95
97 protected $alreadyBlocked;
98
102 protected $preErrors = [];
103
107 protected $codexFormData = [];
108
109 private NamespaceInfo $namespaceInfo;
110
122 public function __construct(
123 BlockUtils $blockUtils,
124 BlockPermissionCheckerFactory $blockPermissionCheckerFactory,
125 BlockUserFactory $blockUserFactory,
126 DatabaseBlockStore $blockStore,
127 UserNameUtils $userNameUtils,
128 UserNamePrefixSearch $userNamePrefixSearch,
129 BlockActionInfo $blockActionInfo,
130 TitleFormatter $titleFormatter,
131 NamespaceInfo $namespaceInfo
132 ) {
133 parent::__construct( 'Block', 'block' );
134
135 $this->blockUtils = $blockUtils;
136 $this->blockPermissionCheckerFactory = $blockPermissionCheckerFactory;
137 $this->blockUserFactory = $blockUserFactory;
138 $this->blockStore = $blockStore;
139 $this->userNameUtils = $userNameUtils;
140 $this->userNamePrefixSearch = $userNamePrefixSearch;
141 $this->blockActionInfo = $blockActionInfo;
142 $this->titleFormatter = $titleFormatter;
143 $this->namespaceInfo = $namespaceInfo;
144 }
145
146 public function execute( $par ) {
147 parent::execute( $par );
148
149 if ( $this->getConfig()->get( MainConfigNames::UseCodexSpecialBlock ) ) {
150 $this->getOutput()->addModules( 'mediawiki.special.block.codex' );
151 $this->getOutput()->addJsConfigVars( $this->codexFormData );
152 }
153 }
154
155 public function doesWrites() {
156 return true;
157 }
158
165 protected function checkExecutePermissions( User $user ) {
166 parent::checkExecutePermissions( $user );
167 // T17810: blocked admins should have limited access here
168 $status = $this->blockPermissionCheckerFactory
169 ->newBlockPermissionChecker( $this->target, $user )
170 ->checkBlockPermissions();
171 if ( $status !== true ) {
172 throw new ErrorPageError( 'badaccess', $status );
173 }
174 }
175
181 public function requiresUnblock() {
182 return false;
183 }
184
190 protected function setParameter( $par ) {
191 // Extract variables from the request. Try not to get into a situation where we
192 // need to extract *every* variable from the form just for processing here, but
193 // there are legitimate uses for some variables
194 $request = $this->getRequest();
195 [ $this->target, $this->type ] = self::getTargetAndTypeInternal( $par, $request );
196 if ( $this->target instanceof UserIdentity ) {
197 // Set the 'relevant user' in the skin, so it displays links like Contributions,
198 // User logs, UserRights, etc.
199 $this->getSkin()->setRelevantUser( $this->target );
200 }
201
202 [ $this->previousTarget, /*...*/ ] = $this->blockUtils
203 ->parseBlockTarget( $request->getVal( 'wpPreviousTarget' ) );
204 $this->requestedHideUser = $request->getBool( 'wpHideUser' );
205 }
206
212 protected function alterForm( HTMLForm $form ) {
213 $form->setHeaderHtml( '' );
214 $form->setSubmitDestructive();
215
216 $msg = $this->alreadyBlocked ? 'ipb-change-block' : 'ipbsubmit';
217 $form->setSubmitTextMsg( $msg );
218
219 $this->addHelpLink( 'Help:Blocking users' );
220
221 // Don't need to do anything if the form has been posted
222 if ( !$this->getRequest()->wasPosted() && $this->preErrors ) {
223 // Mimic error messages normally generated by the form
224 $form->addHeaderHtml( (string)new FieldLayout(
225 new Widget( [] ),
226 [
227 'align' => 'top',
228 'errors' => array_map( function ( $errMsg ) {
229 return new HtmlSnippet( $this->msg( $errMsg )->parse() );
230 }, $this->preErrors ),
231 ]
232 ) );
233 }
234 }
235
236 protected function getDisplayFormat() {
237 return $this->getConfig()->get( MainConfigNames::UseCodexSpecialBlock ) ? 'codex' : 'ooui';
238 }
239
244 protected function getFormFields() {
245 $conf = $this->getConfig();
246 $blockAllowsUTEdit = $conf->get( MainConfigNames::BlockAllowsUTEdit );
247
248 $this->getOutput()->enableOOUI();
249
250 $user = $this->getUser();
251
252 $suggestedDurations = $this->getLanguage()->getBlockDurations();
253
254 $a = [];
255
256 $a['Target'] = [
257 'type' => 'user',
258 'ipallowed' => true,
259 'iprange' => true,
260 'id' => 'mw-bi-target',
261 'size' => '45',
262 'autofocus' => true,
263 'required' => true,
264 'placeholder' => $this->msg( 'block-target-placeholder' )->text(),
265 'validation-callback' => function ( $value, $alldata, $form ) {
266 $status = $this->blockUtils->validateTarget( $value );
267 if ( !$status->isOK() ) {
268 $errors = $status->getMessages();
269 return $form->msg( $errors[0] );
270 }
271 return true;
272 },
273 'section' => 'target',
274 ];
275
276 $editingRestrictionOptions = $this->getConfig()->get( MainConfigNames::UseCodexSpecialBlock ) ?
277 // If we're using Codex, use the option-descriptions feature, which is only supported by Codex
278 [
279 'options-messages' => [
280 'ipb-sitewide' => 'sitewide',
281 'ipb-partial' => 'partial'
282 ],
283 'option-descriptions-messages' => [
284 'sitewide' => 'ipb-sitewide-help',
285 'partial' => 'ipb-partial-help'
286 ],
287 'option-descriptions-messages-parse' => true,
288 ] :
289 // Otherwise, if we're using OOUI, add the options' descriptions as part of their labels
290 [
291 'options' => [
292 $this->msg( 'ipb-sitewide' )->escaped() .
293 new LabelWidget( [
294 'classes' => [ 'oo-ui-inline-help' ],
295 'label' => new HtmlSnippet( $this->msg( 'ipb-sitewide-help' )->parse() ),
296 ] ) => 'sitewide',
297 $this->msg( 'ipb-partial' )->escaped() .
298 new LabelWidget( [
299 'classes' => [ 'oo-ui-inline-help' ],
300 'label' => new HtmlSnippet( $this->msg( 'ipb-partial-help' )->parse() ),
301 ] ) => 'partial',
302 ]
303 ];
304
305 $a['EditingRestriction'] = [
306 'type' => 'radio',
307 'cssclass' => 'mw-block-editing-restriction',
308 'default' => 'sitewide',
309 'section' => 'actions',
310 ] + $editingRestrictionOptions;
311
312 $a['PageRestrictions'] = [
313 'type' => 'titlesmultiselect',
314 'label' => $this->msg( 'ipb-pages-label' )->text(),
315 'exists' => true,
316 'max' => 10,
317 'cssclass' => 'mw-htmlform-checkradio-indent mw-block-partial-restriction',
318 'default' => '',
319 'showMissing' => false,
320 'excludeDynamicNamespaces' => true,
321 'input' => [
322 'autocomplete' => false
323 ],
324 'section' => 'actions',
325 ];
326
327 $a['NamespaceRestrictions'] = [
328 'type' => 'namespacesmultiselect',
329 'label' => $this->msg( 'ipb-namespaces-label' )->text(),
330 'exists' => true,
331 'cssclass' => 'mw-htmlform-checkradio-indent mw-block-partial-restriction',
332 'default' => '',
333 'input' => [
334 'autocomplete' => false
335 ],
336 'section' => 'actions',
337 ];
338
339 if ( $conf->get( MainConfigNames::EnablePartialActionBlocks ) ) {
340 $blockActions = $this->blockActionInfo->getAllBlockActions();
341 $optionMessages = array_combine(
342 array_map( static function ( $action ) {
343 return "ipb-action-$action";
344 }, array_keys( $blockActions ) ),
345 $blockActions
346 );
347
348 $this->codexFormData[ 'partialBlockActionOptions'] = $optionMessages;
349
350 $a['ActionRestrictions'] = [
351 'type' => 'multiselect',
352 'cssclass' => 'mw-htmlform-checkradio-indent mw-block-partial-restriction mw-block-action-restriction',
353 'options-messages' => $optionMessages,
354 'section' => 'actions',
355 ];
356 }
357
358 $a['CreateAccount'] = [
359 'type' => 'check',
360 'cssclass' => 'mw-block-restriction',
361 'label-message' => 'ipbcreateaccount',
362 'default' => true,
363 'section' => 'details',
364 ];
365
366 if ( $this->blockPermissionCheckerFactory
367 ->newBlockPermissionChecker( null, $user )
368 ->checkEmailPermissions()
369 ) {
370 $a['DisableEmail'] = [
371 'type' => 'check',
372 'cssclass' => 'mw-block-restriction',
373 'label-message' => 'ipbemailban',
374 'section' => 'details',
375 ];
376
377 $this->codexFormData[ 'blockAllowsEmailBan'] = true;
378 }
379
380 if ( $blockAllowsUTEdit ) {
381 $a['DisableUTEdit'] = [
382 'type' => 'check',
383 'cssclass' => 'mw-block-restriction',
384 'label-message' => 'ipb-disableusertalk',
385 'default' => false,
386 'section' => 'details',
387 ];
388
389 $this->codexFormData[ 'blockAllowsUTEdit'] = true;
390 }
391
392 $defaultExpiry = $this->msg( 'ipb-default-expiry' )->inContentLanguage();
393 if ( $this->type === DatabaseBlock::TYPE_RANGE || $this->type === DatabaseBlock::TYPE_IP ) {
394 $defaultExpiryIP = $this->msg( 'ipb-default-expiry-ip' )->inContentLanguage();
395 if ( !$defaultExpiryIP->isDisabled() ) {
396 $defaultExpiry = $defaultExpiryIP;
397 }
398 }
399
400 $a['Expiry'] = [
401 'type' => 'expiry',
402 'required' => true,
403 'options' => $suggestedDurations,
404 'default' => $defaultExpiry->text(),
405 'section' => 'expiry',
406 ];
407
408 $a['Reason'] = [
409 'type' => 'selectandother',
410 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
411 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
412 // Unicode codepoints.
413 'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
414 'maxlength-unit' => 'codepoints',
415 'options-message' => 'ipbreason-dropdown',
416 'section' => 'reason',
417 ];
418
419 $a['AutoBlock'] = [
420 'type' => 'check',
421 'label-message' => [
422 'ipbenableautoblock',
424 ],
425 'default' => true,
426 'section' => 'options',
427 ];
428
429 // Allow some users to hide name from block log, blocklist and listusers
430 if ( $this->getAuthority()->isAllowed( 'hideuser' ) ) {
431 $a['HideUser'] = [
432 'type' => 'check',
433 'label-message' => 'ipbhidename',
434 'cssclass' => 'mw-block-hideuser',
435 'section' => 'options',
436 ];
437
438 $this->codexFormData[ 'hideUser'] = true;
439 }
440
441 // Watchlist their user page? (Only if user is logged in)
442 if ( $user->isRegistered() ) {
443 $a['Watch'] = [
444 'type' => 'check',
445 'label-message' => 'ipbwatchuser',
446 'section' => 'options',
447 ];
448 }
449
450 $a['HardBlock'] = [
451 'type' => 'check',
452 'label-message' => 'ipb-hardblock',
453 'default' => false,
454 'section' => 'options',
455 ];
456
457 // This is basically a copy of the Target field, but the user can't change it, so we
458 // can see if the warnings we maybe showed to the user before still apply
459 $a['PreviousTarget'] = [
460 'type' => 'hidden',
461 'default' => false,
462 ];
463
464 // We'll turn this into a checkbox if we need to
465 $a['Confirm'] = [
466 'type' => 'hidden',
467 'default' => '',
468 'label-message' => 'ipb-confirm',
469 'cssclass' => 'mw-block-confirm',
470 ];
471
472 $this->maybeAlterFormDefaults( $a );
473
474 // Allow extensions to add more fields
475 $this->getHookRunner()->onSpecialBlockModifyFormFields( $this, $a );
476
477 return $a;
478 }
479
485 protected function maybeAlterFormDefaults( &$fields ) {
486 // This will be overwritten by request data
487 $fields['Target']['default'] = (string)$this->target;
488
489 if ( $this->target ) {
490 $status = $this->blockUtils->validateTarget( $this->target );
491 if ( !$status->isOK() ) {
492 $errors = $status->getMessages( 'error' );
493 $this->preErrors = array_merge( $this->preErrors, $errors );
494 }
495 }
496
497 // This won't be
498 $fields['PreviousTarget']['default'] = (string)$this->target;
499
500 $block = $this->blockStore->newFromTarget( $this->target );
501
502 // Populate fields if there is a block that is not an autoblock; if it is a range
503 // block, only populate the fields if the range is the same as $this->target
504 if ( $block instanceof DatabaseBlock && $block->getType() !== DatabaseBlock::TYPE_AUTO
505 && ( $this->type != DatabaseBlock::TYPE_RANGE
506 || ( $this->target && $block->isBlocking( $this->target ) ) )
507 ) {
508 $fields['HardBlock']['default'] = $block->isHardblock();
509 $fields['CreateAccount']['default'] = $block->isCreateAccountBlocked();
510 $fields['AutoBlock']['default'] = $block->isAutoblocking();
511
512 if ( isset( $fields['DisableEmail'] ) ) {
513 $fields['DisableEmail']['default'] = $block->isEmailBlocked();
514 }
515
516 if ( isset( $fields['HideUser'] ) ) {
517 $fields['HideUser']['default'] = $block->getHideName();
518 }
519
520 if ( isset( $fields['DisableUTEdit'] ) ) {
521 $fields['DisableUTEdit']['default'] = !$block->isUsertalkEditAllowed();
522 }
523
524 // If the username was hidden (bl_deleted == 1), don't show the reason
525 // unless this user also has rights to hideuser: T37839
526 if ( !$block->getHideName() || $this->getAuthority()->isAllowed( 'hideuser' ) ) {
527 $fields['Reason']['default'] = $block->getReasonComment()->text;
528 } else {
529 $fields['Reason']['default'] = '';
530 }
531
532 if ( $this->getRequest()->wasPosted() ) {
533 // Ok, so we got a POST submission asking us to reblock a user. So show the
534 // confirm checkbox; the user will only see it if they haven't previously
535 $fields['Confirm']['type'] = 'check';
536 } else {
537 // We got a target, but it wasn't a POST request, so the user must have gone
538 // to a link like [[Special:Block/User]]. We don't need to show the checkbox
539 // as long as they go ahead and block *that* user
540 $fields['Confirm']['default'] = 1;
541 }
542
543 if ( $block->getExpiry() == 'infinity' ) {
544 $fields['Expiry']['default'] = 'infinite';
545 } else {
546 $fields['Expiry']['default'] = wfTimestamp( TS_RFC2822, $block->getExpiry() );
547 }
548
549 if ( !$block->isSitewide() ) {
550 $fields['EditingRestriction']['default'] = 'partial';
551
552 $pageRestrictions = [];
553 $namespaceRestrictions = [];
554 foreach ( $block->getRestrictions() as $restriction ) {
555 if ( $restriction instanceof PageRestriction && $restriction->getTitle() ) {
556 $pageRestrictions[] = $restriction->getTitle()->getPrefixedText();
557 } elseif ( $restriction instanceof NamespaceRestriction &&
558 $this->namespaceInfo->exists( $restriction->getValue() )
559 ) {
560 $namespaceRestrictions[] = $restriction->getValue();
561 }
562 }
563
564 // Sort the restrictions so they are in alphabetical order.
565 sort( $pageRestrictions );
566 $fields['PageRestrictions']['default'] = implode( "\n", $pageRestrictions );
567 sort( $namespaceRestrictions );
568 $fields['NamespaceRestrictions']['default'] = implode( "\n", $namespaceRestrictions );
569
571 $actionRestrictions = [];
572 foreach ( $block->getRestrictions() as $restriction ) {
573 if ( $restriction instanceof ActionRestriction ) {
574 $actionRestrictions[] = $restriction->getValue();
575 }
576 }
577 $fields['ActionRestrictions']['default'] = $actionRestrictions;
578 }
579 }
580
581 $this->alreadyBlocked = true;
582 $this->preErrors[] = $this->msg( 'ipb-needreblock', wfEscapeWikiText( $block->getTargetName() ) );
583 }
584
585 if ( $this->alreadyBlocked || $this->getRequest()->wasPosted()
586 || $this->getRequest()->getCheck( 'wpCreateAccount' )
587 ) {
588 $this->getOutput()->addJsConfigVars( 'wgCreateAccountDirty', true );
589 }
590
591 // We always need confirmation to do HideUser
592 if ( $this->requestedHideUser ) {
593 $fields['Confirm']['type'] = 'check';
594 unset( $fields['Confirm']['default'] );
595 $this->preErrors[] = $this->msg( 'ipb-confirmhideuser', 'ipb-confirmaction' );
596 }
597
598 // Or if the user is trying to block themselves
599 if ( (string)$this->target === $this->getUser()->getName() ) {
600 $fields['Confirm']['type'] = 'check';
601 unset( $fields['Confirm']['default'] );
602 $this->preErrors[] = $this->msg( 'ipb-blockingself', 'ipb-confirmaction' );
603 }
604 }
605
610 protected function preHtml() {
611 $this->getOutput()->addModuleStyles( [ 'mediawiki.special' ] );
612 $this->getOutput()->addModules( [ 'mediawiki.special.block' ] );
613
614 $blockCIDRLimit = $this->getConfig()->get( MainConfigNames::BlockCIDRLimit );
615 $text = $this->msg( 'blockiptext', $blockCIDRLimit['IPv4'], $blockCIDRLimit['IPv6'] )->parse();
616
617 $otherBlockMessages = [];
618 if ( $this->target !== null ) {
619 $targetName = $this->target;
620 if ( $this->target instanceof UserIdentity ) {
621 $targetName = $this->target->getName();
622 }
623 // Get other blocks, i.e. from GlobalBlocking or TorBlock extension
624 $this->getHookRunner()->onOtherBlockLogLink(
625 $otherBlockMessages, $targetName );
626
627 if ( count( $otherBlockMessages ) ) {
628 $s = Html::rawElement(
629 'h2',
630 [],
631 $this->msg( 'ipb-otherblocks-header', count( $otherBlockMessages ) )->parse()
632 ) . "\n";
633
634 $list = '';
635
636 foreach ( $otherBlockMessages as $link ) {
637 $list .= Html::rawElement( 'li', [], $link ) . "\n";
638 }
639
640 $s .= Html::rawElement(
641 'ul',
642 [ 'class' => 'mw-blockip-alreadyblocked' ],
643 $list
644 ) . "\n";
645
646 $text .= $s;
647 }
648 }
649
650 return $text;
651 }
652
657 protected function postHtml() {
658 $links = [];
659
660 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
661
662 $linkRenderer = $this->getLinkRenderer();
663 // Link to the user's contributions, if applicable
664 if ( $this->target instanceof UserIdentity ) {
665 $contribsPage = SpecialPage::getTitleFor( 'Contributions', $this->target->getName() );
666 $links[] = $linkRenderer->makeLink(
667 $contribsPage,
668 $this->msg( 'ipb-blocklist-contribs', $this->target->getName() )->text()
669 );
670 }
671
672 // Link to unblock the specified user, or to a blank unblock form
673 if ( $this->target instanceof UserIdentity ) {
674 $message = $this->msg(
675 'ipb-unblock-addr',
676 wfEscapeWikiText( $this->target->getName() )
677 )->parse();
678 $list = SpecialPage::getTitleFor( 'Unblock', $this->target->getName() );
679 } else {
680 $message = $this->msg( 'ipb-unblock' )->parse();
681 $list = SpecialPage::getTitleFor( 'Unblock' );
682 }
683 $links[] = $linkRenderer->makeKnownLink(
684 $list,
685 new HtmlArmor( $message )
686 );
687
688 // Link to the block list
689 $links[] = $linkRenderer->makeKnownLink(
690 SpecialPage::getTitleFor( 'BlockList' ),
691 $this->msg( 'ipb-blocklist' )->text()
692 );
693
694 // Link to edit the block dropdown reasons, if applicable
695 if ( $this->getAuthority()->isAllowed( 'editinterface' ) ) {
696 $links[] = $linkRenderer->makeKnownLink(
697 $this->msg( 'ipbreason-dropdown' )->inContentLanguage()->getTitle(),
698 $this->msg( 'ipb-edit-dropdown' )->text(),
699 [],
700 [ 'action' => 'edit' ]
701 );
702 }
703
704 $text = Html::rawElement(
705 'p',
706 [ 'class' => 'mw-ipb-conveniencelinks' ],
707 $this->getLanguage()->pipeList( $links )
708 );
709
710 $userPage = self::getTargetUserTitle( $this->target );
711 if ( $userPage ) {
712 // Get relevant extracts from the block and suppression logs, if possible
713 $out = '';
714
715 LogEventsList::showLogExtract(
716 $out,
717 'block',
718 $userPage,
719 '',
720 [
721 'lim' => 10,
722 'msgKey' => [
723 'blocklog-showlog',
724 $this->titleFormatter->getText( $userPage ),
725 ],
726 'showIfEmpty' => false
727 ]
728 );
729 $text .= $out;
730
731 // Add suppression block entries if allowed
732 if ( $this->getAuthority()->isAllowed( 'suppressionlog' ) ) {
733 LogEventsList::showLogExtract(
734 $out,
735 'suppress',
736 $userPage,
737 '',
738 [
739 'lim' => 10,
740 'conds' => [ 'log_action' => [ 'block', 'reblock', 'unblock' ] ],
741 'msgKey' => [
742 'blocklog-showsuppresslog',
743 $this->titleFormatter->getText( $userPage ),
744 ],
745 'showIfEmpty' => false
746 ]
747 );
748
749 $text .= $out;
750 }
751 }
752
753 return $text;
754 }
755
762 protected static function getTargetUserTitle( $target ): ?PageReference {
763 if ( $target instanceof UserIdentity ) {
764 return PageReferenceValue::localReference( NS_USER, $target->getName() );
765 }
766
767 if ( is_string( $target ) && IPUtils::isIPAddress( $target ) ) {
768 return PageReferenceValue::localReference( NS_USER, $target );
769 }
770
771 return null;
772 }
773
785 private static function getTargetAndTypeInternal( ?string $par, WebRequest $request = null ) {
786 if ( !$request instanceof WebRequest ) {
787 return MediaWikiServices::getInstance()->getBlockUtils()->parseBlockTarget( $par );
788 }
789
790 $possibleTargets = [
791 $request->getVal( 'wpTarget', null ),
792 $par,
793 $request->getVal( 'ip', null ),
794 // B/C @since 1.18
795 $request->getVal( 'wpBlockAddress', null ),
796 ];
797 foreach ( $possibleTargets as $possibleTarget ) {
798 $targetAndType = MediaWikiServices::getInstance()
799 ->getBlockUtils()
800 ->parseBlockTarget( $possibleTarget );
801 // If type is not null then target is valid
802 if ( $targetAndType[ 1 ] !== null ) {
803 break;
804 }
805 }
806 return $targetAndType;
807 }
808
817 public static function processForm( array $data, IContextSource $context ) {
818 $services = MediaWikiServices::getInstance();
819 return self::processFormInternal(
820 $data,
821 $context->getAuthority(),
822 $services->getBlockUserFactory(),
823 $services->getBlockUtils()
824 );
825 }
826
837 private static function processFormInternal(
838 array $data,
839 Authority $performer,
840 BlockUserFactory $blockUserFactory,
841 BlockUtils $blockUtils
842 ) {
843 // Temporarily access service container until the feature flag is removed: T280532
844 $enablePartialActionBlocks = MediaWikiServices::getInstance()
845 ->getMainConfig()->get( MainConfigNames::EnablePartialActionBlocks );
846
847 $isPartialBlock = isset( $data['EditingRestriction'] ) &&
848 $data['EditingRestriction'] === 'partial';
849
850 // This might have been a hidden field or a checkbox, so interesting data
851 // can come from it
852 $data['Confirm'] = !in_array( $data['Confirm'], [ '', '0', null, false ], true );
853
854 // If the user has done the form 'properly', they won't even have been given the
855 // option to suppress-block unless they have the 'hideuser' permission
856 if ( !isset( $data['HideUser'] ) ) {
857 $data['HideUser'] = false;
858 }
859
861 [ $target, $type ] = $blockUtils->parseBlockTarget( $data['Target'] );
862 if ( $type == DatabaseBlock::TYPE_USER ) {
863 $target = $target->getName();
864
865 // Give admins a heads-up before they go and block themselves. Much messier
866 // to do this for IPs, but it's pretty unlikely they'd ever get the 'block'
867 // permission anyway, although the code does allow for it.
868 // Note: Important to use $target instead of $data['Target']
869 // since both $data['PreviousTarget'] and $target are normalized
870 // but $data['target'] gets overridden by (non-normalized) request variable
871 // from previous request.
872 if ( $target === $performer->getUser()->getName() &&
873 ( $data['PreviousTarget'] !== $target || !$data['Confirm'] )
874 ) {
875 return [ 'ipb-blockingself', 'ipb-confirmaction' ];
876 }
877
878 if ( $data['HideUser'] && !$data['Confirm'] ) {
879 return [ 'ipb-confirmhideuser', 'ipb-confirmaction' ];
880 }
881 } elseif ( $type == DatabaseBlock::TYPE_IP ) {
882 $target = $target->getName();
883 } elseif ( $type != DatabaseBlock::TYPE_RANGE ) {
884 // This should have been caught in the form field validation
885 return [ 'badipaddress' ];
886 }
887
888 // Reason, to be passed to the block object. For default values of reason, see
889 // HTMLSelectAndOtherField::getDefault
890 $blockReason = $data['Reason'][0] ?? '';
891
892 $pageRestrictions = [];
893 $namespaceRestrictions = [];
894 $actionRestrictions = [];
895 if ( $isPartialBlock ) {
896 if ( isset( $data['PageRestrictions'] ) && $data['PageRestrictions'] !== '' ) {
897 $titles = explode( "\n", $data['PageRestrictions'] );
898 foreach ( $titles as $title ) {
899 $pageRestrictions[] = PageRestriction::newFromTitle( $title );
900 }
901 }
902 if ( isset( $data['NamespaceRestrictions'] ) && $data['NamespaceRestrictions'] !== '' ) {
903 $namespaceRestrictions = array_map( static function ( $id ) {
904 return new NamespaceRestriction( 0, (int)$id );
905 }, explode( "\n", $data['NamespaceRestrictions'] ) );
906 }
907 if (
908 $enablePartialActionBlocks &&
909 isset( $data['ActionRestrictions'] ) &&
910 $data['ActionRestrictions'] !== ''
911 ) {
912 $actionRestrictions = array_map( static function ( $id ) {
913 return new ActionRestriction( 0, $id );
914 }, $data['ActionRestrictions'] );
915 }
916 }
917 $restrictions = array_merge( $pageRestrictions, $namespaceRestrictions, $actionRestrictions );
918
919 if ( !isset( $data['Tags'] ) ) {
920 $data['Tags'] = [];
921 }
922
923 $blockOptions = [
924 'isCreateAccountBlocked' => $data['CreateAccount'],
925 'isHardBlock' => $data['HardBlock'],
926 'isAutoblocking' => $data['AutoBlock'],
927 'isHideUser' => $data['HideUser'],
928 'isPartial' => $isPartialBlock,
929 ];
930
931 if ( isset( $data['DisableUTEdit'] ) ) {
932 $blockOptions['isUserTalkEditBlocked'] = $data['DisableUTEdit'];
933 }
934 if ( isset( $data['DisableEmail'] ) ) {
935 $blockOptions['isEmailBlocked'] = $data['DisableEmail'];
936 }
937
938 $blockUser = $blockUserFactory->newBlockUser(
939 $target,
940 $performer,
941 $data['Expiry'],
942 $blockReason,
943 $blockOptions,
944 $restrictions,
945 $data['Tags']
946 );
947
948 // Indicates whether the user is confirming the block and is aware of
949 // the conflict (did not change the block target in the meantime)
950 $blockNotConfirmed = !$data['Confirm'] || ( array_key_exists( 'PreviousTarget', $data )
951 && $data['PreviousTarget'] !== $target );
952
953 // Special case for API - T34434
954 $reblockNotAllowed = ( array_key_exists( 'Reblock', $data ) && !$data['Reblock'] );
955
956 $doReblock = !$blockNotConfirmed && !$reblockNotAllowed;
957
958 $status = $blockUser->placeBlock( $doReblock );
959 if ( !$status->isOK() ) {
960 return $status;
961 }
962
963 if (
964 // Can't watch a range block
965 $type != DatabaseBlock::TYPE_RANGE
966
967 // Technically a wiki can be configured to allow anonymous users to place blocks,
968 // in which case the 'Watch' field isn't included in the form shown, and we should
969 // not try to access it.
970 && array_key_exists( 'Watch', $data )
971 && $data['Watch']
972 ) {
973 MediaWikiServices::getInstance()->getWatchlistManager()->addWatchIgnoringRights(
974 $performer->getUser(),
975 Title::makeTitle( NS_USER, $target )
976 );
977 }
978
979 return true;
980 }
981
993 public static function getSuggestedDurations( Language $lang = null, $includeOther = true ) {
994 $lang ??= MediaWikiServices::getInstance()->getContentLanguage();
995 return $lang->getBlockDurations( $includeOther );
996 }
997
1007 public static function parseExpiryInput( $expiry ) {
1008 return BlockUser::parseExpiryInput( $expiry );
1009 }
1010
1018 public static function canBlockEmail( UserIdentity $user ) {
1020 ->getBlockPermissionCheckerFactory()
1021 ->newBlockPermissionChecker( null, User::newFromIdentity( $user ) )
1022 ->checkEmailPermissions();
1023 }
1024
1031 public function onSubmit( array $data, HTMLForm $form = null ) {
1032 return self::processFormInternal(
1033 $data,
1034 $this->getAuthority(),
1035 $this->blockUserFactory,
1036 $this->blockUtils
1037 );
1038 }
1039
1044 public function onSuccess() {
1045 $out = $this->getOutput();
1046 $out->setPageTitleMsg( $this->msg( 'blockipsuccesssub' ) );
1047 $out->addWikiMsg( 'blockipsuccesstext', wfEscapeWikiText( $this->target ) );
1048 }
1049
1058 public function prefixSearchSubpages( $search, $limit, $offset ) {
1059 $search = $this->userNameUtils->getCanonical( $search );
1060 if ( !$search ) {
1061 // No prefix suggestion for invalid user
1062 return [];
1063 }
1064 // Autocomplete subpage as user list - public to allow caching
1065 return $this->userNamePrefixSearch
1066 ->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset );
1067 }
1068
1069 protected function getGroupName() {
1070 return 'users';
1071 }
1072}
1073
1075class_alias( SpecialBlock::class, 'SpecialBlock' );
getAuthority()
const NS_USER
Definition Defines.php:67
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
An error page which can definitely be safely rendered using the OutputPage.
Marks HTML that shouldn't be escaped.
Definition HtmlArmor.php:30
Base class for language-specific code.
Definition Language.php:66
Defines the actions that can be blocked by a partial block.
Handles the backend logic of blocking users.
Definition BlockUser.php:54
static parseExpiryInput(string $expiry)
Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute ("24 May 2034",...
Backend class for blocking utils.
parseBlockTarget( $target)
From string specification or UserIdentity, get the block target and the type of target.
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
getType()
Get the type of target for this particular block.int|null AbstractBlock::TYPE_ constant,...
Restriction for partial blocks of actions.
Handle database storage of comments such as edit summaries and log reasons.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:208
setHeaderHtml( $html, $section=null)
Set header HTML, inside the form.
Definition HTMLForm.php:969
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
addHeaderHtml( $html, $section=null)
Add HTML to the header, inside the form.
Definition HTMLForm.php:949
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
A class containing constants representing the names of configuration variables.
const UseCodexSpecialBlock
Name constant for the UseCodexSpecialBlock setting, for use with Config::get()
const AutoblockExpiry
Name constant for the AutoblockExpiry setting, for use with Config::get()
const BlockAllowsUTEdit
Name constant for the BlockAllowsUTEdit setting, for use with Config::get()
const BlockCIDRLimit
Name constant for the BlockCIDRLimit setting, for use with Config::get()
const EnablePartialActionBlocks
Name constant for the EnablePartialActionBlocks setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:158
static durationParam( $duration)
Definition Message.php:1231
Immutable value object representing a page reference.
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
Special page which uses an HTMLForm to handle processing.
string null $par
The sub-page of the special page.
Parent class for all special pages.
getSkin()
Shortcut to get the skin being used for this instance.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getUser()
Shortcut to get the User executing this instance.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getAuthority()
Shortcut to get the Authority executing this instance.
getLanguage()
Shortcut to get user's language.
getName()
Get the canonical, unlocalized name of this special page without namespace.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
Allow users with 'block' user right to block IPs and user accounts from editing pages and other actio...
static parseExpiryInput( $expiry)
Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute ("24 May 2034",...
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
onSuccess()
Do something exciting on successful processing of the form, most likely to show a confirmation messag...
onSubmit(array $data, HTMLForm $form=null)
Process the form on POST submission.
preHtml()
Add header elements like block log entries, etc.
static canBlockEmail(UserIdentity $user)
Can we do an email block?
int $type
DatabaseBlock::TYPE_ constant.
static getSuggestedDurations(Language $lang=null, $includeOther=true)
Get an array of suggested block durations from MediaWiki:Ipboptions.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
User string $previousTarget
The previous block target.
alterForm(HTMLForm $form)
Customizes the HTMLForm a bit.
postHtml()
Add footer elements to the form.
requiresUnblock()
We allow certain special cases where user is blocked.
UserIdentity string null $target
User to be blocked, as passed either by parameter (url?wpTarget=Foo) or as subpage (Special:Block/Foo...
getDisplayFormat()
Get display format for the form.
setParameter( $par)
Handle some magic here.
array $codexFormData
<mixed,mixed> An associative array used to pass vars to Codex form
maybeAlterFormDefaults(&$fields)
If the user has already been blocked with similar settings, load that block and change the defaults f...
checkExecutePermissions(User $user)
Check that the user can unblock themselves if they are trying to do so.
doesWrites()
Indicates whether this special page may perform database writes.
static getTargetUserTitle( $target)
Get a user page target for things like logs.
bool $requestedHideUser
Whether the previous submission of the form asked for HideUser.
static processForm(array $data, IContextSource $context)
Given the form data, actually implement a block.
__construct(BlockUtils $blockUtils, BlockPermissionCheckerFactory $blockPermissionCheckerFactory, BlockUserFactory $blockUserFactory, DatabaseBlockStore $blockStore, UserNameUtils $userNameUtils, UserNamePrefixSearch $userNamePrefixSearch, BlockActionInfo $blockActionInfo, TitleFormatter $titleFormatter, NamespaceInfo $namespaceInfo)
getFormFields()
Get the HTMLForm descriptor array for the block form.
execute( $par)
Basic SpecialPage workflow: get a form, send it to the user; get some data back,.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents a title within MediaWiki.
Definition Title.php:79
Handles searching prefixes of user names.
UserNameUtils service.
internal since 1.36
Definition User.php:93
newBlockUser( $target, Authority $performer, string $expiry, string $reason='', array $blockOptions=[], array $blockRestrictions=[], $tags=[])
Create BlockUser.
Interface for objects which can provide a MediaWiki context on request.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37
getUser()
Returns the performer of the actions associated with this authority.
A title formatter service for MediaWiki.
Interface for objects representing user identity.