resourceloader: Implement '$pages' parameter to ResourceLoaderWikiModule constructor
authorKunal Mehta <legoktm@gmail.com>
Tue, 21 Oct 2014 00:43:26 +0000 (17:43 -0700)
committerKrinkle <krinklemail@gmail.com>
Tue, 3 Mar 2015 17:17:02 +0000 (17:17 +0000)
This makes it easier for subclasses to use ResourceLoaderWikiModule. Currently
many subclasses of this simply need to override the getPages() method.

UserModule and SiteModule keep their getPages override due to the set of pages
being dependent on context.

Change-Id: I388531398671afacfec36c6c5746d72267b5bdac

autoload.php
includes/resourceloader/ResourceLoaderFilePageModule.php [deleted file]
includes/resourceloader/ResourceLoaderNoscriptModule.php [deleted file]
includes/resourceloader/ResourceLoaderSiteModule.php
includes/resourceloader/ResourceLoaderUserGroupsModule.php
includes/resourceloader/ResourceLoaderUserModule.php
includes/resourceloader/ResourceLoaderWikiModule.php
resources/Resources.php
tests/TestsAutoLoader.php
tests/phpunit/ResourceLoaderTestCase.php
tests/phpunit/includes/resourceloader/ResourceLoaderWikiModuleTest.php

