Merge "fix the call of count( $module->getDependencies() ) in startup (bracket was...
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
index 5bffad7..3d657e1 100644 (file)
@@ -20,8 +20,6 @@
  * @author Roan Kattouw
  */
 
-defined( 'MEDIAWIKI' ) || die( 1 );
-
 /**
  * ResourceLoader module based on local JavaScript/CSS files.
  */
@@ -29,74 +27,127 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
        /* Protected Members */
 
+       /** String: Local base path, see __construct() */
+       protected $localBasePath = '';
+       /** String: Remote base path, see __construct() */
+       protected $remoteBasePath = '';
        /**
-        * @var {array} List of paths to JavaScript files to always include
-        * @format array( [file-path], [file-path], ... )
+        * Array: List of paths to JavaScript files to always include
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $scripts = array();
        /**
-        * @var {array} List of JavaScript files to include when using a specific language
-        * @format array( [language-code] => array( [file-path], [file-path], ... ), ... )
+        * Array: List of JavaScript files to include when using a specific language
+        * @par Usage:
+        * @code
+        * array( [language-code] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $languageScripts = array();
        /**
-        * @var {array} List of JavaScript files to include when using a specific skin
-        * @format array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * Array: List of JavaScript files to include when using a specific skin
+        * @par Usage:
+        * @code
+        * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $skinScripts = array();
        /**
-        * @var {array} List of paths to JavaScript files to include in debug mode
-        * @format array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * Array: List of paths to JavaScript files to include in debug mode
+        * @par Usage:
+        * @code
+        * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
+        * @endcode
         */
        protected $debugScripts = array();
        /**
-        * @var {array} List of paths to JavaScript files to include in the startup module
-        * @format array( [file-path], [file-path], ... )
+        * Array: List of paths to JavaScript files to include in the startup module
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $loaderScripts = array();
        /**
-        * @var {array} List of paths to CSS files to always include
-        * @format array( [file-path], [file-path], ... )
+        * Array: List of paths to CSS files to always include
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $styles = array();
        /**
-        * @var {array} List of paths to CSS files to include when using specific skins
-        * @format array( [file-path], [file-path], ... )
+        * Array: List of paths to CSS files to include when using specific skins
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $skinStyles = array();
        /**
-        * @var {array} List of modules this module depends on
-        * @format array( [file-path], [file-path], ... )
+        * Array: List of modules this module depends on
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
         */
        protected $dependencies = array();
        /**
-        * @var {array} List of message keys used by this module
-        * @format array( [module-name], [module-name], ... )
+        * Array: List of message keys used by this module
+        * @par Usage:
+        * @code
+        * array( [message-key], [message-key], ... )
+        * @endcode
         */
        protected $messages = array();
-       /**
-        * @var {string} Name of group this module should be loaded in
-        * @format array( [message-key], [message-key], ... )
-        */
+       /** String: Name of group to load this module in */
        protected $group;
-       /** @var {boolean} Link to raw files in debug mode */
+       /** String: Position on the page to load this module at */
+       protected $position = 'bottom';
+       /** Boolean: Link to raw files in debug mode */
        protected $debugRaw = true;
        /**
-        * @var {array}  Cache for mtime
-        * @format array( [hash] => [mtime], [hash] => [mtime], ... )
+        * Array: Cache for mtime
+        * @par Usage:
+        * @code
+        * array( [hash] => [mtime], [hash] => [mtime], ... )
+        * @endcode
         */
        protected $modifiedTime = array();
+       /**
+        * Array: Place where readStyleFile() tracks file dependencies
+        * @par Usage:
+        * @code
+        * array( [file-path], [file-path], ... )
+        * @endcode
+        */
+       protected $localFileRefs = array();
 
        /* Methods */
 
        /**
         * Constructs a new module from an options array.
-        * 
-        * @param {array} $options Options array. If not given or empty, an empty module will be constructed
-        * @param {string} $basePath base path to prepend to all paths in $options
-        * 
-        * @format $options
+        *
+        * @param $options Array: List of options; if not given or empty, an empty module will be
+        *     constructed
+        * @param $localBasePath String: Base path to prepend to all local paths in $options. Defaults
+        *     to $IP
+        * @param $remoteBasePath String: Base path to prepend to all remote paths in $options. Defaults
+        *     to $wgScriptPath
+        *
+        * Below is a description for the $options array:
+        * @par Construction options:
+        * @code
         *      array(
+        *              // Base path to prepend to all local paths in $options. Defaults to $IP
+        *              'localBasePath' => [base path],
+        *              // Base path to prepend to all remote paths in $options. Defaults to $wgScriptPath
+        *              'remoteBasePath' => [base path],
+        *              // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
+        *              'remoteExtPath' => [base path],
         *              // Scripts to always include
         *              'scripts' => [file path string or array of file path strings],
         *              // Scripts to include in specific language contexts
@@ -123,9 +174,27 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *              'messages' => [array of message key strings],
         *              // Group which this module should be loaded together with
         *              'group' => [group name string],
+        *              // Position on the page to load this module at
+        *              'position' => ['bottom' (default) or 'top']
         *      )
+        * @endcode
         */
