Simplify checking for widgets on special block page
authorThalia <thalia.e.chan@googlemail.com>
Tue, 2 Apr 2019 10:43:37 +0000 (11:43 +0100)
committerThalia <thalia.e.chan@googlemail.com>
Fri, 5 Apr 2019 14:37:54 +0000 (15:37 +0100)
Certain groups of widgets always exist if the form is loaded, or always exist
if partial blocks is enabled. Others depend on certain user rights or config
variables. Check these widgets in groups rather than checking for the
existence of each one individually.

Change-Id: I6950ebc22f4564aadaf6d6acd5ca36534d8b9962

includes/specials/SpecialBlock.php
resources/Resources.php
resources/src/mediawiki.special.block.js

index b558d5e..f0f6a12 100644 (file)
@@ -141,7 +141,9 @@ class SpecialBlock extends FormSpecialPage {
         * @return array
         */
        protected function getFormFields() {
-               global $wgBlockAllowsUTEdit;
+               $conf = $this->getConfig();
+               $enablePartialBlocks = $conf->get( 'EnablePartialBlocks' );
+               $blockAllowsUTEdit = $conf->get( 'BlockAllowsUTEdit' );
 
                $this->getOutput()->enableOOUI();
 
@@ -149,9 +151,6 @@ class SpecialBlock extends FormSpecialPage {
 
                $suggestedDurations = self::getSuggestedDurations();
 
-               $conf = $this->getConfig();
-               $enablePartialBlocks = $conf->get( 'EnablePartialBlocks' );
-
                $a = [];
 
                $a['Target'] = [
@@ -232,7 +231,7 @@ class SpecialBlock extends FormSpecialPage {
                        ];
                }
 
-               if ( $wgBlockAllowsUTEdit ) {
+               if ( $blockAllowsUTEdit ) {
                        $a['DisableUTEdit'] = [
                                'type' => 'check',
                                'label-message' => 'ipb-disableusertalk',
index af40b73..fedf429 100644 (file)
@@ -2105,7 +2105,15 @@ return [
                ],
        ],
        'mediawiki.special.block' => [
-               'scripts' => 'resources/src/mediawiki.special.block.js',
+               'localBasePath' => "$IP/resources/src",
+               'remoteBasePath' => "$wgResourceBasePath/resources/src",
+               'packageFiles' => [
+                       'mediawiki.special.block.js',
+                       [ 'name' => 'config.json', 'config' => [
+                               'EnablePartialBlocks',
+                               'BlockAllowsUTEdit',
+                       ] ],
+               ],
                'dependencies' => [
                        'oojs-ui-core',
                        'oojs-ui.styles.icons-editing-core',
index b46df85..1767411 100644 (file)
        }
 
        $( function () {
-               // This code is also loaded on the "block succeeded" page where there is no form,
-               // so username and expiry fields might also be missing.
-               var blockTargetWidget = infuseIfExists( $( '#mw-bi-target' ) ),
-                       anonOnlyField = infuseIfExists( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) ),
-                       enableAutoblockField = infuseIfExists( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) ),
-                       hideUserWidget = infuseIfExists( $( '#mw-input-wpHideUser' ) ),
-                       hideUserField = infuseIfExists( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) ),
-                       watchUserField = infuseIfExists( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) ),
-                       expiryWidget = infuseIfExists( $( '#mw-input-wpExpiry' ) ),
-                       editingWidget = infuseIfExists( $( '#mw-input-wpEditing' ) ),
-                       editingRestrictionWidget = infuseIfExists( $( '#mw-input-wpEditingRestriction' ) ),
-                       preventTalkPageEdit = infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) ),
-                       pageRestrictionsWidget = infuseIfExists( $( '#mw-input-wpPageRestrictions' ) ),
-                       namespaceRestrictionsWidget = infuseIfExists( $( '#mw-input-wpNamespaceRestrictions' ) ),
-                       createAccountWidget = infuseIfExists( $( '#mw-input-wpCreateAccount' ) ),
-                       userChangedCreateAccount = mw.config.get( 'wgCreateAccountDirty' ),
-                       updatingBlockOptions = false;
+               var blockTargetWidget, anonOnlyField, enableAutoblockField, hideUserWidget, hideUserField,
+                       watchUserField, expiryWidget, editingWidget, editingRestrictionWidget, preventTalkPageEditWidget,
+                       pageRestrictionsWidget, namespaceRestrictionsWidget, createAccountWidget, data,
+                       enablePartialBlocks, blockAllowsUTEdit, userChangedCreateAccount, updatingBlockOptions;
 
                function updateBlockOptions() {
                        var blocktarget = blockTargetWidget.getValue().trim(),
                                // infinityValues are the values the SpecialBlock class accepts as infinity (sf. wfIsInfinity)
                                infinityValues = [ 'infinite', 'indefinite', 'infinity', 'never' ],
                                isIndefinite = infinityValues.indexOf( expiryValue ) !== -1,
-                               // editingRestrictionWidget only exists if partial blocks is enabled; if not, block must be sitewide
-                               editingRestrictionValue = editingRestrictionWidget ? editingRestrictionWidget.getValue() : 'sitewide',
-                               editingIsSelected = editingWidget ? editingWidget.isSelected() : false,
+                               editingRestrictionValue = enablePartialBlocks ? editingRestrictionWidget.getValue() : 'sitewide',
+                               editingIsSelected = editingWidget.isSelected(),
                                isSitewide = editingIsSelected && editingRestrictionValue === 'sitewide';
 
-                       if ( enableAutoblockField ) {
-                               enableAutoblockField.toggle( !isNonEmptyIp );
-                       }
+                       enableAutoblockField.toggle( !isNonEmptyIp );
+                       anonOnlyField.toggle( isIp || isEmpty );
+
                        if ( hideUserField ) {
                                hideUserField.toggle( !isNonEmptyIp && isIndefinite && isSitewide );
                                if ( !hideUserField.isVisible() ) {
                                        hideUserWidget.setSelected( false );
                                }
                        }
-                       if ( anonOnlyField ) {
-                               anonOnlyField.toggle( isIp || isEmpty );
-                       }
+
                        if ( watchUserField ) {
                                watchUserField.toggle( !isIpRange || isEmpty );
                        }
-                       if ( editingRestrictionWidget ) {
+
+                       if ( enablePartialBlocks ) {
                                editingRestrictionWidget.setDisabled( !editingIsSelected );
-                       }
-                       if ( pageRestrictionsWidget ) {
                                pageRestrictionsWidget.setDisabled( !editingIsSelected || isSitewide );
-                       }
-                       if ( namespaceRestrictionsWidget ) {
                                namespaceRestrictionsWidget.setDisabled( !editingIsSelected || isSitewide );
+                               if ( blockAllowsUTEdit ) {
+                                       // This option is disabled for partial blocks unless a namespace restriction
+                                       // for the User_talk namespace is in place.
+                                       preventTalkPageEditWidget.setDisabled(
+                                               editingIsSelected &&
+                                               editingRestrictionValue === 'partial' &&
+                                               namespaceRestrictionsWidget.getValue().indexOf(
+                                                       String( mw.config.get( 'wgNamespaceIds' ).user_talk )
+                                               ) === -1
+                                       );
+                               }
                        }
-                       if ( preventTalkPageEdit && namespaceRestrictionsWidget ) {
-                               // This option is disabled for partial blocks unless a namespace restriction
-                               // for the User_talk namespace is in place.
-                               preventTalkPageEdit.setDisabled(
-                                       editingIsSelected &&
-                                       editingRestrictionValue === 'partial' &&
-                                       namespaceRestrictionsWidget.getValue().indexOf(
-                                               String( mw.config.get( 'wgNamespaceIds' ).user_talk )
-                                       ) === -1
-                               );
-                       }
+
                        if ( !userChangedCreateAccount ) {
                                updatingBlockOptions = true;
                                createAccountWidget.setSelected( isSitewide );
 
                }
 
+               // This code is also loaded on the "block succeeded" page where there is no form,
+               // so check for block target widget; if it exists, the form is present
+               blockTargetWidget = infuseIfExists( $( '#mw-bi-target' ) );
+
                if ( blockTargetWidget ) {
-                       // Bind functions so they're checked whenever stuff changes
+                       data = require( './config.json' );
+                       enablePartialBlocks = data.EnablePartialBlocks;
+                       blockAllowsUTEdit = data.BlockAllowsUTEdit;
+                       userChangedCreateAccount = mw.config.get( 'wgCreateAccountDirty' );
+                       updatingBlockOptions = false;
+
+                       // Always present if blockTargetWidget is present
+                       editingWidget = OO.ui.infuse( $( '#mw-input-wpEditing' ) );
+                       expiryWidget = OO.ui.infuse( $( '#mw-input-wpExpiry' ) );
+                       createAccountWidget = OO.ui.infuse( $( '#mw-input-wpCreateAccount' ) );
+                       enableAutoblockField = OO.ui.infuse( $( '#mw-input-wpAutoBlock' ).closest( '.oo-ui-fieldLayout' ) );
+                       anonOnlyField = OO.ui.infuse( $( '#mw-input-wpHardBlock' ).closest( '.oo-ui-fieldLayout' ) );
                        blockTargetWidget.on( 'change', updateBlockOptions );
+                       editingWidget.on( 'change', updateBlockOptions );
                        expiryWidget.on( 'change', updateBlockOptions );
-                       if ( editingWidget ) {
-                               editingWidget.on( 'change', updateBlockOptions );
-                       }
-                       if ( editingRestrictionWidget ) {
-                               editingRestrictionWidget.on( 'change', updateBlockOptions );
-                       }
-                       if ( namespaceRestrictionsWidget ) {
-                               namespaceRestrictionsWidget.on( 'change', updateBlockOptions );
-                       }
-
                        createAccountWidget.on( 'change', function () {
                                if ( !updatingBlockOptions ) {
                                        userChangedCreateAccount = true;
                                }
                        } );
 
-                       // Call them now to set initial state (ie. Special:Block/Foobar?wpBlockExpiry=2+hours)
+                       // Present for certain rights
+                       watchUserField = infuseIfExists( $( '#mw-input-wpWatch' ).closest( '.oo-ui-fieldLayout' ) );
+                       hideUserField = infuseIfExists( $( '#mw-input-wpHideUser' ).closest( '.oo-ui-fieldLayout' ) );
+                       hideUserWidget = infuseIfExists( $( '#mw-input-wpHideUser' ) );
+
+                       // Present for certain global configs
+                       if ( enablePartialBlocks ) {
+                               editingRestrictionWidget = OO.ui.infuse( $( '#mw-input-wpEditingRestriction' ) );
+                               pageRestrictionsWidget = OO.ui.infuse( $( '#mw-input-wpPageRestrictions' ) );
+                               namespaceRestrictionsWidget = OO.ui.infuse( $( '#mw-input-wpNamespaceRestrictions' ) );
+                               editingRestrictionWidget.on( 'change', updateBlockOptions );
+                               namespaceRestrictionsWidget.on( 'change', updateBlockOptions );
+                       }
+                       if ( blockAllowsUTEdit ) {
+                               preventTalkPageEditWidget = infuseIfExists( $( '#mw-input-wpDisableUTEdit' ) );
+                       }
+
                        updateBlockOptions();
                }
        } );