specials: Cleanup and fix PHPDoc comments
[lhc/web/wiklou.git] / includes / specials / SpecialBlock.php
index a482720..a816edc 100644 (file)
@@ -23,6 +23,7 @@
 
 use MediaWiki\Block\BlockRestriction;
 use MediaWiki\Block\Restriction\PageRestriction;
+use MediaWiki\Block\Restriction\NamespaceRestriction;
 
 /**
  * A special page that allows users with 'block' right to block users from
@@ -147,7 +148,6 @@ class SpecialBlock extends FormSpecialPage {
                $suggestedDurations = self::getSuggestedDurations();
 
                $conf = $this->getConfig();
-               $oldCommentSchema = $conf->get( 'CommentTableSchemaMigrationStage' ) === MIGRATION_OLD;
                $enablePartialBlocks = $conf->get( 'EnablePartialBlocks' );
 
                $a = [];
@@ -177,8 +177,8 @@ class SpecialBlock extends FormSpecialPage {
                                'type' => 'radio',
                                'cssclass' => 'mw-block-editing-restriction',
                                'options' => [
-                                       $this->msg( 'ipb-sitewide' )->text() => 'sitewide',
-                                       $this->msg( 'ipb-partial' )->text() => 'partial',
+                                       $this->msg( 'ipb-sitewide' )->escaped() => 'sitewide',
+                                       $this->msg( 'ipb-partial' )->escaped() => 'partial',
                                ],
                                'section' => 'actions',
                        ];
@@ -187,8 +187,19 @@ class SpecialBlock extends FormSpecialPage {
                                'label' => $this->msg( 'ipb-pages-label' )->text(),
                                'exists' => true,
                                'max' => 10,
-                               'cssclass' => 'mw-block-page-restrictions',
+                               'cssclass' => 'mw-block-restriction',
                                'showMissing' => false,
+                               'excludeDynamicNamespaces' => true,
+                               'input' => [
+                                       'autocomplete' => false
+                               ],
+                               'section' => 'actions',
+                       ];
+                       $a['NamespaceRestrictions'] = [
+                               'type' => 'namespacesmultiselect',
+                               'label' => $this->msg( 'ipb-namespaces-label' )->text(),
+                               'exists' => true,
+                               'cssclass' => 'mw-block-restriction',
                                'input' => [
                                        'autocomplete' => false
                                ],
@@ -232,8 +243,8 @@ class SpecialBlock extends FormSpecialPage {
                        'type' => 'selectandother',
                        // HTML maxlength uses "UTF-16 code units", which means that characters outside BMP
                        // (e.g. emojis) count for two each. This limit is overridden in JS to instead count
-                       // Unicode codepoints (or 255 UTF-8 bytes for old schema).
-                       'maxlength' => $oldCommentSchema ? 255 : CommentStore::COMMENT_CHARACTER_LIMIT,
+                       // Unicode codepoints.
+                       'maxlength' => CommentStore::COMMENT_CHARACTER_LIMIT,
                        'maxlength-unit' => 'codepoints',
                        'options-message' => 'ipbreason-dropdown',
                        'section' => 'reason',
@@ -299,8 +310,6 @@ class SpecialBlock extends FormSpecialPage {
         * If the user has already been blocked with similar settings, load that block
         * and change the defaults for the form fields to match the existing settings.
         * @param array &$fields HTMLForm descriptor array
-        * @return bool Whether fields were altered (that is, whether the target is
-        *     already blocked)
         */
        protected function maybeAlterFormDefaults( &$fields ) {
                # This will be overwritten by request data
@@ -324,11 +333,11 @@ class SpecialBlock extends FormSpecialPage {
                                || $block->getTarget() == $this->target ) # or if it is, the range is what we're about to block
                ) {
                        $fields['HardBlock']['default'] = $block->isHardblock();
-                       $fields['CreateAccount']['default'] = $block->prevents( 'createaccount' );
+                       $fields['CreateAccount']['default'] = $block->isCreateAccountBlocked();
                        $fields['AutoBlock']['default'] = $block->isAutoblocking();
 
                        if ( isset( $fields['DisableEmail'] ) ) {
-                               $fields['DisableEmail']['default'] = $block->prevents( 'sendemail' );
+                               $fields['DisableEmail']['default'] = $block->isEmailBlocked();
                        }
 
                        if ( isset( $fields['HideUser'] ) ) {
@@ -336,7 +345,7 @@ class SpecialBlock extends FormSpecialPage {
                        }
 
                        if ( isset( $fields['DisableUTEdit'] ) ) {
-                               $fields['DisableUTEdit']['default'] = $block->prevents( 'editownusertalk' );
+                               $fields['DisableUTEdit']['default'] = !$block->isUsertalkEditAllowed();
                        }
 
                        // If the username was hidden (ipb_deleted == 1), don't show the reason
@@ -391,21 +400,33 @@ class SpecialBlock extends FormSpecialPage {
 
                        if ( $block instanceof Block ) {
                                $pageRestrictions = [];
+                               $namespaceRestrictions = [];
                                foreach ( $block->getRestrictions() as $restriction ) {
-                                       if ( $restriction->getType() !== 'page' ) {
-                                               continue;
+                                       switch ( $restriction->getType() ) {
+                                               case PageRestriction::TYPE:
+                                                       if ( $restriction->getTitle() ) {
+                                                               $pageRestrictions[] = $restriction->getTitle()->getPrefixedText();
+                                                       }
+                                                       break;
+                                               case NamespaceRestriction::TYPE:
+                                                       $namespaceRestrictions[] = $restriction->getValue();
+                                                       break;
                                        }
-
-                                       $pageRestrictions[] = $restriction->getTitle()->getPrefixedText();
                                }
 
-                               if ( !$block->isSitewide() && empty( $pageRestrictions ) ) {
+                               if (
+                                       !$block->isSitewide() &&
+                                       empty( $pageRestrictions ) &&
+                                       empty( $namespaceRestrictions )
+                               ) {
                                        $fields['Editing']['default'] = false;
                                }
 
                                // Sort the restrictions so they are in alphabetical order.
                                sort( $pageRestrictions );
                                $fields['PageRestrictions']['default'] = implode( "\n", $pageRestrictions );
+                               sort( $namespaceRestrictions );
+                               $fields['NamespaceRestrictions']['default'] = implode( "\n", $namespaceRestrictions );
                        }
                }
        }
@@ -718,6 +739,9 @@ class SpecialBlock extends FormSpecialPage {
 
                $performer = $context->getUser();
                $enablePartialBlocks = $context->getConfig()->get( 'EnablePartialBlocks' );
+               $isPartialBlock = $enablePartialBlocks &&
+                       isset( $data['EditingRestriction'] ) &&
+                       $data['EditingRestriction'] === 'partial';
 
                // Handled by field validator callback
                // self::validateTargetField( $data['Target'] );
@@ -795,6 +819,10 @@ class SpecialBlock extends FormSpecialPage {
                                return [ 'badaccess-group0' ];
                        }
 
+                       if ( $isPartialBlock ) {
+                               return [ 'ipb_hide_partial' ];
+                       }
+
                        # Recheck params here...
                        if ( $type != Block::TYPE_USER ) {
                                $data['HideUser'] = false; # IP users should not be hidden
@@ -819,19 +847,15 @@ class SpecialBlock extends FormSpecialPage {
                $block->setBlocker( $performer );
                $block->mReason = $data['Reason'][0];
                $block->mExpiry = $expiryTime;
-               $block->prevents( 'createaccount', $data['CreateAccount'] );
-               $block->prevents( 'editownusertalk', ( !$wgBlockAllowsUTEdit || $data['DisableUTEdit'] ) );
-               $block->prevents( 'sendemail', $data['DisableEmail'] );
+               $block->isCreateAccountBlocked( $data['CreateAccount'] );
+               $block->isUsertalkEditAllowed( !$wgBlockAllowsUTEdit || !$data['DisableUTEdit'] );
+               $block->isEmailBlocked( $data['DisableEmail'] );
                $block->isHardblock( $data['HardBlock'] );
                $block->isAutoblocking( $data['AutoBlock'] );
                $block->mHideName = $data['HideUser'];
 
-               if (
-                       $enablePartialBlocks &&
-                       isset( $data['EditingRestriction'] ) &&
-                       $data['EditingRestriction'] === 'partial'
-                ) {
-                        $block->isSitewide( false );
+               if ( $isPartialBlock ) {
+                       $block->isSitewide( false );
                }
 
                $reason = [ 'hookaborted' ];
@@ -839,19 +863,26 @@ class SpecialBlock extends FormSpecialPage {
                        return $reason;
                }
 
-               $restrictions = [];
+               $pageRestrictions = [];
+               $namespaceRestrictions = [];
                if ( $enablePartialBlocks ) {
                        if ( $data['PageRestrictions'] !== '' ) {
-                               $restrictions = array_map( function ( $text ) {
+                               $pageRestrictions = array_map( function ( $text ) {
                                        $title = Title::newFromText( $text );
                                        // Use the link cache since the title has already been loaded when
                                        // the field was validated.
-                                       $restriction = new PageRestriction( 0, $title->getArticleId() );
+                                       $restriction = new PageRestriction( 0, $title->getArticleID() );
                                        $restriction->setTitle( $title );
                                        return $restriction;
                                }, explode( "\n", $data['PageRestrictions'] ) );
                        }
+                       if ( $data['NamespaceRestrictions'] !== '' ) {
+                               $namespaceRestrictions = array_map( function ( $id ) {
+                                       return new NamespaceRestriction( 0, $id );
+                               }, explode( "\n", $data['NamespaceRestrictions'] ) );
+                       }
 
+                       $restrictions = ( array_merge( $pageRestrictions, $namespaceRestrictions ) );
                        $block->setRestrictions( $restrictions );
                }
 
@@ -886,12 +917,12 @@ class SpecialBlock extends FormSpecialPage {
 
                                $priorBlock = clone $currentBlock;
                                $currentBlock->isHardblock( $block->isHardblock() );
-                               $currentBlock->prevents( 'createaccount', $block->prevents( 'createaccount' ) );
+                               $currentBlock->isCreateAccountBlocked( $block->isCreateAccountBlocked() );
                                $currentBlock->mExpiry = $block->mExpiry;
                                $currentBlock->isAutoblocking( $block->isAutoblocking() );
                                $currentBlock->mHideName = $block->mHideName;
-                               $currentBlock->prevents( 'sendemail', $block->prevents( 'sendemail' ) );
-                               $currentBlock->prevents( 'editownusertalk', $block->prevents( 'editownusertalk' ) );
+                               $currentBlock->isEmailBlocked( $block->isEmailBlocked() );
+                               $currentBlock->isUsertalkEditAllowed( $block->isUsertalkEditAllowed() );
                                $currentBlock->mReason = $block->mReason;
 
                                if ( $enablePartialBlocks ) {
@@ -943,7 +974,7 @@ class SpecialBlock extends FormSpecialPage {
                }
 
                # Block constructor sanitizes certain block options on insert
-               $data['BlockEmail'] = $block->prevents( 'sendemail' );
+               $data['BlockEmail'] = $block->isEmailBlocked();
                $data['AutoBlock'] = $block->isAutoblocking();
 
                # Prepare log parameters
@@ -952,10 +983,14 @@ class SpecialBlock extends FormSpecialPage {
                $logParams['6::flags'] = self::blockLogFlags( $data, $type );
                $logParams['sitewide'] = $block->isSitewide();
 
-               if ( $enablePartialBlocks && $data['PageRestrictions'] !== '' ) {
-                       $logParams['7::restrictions'] = [
-                               'pages' => explode( "\n", $data['PageRestrictions'] ),
-                       ];
+               if ( $enablePartialBlocks && !$block->isSitewide() ) {
+                       if ( $data['PageRestrictions'] !== '' ) {
+                               $logParams['7::restrictions']['pages'] = explode( "\n", $data['PageRestrictions'] );
+                       }
+
+                       if ( $data['NamespaceRestrictions'] !== '' ) {
+                               $logParams['7::restrictions']['namespaces'] = explode( "\n", $data['NamespaceRestrictions'] );
+                       }
                }
 
                # Make log entry, if the name is hidden, put it in the suppression log
@@ -1167,6 +1202,7 @@ class SpecialBlock extends FormSpecialPage {
                if ( isset( $data['Editing'] ) && $data['Editing'] === false ) {
                        $data['EditingRestriction'] = 'partial';
                        $data['PageRestrictions'] = '';
+                       $data['NamespaceRestrictions'] = '';
                }
                return self::processForm( $data, $form->getContext() );
        }