index 90fab64..bf0ba98 100644 (file)
@@ -977,14 +977,12 @@ $wgAutoloadLocalClasses = array(
        'ResourceLoaderContext' => __DIR__ . '/includes/resourceloader/ResourceLoaderContext.php',
        'ResourceLoaderEditToolbarModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderEditToolbarModule.php',
        'ResourceLoaderFileModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderFileModule.php',
-       'ResourceLoaderFilePageModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderFilePageModule.php',
        'ResourceLoaderFilePath' => __DIR__ . '/includes/resourceloader/ResourceLoaderFilePath.php',
        'ResourceLoaderImage' => __DIR__ . '/includes/resourceloader/ResourceLoaderImage.php',
        'ResourceLoaderImageModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderImageModule.php',
        'ResourceLoaderLanguageDataModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderLanguageDataModule.php',
        'ResourceLoaderLanguageNamesModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderLanguageNamesModule.php',
        'ResourceLoaderModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderModule.php',
-       'ResourceLoaderNoscriptModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderNoscriptModule.php',
        'ResourceLoaderSiteModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderSiteModule.php',
        'ResourceLoaderSkinModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderSkinModule.php',
        'ResourceLoaderStartUpModule' => __DIR__ . '/includes/resourceloader/ResourceLoaderStartUpModule.php',
diff --git a/includes/resourceloader/ResourceLoaderFilePageModule.php b/includes/resourceloader/ResourceLoaderFilePageModule.php
deleted file mode 100644 (file)
index 8c7fbe7..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-<?php
-/**
- * Resource loader module for MediaWiki:Filepage.css
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- */
-
-/**
- * ResourceLoader definition for MediaWiki:Filepage.css
- */
-class ResourceLoaderFilePageModule extends ResourceLoaderWikiModule {
-
-       /**
-        * @param ResourceLoaderContext $context
-        * @return array
-        */
-       protected function getPages( ResourceLoaderContext $context ) {
-               return array(
-                       'MediaWiki:Filepage.css' => array( 'type' => 'style' ),
-               );
-       }
-}
diff --git a/includes/resourceloader/ResourceLoaderNoscriptModule.php b/includes/resourceloader/ResourceLoaderNoscriptModule.php
deleted file mode 100644 (file)
index 61927d7..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-<?php
-/**
- * Resource loader for site customizations for users without JavaScript enabled.
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @author Trevor Parscal
- * @author Roan Kattouw
- */
-
-/**
- * Module for site customizations
- */
-class ResourceLoaderNoscriptModule extends ResourceLoaderWikiModule {
-
-       /* Protected Methods */
-
-       /**
-        * Gets list of pages used by this module.  Obviously, it makes absolutely no
-        * sense to include JavaScript files here... :D
-        *
-        * @param ResourceLoaderContext $context
-        *
-        * @return array List of pages
-        */
-       protected function getPages( ResourceLoaderContext $context ) {
-               return array( 'MediaWiki:Noscript.css' => array( 'type' => 'style' ) );
-       }
-
-       /* Methods */
-
-       /**
-        * Gets group name
-        *
-        * @return string Name of group
-        */
-       public function getGroup() {
-               return 'noscript';
-       }
-}
index 35d5c05..19e0bae 100644 (file)
  */
 class ResourceLoaderSiteModule extends ResourceLoaderWikiModule {
 
-       /* Protected Methods */
-
        /**
-        * Gets list of pages used by this module
+        * Get list of pages used by this module
         *
         * @param ResourceLoaderContext $context
-        *
         * @return array List of pages
         */
        protected function getPages( ResourceLoaderContext $context ) {
@@ -51,12 +48,10 @@ class ResourceLoaderSiteModule extends ResourceLoaderWikiModule {
                return $pages;
        }
 
-       /* Methods */
-
        /**
-        * Gets group name
+        * Get group name
         *
-        * @return string Name of group
+        * @return string
         */
        public function getGroup() {
                return 'site';
index ee350cf..417cfce 100644 (file)
  */
 class ResourceLoaderUserGroupsModule extends ResourceLoaderWikiModule {
 
-       /* Protected Members */
-
        protected $origin = self::ORIGIN_USER_SITEWIDE;
        protected $targets = array( 'desktop', 'mobile' );
 
-       /* Protected Methods */
-
        /**
         * @param ResourceLoaderContext $context
         * @return array
@@ -63,9 +59,9 @@ class ResourceLoaderUserGroupsModule extends ResourceLoaderWikiModule {
                return $pages;
        }
 
-       /* Methods */
-
        /**
+        * Get group name
+        *
         * @return string
         */
        public function getGroup() {
index e2d39d0..a097844 100644 (file)
  */
 class ResourceLoaderUserModule extends ResourceLoaderWikiModule {
 
-       /* Protected Members */
-
        protected $origin = self::ORIGIN_USER_INDIVIDUAL;
 
-       /* Protected Methods */
-
        /**
+        * Get list of pages used by this module
+        *
         * @param ResourceLoaderContext $context
-        * @return array
+        * @return array List of pages
         */
        protected function getPages( ResourceLoaderContext $context ) {
                $allowUserJs = $this->getConfig()->get( 'AllowUserJs' );
@@ -74,9 +72,9 @@ class ResourceLoaderUserModule extends ResourceLoaderWikiModule {
                return $pages;
        }
 
-       /* Methods */
-
        /**
+        * Get group name
+        *
         * @return string
         */
        public function getGroup() {
index 1a1a6d0..7b44cc6 100644 (file)
  * because of its dependence on the functionality of
  * Title::isCssJsSubpage.
  */
-abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
+class ResourceLoaderWikiModule extends ResourceLoaderModule {
 
-       /* Protected Members */
-
-       # Origin is user-supplied code
+       // Origin defaults to users with sitewide authority
        protected $origin = self::ORIGIN_USER_SITEWIDE;
 
        // In-object cache for title info
        protected $titleInfo = array();
 
-       /* Abstract Protected Methods */
+       // List of page names that contain CSS
+       protected $styles = array();
+
+       // List of page names that contain JavaScript
+       protected $scripts = array();
+
+       // Group of module
+       protected $group;
+
+       /**
+        * @param array $options For back-compat, this can be omitted in favour of overwriting getPages.
+        */
+       public function __construct( array $options = null ) {
+               if ( isset( $options['styles'] ) ) {
+                       $this->styles = $options['styles'];
+               }
+               if ( isset( $options['scripts'] ) ) {
+                       $this->scripts = $options['scripts'];
+               }
+               if ( isset( $options['group'] ) ) {
+                       $this->group = $options['group'];
+               }
+       }
 
        /**
         * Subclasses should return an associative array of resources in the module.
@@ -57,9 +77,34 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
         * @param ResourceLoaderContext $context
         * @return array
         */
-       abstract protected function getPages( ResourceLoaderContext $context );
+       protected function getPages( ResourceLoaderContext $context ) {
+               $config = $this->getConfig();
+               $pages = array();
+
+               // Filter out pages from origins not allowed by the current wiki configuration.
+               if ( $config->get( 'UseSiteJs' ) ) {
+                       foreach ( $this->scripts as $script ) {
+                               $pages[$script] = array( 'type' => 'script' );
+                       }
+               }
+
+               if ( $config->get( 'UseSiteCss' ) ) {
+                       foreach ( $this->styles as $style ) {
+                               $pages[$style] = array( 'type' => 'style' );
+                       }
+               }
+
+               return $pages;
+       }
 
-       /* Protected Methods */
+       /**
+        * Get group name
+        *
+        * @return string
+        */
+       public function getGroup() {
+               return $this->group;
+       }
 
        /**
         * Get the Database object used in getTitleMTimes(). Defaults to the local slave DB
@@ -105,8 +150,6 @@ abstract class ResourceLoaderWikiModule extends ResourceLoaderModule {
                return $content->serialize( $format );
        }
 
-       /* Methods */
-
        /**
         * @param ResourceLoaderContext $context
         * @return string
index 965c783..0874e68 100644 (file)
@@ -29,12 +29,19 @@ return array(
        /**
         * Special modules who have their own classes
         */
+       'startup' => array( 'class' => 'ResourceLoaderStartUpModule' ),
 
        // Scripts managed by the local wiki (stored in the MediaWiki namespace)
        'site' => array( 'class' => 'ResourceLoaderSiteModule' ),
-       'noscript' => array( 'class' => 'ResourceLoaderNoscriptModule' ),
-       'startup' => array( 'class' => 'ResourceLoaderStartUpModule' ),
-       'filepage' => array( 'class' => 'ResourceLoaderFilePageModule' ),
+       'noscript' => array(
+               'class' => 'ResourceLoaderWikiModule',
+               'styles' => array( 'MediaWiki:Noscript.css' ),
+               'group' => 'noscript',
+       ),
+       'filepage' => array(
+               'class' => 'ResourceLoaderWikiModule',
+               'styles' => array( 'MediaWiki:Filepage.css' ),
+       ),
        'user.groups' => array( 'class' => 'ResourceLoaderUserGroupsModule' ),
 
        // Scripts managed by the current user (stored in their user space)
index 4ed28a8..b410898 100644 (file)
@@ -44,7 +44,6 @@ $wgAutoloadClasses += array(
        'ResourceLoaderTestCase' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'ResourceLoaderTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'ResourceLoaderFileModuleTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
-       'ResourceLoaderWikiModuleTestModule' => "$testDir/phpunit/ResourceLoaderTestCase.php",
        'TestUser' => "$testDir/phpunit/includes/TestUser.php",
        'LessFileCompilationTest' => "$testDir/phpunit/LessFileCompilationTest.php",
 
index 055beb0..deecb31 100644 (file)
@@ -98,10 +98,3 @@ class ResourceLoaderTestModule extends ResourceLoaderModule {
 
 class ResourceLoaderFileModuleTestModule extends ResourceLoaderFileModule {
 }
-
-class ResourceLoaderWikiModuleTestModule extends ResourceLoaderWikiModule {
-       // Override expected via PHPUnit mocks and stubs
-       protected function getPages( ResourceLoaderContext $context ) {
-               return array();
-       }
-}
index 9dc1805..93a3ebb 100644 (file)
@@ -2,12 +2,94 @@
 
 class ResourceLoaderWikiModuleTest extends ResourceLoaderTestCase {
 
+       /**
+        * @covers ResourceLoaderWikiModule::__construct
+        * @dataProvider provideConstructor
+        */
+       public function testConstructor( $params ) {
+               $module = new ResourceLoaderWikiModule( $params );
+               $this->assertInstanceOf( 'ResourceLoaderWikiModule', $module );
+       }
+
+       public static function provideConstructor() {
+               return array(
+                       // Nothing
+                       array( null ),
+                       array( array() ),
+                       // Unrecognized settings
+                       array( array( 'foo' => 'baz' ) ),
+                       // Real settings
+                       array( array( 'scripts' => array( 'MediaWiki:Common.js' ) ) ),
+               );
+       }
+
+       /**
+        * @dataProvider provideGetPages
+        * @covers ResourceLoaderWikiModule::getPages
+        */
+       public function testGetPages( $params, Config $config, $expected ) {
+               $module = new ResourceLoaderWikiModule( $params );
+               $module->setConfig( $config );
+
+               // Use getDefinitionSummary because getPages is protected
+               $summary = $module->getDefinitionSummary( ResourceLoaderContext::newDummyContext() );
+               $this->assertEquals(
+                       $expected,
+                       $summary['pages']
+               );
+       }
+
+       public static function provideGetPages() {
+               $settings = array(
+                       'UseSiteJs' => true,
+                       'UseSiteCss' => true,
+               );
+
+               $params = array(
+                       'styles' => array( 'MediaWiki:Common.css' ),
+                       'scripts' => array( 'MediaWiki:Common.js' ),
+               );
+
+               return array(
+                       array( array(), new HashConfig( $settings ), array() ),
+                       array( $params, new HashConfig( $settings ), array(
+                               'MediaWiki:Common.js' => array( 'type' => 'script' ),
+                               'MediaWiki:Common.css' => array( 'type' => 'style' )
+                       ) ),
+                       array( $params, new HashConfig( array( 'UseSiteCss' => false ) + $settings ), array(
+                               'MediaWiki:Common.js' => array( 'type' => 'script' ),
+                       ) ),
+                       array( $params, new HashConfig( array( 'UseSiteJs' => false ) + $settings ), array(
+                               'MediaWiki:Common.css' => array( 'type' => 'style' ),
+                       ) ),
+                       array( $params, new HashConfig( array( 'UseSiteJs' => false, 'UseSiteCss' => false ) ), array() ),
+               );
+       }
+
+       /**
+        * @covers ResourceLoaderWikiModule::getGroup
+        * @dataProvider provideGetGroup
+        */
+       public function testGetGroup( $params, $expected ) {
+               $module = new ResourceLoaderWikiModule( $params );
+               $this->assertEquals( $expected, $module->getGroup() );
+       }
+
+       public static function provideGetGroup() {
+               return array(
+                       // No group specified
+                       array( array(), null ),
+                       // A random group
+                       array( array( 'group' => 'foobar' ), 'foobar' ),
+               );
+       }
+
        /**
         * @covers ResourceLoaderWikiModule::isKnownEmpty
         * @dataProvider provideIsKnownEmpty
         */
        public function testIsKnownEmpty( $titleInfo, $group, $expected ) {
-               $module = $this->getMockBuilder( 'ResourceLoaderWikiModuleTestModule' )
+               $module = $this->getMockBuilder( 'ResourceLoaderWikiModule' )
                        ->setMethods( array( 'getTitleInfo', 'getGroup' ) )
                        ->getMock();
                $module->expects( $this->any() )