resourceloader: Add support for variables in WikiModule
authorTimo Tijhof <krinklemail@gmail.com>
Mon, 6 Jun 2016 18:09:32 +0000 (19:09 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Mon, 6 Jun 2016 18:09:32 +0000 (19:09 +0100)
To be used for the 'site' module. This will make it easier to split up
'site' into 'site' and 'site.styles' (per T92459).

Change-Id: Iaac3e458d5107e4c10c2826bd64608d5c47e8b87

includes/resourceloader/ResourceLoaderWikiModule.php
tests/phpunit/ResourceLoaderTestCase.php
tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php

index a3f8825..e3ada8e 100644 (file)
@@ -61,6 +61,9 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
        // Group of module
        protected $group;
 
+       // Whether to enable variable expansion (e.g. "{skin}")
+       protected $allowVariables = false;
+
        /**
         * @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
         */
@@ -76,6 +79,7 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                                case 'scripts':
                                case 'group':
                                case 'targets':
+                               case 'allowVariables':
                                        $this->{$member} = $option;
                                        break;
                        }
@@ -105,19 +109,30 @@ class ResourceLoaderWikiModule extends ResourceLoaderModule {
                // Filter out pages from origins not allowed by the current wiki configuration.
                if ( $config->get( 'UseSiteJs' ) ) {
                        foreach ( $this->scripts as $script ) {
-                               $pages[$script] = [ 'type' => 'script' ];
+                               $page = $this->expandVariables( $context, $script );
+                               $pages[$page] = [ 'type' => 'script' ];
                        }
                }
 
                if ( $config->get( 'UseSiteCss' ) ) {
                        foreach ( $this->styles as $style ) {
-                               $pages[$style] = [ 'type' => 'style' ];
+                               $page = $this->expandVariables( $context, $style );
+                               $pages[$page] = [ 'type' => 'style' ];
                        }
                }
 
                return $pages;
        }
 
+       private function expandVariables( ResourceLoaderContext $context, $pageName ) {
+               if ( !$this->allowVariables ) {
+                       return $pageName;
+               }
+               return strtr( $pageName, [
+                       '{skin}' => $context->getSkin()
+               ] );
+       }
+
        /**
         * Get group name
         *
index 85bf954..275ed27 100644 (file)
@@ -30,6 +30,9 @@ abstract class ResourceLoaderTestCase extends MediaWikiTestCase {
                        // For ResourceLoader::inDebugMode since it doesn't have context
                        'ResourceLoaderDebug' => true,
 
+                       // For ResourceLoaderContext::newDummyContext()
+                       'DefaultSkin' => 'vector',
+
                        // Avoid influence from wgInvalidateCacheOnLocalSettingsChange
                        'CacheEpoch' => '20140101000000',
 
index 85834d7..e6bb5a7 100644 (file)
@@ -49,6 +49,12 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                        'scripts' => [ 'MediaWiki:Common.js' ],
                ];
 
+               $variableParams = [
+                       'allowVariables' => true,
+                       'styles' => [ 'MediaWiki:Common.css', 'MediaWiki:{skin}.css' ],
+                       'scripts' => [ 'MediaWiki:Common.js', 'MediaWiki:{skin}.js' ],
+               ];
+
                return [
                        [ [], new HashConfig( $settings ), [] ],
                        [ $params, new HashConfig( $settings ), [
@@ -67,6 +73,12 @@ class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
                                ),
                                []
                        ],
+                       [ $variableParams, new HashConfig( $settings ), [
+                               'MediaWiki:Common.js' => [ 'type' => 'script' ],
+                               'MediaWiki:vector.js' => [ 'type' => 'script' ],
+                               'MediaWiki:Common.css' => [ 'type' => 'style' ],
+                               'MediaWiki:vector.css' => [ 'type' => 'style' ]
+                       ] ],
                ];
        }