Restore the ability to set $wgResourceModuleSkinStyles for 'oojs-ui-core.styles'
authorBartosz Dziewoński <matma.rex@gmail.com>
Mon, 5 Jun 2017 17:32:23 +0000 (19:32 +0200)
committerBartosz Dziewoński <matma.rex@gmail.com>
Mon, 5 Jun 2017 22:05:17 +0000 (00:05 +0200)
In c8ad83310f425433791102e8a958397f2d701b65, 'oojs-ui-core.styles'
was changed to use ResourceLoaderOOUIFileModule instead of plain
ResourceLoaderFileModule. This had the unintended consequence that
ResourceLoader::isFileModule() no longer returned true for it, and
this $wgResourceModuleSkinStyles no longer worked, breaking styling
in Vector.

Additionally, the new ResourceLoaderOOUIFileModule did not respect
the skinScripts/skinStyles options passed in the module definition
(therefore also those from $wgResourceModuleSkinStyles). Merging
them turns out to be a major pain, but it can be done.

Bug: T167042
Change-Id: I7547bbe996467745c1f0b168b40f27eb95c6238d

includes/resourceloader/ResourceLoader.php
includes/resourceloader/ResourceLoaderOOUIFileModule.php

index d144987..767046b 100644 (file)
@@ -573,7 +573,12 @@ class ResourceLoader implements LoggerAwareInterface {
                        return false;
                }
                $info = $this->moduleInfos[$name];
-               if ( isset( $info['object'] ) || isset( $info['class'] ) ) {
+               if (
+                       isset( $info['object'] ) ||
+                       // This special case is dumb, but we need $wgResourceModuleSkinStyles
+                       // to work for 'oojs-ui-core.styles'. See T167042.
+                       ( isset( $info['class'] ) && $info['class'] !== 'ResourceLoaderOOUIFileModule' )
+               ) {
                        return false;
                }
                return true;
index 135efa7..e97e074 100644 (file)
@@ -29,10 +29,18 @@ class ResourceLoaderOOUIFileModule extends ResourceLoaderFileModule {
 
        public function __construct( $options = [] ) {
                if ( isset( $options[ 'themeScripts' ] ) ) {
-                       $options['skinScripts'] = $this->getSkinSpecific( $options[ 'themeScripts' ], 'scripts' );
+                       $skinScripts = $this->getSkinSpecific( $options[ 'themeScripts' ], 'scripts' );
+                       if ( !isset( $options['skinScripts'] ) ) {
+                               $options['skinScripts'] = [];
+                       }
+                       $this->extendSkinSpecific( $options['skinScripts'], $skinScripts );
                }
                if ( isset( $options[ 'themeStyles' ] ) ) {
-                       $options['skinStyles'] = $this->getSkinSpecific( $options[ 'themeStyles' ], 'styles' );
+                       $skinStyles = $this->getSkinSpecific( $options[ 'themeStyles' ], 'styles' );
+                       if ( !isset( $options['skinStyles'] ) ) {
+                               $options['skinStyles'] = [];
+                       }
+                       $this->extendSkinSpecific( $options['skinStyles'], $skinStyles );
                }
 
                parent::__construct( $options );
@@ -60,4 +68,31 @@ class ResourceLoaderOOUIFileModule extends ResourceLoaderFileModule {
                        }, array_values( $themes ) )
                );
        }
+
+       /**
+        * Prepend the $extraSkinSpecific assoc. array to the $skinSpecific assoc. array.
+        * Both of them represent a 'skinScripts' or 'skinStyles' definition.
+        *
+        * @param array &$skinSpecific
+        * @param array $extraSkinSpecific
+        */
+       private function extendSkinSpecific( &$skinSpecific, $extraSkinSpecific ) {
+               // For each skin where skinStyles/skinScripts are defined, add our ones at the beginning
+               foreach ( $skinSpecific as $skin => $files ) {
+                       if ( !is_array( $files ) ) {
+                               $files = [ $files ];
+                       }
+                       if ( isset( $extraSkinSpecific[$skin] ) ) {
+                               $skinSpecific[$skin] = array_merge( [ $extraSkinSpecific[$skin] ], $files );
+                       } elseif ( isset( $extraSkinSpecific['default'] ) ) {
+                               $skinSpecific[$skin] = array_merge( [ $extraSkinSpecific['default'] ], $files );
+                       }
+               }
+               // Add our remaining skinStyles/skinScripts for skins that did not have them defined
+               foreach ( $extraSkinSpecific as $skin => $file ) {
+                       if ( !isset( $skinSpecific[$skin] ) ) {
+                               $skinSpecific[$skin] = $file;
+                       }
+               }
+       }
 }