92c6f504dd246444f1e2e1d6edbe1f310c04076a
3 * Implements Special:Block
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 * A special page that allows users with 'block' right to block users from
26 * editing pages and other actions
28 * @ingroup SpecialPage
30 class SpecialBlock
extends FormSpecialPage
{
31 /** @var User|string|null User to be blocked, as passed either by parameter (url?wpTarget=Foo)
32 * or as subpage (Special:Block/Foo) */
35 /** @var int Block::TYPE_ constant */
38 /** @var User|string The previous block target */
39 protected $previousTarget;
41 /** @var bool Whether the previous submission of the form asked for HideUser */
42 protected $requestedHideUser;
45 protected $alreadyBlocked;
48 protected $preErrors = [];
50 public function __construct() {
51 parent
::__construct( 'Block', 'block' );
54 public function doesWrites() {
59 * Checks that the user can unblock themselves if they are trying to do so
62 * @throws ErrorPageError
64 protected function checkExecutePermissions( User
$user ) {
65 parent
::checkExecutePermissions( $user );
67 # T17810: blocked admins should have limited access here
68 $status = self
::checkUnblockSelf( $this->target
, $user );
69 if ( $status !== true ) {
70 throw new ErrorPageError( 'badaccess', $status );
75 * Handle some magic here
79 protected function setParameter( $par ) {
80 # Extract variables from the request. Try not to get into a situation where we
81 # need to extract *every* variable from the form just for processing here, but
82 # there are legitimate uses for some variables
83 $request = $this->getRequest();
84 list( $this->target
, $this->type
) = self
::getTargetAndType( $par, $request );
85 if ( $this->target
instanceof User
) {
86 # Set the 'relevant user' in the skin, so it displays links like Contributions,
87 # User logs, UserRights, etc.
88 $this->getSkin()->setRelevantUser( $this->target
);
91 list( $this->previousTarget
, /*...*/ ) =
92 Block
::parseTarget( $request->getVal( 'wpPreviousTarget' ) );
93 $this->requestedHideUser
= $request->getBool( 'wpHideUser' );
97 * Customizes the HTMLForm a bit
99 * @param HTMLForm $form
101 protected function alterForm( HTMLForm
$form ) {
102 $form->setHeaderText( '' );
103 $form->setSubmitDestructive();
105 $msg = $this->alreadyBlocked ?
'ipb-change-block' : 'ipbsubmit';
106 $form->setSubmitTextMsg( $msg );
108 $this->addHelpLink( 'Help:Blocking users' );
110 # Don't need to do anything if the form has been posted
111 if ( !$this->getRequest()->wasPosted() && $this->preErrors
) {
112 $s = $form->formatErrors( $this->preErrors
);
114 $form->addHeaderText( Html
::rawElement(
116 [ 'class' => 'error' ],
123 protected function getDisplayFormat() {
128 * Get the HTMLForm descriptor array for the block form
131 protected function getFormFields() {
132 global $wgBlockAllowsUTEdit;
134 $user = $this->getUser();
136 $suggestedDurations = self
::getSuggestedDurations();
138 $conf = $this->getConfig();
139 $oldCommentSchema = $conf->get( 'CommentTableSchemaMigrationStage' ) === MIGRATION_OLD
;
146 'label-message' => 'ipaddressorusername',
147 'id' => 'mw-bi-target',
151 'validation-callback' => [ __CLASS__
, 'validateTargetField' ],
155 'label-message' => 'ipbexpiry',
157 'options' => $suggestedDurations,
158 'default' => $this->msg( 'ipb-default-expiry' )->inContentLanguage()->text(),
161 'type' => 'selectandother',
162 // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
163 // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
164 // Unicode codepoints (or 255 UTF-8 bytes for old schema).
165 'maxlength' => $oldCommentSchema ?
255 : CommentStore
::COMMENT_CHARACTER_LIMIT
,
166 'maxlength-unit' => 'codepoints',
167 'label-message' => 'ipbreason',
168 'options-message' => 'ipbreason-dropdown',
172 'label-message' => 'ipbcreateaccount',
177 if ( self
::canBlockEmail( $user ) ) {
178 $a['DisableEmail'] = [
180 'label-message' => 'ipbemailban',
184 if ( $wgBlockAllowsUTEdit ) {
185 $a['DisableUTEdit'] = [
187 'label-message' => 'ipb-disableusertalk',
194 'label-message' => 'ipbenableautoblock',
198 # Allow some users to hide name from block log, blocklist and listusers
199 if ( $user->isAllowed( 'hideuser' ) ) {
202 'label-message' => 'ipbhidename',
203 'cssclass' => 'mw-block-hideuser',
207 # Watchlist their user page? (Only if user is logged in)
208 if ( $user->isLoggedIn() ) {
211 'label-message' => 'ipbwatchuser',
217 'label-message' => 'ipb-hardblock',
221 # This is basically a copy of the Target field, but the user can't change it, so we
222 # can see if the warnings we maybe showed to the user before still apply
223 $a['PreviousTarget'] = [
228 # We'll turn this into a checkbox if we need to
232 'label-message' => 'ipb-confirm',
233 'cssclass' => 'mw-block-confirm',
236 $this->maybeAlterFormDefaults( $a );
238 // Allow extensions to add more fields
239 Hooks
::run( 'SpecialBlockModifyFormFields', [ $this, &$a ] );
245 * If the user has already been blocked with similar settings, load that block
246 * and change the defaults for the form fields to match the existing settings.
247 * @param array &$fields HTMLForm descriptor array
248 * @return bool Whether fields were altered (that is, whether the target is
251 protected function maybeAlterFormDefaults( &$fields ) {
252 # This will be overwritten by request data
253 $fields['Target']['default'] = (string)$this->target
;
255 if ( $this->target
) {
256 $status = self
::validateTarget( $this->target
, $this->getUser() );
257 if ( !$status->isOK() ) {
258 $errors = $status->getErrorsArray();
259 $this->preErrors
= array_merge( $this->preErrors
, $errors );
264 $fields['PreviousTarget']['default'] = (string)$this->target
;
266 $block = Block
::newFromTarget( $this->target
);
268 if ( $block instanceof Block
&& !$block->mAuto
# The block exists and isn't an autoblock
269 && ( $this->type
!= Block
::TYPE_RANGE
# The block isn't a rangeblock
270 ||
$block->getTarget() == $this->target
) # or if it is, the range is what we're about to block
272 $fields['HardBlock']['default'] = $block->isHardblock();
273 $fields['CreateAccount']['default'] = $block->prevents( 'createaccount' );
274 $fields['AutoBlock']['default'] = $block->isAutoblocking();
276 if ( isset( $fields['DisableEmail'] ) ) {
277 $fields['DisableEmail']['default'] = $block->prevents( 'sendemail' );
280 if ( isset( $fields['HideUser'] ) ) {
281 $fields['HideUser']['default'] = $block->mHideName
;
284 if ( isset( $fields['DisableUTEdit'] ) ) {
285 $fields['DisableUTEdit']['default'] = $block->prevents( 'editownusertalk' );
288 // If the username was hidden (ipb_deleted == 1), don't show the reason
289 // unless this user also has rights to hideuser: T37839
290 if ( !$block->mHideName ||
$this->getUser()->isAllowed( 'hideuser' ) ) {
291 $fields['Reason']['default'] = $block->mReason
;
293 $fields['Reason']['default'] = '';
296 if ( $this->getRequest()->wasPosted() ) {
297 # Ok, so we got a POST submission asking us to reblock a user. So show the
298 # confirm checkbox; the user will only see it if they haven't previously
299 $fields['Confirm']['type'] = 'check';
301 # We got a target, but it wasn't a POST request, so the user must have gone
302 # to a link like [[Special:Block/User]]. We don't need to show the checkbox
303 # as long as they go ahead and block *that* user
304 $fields['Confirm']['default'] = 1;
307 if ( $block->mExpiry
== 'infinity' ) {
308 $fields['Expiry']['default'] = 'infinite';
310 $fields['Expiry']['default'] = wfTimestamp( TS_RFC2822
, $block->mExpiry
);
313 $this->alreadyBlocked
= true;
314 $this->preErrors
[] = [ 'ipb-needreblock', wfEscapeWikiText( (string)$block->getTarget() ) ];
317 # We always need confirmation to do HideUser
318 if ( $this->requestedHideUser
) {
319 $fields['Confirm']['type'] = 'check';
320 unset( $fields['Confirm']['default'] );
321 $this->preErrors
[] = [ 'ipb-confirmhideuser', 'ipb-confirmaction' ];
324 # Or if the user is trying to block themselves
325 if ( (string)$this->target
=== $this->getUser()->getName() ) {
326 $fields['Confirm']['type'] = 'check';
327 unset( $fields['Confirm']['default'] );
328 $this->preErrors
[] = [ 'ipb-blockingself', 'ipb-confirmaction' ];
333 * Add header elements like block log entries, etc.
336 protected function preText() {
337 $this->getOutput()->addModules( [ 'mediawiki.special.block' ] );
339 $blockCIDRLimit = $this->getConfig()->get( 'BlockCIDRLimit' );
340 $text = $this->msg( 'blockiptext', $blockCIDRLimit['IPv4'], $blockCIDRLimit['IPv6'] )->parse();
342 $otherBlockMessages = [];
343 if ( $this->target
!== null ) {
344 $targetName = $this->target
;
345 if ( $this->target
instanceof User
) {
346 $targetName = $this->target
->getName();
348 # Get other blocks, i.e. from GlobalBlocking or TorBlock extension
349 Hooks
::run( 'OtherBlockLogLink', [ &$otherBlockMessages, $targetName ] );
351 if ( count( $otherBlockMessages ) ) {
352 $s = Html
::rawElement(
355 $this->msg( 'ipb-otherblocks-header', count( $otherBlockMessages ) )->parse()
360 foreach ( $otherBlockMessages as $link ) {
361 $list .= Html
::rawElement( 'li', [], $link ) . "\n";
364 $s .= Html
::rawElement(
366 [ 'class' => 'mw-blockip-alreadyblocked' ],
378 * Add footer elements to the form
381 protected function postText() {
384 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
386 $linkRenderer = $this->getLinkRenderer();
387 # Link to the user's contributions, if applicable
388 if ( $this->target
instanceof User
) {
389 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $this->target
->getName() );
390 $links[] = $linkRenderer->makeLink(
392 $this->msg( 'ipb-blocklist-contribs', $this->target
->getName() )->text()
396 # Link to unblock the specified user, or to a blank unblock form
397 if ( $this->target
instanceof User
) {
398 $message = $this->msg(
400 wfEscapeWikiText( $this->target
->getName() )
402 $list = SpecialPage
::getTitleFor( 'Unblock', $this->target
->getName() );
404 $message = $this->msg( 'ipb-unblock' )->parse();
405 $list = SpecialPage
::getTitleFor( 'Unblock' );
407 $links[] = $linkRenderer->makeKnownLink(
409 new HtmlArmor( $message )
412 # Link to the block list
413 $links[] = $linkRenderer->makeKnownLink(
414 SpecialPage
::getTitleFor( 'BlockList' ),
415 $this->msg( 'ipb-blocklist' )->text()
418 $user = $this->getUser();
420 # Link to edit the block dropdown reasons, if applicable
421 if ( $user->isAllowed( 'editinterface' ) ) {
422 $links[] = $linkRenderer->makeKnownLink(
423 $this->msg( 'ipbreason-dropdown' )->inContentLanguage()->getTitle(),
424 $this->msg( 'ipb-edit-dropdown' )->text(),
426 [ 'action' => 'edit' ]
430 $text = Html
::rawElement(
432 [ 'class' => 'mw-ipb-conveniencelinks' ],
433 $this->getLanguage()->pipeList( $links )
436 $userTitle = self
::getTargetUserTitle( $this->target
);
438 # Get relevant extracts from the block and suppression logs, if possible
441 LogEventsList
::showLogExtract(
448 'msgKey' => [ 'blocklog-showlog', $userTitle->getText() ],
449 'showIfEmpty' => false
454 # Add suppression block entries if allowed
455 if ( $user->isAllowed( 'suppressionlog' ) ) {
456 LogEventsList
::showLogExtract(
463 'conds' => [ 'log_action' => [ 'block', 'reblock', 'unblock' ] ],
464 'msgKey' => [ 'blocklog-showsuppresslog', $userTitle->getText() ],
465 'showIfEmpty' => false
477 * Get a user page target for things like logs.
478 * This handles account and IP range targets.
479 * @param User|string $target
482 protected static function getTargetUserTitle( $target ) {
483 if ( $target instanceof User
) {
484 return $target->getUserPage();
485 } elseif ( IP
::isIPAddress( $target ) ) {
486 return Title
::makeTitleSafe( NS_USER
, $target );
493 * Determine the target of the block, and the type of target
494 * @todo Should be in Block.php?
495 * @param string $par Subpage parameter passed to setup, or data value from
497 * @param WebRequest|null $request Optionally try and get data from a request too
498 * @return array [ User|string|null, Block::TYPE_ constant|null ]
500 public static function getTargetAndType( $par, WebRequest
$request = null ) {
507 # The HTMLForm will check wpTarget first and only if it doesn't get
508 # a value use the default, which will be generated from the options
509 # below; so this has to have a higher precedence here than $par, or
510 # we could end up with different values in $this->target and the HTMLForm!
511 if ( $request instanceof WebRequest
) {
512 $target = $request->getText( 'wpTarget', null );
519 if ( $request instanceof WebRequest
) {
520 $target = $request->getText( 'ip', null );
525 if ( $request instanceof WebRequest
) {
526 $target = $request->getText( 'wpBlockAddress', null );
533 list( $target, $type ) = Block
::parseTarget( $target );
535 if ( $type !== null ) {
536 return [ $target, $type ];
540 return [ null, null ];
544 * HTMLForm field validation-callback for Target field.
546 * @param string $value
547 * @param array $alldata
548 * @param HTMLForm $form
551 public static function validateTargetField( $value, $alldata, $form ) {
552 $status = self
::validateTarget( $value, $form->getUser() );
553 if ( !$status->isOK() ) {
554 $errors = $status->getErrorsArray();
556 return $form->msg( ...$errors[0] );
563 * Validate a block target.
566 * @param string $value Block target to check
567 * @param User $user Performer of the block
570 public static function validateTarget( $value, User
$user ) {
571 global $wgBlockCIDRLimit;
573 /** @var User $target */
574 list( $target, $type ) = self
::getTargetAndType( $value );
575 $status = Status
::newGood( $target );
577 if ( $type == Block
::TYPE_USER
) {
578 if ( $target->isAnon() ) {
581 wfEscapeWikiText( $target->getName() )
585 $unblockStatus = self
::checkUnblockSelf( $target, $user );
586 if ( $unblockStatus !== true ) {
587 $status->fatal( 'badaccess', $unblockStatus );
589 } elseif ( $type == Block
::TYPE_RANGE
) {
590 list( $ip, $range ) = explode( '/', $target, 2 );
593 ( IP
::isIPv4( $ip ) && $wgBlockCIDRLimit['IPv4'] == 32 ) ||
594 ( IP
::isIPv6( $ip ) && $wgBlockCIDRLimit['IPv6'] == 128 )
596 // Range block effectively disabled
597 $status->fatal( 'range_block_disabled' );
601 ( IP
::isIPv4( $ip ) && $range > 32 ) ||
602 ( IP
::isIPv6( $ip ) && $range > 128 )
605 $status->fatal( 'ip_range_invalid' );
608 if ( IP
::isIPv4( $ip ) && $range < $wgBlockCIDRLimit['IPv4'] ) {
609 $status->fatal( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
612 if ( IP
::isIPv6( $ip ) && $range < $wgBlockCIDRLimit['IPv6'] ) {
613 $status->fatal( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
615 } elseif ( $type == Block
::TYPE_IP
) {
618 $status->fatal( 'badipaddress' );
625 * Given the form data, actually implement a block. This is also called from ApiBlock.
628 * @param IContextSource $context
629 * @return bool|string
631 public static function processForm( array $data, IContextSource
$context ) {
632 global $wgBlockAllowsUTEdit, $wgHideUserContribLimit;
634 $performer = $context->getUser();
636 // Handled by field validator callback
637 // self::validateTargetField( $data['Target'] );
639 # This might have been a hidden field or a checkbox, so interesting data
641 $data['Confirm'] = !in_array( $data['Confirm'], [ '', '0', null, false ], true );
643 /** @var User $target */
644 list( $target, $type ) = self
::getTargetAndType( $data['Target'] );
645 if ( $type == Block
::TYPE_USER
) {
647 $target = $user->getName();
648 $userId = $user->getId();
650 # Give admins a heads-up before they go and block themselves. Much messier
651 # to do this for IPs, but it's pretty unlikely they'd ever get the 'block'
652 # permission anyway, although the code does allow for it.
653 # Note: Important to use $target instead of $data['Target']
654 # since both $data['PreviousTarget'] and $target are normalized
655 # but $data['target'] gets overridden by (non-normalized) request variable
656 # from previous request.
657 if ( $target === $performer->getName() &&
658 ( $data['PreviousTarget'] !== $target ||
!$data['Confirm'] )
660 return [ 'ipb-blockingself', 'ipb-confirmaction' ];
662 } elseif ( $type == Block
::TYPE_RANGE
) {
665 } elseif ( $type == Block
::TYPE_IP
) {
667 $target = $target->getName();
670 # This should have been caught in the form field validation
671 return [ 'badipaddress' ];
674 $expiryTime = self
::parseExpiryInput( $data['Expiry'] );
677 // an expiry time is needed
678 ( strlen( $data['Expiry'] ) == 0 ) ||
679 // can't be a larger string as 50 (it should be a time format in any way)
680 ( strlen( $data['Expiry'] ) > 50 ) ||
681 // check, if the time could be parsed
684 return [ 'ipb_expiry_invalid' ];
687 // an expiry time should be in the future, not in the
688 // past (wouldn't make any sense) - bug T123069
689 if ( $expiryTime < wfTimestampNow() ) {
690 return [ 'ipb_expiry_old' ];
693 if ( !isset( $data['DisableEmail'] ) ) {
694 $data['DisableEmail'] = false;
697 # If the user has done the form 'properly', they won't even have been given the
698 # option to suppress-block unless they have the 'hideuser' permission
699 if ( !isset( $data['HideUser'] ) ) {
700 $data['HideUser'] = false;
703 if ( $data['HideUser'] ) {
704 if ( !$performer->isAllowed( 'hideuser' ) ) {
705 # this codepath is unreachable except by a malicious user spoofing forms,
706 # or by race conditions (user has hideuser and block rights, loads block form,
707 # and loses hideuser rights before submission); so need to fail completely
708 # rather than just silently disable hiding
709 return [ 'badaccess-group0' ];
712 # Recheck params here...
713 if ( $type != Block
::TYPE_USER
) {
714 $data['HideUser'] = false; # IP users should not be hidden
715 } elseif ( !wfIsInfinity( $data['Expiry'] ) ) {
717 return [ 'ipb_expiry_temp' ];
718 } elseif ( $wgHideUserContribLimit !== false
719 && $user->getEditCount() > $wgHideUserContribLimit
721 # Typically, the user should have a handful of edits.
722 # Disallow hiding users with many edits for performance.
723 return [ [ 'ipb_hide_invalid',
724 Message
::numParam( $wgHideUserContribLimit ) ] ];
725 } elseif ( !$data['Confirm'] ) {
726 return [ 'ipb-confirmhideuser', 'ipb-confirmaction' ];
730 # Create block object.
731 $block = new Block();
732 $block->setTarget( $target );
733 $block->setBlocker( $performer );
734 $block->mReason
= $data['Reason'][0];
735 $block->mExpiry
= $expiryTime;
736 $block->prevents( 'createaccount', $data['CreateAccount'] );
737 $block->prevents( 'editownusertalk', ( !$wgBlockAllowsUTEdit ||
$data['DisableUTEdit'] ) );
738 $block->prevents( 'sendemail', $data['DisableEmail'] );
739 $block->isHardblock( $data['HardBlock'] );
740 $block->isAutoblocking( $data['AutoBlock'] );
741 $block->mHideName
= $data['HideUser'];
743 $reason = [ 'hookaborted' ];
744 if ( !Hooks
::run( 'BlockIp', [ &$block, &$performer, &$reason ] ) ) {
749 # Try to insert block. Is there a conflicting block?
750 $status = $block->insert();
752 # Indicates whether the user is confirming the block and is aware of
753 # the conflict (did not change the block target in the meantime)
754 $blockNotConfirmed = !$data['Confirm'] ||
( array_key_exists( 'PreviousTarget', $data )
755 && $data['PreviousTarget'] !== $target );
757 # Special case for API - T34434
758 $reblockNotAllowed = ( array_key_exists( 'Reblock', $data ) && !$data['Reblock'] );
760 # Show form unless the user is already aware of this...
761 if ( $blockNotConfirmed ||
$reblockNotAllowed ) {
762 return [ [ 'ipb_already_blocked', $block->getTarget() ] ];
763 # Otherwise, try to update the block...
765 # This returns direct blocks before autoblocks/rangeblocks, since we should
766 # be sure the user is blocked by now it should work for our purposes
767 $currentBlock = Block
::newFromTarget( $target );
768 if ( $block->equals( $currentBlock ) ) {
769 return [ [ 'ipb_already_blocked', $block->getTarget() ] ];
771 # If the name was hidden and the blocking user cannot hide
772 # names, then don't allow any block changes...
773 if ( $currentBlock->mHideName
&& !$performer->isAllowed( 'hideuser' ) ) {
774 return [ 'cant-see-hidden-user' ];
777 $priorBlock = clone $currentBlock;
778 $currentBlock->isHardblock( $block->isHardblock() );
779 $currentBlock->prevents( 'createaccount', $block->prevents( 'createaccount' ) );
780 $currentBlock->mExpiry
= $block->mExpiry
;
781 $currentBlock->isAutoblocking( $block->isAutoblocking() );
782 $currentBlock->mHideName
= $block->mHideName
;
783 $currentBlock->prevents( 'sendemail', $block->prevents( 'sendemail' ) );
784 $currentBlock->prevents( 'editownusertalk', $block->prevents( 'editownusertalk' ) );
785 $currentBlock->mReason
= $block->mReason
;
787 $status = $currentBlock->update();
789 $logaction = 'reblock';
791 # Unset _deleted fields if requested
792 if ( $currentBlock->mHideName
&& !$data['HideUser'] ) {
793 RevisionDeleteUser
::unsuppressUserName( $target, $userId );
796 # If hiding/unhiding a name, this should go in the private logs
797 if ( (bool)$currentBlock->mHideName
) {
798 $data['HideUser'] = true;
802 $logaction = 'block';
805 Hooks
::run( 'BlockIpComplete', [ $block, $performer, $priorBlock ] );
807 # Set *_deleted fields if requested
808 if ( $data['HideUser'] ) {
809 RevisionDeleteUser
::suppressUserName( $target, $userId );
812 # Can't watch a rangeblock
813 if ( $type != Block
::TYPE_RANGE
&& $data['Watch'] ) {
814 WatchAction
::doWatch(
815 Title
::makeTitle( NS_USER
, $target ),
817 User
::IGNORE_USER_RIGHTS
821 # Block constructor sanitizes certain block options on insert
822 $data['BlockEmail'] = $block->prevents( 'sendemail' );
823 $data['AutoBlock'] = $block->isAutoblocking();
825 # Prepare log parameters
827 $logParams['5::duration'] = $data['Expiry'];
828 $logParams['6::flags'] = self
::blockLogFlags( $data, $type );
830 # Make log entry, if the name is hidden, put it in the suppression log
831 $log_type = $data['HideUser'] ?
'suppress' : 'block';
832 $logEntry = new ManualLogEntry( $log_type, $logaction );
833 $logEntry->setTarget( Title
::makeTitle( NS_USER
, $target ) );
834 $logEntry->setComment( $data['Reason'][0] );
835 $logEntry->setPerformer( $performer );
836 $logEntry->setParameters( $logParams );
837 # Relate log ID to block IDs (T27763)
838 $blockIds = array_merge( [ $status['id'] ], $status['autoIds'] );
839 $logEntry->setRelations( [ 'ipb_id' => $blockIds ] );
840 $logId = $logEntry->insert();
842 if ( !empty( $data['Tags'] ) ) {
843 $logEntry->setTags( $data['Tags'] );
846 $logEntry->publish( $logId );
852 * Get an array of suggested block durations from MediaWiki:Ipboptions
853 * @todo FIXME: This uses a rather odd syntax for the options, should it be converted
854 * to the standard "**<duration>|<displayname>" format?
855 * @param Language|null $lang The language to get the durations in, or null to use
856 * the wiki's content language
859 public static function getSuggestedDurations( $lang = null ) {
861 $msg = $lang === null
862 ?
wfMessage( 'ipboptions' )->inContentLanguage()->text()
863 : wfMessage( 'ipboptions' )->inLanguage( $lang )->text();
869 foreach ( explode( ',', $msg ) as $option ) {
870 if ( strpos( $option, ':' ) === false ) {
871 $option = "$option:$option";
874 list( $show, $value ) = explode( ':', $option );
879 // if options exist, add other to the end instead of the begining (which
880 // is what happens by default).
881 $a[ wfMessage( 'ipbother' )->text() ] = 'other';
888 * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
889 * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
891 * @todo strtotime() only accepts English strings. This means the expiry input
892 * can only be specified in English.
893 * @see https://secure.php.net/manual/en/function.strtotime.php
895 * @param string $expiry Whatever was typed into the form
896 * @return string|bool Timestamp or 'infinity' or false on error.
898 public static function parseExpiryInput( $expiry ) {
899 if ( wfIsInfinity( $expiry ) ) {
903 $expiry = strtotime( $expiry );
905 if ( $expiry < 0 ||
$expiry === false ) {
909 return wfTimestamp( TS_MW
, $expiry );
913 * Can we do an email block?
914 * @param User $user The sysop wanting to make a block
917 public static function canBlockEmail( $user ) {
918 global $wgEnableUserEmail, $wgSysopEmailBans;
920 return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
924 * T17810: blocked admins should not be able to block/unblock
925 * others, and probably shouldn't be able to unblock themselves
927 * @param User|int|string $user
928 * @param User $performer User doing the request
929 * @return bool|string True or error message key
931 public static function checkUnblockSelf( $user, User
$performer ) {
932 if ( is_int( $user ) ) {
933 $user = User
::newFromId( $user );
934 } elseif ( is_string( $user ) ) {
935 $user = User
::newFromName( $user );
938 if ( $performer->isBlocked() ) {
939 if ( $user instanceof User
&& $user->getId() == $performer->getId() ) {
940 # User is trying to unblock themselves
941 if ( $performer->isAllowed( 'unblockself' ) ) {
943 # User blocked themselves and is now trying to reverse it
944 } elseif ( $performer->blockedBy() === $performer->getName() ) {
947 return 'ipbnounblockself';
950 # User is trying to block/unblock someone else
959 * Return a comma-delimited list of "flags" to be passed to the log
960 * reader for this block, to provide more information in the logs
961 * @param array $data From HTMLForm data
962 * @param int $type Block::TYPE_ constant (USER, RANGE, or IP)
965 protected static function blockLogFlags( array $data, $type ) {
966 global $wgBlockAllowsUTEdit;
969 # when blocking a user the option 'anononly' is not available/has no effect
970 # -> do not write this into log
971 if ( !$data['HardBlock'] && $type != Block
::TYPE_USER
) {
972 // For grepping: message block-log-flags-anononly
973 $flags[] = 'anononly';
976 if ( $data['CreateAccount'] ) {
977 // For grepping: message block-log-flags-nocreate
978 $flags[] = 'nocreate';
981 # Same as anononly, this is not displayed when blocking an IP address
982 if ( !$data['AutoBlock'] && $type == Block
::TYPE_USER
) {
983 // For grepping: message block-log-flags-noautoblock
984 $flags[] = 'noautoblock';
987 if ( $data['DisableEmail'] ) {
988 // For grepping: message block-log-flags-noemail
989 $flags[] = 'noemail';
992 if ( $wgBlockAllowsUTEdit && $data['DisableUTEdit'] ) {
993 // For grepping: message block-log-flags-nousertalk
994 $flags[] = 'nousertalk';
997 if ( $data['HideUser'] ) {
998 // For grepping: message block-log-flags-hiddenname
999 $flags[] = 'hiddenname';
1002 return implode( ',', $flags );
1006 * Process the form on POST submission.
1007 * @param array $data
1008 * @param HTMLForm|null $form
1009 * @return bool|array True for success, false for didn't-try, array of errors on failure
1011 public function onSubmit( array $data, HTMLForm
$form = null ) {
1012 return self
::processForm( $data, $form->getContext() );
1016 * Do something exciting on successful processing of the form, most likely to show a
1017 * confirmation message
1019 public function onSuccess() {
1020 $out = $this->getOutput();
1021 $out->setPageTitle( $this->msg( 'blockipsuccesssub' ) );
1022 $out->addWikiMsg( 'blockipsuccesstext', wfEscapeWikiText( $this->target
) );
1026 * Return an array of subpages beginning with $search that this special page will accept.
1028 * @param string $search Prefix to search for
1029 * @param int $limit Maximum number of results to return (usually 10)
1030 * @param int $offset Number of results to skip (usually 0)
1031 * @return string[] Matching subpages
1033 public function prefixSearchSubpages( $search, $limit, $offset ) {
1034 $user = User
::newFromName( $search );
1036 // No prefix suggestion for invalid user
1039 // Autocomplete subpage as user list - public to allow caching
1040 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
1043 protected function getGroupName() {