-       public function __construct( $options = array(), $basePath = null ) {
+       public function __construct( $options = array(), $localBasePath = null,
+               $remoteBasePath = null )
+       {
+               global $IP, $wgScriptPath, $wgResourceBasePath;
+               $this->localBasePath = $localBasePath === null ? $IP : $localBasePath;
+               if ( $remoteBasePath !== null ) {
+                       $this->remoteBasePath = $remoteBasePath;
+               } else {
+                       $this->remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
+               }
+
+               if ( isset( $options['remoteExtPath'] ) ) {
+                       global $wgExtensionAssetsPath;
+                       $this->remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
+               }
+
                foreach ( $options as $member => $option ) {
                        switch ( $member ) {
                                // Lists of file paths
@@ -133,7 +202,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                case 'debugScripts':
                                case 'loaderScripts':
                                case 'styles':
-                                       $this->{$member} = self::prefixFilePathList( (array) $option, $basePath );
+                                       $this->{$member} = (array) $option;
                                        break;
                                // Collated lists of file paths
                                case 'languageScripts':
@@ -141,16 +210,18 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                case 'skinStyles':
                                        if ( !is_array( $option ) ) {
                                                throw new MWException(
-                                                       "Invalid collated file path list error. '$option' given, array expected."
+                                                       "Invalid collated file path list error. " .
+                                                       "'$option' given, array expected."
                                                );
                                        }
                                        foreach ( $option as $key => $value ) {
                                                if ( !is_string( $key ) ) {
                                                        throw new MWException(
-                                                               "Invalid collated file path list key error. '$key' given, string expected."
+                                                               "Invalid collated file path list key error. " .
+                                                               "'$key' given, string expected."
                                                        );
                                                }
-                                               $this->{$member}[$key] = self::prefixFilePathList( (array) $value, $basePath );
+                                               $this->{$member}[$key] = (array) $value;
                                        }
                                        break;
                                // Lists of strings
@@ -160,6 +231,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                        break;
                                // Single strings
                                case 'group':
+                               case 'position':
+                               case 'localBasePath':
+                               case 'remoteBasePath':
                                        $this->{$member} = (string) $option;
                                        break;
                                // Single booleans
@@ -168,89 +242,99 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                        break;
                        }
                }
+               // Make sure the remote base path is a complete valid URL,
+               // but possibly protocol-relative to avoid cache pollution
+               $this->remoteBasePath = wfExpandUrl( $this->remoteBasePath, PROTO_RELATIVE );
        }
 
        /**
         * Gets all scripts for a given context concatenated together.
-        * 
-        * @param {ResourceLoaderContext} $context Context in which to generate script
-        * @return {string} JavaScript code for $context
+        *
+        * @param $context ResourceLoaderContext: Context in which to generate script
+        * @return String: JavaScript code for $context
         */
        public function getScript( ResourceLoaderContext $context ) {
-               global $wgServer, $wgScriptPath;
-               
-               $files = array_merge(
-                       $this->scripts,
-                       self::tryForKey( $this->languageScripts, $context->getLanguage() ),
-                       self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
-               );
-               if ( $context->getDebug() ) {
-                       $files = array_merge( $files, $this->debugScripts );
-                       if ( $this->debugRaw ) {
-                               $script = '';
-                               foreach ( $files as $file ) {
-                                       $path = FormatJson::encode( "$wgServer$wgScriptPath/$file" );
-                                       $script .= "\n\tmediaWiki.loader.load( $path );";
-                               }
-                               return $script;
-                       }
+               $files = $this->getScriptFiles( $context );
+               return $this->readScriptFiles( $files );
+       }
+
+       /**
+        * @param $context ResourceLoaderContext
+        * @return array
+        */
+       public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
+               $urls = array();
+               foreach ( $this->getScriptFiles( $context ) as $file ) {
+                       $urls[] = $this->getRemotePath( $file );
                }
-               return self::readScriptFiles( $files );
+               return $urls;
+       }
+
+       /**
+        * @return bool
+        */
+       public function supportsURLLoading() {
+               return $this->debugRaw;
        }
 
        /**
         * Gets loader script.
-        * 
-        * @return {string} JavaScript code to be added to startup module
+        *
+        * @return String: JavaScript code to be added to startup module
         */
        public function getLoaderScript() {
                if ( count( $this->loaderScripts ) == 0 ) {
                        return false;
                }
-               return self::readScriptFiles( $this->loaderScripts );
+               return $this->readScriptFiles( $this->loaderScripts );
        }
 
        /**
         * Gets all styles for a given context concatenated together.
-        * 
-        * @param {ResourceLoaderContext} $context Context in which to generate styles
-        * @return {string} CSS code for $context
+        *
+        * @param $context ResourceLoaderContext: Context in which to generate styles
+        * @return String: CSS code for $context
         */
        public function getStyles( ResourceLoaderContext $context ) {
-               // Merge general styles and skin specific styles, retaining media type collation
-               $styles = self::readStyleFiles( $this->styles );
-               $skinStyles = self::readStyleFiles( self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ) );
-               
-               foreach ( $skinStyles as $media => $style ) {
-                       if ( isset( $styles[$media] ) ) {
-                               $styles[$media] .= $style;
-                       } else {
-                               $styles[$media] = $style;
-                       }
-               }
+               $styles = $this->readStyleFiles(
+                       $this->getStyleFiles( $context ),
+                       $this->getFlip( $context )
+               );
                // Collect referenced files
-               $files = array();
-               foreach ( $styles as /* $media => */ $style ) {
-                       $files = array_merge( $files, CSSMin::getLocalFileReferences( $style ) );
-               }
+               $this->localFileRefs = array_unique( $this->localFileRefs );
                // If the list has been modified since last time we cached it, update the cache
-               if ( $files !== $this->getFileDependencies( $context->getSkin() ) ) {
+               if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) && !wfReadOnly() ) {
                        $dbw = wfGetDB( DB_MASTER );
                        $dbw->replace( 'module_deps',
                                array( array( 'md_module', 'md_skin' ) ), array(
                                        'md_module' => $this->getName(),
                                        'md_skin' => $context->getSkin(),
-                                       'md_deps' => FormatJson::encode( $files ),
+                                       'md_deps' => FormatJson::encode( $this->localFileRefs ),
                                )
                        );
                }
                return $styles;
        }
 
