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();
143 'label-message' => 'ipaddressorusername',
144 'id' => 'mw-bi-target',
148 'validation-callback' => [ __CLASS__
, 'validateTargetField' ],
151 'type' => !count( $suggestedDurations ) ?
'text' : 'selectorother',
152 'label-message' => 'ipbexpiry',
154 'options' => $suggestedDurations,
155 'other' => $this->msg( 'ipbother' )->text(),
156 'default' => $this->msg( 'ipb-default-expiry' )->inContentLanguage()->text(),
159 'type' => 'selectandother',
161 'label-message' => 'ipbreason',
162 'options-message' => 'ipbreason-dropdown',
166 'label-message' => 'ipbcreateaccount',
171 if ( self
::canBlockEmail( $user ) ) {
172 $a['DisableEmail'] = [
174 'label-message' => 'ipbemailban',
178 if ( $wgBlockAllowsUTEdit ) {
179 $a['DisableUTEdit'] = [
181 'label-message' => 'ipb-disableusertalk',
188 'label-message' => 'ipbenableautoblock',
192 # Allow some users to hide name from block log, blocklist and listusers
193 if ( $user->isAllowed( 'hideuser' ) ) {
196 'label-message' => 'ipbhidename',
197 'cssclass' => 'mw-block-hideuser',
201 # Watchlist their user page? (Only if user is logged in)
202 if ( $user->isLoggedIn() ) {
205 'label-message' => 'ipbwatchuser',
211 'label-message' => 'ipb-hardblock',
215 # This is basically a copy of the Target field, but the user can't change it, so we
216 # can see if the warnings we maybe showed to the user before still apply
217 $a['PreviousTarget'] = [
222 # We'll turn this into a checkbox if we need to
226 'label-message' => 'ipb-confirm',
229 $this->maybeAlterFormDefaults( $a );
231 // Allow extensions to add more fields
232 Hooks
::run( 'SpecialBlockModifyFormFields', [ $this, &$a ] );
238 * If the user has already been blocked with similar settings, load that block
239 * and change the defaults for the form fields to match the existing settings.
240 * @param array &$fields HTMLForm descriptor array
241 * @return bool Whether fields were altered (that is, whether the target is
244 protected function maybeAlterFormDefaults( &$fields ) {
245 # This will be overwritten by request data
246 $fields['Target']['default'] = (string)$this->target
;
248 if ( $this->target
) {
249 $status = self
::validateTarget( $this->target
, $this->getUser() );
250 if ( !$status->isOK() ) {
251 $errors = $status->getErrorsArray();
252 $this->preErrors
= array_merge( $this->preErrors
, $errors );
257 $fields['PreviousTarget']['default'] = (string)$this->target
;
259 $block = Block
::newFromTarget( $this->target
);
261 if ( $block instanceof Block
&& !$block->mAuto
# The block exists and isn't an autoblock
262 && ( $this->type
!= Block
::TYPE_RANGE
# The block isn't a rangeblock
263 ||
$block->getTarget() == $this->target
) # or if it is, the range is what we're about to block
265 $fields['HardBlock']['default'] = $block->isHardblock();
266 $fields['CreateAccount']['default'] = $block->prevents( 'createaccount' );
267 $fields['AutoBlock']['default'] = $block->isAutoblocking();
269 if ( isset( $fields['DisableEmail'] ) ) {
270 $fields['DisableEmail']['default'] = $block->prevents( 'sendemail' );
273 if ( isset( $fields['HideUser'] ) ) {
274 $fields['HideUser']['default'] = $block->mHideName
;
277 if ( isset( $fields['DisableUTEdit'] ) ) {
278 $fields['DisableUTEdit']['default'] = $block->prevents( 'editownusertalk' );
281 // If the username was hidden (ipb_deleted == 1), don't show the reason
282 // unless this user also has rights to hideuser: T37839
283 if ( !$block->mHideName ||
$this->getUser()->isAllowed( 'hideuser' ) ) {
284 $fields['Reason']['default'] = $block->mReason
;
286 $fields['Reason']['default'] = '';
289 if ( $this->getRequest()->wasPosted() ) {
290 # Ok, so we got a POST submission asking us to reblock a user. So show the
291 # confirm checkbox; the user will only see it if they haven't previously
292 $fields['Confirm']['type'] = 'check';
294 # We got a target, but it wasn't a POST request, so the user must have gone
295 # to a link like [[Special:Block/User]]. We don't need to show the checkbox
296 # as long as they go ahead and block *that* user
297 $fields['Confirm']['default'] = 1;
300 if ( $block->mExpiry
== 'infinity' ) {
301 $fields['Expiry']['default'] = 'infinite';
303 $fields['Expiry']['default'] = wfTimestamp( TS_RFC2822
, $block->mExpiry
);
306 $this->alreadyBlocked
= true;
307 $this->preErrors
[] = [ 'ipb-needreblock', wfEscapeWikiText( (string)$block->getTarget() ) ];
310 # We always need confirmation to do HideUser
311 if ( $this->requestedHideUser
) {
312 $fields['Confirm']['type'] = 'check';
313 unset( $fields['Confirm']['default'] );
314 $this->preErrors
[] = [ 'ipb-confirmhideuser', 'ipb-confirmaction' ];
317 # Or if the user is trying to block themselves
318 if ( (string)$this->target
=== $this->getUser()->getName() ) {
319 $fields['Confirm']['type'] = 'check';
320 unset( $fields['Confirm']['default'] );
321 $this->preErrors
[] = [ 'ipb-blockingself', 'ipb-confirmaction' ];
326 * Add header elements like block log entries, etc.
329 protected function preText() {
330 $this->getOutput()->addModules( [ 'mediawiki.special.block' ] );
332 $blockCIDRLimit = $this->getConfig()->get( 'BlockCIDRLimit' );
333 $text = $this->msg( 'blockiptext', $blockCIDRLimit['IPv4'], $blockCIDRLimit['IPv6'] )->parse();
335 $otherBlockMessages = [];
336 if ( $this->target
!== null ) {
337 $targetName = $this->target
;
338 if ( $this->target
instanceof User
) {
339 $targetName = $this->target
->getName();
341 # Get other blocks, i.e. from GlobalBlocking or TorBlock extension
342 Hooks
::run( 'OtherBlockLogLink', [ &$otherBlockMessages, $targetName ] );
344 if ( count( $otherBlockMessages ) ) {
345 $s = Html
::rawElement(
348 $this->msg( 'ipb-otherblocks-header', count( $otherBlockMessages ) )->parse()
353 foreach ( $otherBlockMessages as $link ) {
354 $list .= Html
::rawElement( 'li', [], $link ) . "\n";
357 $s .= Html
::rawElement(
359 [ 'class' => 'mw-blockip-alreadyblocked' ],
371 * Add footer elements to the form
374 protected function postText() {
377 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
379 $linkRenderer = $this->getLinkRenderer();
380 # Link to the user's contributions, if applicable
381 if ( $this->target
instanceof User
) {
382 $contribsPage = SpecialPage
::getTitleFor( 'Contributions', $this->target
->getName() );
383 $links[] = $linkRenderer->makeLink(
385 $this->msg( 'ipb-blocklist-contribs', $this->target
->getName() )->text()
389 # Link to unblock the specified user, or to a blank unblock form
390 if ( $this->target
instanceof User
) {
391 $message = $this->msg(
393 wfEscapeWikiText( $this->target
->getName() )
395 $list = SpecialPage
::getTitleFor( 'Unblock', $this->target
->getName() );
397 $message = $this->msg( 'ipb-unblock' )->parse();
398 $list = SpecialPage
::getTitleFor( 'Unblock' );
400 $links[] = $linkRenderer->makeKnownLink(
402 new HtmlArmor( $message )
405 # Link to the block list
406 $links[] = $linkRenderer->makeKnownLink(
407 SpecialPage
::getTitleFor( 'BlockList' ),
408 $this->msg( 'ipb-blocklist' )->text()
411 $user = $this->getUser();
413 # Link to edit the block dropdown reasons, if applicable
414 if ( $user->isAllowed( 'editinterface' ) ) {
415 $links[] = $linkRenderer->makeKnownLink(
416 $this->msg( 'ipbreason-dropdown' )->inContentLanguage()->getTitle(),
417 $this->msg( 'ipb-edit-dropdown' )->text(),
419 [ 'action' => 'edit' ]
423 $text = Html
::rawElement(
425 [ 'class' => 'mw-ipb-conveniencelinks' ],
426 $this->getLanguage()->pipeList( $links )
429 $userTitle = self
::getTargetUserTitle( $this->target
);
431 # Get relevant extracts from the block and suppression logs, if possible
434 LogEventsList
::showLogExtract(
441 'msgKey' => [ 'blocklog-showlog', $userTitle->getText() ],
442 'showIfEmpty' => false
447 # Add suppression block entries if allowed
448 if ( $user->isAllowed( 'suppressionlog' ) ) {
449 LogEventsList
::showLogExtract(
456 'conds' => [ 'log_action' => [ 'block', 'reblock', 'unblock' ] ],
457 'msgKey' => [ 'blocklog-showsuppresslog', $userTitle->getText() ],
458 'showIfEmpty' => false
470 * Get a user page target for things like logs.
471 * This handles account and IP range targets.
472 * @param User|string $target
475 protected static function getTargetUserTitle( $target ) {
476 if ( $target instanceof User
) {
477 return $target->getUserPage();
478 } elseif ( IP
::isIPAddress( $target ) ) {
479 return Title
::makeTitleSafe( NS_USER
, $target );
486 * Determine the target of the block, and the type of target
487 * @todo Should be in Block.php?
488 * @param string $par Subpage parameter passed to setup, or data value from
490 * @param WebRequest $request Optionally try and get data from a request too
491 * @return array [ User|string|null, Block::TYPE_ constant|null ]
493 public static function getTargetAndType( $par, WebRequest
$request = null ) {
500 # The HTMLForm will check wpTarget first and only if it doesn't get
501 # a value use the default, which will be generated from the options
502 # below; so this has to have a higher precedence here than $par, or
503 # we could end up with different values in $this->target and the HTMLForm!
504 if ( $request instanceof WebRequest
) {
505 $target = $request->getText( 'wpTarget', null );
512 if ( $request instanceof WebRequest
) {
513 $target = $request->getText( 'ip', null );
518 if ( $request instanceof WebRequest
) {
519 $target = $request->getText( 'wpBlockAddress', null );
526 list( $target, $type ) = Block
::parseTarget( $target );
528 if ( $type !== null ) {
529 return [ $target, $type ];
533 return [ null, null ];
537 * HTMLForm field validation-callback for Target field.
539 * @param string $value
540 * @param array $alldata
541 * @param HTMLForm $form
544 public static function validateTargetField( $value, $alldata, $form ) {
545 $status = self
::validateTarget( $value, $form->getUser() );
546 if ( !$status->isOK() ) {
547 $errors = $status->getErrorsArray();
549 return call_user_func_array( [ $form, 'msg' ], $errors[0] );
556 * Validate a block target.
559 * @param string $value Block target to check
560 * @param User $user Performer of the block
563 public static function validateTarget( $value, User
$user ) {
564 global $wgBlockCIDRLimit;
566 /** @var User $target */
567 list( $target, $type ) = self
::getTargetAndType( $value );
568 $status = Status
::newGood( $target );
570 if ( $type == Block
::TYPE_USER
) {
571 if ( $target->isAnon() ) {
574 wfEscapeWikiText( $target->getName() )
578 $unblockStatus = self
::checkUnblockSelf( $target, $user );
579 if ( $unblockStatus !== true ) {
580 $status->fatal( 'badaccess', $unblockStatus );
582 } elseif ( $type == Block
::TYPE_RANGE
) {
583 list( $ip, $range ) = explode( '/', $target, 2 );
586 ( IP
::isIPv4( $ip ) && $wgBlockCIDRLimit['IPv4'] == 32 ) ||
587 ( IP
::isIPv6( $ip ) && $wgBlockCIDRLimit['IPv6'] == 128 )
589 // Range block effectively disabled
590 $status->fatal( 'range_block_disabled' );
594 ( IP
::isIPv4( $ip ) && $range > 32 ) ||
595 ( IP
::isIPv6( $ip ) && $range > 128 )
598 $status->fatal( 'ip_range_invalid' );
601 if ( IP
::isIPv4( $ip ) && $range < $wgBlockCIDRLimit['IPv4'] ) {
602 $status->fatal( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv4'] );
605 if ( IP
::isIPv6( $ip ) && $range < $wgBlockCIDRLimit['IPv6'] ) {
606 $status->fatal( 'ip_range_toolarge', $wgBlockCIDRLimit['IPv6'] );
608 } elseif ( $type == Block
::TYPE_IP
) {
611 $status->fatal( 'badipaddress' );
618 * Given the form data, actually implement a block. This is also called from ApiBlock.
621 * @param IContextSource $context
622 * @return bool|string
624 public static function processForm( array $data, IContextSource
$context ) {
625 global $wgBlockAllowsUTEdit, $wgHideUserContribLimit;
627 $performer = $context->getUser();
629 // Handled by field validator callback
630 // self::validateTargetField( $data['Target'] );
632 # This might have been a hidden field or a checkbox, so interesting data
634 $data['Confirm'] = !in_array( $data['Confirm'], [ '', '0', null, false ], true );
636 /** @var User $target */
637 list( $target, $type ) = self
::getTargetAndType( $data['Target'] );
638 if ( $type == Block
::TYPE_USER
) {
640 $target = $user->getName();
641 $userId = $user->getId();
643 # Give admins a heads-up before they go and block themselves. Much messier
644 # to do this for IPs, but it's pretty unlikely they'd ever get the 'block'
645 # permission anyway, although the code does allow for it.
646 # Note: Important to use $target instead of $data['Target']
647 # since both $data['PreviousTarget'] and $target are normalized
648 # but $data['target'] gets overridden by (non-normalized) request variable
649 # from previous request.
650 if ( $target === $performer->getName() &&
651 ( $data['PreviousTarget'] !== $target ||
!$data['Confirm'] )
653 return [ 'ipb-blockingself', 'ipb-confirmaction' ];
655 } elseif ( $type == Block
::TYPE_RANGE
) {
658 } elseif ( $type == Block
::TYPE_IP
) {
660 $target = $target->getName();
663 # This should have been caught in the form field validation
664 return [ 'badipaddress' ];
667 $expiryTime = self
::parseExpiryInput( $data['Expiry'] );
670 // an expiry time is needed
671 ( strlen( $data['Expiry'] ) == 0 ) ||
672 // can't be a larger string as 50 (it should be a time format in any way)
673 ( strlen( $data['Expiry'] ) > 50 ) ||
674 // check, if the time could be parsed
677 return [ 'ipb_expiry_invalid' ];
680 // an expiry time should be in the future, not in the
681 // past (wouldn't make any sense) - bug T123069
682 if ( $expiryTime < wfTimestampNow() ) {
683 return [ 'ipb_expiry_old' ];
686 if ( !isset( $data['DisableEmail'] ) ) {
687 $data['DisableEmail'] = false;
690 # If the user has done the form 'properly', they won't even have been given the
691 # option to suppress-block unless they have the 'hideuser' permission
692 if ( !isset( $data['HideUser'] ) ) {
693 $data['HideUser'] = false;
696 if ( $data['HideUser'] ) {
697 if ( !$performer->isAllowed( 'hideuser' ) ) {
698 # this codepath is unreachable except by a malicious user spoofing forms,
699 # or by race conditions (user has hideuser and block rights, loads block form,
700 # and loses hideuser rights before submission); so need to fail completely
701 # rather than just silently disable hiding
702 return [ 'badaccess-group0' ];
705 # Recheck params here...
706 if ( $type != Block
::TYPE_USER
) {
707 $data['HideUser'] = false; # IP users should not be hidden
708 } elseif ( !wfIsInfinity( $data['Expiry'] ) ) {
710 return [ 'ipb_expiry_temp' ];
711 } elseif ( $wgHideUserContribLimit !== false
712 && $user->getEditCount() > $wgHideUserContribLimit
714 # Typically, the user should have a handful of edits.
715 # Disallow hiding users with many edits for performance.
716 return [ [ 'ipb_hide_invalid',
717 Message
::numParam( $wgHideUserContribLimit ) ] ];
718 } elseif ( !$data['Confirm'] ) {
719 return [ 'ipb-confirmhideuser', 'ipb-confirmaction' ];
723 # Create block object.
724 $block = new Block();
725 $block->setTarget( $target );
726 $block->setBlocker( $performer );
727 $block->mReason
= $data['Reason'][0];
728 $block->mExpiry
= $expiryTime;
729 $block->prevents( 'createaccount', $data['CreateAccount'] );
730 $block->prevents( 'editownusertalk', ( !$wgBlockAllowsUTEdit ||
$data['DisableUTEdit'] ) );
731 $block->prevents( 'sendemail', $data['DisableEmail'] );
732 $block->isHardblock( $data['HardBlock'] );
733 $block->isAutoblocking( $data['AutoBlock'] );
734 $block->mHideName
= $data['HideUser'];
736 $reason = [ 'hookaborted' ];
737 if ( !Hooks
::run( 'BlockIp', [ &$block, &$performer, &$reason ] ) ) {
742 # Try to insert block. Is there a conflicting block?
743 $status = $block->insert();
745 # Indicates whether the user is confirming the block and is aware of
746 # the conflict (did not change the block target in the meantime)
747 $blockNotConfirmed = !$data['Confirm'] ||
( array_key_exists( 'PreviousTarget', $data )
748 && $data['PreviousTarget'] !== $target );
750 # Special case for API - T34434
751 $reblockNotAllowed = ( array_key_exists( 'Reblock', $data ) && !$data['Reblock'] );
753 # Show form unless the user is already aware of this...
754 if ( $blockNotConfirmed ||
$reblockNotAllowed ) {
755 return [ [ 'ipb_already_blocked', $block->getTarget() ] ];
756 # Otherwise, try to update the block...
758 # This returns direct blocks before autoblocks/rangeblocks, since we should
759 # be sure the user is blocked by now it should work for our purposes
760 $currentBlock = Block
::newFromTarget( $target );
761 if ( $block->equals( $currentBlock ) ) {
762 return [ [ 'ipb_already_blocked', $block->getTarget() ] ];
764 # If the name was hidden and the blocking user cannot hide
765 # names, then don't allow any block changes...
766 if ( $currentBlock->mHideName
&& !$performer->isAllowed( 'hideuser' ) ) {
767 return [ 'cant-see-hidden-user' ];
770 $priorBlock = clone $currentBlock;
771 $currentBlock->isHardblock( $block->isHardblock() );
772 $currentBlock->prevents( 'createaccount', $block->prevents( 'createaccount' ) );
773 $currentBlock->mExpiry
= $block->mExpiry
;
774 $currentBlock->isAutoblocking( $block->isAutoblocking() );
775 $currentBlock->mHideName
= $block->mHideName
;
776 $currentBlock->prevents( 'sendemail', $block->prevents( 'sendemail' ) );
777 $currentBlock->prevents( 'editownusertalk', $block->prevents( 'editownusertalk' ) );
778 $currentBlock->mReason
= $block->mReason
;
780 $status = $currentBlock->update();
782 $logaction = 'reblock';
784 # Unset _deleted fields if requested
785 if ( $currentBlock->mHideName
&& !$data['HideUser'] ) {
786 RevisionDeleteUser
::unsuppressUserName( $target, $userId );
789 # If hiding/unhiding a name, this should go in the private logs
790 if ( (bool)$currentBlock->mHideName
) {
791 $data['HideUser'] = true;
795 $logaction = 'block';
798 Hooks
::run( 'BlockIpComplete', [ $block, $performer, $priorBlock ] );
800 # Set *_deleted fields if requested
801 if ( $data['HideUser'] ) {
802 RevisionDeleteUser
::suppressUserName( $target, $userId );
805 # Can't watch a rangeblock
806 if ( $type != Block
::TYPE_RANGE
&& $data['Watch'] ) {
807 WatchAction
::doWatch(
808 Title
::makeTitle( NS_USER
, $target ),
810 User
::IGNORE_USER_RIGHTS
814 # Block constructor sanitizes certain block options on insert
815 $data['BlockEmail'] = $block->prevents( 'sendemail' );
816 $data['AutoBlock'] = $block->isAutoblocking();
818 # Prepare log parameters
820 $logParams['5::duration'] = $data['Expiry'];
821 $logParams['6::flags'] = self
::blockLogFlags( $data, $type );
823 # Make log entry, if the name is hidden, put it in the suppression log
824 $log_type = $data['HideUser'] ?
'suppress' : 'block';
825 $logEntry = new ManualLogEntry( $log_type, $logaction );
826 $logEntry->setTarget( Title
::makeTitle( NS_USER
, $target ) );
827 $logEntry->setComment( $data['Reason'][0] );
828 $logEntry->setPerformer( $performer );
829 $logEntry->setParameters( $logParams );
830 # Relate log ID to block IDs (T27763)
831 $blockIds = array_merge( [ $status['id'] ], $status['autoIds'] );
832 $logEntry->setRelations( [ 'ipb_id' => $blockIds ] );
833 $logId = $logEntry->insert();
835 if ( !empty( $data['Tags'] ) ) {
836 $logEntry->setTags( $data['Tags'] );
839 $logEntry->publish( $logId );
845 * Get an array of suggested block durations from MediaWiki:Ipboptions
846 * @todo FIXME: This uses a rather odd syntax for the options, should it be converted
847 * to the standard "**<duration>|<displayname>" format?
848 * @param Language|null $lang The language to get the durations in, or null to use
849 * the wiki's content language
852 public static function getSuggestedDurations( $lang = null ) {
854 $msg = $lang === null
855 ?
wfMessage( 'ipboptions' )->inContentLanguage()->text()
856 : wfMessage( 'ipboptions' )->inLanguage( $lang )->text();
862 foreach ( explode( ',', $msg ) as $option ) {
863 if ( strpos( $option, ':' ) === false ) {
864 $option = "$option:$option";
867 list( $show, $value ) = explode( ':', $option );
875 * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
876 * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
877 * @param string $expiry Whatever was typed into the form
878 * @return string Timestamp or 'infinity'
880 public static function parseExpiryInput( $expiry ) {
881 if ( wfIsInfinity( $expiry ) ) {
882 $expiry = 'infinity';
884 $expiry = strtotime( $expiry );
886 if ( $expiry < 0 ||
$expiry === false ) {
890 $expiry = wfTimestamp( TS_MW
, $expiry );
897 * Can we do an email block?
898 * @param User $user The sysop wanting to make a block
901 public static function canBlockEmail( $user ) {
902 global $wgEnableUserEmail, $wgSysopEmailBans;
904 return ( $wgEnableUserEmail && $wgSysopEmailBans && $user->isAllowed( 'blockemail' ) );
908 * T17810: blocked admins should not be able to block/unblock
909 * others, and probably shouldn't be able to unblock themselves
911 * @param User|int|string $user
912 * @param User $performer User doing the request
913 * @return bool|string True or error message key
915 public static function checkUnblockSelf( $user, User
$performer ) {
916 if ( is_int( $user ) ) {
917 $user = User
::newFromId( $user );
918 } elseif ( is_string( $user ) ) {
919 $user = User
::newFromName( $user );
922 if ( $performer->isBlocked() ) {
923 if ( $user instanceof User
&& $user->getId() == $performer->getId() ) {
924 # User is trying to unblock themselves
925 if ( $performer->isAllowed( 'unblockself' ) ) {
927 # User blocked themselves and is now trying to reverse it
928 } elseif ( $performer->blockedBy() === $performer->getName() ) {
931 return 'ipbnounblockself';
934 # User is trying to block/unblock someone else
943 * Return a comma-delimited list of "flags" to be passed to the log
944 * reader for this block, to provide more information in the logs
945 * @param array $data From HTMLForm data
946 * @param int $type Block::TYPE_ constant (USER, RANGE, or IP)
949 protected static function blockLogFlags( array $data, $type ) {
950 global $wgBlockAllowsUTEdit;
953 # when blocking a user the option 'anononly' is not available/has no effect
954 # -> do not write this into log
955 if ( !$data['HardBlock'] && $type != Block
::TYPE_USER
) {
956 // For grepping: message block-log-flags-anononly
957 $flags[] = 'anononly';
960 if ( $data['CreateAccount'] ) {
961 // For grepping: message block-log-flags-nocreate
962 $flags[] = 'nocreate';
965 # Same as anononly, this is not displayed when blocking an IP address
966 if ( !$data['AutoBlock'] && $type == Block
::TYPE_USER
) {
967 // For grepping: message block-log-flags-noautoblock
968 $flags[] = 'noautoblock';
971 if ( $data['DisableEmail'] ) {
972 // For grepping: message block-log-flags-noemail
973 $flags[] = 'noemail';
976 if ( $wgBlockAllowsUTEdit && $data['DisableUTEdit'] ) {
977 // For grepping: message block-log-flags-nousertalk
978 $flags[] = 'nousertalk';
981 if ( $data['HideUser'] ) {
982 // For grepping: message block-log-flags-hiddenname
983 $flags[] = 'hiddenname';
986 return implode( ',', $flags );
990 * Process the form on POST submission.
992 * @param HTMLForm $form
993 * @return bool|array True for success, false for didn't-try, array of errors on failure
995 public function onSubmit( array $data, HTMLForm
$form = null ) {
996 return self
::processForm( $data, $form->getContext() );
1000 * Do something exciting on successful processing of the form, most likely to show a
1001 * confirmation message
1003 public function onSuccess() {
1004 $out = $this->getOutput();
1005 $out->setPageTitle( $this->msg( 'blockipsuccesssub' ) );
1006 $out->addWikiMsg( 'blockipsuccesstext', wfEscapeWikiText( $this->target
) );
1010 * Return an array of subpages beginning with $search that this special page will accept.
1012 * @param string $search Prefix to search for
1013 * @param int $limit Maximum number of results to return (usually 10)
1014 * @param int $offset Number of results to skip (usually 0)
1015 * @return string[] Matching subpages
1017 public function prefixSearchSubpages( $search, $limit, $offset ) {
1018 $user = User
::newFromName( $search );
1020 // No prefix suggestion for invalid user
1023 // Autocomplete subpage as user list - public to allow caching
1024 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
1027 protected function getGroupName() {