Fixes issue in r72695 where ResourceLoaderFileModule::getStyles was iterating twice...
[lhc/web/wiklou.git] / includes / ResourceLoaderModule.php
index febaeb7..cb1977a 100644 (file)
@@ -15,6 +15,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
+ * @file
  * @author Trevor Parscal
  * @author Roan Kattouw
  */
@@ -32,7 +33,8 @@ abstract class ResourceLoaderModule {
        /**
         * Get this module's name. This is set when the module is registered
         * with ResourceLoader::register()
-        * @return mixed Name (string) or null if no name was set
+        *
+        * @return Mixed: name (string) or null if no name was set
         */
        public function getName() {
                return $this->name;
@@ -41,7 +43,8 @@ abstract class ResourceLoaderModule {
        /**
         * Set this module's name. This is called by ResourceLodaer::register()
         * when registering the module. Other code should not call this.
-        * @param $name string Name
+        *
+        * @param $name String: name
         */
        public function setName( $name ) {
                $this->name = $name;
@@ -51,7 +54,8 @@ abstract class ResourceLoaderModule {
         * The maximum number of seconds to cache this module for in the
         * client-side (browser) cache. Override this only if you have a good
         * reason not to use $wgResourceLoaderClientMaxage.
-        * @return int Cache maxage in seconds
+        *
+        * @return Integer: cache maxage in seconds
         */
        public function getClientMaxage() {
                global $wgResourceLoaderClientMaxage;
@@ -62,7 +66,8 @@ abstract class ResourceLoaderModule {
         * The maximum number of seconds to cache this module for in the
         * server-side (Squid / proxy) cache. Override this only if you have a
         * good reason not to use $wgResourceLoaderServerMaxage.
-        * @return int Cache maxage in seconds
+        *
+        * @return Integer: cache maxage in seconds
         */
        public function getServerMaxage() {
                global $wgResourceLoaderServerMaxage;
@@ -81,31 +86,33 @@ abstract class ResourceLoaderModule {
        /**
         * Get all JS for this module for a given language and skin.
         * Includes all relevant JS except loader scripts.
-        * @param $lang string Language code
-        * @param $skin string Skin name
-        * @param $debug bool Whether to include debug scripts
-        * @return string JS
+        *
+        * @param $context ResourceLoaderContext object
+        * @return String: JS
         */
        public abstract function getScript( ResourceLoaderContext $context );
 
        /**
         * Get all CSS for this module for a given skin.
-        * @param $skin string Skin name
-        * @return string CSS
+        *
+        * @param $context ResourceLoaderContext object
+        * @return array: strings of CSS keyed by media type
         */
-       public abstract function getStyle( ResourceLoaderContext $context );
+       public abstract function getStyles( ResourceLoaderContext $context );
 
        /**
         * Get the messages needed for this module.
         *
         * To get a JSON blob with messages, use MessageBlobStore::get()
+        *
         * @return array of message keys. Keys may occur more than once
         */
        public abstract function getMessages();
 
        /**
         * Get the loader JS for this module, if set.
-        * @return mixed Loader JS (string) or false if no custom loader set
+        *
+        * @return Mixed: loader JS (string) or false if no custom loader set
         */
        public abstract function getLoaderScript();
 
@@ -122,7 +129,7 @@ abstract class ResourceLoaderModule {
         *
         * To add dependencies dynamically on the client side, use a custom
         * loader script, see getLoaderScript()
-        * @return array Array of module names (strings)
+        * @return Array of module names (strings)
         */
        public abstract function getDependencies();
 
@@ -132,9 +139,8 @@ abstract class ResourceLoaderModule {
         * the highest of each of the relevant components' modification
         * timestamps. Whenever anything happens that changes the module's
         * contents for these parameters, the mtime should increase.
-        * @param $lang string Language code
-        * @param $skin string Skin name
-        * @param $debug bool Debug mode flag
+        *
+        * @param $context ResourceLoaderContext object
         * @return int UNIX timestamp
         */
        public abstract function getModifiedTime( ResourceLoaderContext $context );
@@ -185,9 +191,11 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *              // Non-raw module options
         *              'dependencies' => 'module' | array( 'module1', 'module2' ... )
         *              'loaderScripts' => 'dir/loader.js' | array( 'dir/loader1.js', 'dir/loader2.js' ... ),
-        *              'styles' => 'dir/file.css' | array( 'dir/file1.css', 'dir/file2.css' ... ),
+        *              'styles' => 'dir/file.css' | array( 'dir/file1.css', 'dir/file2.css' ... ), |
+        *                      array( 'dir/file1.css' => array( 'media' => 'print' ) ),
         *              'skinStyles' => array(
-        *                      '[skin name]' => 'dir/skin.css' | '[skin name]' => array( 'dir/skin1.css', 'dir/skin2.css' ... )
+        *                      '[skin name]' => 'dir/skin.css' |  array( 'dir/skin1.css', 'dir/skin2.css' ... ) |
+        *                              array( 'dir/file1.css' => array( 'media' => 'print' )
         *                      ...
         *              ),
         *              'messages' => array( 'message1', 'message2' ... ),
@@ -230,7 +238,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Add script files to this module. In order to be valid, a module
         * must contain at least one script file.
-        * @param $scripts mixed Path to script file (string) or array of paths
+        *
+        * @param $scripts Mixed: path to script file (string) or array of paths
         */
        public function addScripts( $scripts ) {
                $this->scripts = array_merge( $this->scripts, (array)$scripts );
@@ -238,7 +247,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
        /**
         * Add style (CSS) files to this module.
-        * @param $styles mixed Path to CSS file (string) or array of paths
+        *
+        * @param $styles Mixed: path to CSS file (string) or array of paths
         */
        public function addStyles( $styles ) {
                $this->styles = array_merge( $this->styles, (array)$styles );
@@ -246,7 +256,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
        /**
         * Add messages to this module.
-        * @param $messages mixed Message key (string) or array of message keys
+        *
+        * @param $messages Mixed: message key (string) or array of message keys
         */
        public function addMessages( $messages ) {
                $this->messages = array_merge( $this->messages, (array)$messages );
@@ -264,7 +275,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * To add dependencies dynamically on the client side, use a custom
         * loader (see addLoaders())
         *
-        * @param $dependencies mixed Module name (string) or array of module names
+        * @param $dependencies Mixed: module name (string) or array of module names
         */
        public function addDependencies( $dependencies ) {
                $this->dependencies = array_merge( $this->dependencies, (array)$dependencies );
@@ -273,7 +284,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Add debug scripts to the module. These scripts are only included
         * in debug mode.
-        * @param $scripts mixed Path to script file (string) or array of paths
+        *
+        * @param $scripts Mixed: path to script file (string) or array of paths
         */
        public function addDebugScripts( $scripts ) {
                $this->debugScripts = array_merge( $this->debugScripts, (array)$scripts );
@@ -282,8 +294,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Add language-specific scripts. These scripts are only included for
         * a given language.
-        * @param $lang string Language code
-        * @param $scripts mixed Path to script file (string) or array of paths
+        *
+        * @param $lang String: language code
+        * @param $scripts Mixed: path to script file (string) or array of paths
         */
        public function addLanguageScripts( $lang, $scripts ) {
                $this->languageScripts = array_merge_recursive(
@@ -295,8 +308,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Add skin-specific scripts. These scripts are only included for
         * a given skin.
-        * @param $skin string Skin name, or 'default'
-        * @param $scripts mixed Path to script file (string) or array of paths
+        *
+        * @param $skin String: skin name, or 'default'
+        * @param $scripts Mixed: path to script file (string) or array of paths
         */
        public function addSkinScripts( $skin, $scripts ) {
                $this->skinScripts = array_merge_recursive(
@@ -309,8 +323,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * Add skin-specific CSS. These CSS files are only included for a
         * given skin. If there are no skin-specific CSS files for a skin,
         * the files defined for 'default' will be used, if any.
-        * @param $skin string Skin name, or 'default'
-        * @param $scripts mixed Path to CSS file (string) or array of paths
+        *
+        * @param $skin String: skin name, or 'default'
+        * @param $scripts Mixed: path to CSS file (string) or array of paths
         */
        public function addSkinStyles( $skin, $scripts ) {
                $this->skinStyles = array_merge_recursive(
@@ -330,7 +345,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * Note that loader scripts are included on every page, so they should
         * be lightweight and use mediaWiki.loader.register()'s callback
         * feature to defer dependency calculation.
-        * @param $scripts mixed Path to script file (string) or array of paths
+        *
+        * @param $scripts Mixed: path to script file (string) or array of paths
         */
        public function addLoaders( $scripts ) {
                $this->loaders = array_merge( $this->loaders, (array)$scripts );
@@ -348,12 +364,28 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return $retval;
        }
 
-       public function getStyle( ResourceLoaderContext $context ) {
-               $style = $this->getPrimaryStyle() . "\n" . $this->getSkinStyle( $context->getSkin() );
-
-               // Extract and store the list of referenced files
-               $files = CSSMin::getLocalFileReferences( $style );
-
+       public function getStyles( ResourceLoaderContext $context ) {
+               $styles = array();
+               foreach ( $this->getPrimaryStyles() as $media => $style ) {
+                       if ( !isset( $styles[$media] ) ) {
+                               $styles[$media] = '';
+                       }
+                       $styles[$media] .= $style;
+               }
+               foreach ( $this->getSkinStyles( $context->getSkin() ) as $media => $style ) {
+                       if ( !isset( $styles[$media] ) ) {
+                               $styles[$media] = '';
+                       }
+                       $styles[$media] .= $style;
+               }
+               
+               // Collect referenced files
+               $files = array();
+               foreach ( $styles as $media => $style ) {
+                       // Extract and store the list of referenced files
+                       $files = array_merge( $files, CSSMin::getLocalFileReferences( $style ) );
+               }
+               
                // Only store if modified
                if ( $files !== $this->getFileDependencies( $context->getSkin() ) ) {
                        $encFiles = FormatJson::encode( $files );
@@ -365,15 +397,15 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                        'md_deps' => $encFiles,
                                )
                        );
-
+                       
                        // Save into memcached
                        global $wgMemc;
-
+                       
                        $key = wfMemcKey( 'resourceloader', 'module_deps', $this->getName(), $context->getSkin() );
                        $wgMemc->set( $key, $encFiles );
                }
-
-               return $style;
+               
+               return $styles;
        }
 
        public function getMessages() {
@@ -399,28 +431,39 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * relevant to the given language and skin are taken into account, and
         * files only relevant in debug mode are not taken into account when
         * debug mode is off.
-        * @param $lang string Language code
-        * @param $skin string Skin name
-        * @param $debug bool Debug mode flag
-        * @return int UNIX timestamp
+        *
+        * @param $context ResourceLoaderContext object
+        * @return Integer: UNIX timestamp
         */
        public function getModifiedTime( ResourceLoaderContext $context ) {
                if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
                        return $this->modifiedTime[$context->getHash()];
                }
-
+               
+               // Sort of nasty way we can get a flat list of files depended on by all styles
+               $styles = array();
+               foreach ( self::organizeFilesByOption( $this->styles, 'media', 'all' ) as $media => $styleFiles ) {
+                       $styles = array_merge( $styles, $styleFiles );
+               }
+               $skinFiles = (array) self::getSkinFiles(
+                       $context->getSkin(), self::organizeFilesByOption( $this->skinStyles, 'media', 'all' )
+               );
+               foreach ( $skinFiles as $media => $styleFiles ) {
+                       $styles = array_merge( $styles, $styleFiles );
+               }
+               
+               // Final merge, this should result in a master list of dependent files
                $files = array_merge(
                        $this->scripts,
-                       $this->styles,
+                       $styles,
                        $context->getDebug() ? $this->debugScripts : array(),
                        isset( $this->languageScripts[$context->getLanguage()] ) ?
                                (array) $this->languageScripts[$context->getLanguage()] : array(),
                        (array) self::getSkinFiles( $context->getSkin(), $this->skinScripts ),
-                       (array) self::getSkinFiles( $context->getSkin(), $this->skinStyles ),
                        $this->loaders,
                        $this->getFileDependencies( $context->getSkin() )
                );
-
+               
                $filesMtime = max( array_map( 'filemtime', array_map( array( __CLASS__, 'remapFilename' ), $files ) ) );
 
                // Get the mtime of the message blob
@@ -442,7 +485,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Get the primary JS for this module. This is pulled from the
         * script files added through addScripts()
-        * @return string JS
+        *
+        * @return String: JS
         */
        protected function getPrimaryScript() {
                return self::concatScripts( $this->scripts );
@@ -451,16 +495,18 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Get the primary CSS for this module. This is pulled from the CSS
         * files added through addStyles()
-        * @return string JS
+        *
+        * @return String: JS
         */
-       protected function getPrimaryStyle() {
+       protected function getPrimaryStyles() {
                return self::concatStyles( $this->styles );
        }
 
        /**
         * Get the debug JS for this module. This is pulled from the script
         * files added through addDebugScripts()
-        * @return string JS
+        *
+        * @return String: JS
         */
        protected function getDebugScript() {
                return self::concatScripts( $this->debugScripts );
@@ -469,7 +515,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Get the language-specific JS for a given language. This is pulled
         * from the language-specific script files added through addLanguageScripts()
-        * @return string JS
+        *
+        * @return String: JS
         */
        protected function getLanguageScript( $lang ) {
                if ( !isset( $this->languageScripts[$lang] ) ) {
@@ -481,7 +528,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Get the skin-specific JS for a given skin. This is pulled from the
         * skin-specific JS files added through addSkinScripts()
-        * @return string JS
+        *
+        * @return String: JS
         */
        protected function getSkinScript( $skin ) {
                return self::concatScripts( self::getSkinFiles( $skin, $this->skinScripts ) );
@@ -490,16 +538,18 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Get the skin-specific CSS for a given skin. This is pulled from the
         * skin-specific CSS files added through addSkinStyles()
-        * @return string CSS
+        *
+        * @return Array: list of CSS strings keyed by media type
         */
-       protected function getSkinStyle( $skin ) {
+       protected function getSkinStyles( $skin ) {
                return self::concatStyles( self::getSkinFiles( $skin, $this->skinStyles ) );
        }
 
        /**
         * Helper function to get skin-specific data from an array.
-        * @param $skin string Skin name
-        * @param $map array Map of skin names to arrays
+        *
+        * @param $skin String: skin name
+        * @param $map Array: map of skin names to arrays
         * @return $map[$skin] if set and non-empty, or $map['default'] if set, or an empty array
         */
        protected static function getSkinFiles( $skin, $map ) {
@@ -517,7 +567,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Get the files this module depends on indirectly for a given skin.
         * Currently these are only image files referenced by the module's CSS.
-        * @param $skin string Skin name
+        *
+        * @param $skin String: skin name
         * @return array of files
         */
        protected function getFileDependencies( $skin ) {
@@ -553,26 +604,55 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Get the contents of a set of files and concatenate them, with
         * newlines in between. Each file is used only once.
-        * @param $files array Array of file names
-        * @return string Concatenated contents of $files
+        *
+        * @param $files Array of file names
+        * @return String: concatenated contents of $files
         */
        protected static function concatScripts( $files ) {
                return implode( "\n", array_map( 'file_get_contents', array_map( array( __CLASS__, 'remapFilename' ), array_unique( (array) $files ) ) ) );
        }
 
+       protected static function organizeFilesByOption( $files, $option, $default ) {
+               $organizedFiles = array();
+               foreach ( (array) $files as $key => $value ) {
+                       if ( is_int( $key ) ) {
+                               // File name as the value
+                               if ( !isset( $organizedFiles[$default] ) ) {
+                                       $organizedFiles[$default] = array();
+                               }
+                               $organizedFiles[$default][] = $value;
+                       } else if ( is_array( $value ) ) {
+                               // File name as the key, options array as the value
+                               $media = isset( $value[$option] ) ? $value[$option] : $default;
+                               if ( !isset( $organizedFiles[$media] ) ) {
+                                       $organizedFiles[$media] = array();
+                               }
+                               $organizedFiles[$media][] = $key;
+                       }
+               }
+               return $organizedFiles;
+       }
+       
        /**
         * Get the contents of a set of CSS files, remap then and concatenate
         * them, with newlines in between. Each file is used only once.
-        * @param $files array Array of file names
-        * @return string Concatenated and remapped contents of $files
-        */
-       protected static function concatStyles( $files ) {
-               return implode( "\n", array_map( array( __CLASS__, 'remapStyle' ), array_unique( (array) $files ) ) );
+        *
+        * @param $files Array of file names
+        * @return Array: list of concatenated and remapped contents of $files keyed by media type
+        */
+       protected static function concatStyles( $styles ) {
+               $styles = self::organizeFilesByOption( $styles, 'media', 'all' );
+               foreach ( $styles as $media => $files ) {
+                       $styles[$media] =
+                               implode( "\n", array_map( array( __CLASS__, 'remapStyle' ), array_unique( (array) $files ) ) );
+               }
+               return $styles;
        }
 
        /**
         * Remap a relative to $IP. Used as a callback for array_map()
-        * @param $file string File name
+        *
+        * @param $file String: file name
         * @return string $IP/$file
         */
        protected static function remapFilename( $file ) {
@@ -584,11 +664,13 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        /**
         * Get the contents of a CSS file and run it through CSSMin::remap().
         * This wrapper is needed so we can use array_map() in concatStyles()
-        * @param $file string File name
+        *
+        * @param $file String: file name
         * @return string Remapped CSS
         */
        protected static function remapStyle( $file ) {
-               return CSSMin::remap( file_get_contents( self::remapFilename( $file ) ), dirname( $file ) );
+               global $wgUseDataURLs;
+               return CSSMin::remap( file_get_contents( self::remapFilename( $file ) ), dirname( $file ), $wgUseDataURLs );
        }
 }
 
@@ -636,7 +718,9 @@ class ResourceLoaderSiteModule extends ResourceLoaderModule {
                return $this->modifiedTime;
        }
 
-       public function getStyle( ResourceLoaderContext $context ) { return ''; }
+       public function getStyles( ResourceLoaderContext $context ) {
+               return array();
+       }
        public function getMessages() { return array(); }
        public function getLoaderScript() { return ''; }
        public function getDependencies() { return array(); }
@@ -712,7 +796,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                return 300; // 5 minutes
        }
 
-       public function getStyle( ResourceLoaderContext $context ) { return ''; }
+       public function getStyles( ResourceLoaderContext $context ) { return array(); }
 
        public function getFlip( $context ) {
                global $wgContLang;