+       /**
+        * @param $context ResourceLoaderContext
+        * @return array
+        */
+       public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
+               $urls = array();
+               foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
+                       $urls[$mediaType] = array();
+                       foreach ( $list as $file ) {
+                               $urls[$mediaType][] = $this->getRemotePath( $file );
+                       }
+               }
+               return $urls;
+       }
+
        /**
         * Gets list of message keys used by this module.
-        * 
-        * @return {array} List of message keys
+        *
+        * @return Array: List of message keys
         */
        public function getMessages() {
                return $this->messages;
@@ -258,17 +342,24 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
        /**
         * Gets the name of the group this module should be loaded in.
-        * 
-        * @return {string} Group name
+        *
+        * @return String: Group name
         */
        public function getGroup() {
                return $this->group;
        }
 
+       /**
+        * @return string
+        */
+       public function getPosition() {
+               return $this->position;
+       }
+
        /**
         * Gets list of names of modules this module depends on.
-        * 
-        * @return {array} List of module names
+        *
+        * @return Array: List of module names
         */
        public function getDependencies() {
                return $this->dependencies;
@@ -276,35 +367,40 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
        /**
         * Get the last modified timestamp of this module.
-        * 
-        * Last modified timestamps are calculated from the highest last modified timestamp of this module's constituent
-        * files as well as the files it depends on. This function is context-sensitive, only performing calculations on
-        * files relevant to the given language, skin and debug mode.
-        * 
-        * @param {ResourceLoaderContext} $context Context in which to calculate the modified time
-        * @return {integer} UNIX timestamp
-        * @see {ResourceLoaderModule::getFileDependencies}
+        *
+        * Last modified timestamps are calculated from the highest last modified
+        * timestamp of this module's constituent files as well as the files it
+        * depends on. This function is context-sensitive, only performing
+        * calculations on files relevant to the given language, skin and debug
+        * mode.
+        *
+        * @param $context ResourceLoaderContext: Context in which to calculate
+        *     the modified time
+        * @return Integer: UNIX timestamp
+        * @see ResourceLoaderModule::getFileDependencies
         */
        public function getModifiedTime( ResourceLoaderContext $context ) {
                if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
                        return $this->modifiedTime[$context->getHash()];
                }
                wfProfileIn( __METHOD__ );
-               
+
                $files = array();
-               
+
                // Flatten style files into $files
                $styles = self::collateFilePathListByOption( $this->styles, 'media', 'all' );
                foreach ( $styles as $styleFiles ) {
                        $files = array_merge( $files, $styleFiles );
                }
                $skinFiles = self::tryForKey(
-                       self::collateFilePathListByOption( $this->skinStyles, 'media', 'all' ), $context->getSkin(), 'default'
+                       self::collateFilePathListByOption( $this->skinStyles, 'media', 'all' ),
+                       $context->getSkin(),
+                       'default'
                );
                foreach ( $skinFiles as $styleFiles ) {
                        $files = array_merge( $files, $styleFiles );
                }
-               
+
                // Final merge, this should result in a master list of dependent files
                $files = array_merge(
                        $files,
@@ -312,53 +408,56 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $context->getDebug() ? $this->debugScripts : array(),
                        self::tryForKey( $this->languageScripts, $context->getLanguage() ),
                        self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ),
-                       $this->loaderScripts,
-                       $this->getFileDependencies( $context->getSkin() )
+                       $this->loaderScripts
                );
-               
-               // If a module is nothing but a list of dependencies, we need to avoid giving max() an empty array
+               $files = array_map( array( $this, 'getLocalPath' ), $files );
+               // File deps need to be treated separately because they're already prefixed
+               $files = array_merge( $files, $this->getFileDependencies( $context->getSkin() ) );
+
+               // If a module is nothing but a list of dependencies, we need to avoid
+               // giving max() an empty array
                if ( count( $files ) === 0 ) {
+                       wfProfileOut( __METHOD__ );
                        return $this->modifiedTime[$context->getHash()] = 1;
                }
-               
+
                wfProfileIn( __METHOD__.'-filemtime' );
-               $filesMtime = max( array_map( 'filemtime', array_map( array( __CLASS__, 'resolveFilePath' ), $files ) ) );
+               $filesMtime = max( array_map( array( __CLASS__, 'safeFilemtime' ), $files ) );
                wfProfileOut( __METHOD__.'-filemtime' );
-               $this->modifiedTime[$context->getHash()] = max( $filesMtime, $this->getMsgBlobMtime( $context->getLanguage() ) );
+               $this->modifiedTime[$context->getHash()] = max(
+                       $filesMtime,
+                       $this->getMsgBlobMtime( $context->getLanguage() ) );
+
                wfProfileOut( __METHOD__ );
                return $this->modifiedTime[$context->getHash()];
        }
 
-       /* Protected Members */
+       /* Protected Methods */
 
        /**
-        * Prefixes each file path in a list.
-        * 
-        * @param {array} $list List of file paths in any combination of index/path or path/options pairs
-        * @param {string} $prefix String to prepend to each file path in $list
-        * @return {array} List of prefixed file paths
-        */
-       protected static function prefixFilePathList( array $list, $prefix ) {
-               $prefixed = array();
-               foreach ( $list as $key => $value ) {
-                       if ( is_string( $key ) && is_array( $value ) ) {
-                               // array( [path] => array( [options] ) )
-                               $prefixed[$prefix . $key] = $value;
-                       } else if ( is_int( $key ) && is_string( $value ) ) {
-                               // array( [path] )
-                               $prefixed[$key] = $prefix . $value;
-                       } else {
-                               throw new MWException( "Invalid file path list error. '$key' => '$value' given." );
-                       }
-               }
-               return $prefixed;
+        * @param $path string
+        * @return string
+        */
+       protected function getLocalPath( $path ) {
+               return "{$this->localBasePath}/$path";
+       }
+
+       /**
+        * @param $path string
+        * @return string
+        */
+       protected function getRemotePath( $path ) {
+               return "{$this->remoteBasePath}/$path";
        }
 
        /**
         * Collates file paths by option (where provided).
-        * 
-        * @param {array} $list List of file paths in any combination of index/path or path/options pairs
-        * @return {array} List of file paths, collated by $option
+        *
+        * @param $list Array: List of file paths in any combination of index/path
+        *     or path/options pairs
+        * @param $option String: option name
+        * @param $default Mixed: default value if the option isn't set
+        * @return Array: List of file paths, collated by $option
         */
        protected static function collateFilePathListByOption( array $list, $option, $default ) {
                $collatedFiles = array();
@@ -369,7 +468,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                        $collatedFiles[$default] = array();
                                }
                                $collatedFiles[$default][] = $value;
-                       } else if ( is_array( $value ) ) {
+                       } elseif ( is_array( $value ) ) {
                                // File name as the key, options array as the value
                                $optionValue = isset( $value[$option] ) ? $value[$option] : $default;
                                if ( !isset( $collatedFiles[$optionValue] ) ) {
@@ -383,94 +482,175 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
 
        /**
         * Gets a list of element that match a key, optionally using a fallback key.
-        * 
-        * @param {array} $list List of lists to select from
-        * @param {string} $key Key to look for in $map
-        * @param {string} $fallback Key to look for in $list if $key doesn't exist
-        * @return {array} List of elements from $map which matched $key or $fallback, or an empty list in case of no match
+        *
+        * @param $list Array: List of lists to select from
+        * @param $key String: Key to look for in $map
+        * @param $fallback String: Key to look for in $list if $key doesn't exist
+        * @return Array: List of elements from $map which matched $key or $fallback,
+        *     or an empty list in case of no match
         */
        protected static function tryForKey( array $list, $key, $fallback = null ) {
                if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
                        return $list[$key];
-               } else if ( is_string( $fallback ) && isset( $list[$fallback] ) && is_array( $list[$fallback] ) ) {
+               } elseif ( is_string( $fallback )
+                       && isset( $list[$fallback] )
+                       && is_array( $list[$fallback] ) )
+               {
                        return $list[$fallback];
                }
                return array();
        }
 
+       /**
+        * Gets a list of file paths for all scripts in this module, in order of propper execution.
+        *
+        * @param $context ResourceLoaderContext: Context
+        * @return Array: List of file paths
+        */
+       protected function getScriptFiles( ResourceLoaderContext $context ) {
+               $files = array_merge(
+                       $this->scripts,
+                       self::tryForKey( $this->languageScripts, $context->getLanguage() ),
+                       self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' )
+               );
+               if ( $context->getDebug() ) {
+                       $files = array_merge( $files, $this->debugScripts );
+               }
+               return $files;
+       }
+
+       /**
+        * Gets a list of file paths for all styles in this module, in order of propper inclusion.
+        *
+        * @param $context ResourceLoaderContext: Context
+        * @return Array: List of file paths
+        */
+       protected function getStyleFiles( ResourceLoaderContext $context ) {
+               return array_merge_recursive(
+                       self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
+                       self::collateFilePathListByOption(
+                               self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ), 'media', 'all'
+                       )
+               );
+       }
+
        /**
         * Gets the contents of a list of JavaScript files.
-        * 
-        * @param {array} $scripts List of file paths to scripts to read, remap and concetenate
-        * @return {string} Concatenated and remapped JavaScript data from $scripts
+        *
+        * @param $scripts Array: List of file paths to scripts to read, remap and concetenate
+        * @return String: Concatenated and remapped JavaScript data from $scripts
         */
-       protected static function readScriptFiles( array $scripts ) {
+       protected function readScriptFiles( array $scripts ) {
+               global $wgResourceLoaderValidateStaticJS;
                if ( empty( $scripts ) ) {
                        return '';
                }
-               return implode( "\n", array_map( array( __CLASS__, 'readScriptFile' ), array_unique( $scripts ) ) );
+               $js = '';
+               foreach ( array_unique( $scripts ) as $fileName ) {
+                       $localPath = $this->getLocalPath( $fileName );
+                       if ( !file_exists( $localPath ) ) {
+                               throw new MWException( __METHOD__.": script file not found: \"$localPath\"" );
+                       }
+                       $contents = file_get_contents( $localPath );
+                       if ( $wgResourceLoaderValidateStaticJS ) {
+                               // Static files don't really need to be checked as often; unlike
+                               // on-wiki module they shouldn't change unexpectedly without
+                               // admin interference.
+                               $contents = $this->validateScriptFile( $fileName, $contents );
+                       }
+                       $js .= $contents . "\n";
+               }
+               return $js;
        }
 
        /**
         * Gets the contents of a list of CSS files.
-        * 
-        * @param {array} $styles List of file paths to styles to read, remap and concetenate
-        * @return {array} List of concatenated and remapped CSS data from $styles, keyed by media type
+        *
+        * @param $styles Array: List of media type/list of file paths pairs, to read, remap and
+        * concetenate
+        *
+        * @param $flip bool
+        *
+        * @return Array: List of concatenated and remapped CSS data from $styles,
+        *     keyed by media type
         */
-       protected static function readStyleFiles( array $styles ) {
+       protected function readStyleFiles( array $styles, $flip ) {
                if ( empty( $styles ) ) {
                        return array();
                }
-               $styles = self::collateFilePathListByOption( $styles, 'media', 'all' );
                foreach ( $styles as $media => $files ) {
+                       $uniqueFiles = array_unique( $files );
                        $styles[$media] = implode(
-                               "\n", array_map( array( __CLASS__, 'readStyleFile' ), array_unique( $files ) )
+                               "\n",
+                               array_map(
+                                       array( $this, 'readStyleFile' ),
+                                       $uniqueFiles,
+                                       array_fill( 0, count( $uniqueFiles ), $flip )
+                               )
                        );
                }
                return $styles;
        }
 
-       /**
-        * Reads a script file.
-        * 
-        * This method can be used as a callback for array_map()
-        * 
-        * @param {string} $path File path of script file to read
-        * @return {string} JavaScript data in script file
-        */
-       protected static function readScriptFile( $path ) {
-               global $IP;
-               
-               return file_get_contents( "$IP/$path" );
-       }
-
        /**
         * Reads a style file.
-        * 
+        *
         * This method can be used as a callback for array_map()
-        * 
-        * @param {string} $path File path of script file to read
-        * @return {string} CSS data in script file
+        *
+        * @param $path String: File path of style file to read
+        * @param $flip bool
+        *
+        * @return String: CSS data in script file
+        * @throws MWException if the file doesn't exist
         */
-       protected static function readStyleFile( $path ) {
-               global $wgScriptPath, $IP;
-               
+       protected function readStyleFile( $path, $flip ) {
+               $localPath = $this->getLocalPath( $path );
+               if ( !file_exists( $localPath ) ) {
+                       throw new MWException( __METHOD__.": style file not found: \"$localPath\"" );
+               }
+               $style = file_get_contents( $localPath );
+               if ( $flip ) {
+                       $style = CSSJanus::transform( $style, true, false );
+               }
+               $dirname = dirname( $path );
+               if ( $dirname == '.' ) {
+                       // If $path doesn't have a directory component, don't prepend a dot
+                       $dirname = '';
+               }
+               $dir = $this->getLocalPath( $dirname );
+               $remoteDir = $this->getRemotePath( $dirname );
+               // Get and register local file references
+               $this->localFileRefs = array_merge(
+                       $this->localFileRefs,
+                       CSSMin::getLocalFileReferences( $style, $dir ) );
                return CSSMin::remap(
-                       file_get_contents( "$IP/$path" ), dirname( "$IP/$path" ), $wgScriptPath . '/' . dirname( $path ), true
+                       $style, $dir, $remoteDir, true
                );
        }
 
        /**
-        * Resolves a file name.
-        * 
-        * This method can be used as a callback for array_map()
-        * 
-        * @param {string} $path File path to resolve
-        * @return {string} Absolute file path
-        */
-       protected static function resolveFilePath( $path ) {
-               global $IP;
-               
-               return "$IP/$path";
+        * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist
+        * but returns 1 instead.
+        * @param $filename string File name
+        * @return int UNIX timestamp, or 1 if the file doesn't exist
+        */
+       protected static function safeFilemtime( $filename ) {
+               if ( file_exists( $filename ) ) {
+                       return filemtime( $filename );
+               } else {
+                       // We only ever map this function on an array if we're gonna call max() after,
+                       // so return our standard minimum timestamps here. This is 1, not 0, because
+                       // wfTimestamp(0) == NOW
+                       return 1;
+               }
+       }
+
+       /**
+        * Get whether CSS for this module should be flipped
+        * @param $context ResourceLoaderContext
+        * @return bool
+        */
+       public function getFlip( $context ) {
+               return $context->getDirection() === 'rtl';
        }
 }