ResourceLoaderFileModule: Implement remoteSkinPath option
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderFileModule.php
index 382bdd9..190801c 100644 (file)
@@ -106,6 +106,11 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         */
        protected $dependencies = array();
 
+       /**
+        * @var string File name containing the body of the skip function
+        */
+       protected $skipFunction = null;
+
        /**
         * @var array List of message keys used by this module
         * @par Usage:
@@ -176,6 +181,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *         'remoteBasePath' => [base path],
         *         // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
         *         'remoteExtPath' => [base path],
+        *         // Equivalent of remoteBasePath, but relative to $wgStylePath
+        *         'remoteSkinPath' => [base path],
         *         // Scripts to always include
         *         'scripts' => [file path string or array of file path strings],
         *         // Scripts to include in specific language contexts
@@ -204,6 +211,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         *         'group' => [group name string],
         *         // Position on the page to load this module at
         *         'position' => ['bottom' (default) or 'top']
+        *         // Function that, if it returns true, makes the loader skip this module.
+        *         // The file must contain valid JavaScript for execution in a private function.
+        *         // The file must not contain the "function () {" and "}" wrapper though.
+        *         'skipFunction' => [file path]
         *     )
         * @endcode
         */
@@ -223,6 +234,11 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        $this->remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
                }
 
+               if ( isset( $options['remoteSkinPath'] ) ) {
+                       global $wgStylePath;
+                       $this->remoteBasePath = $wgStylePath . '/' . $options['remoteSkinPath'];
+               }
+
                foreach ( $options as $member => $option ) {
                        switch ( $member ) {
                                // Lists of file paths
@@ -267,6 +283,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                                case 'position':
                                case 'localBasePath':
                                case 'remoteBasePath':
+                               case 'skipFunction':
                                        $this->{$member} = (string)$option;
                                        break;
                                // Single booleans
@@ -312,22 +329,22 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Gets loader script.
+        * Get loader script.
         *
-        * @return string JavaScript code to be added to startup module
+        * @return string|false JavaScript code to be added to startup module
         */
        public function getLoaderScript() {
-               if ( count( $this->loaderScripts ) == 0 ) {
+               if ( count( $this->loaderScripts ) === 0 ) {
                        return false;
                }
                return $this->readScriptFiles( $this->loaderScripts );
        }
 
        /**
-        * Gets all styles for a given context concatenated together.
+        * Get all styles for a given context.
         *
-        * @param ResourceLoaderContext $context Context in which to generate styles
-        * @return string CSS code for $context
+        * @param ResourceLoaderContext $context
+        * @return array CSS code for $context as an associative array mapping media type to CSS text.
         */
        public function getStyles( ResourceLoaderContext $context ) {
                $styles = $this->readStyleFiles(
@@ -410,6 +427,28 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                return $this->dependencies;
        }
 
+       /**
+        * Get the skip function.
+        *
+        * @return string|null
+        */
+       public function getSkipFunction() {
+               if ( !$this->skipFunction ) {
+                       return null;
+               }
+
+               global $wgResourceLoaderValidateStaticJS;
+               $localPath = $this->getLocalPath( $this->skipFunction );
+               if ( !file_exists( $localPath ) ) {
+                       throw new MWException( __METHOD__ . ": skip function file not found: \"$localPath\"" );
+               }
+               $contents = file_get_contents( $localPath );
+               if ( $wgResourceLoaderValidateStaticJS ) {
+                       $contents = $this->validateScriptFile( $fileName, $contents );
+               }
+               return $contents;
+       }
+
        /**
         * @return bool
         */
@@ -463,6 +502,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ),
                        $this->loaderScripts
                );
+               if ( $this->skipFunction ) {
+                       $files[] = $this->skipFunction;
+               }
                $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() ) );
@@ -511,6 +553,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
                        'targets',
                        'group',
                        'position',
+                       'skipFunction',
                        'localBasePath',
                        'remoteBasePath',
                        'debugRaw',
@@ -581,13 +624,13 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Gets a list of element that match a key, optionally using a fallback key.
+        * Get 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
+        *  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] ) ) {
@@ -602,7 +645,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Gets a list of file paths for all scripts in this module, in order of propper execution.
+        * Get a list of file paths for all scripts in this module, in order of proper execution.
         *
         * @param ResourceLoaderContext $context
         * @return array List of file paths
@@ -621,12 +664,12 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
        }
 
        /**
-        * Gets a list of file paths for all styles in this module, in order of propper inclusion.
+        * Get a list of file paths for all styles in this module, in order of proper inclusion.
         *
         * @param ResourceLoaderContext $context
         * @return array List of file paths
         */
-       protected function getStyleFiles( ResourceLoaderContext $context ) {
+       public function getStyleFiles( ResourceLoaderContext $context ) {
                return array_merge_recursive(
                        self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
                        self::collateFilePathListByOption(
@@ -696,7 +739,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
         * @return array List of concatenated and remapped CSS data from $styles,
         *     keyed by media type
         */
-       protected function readStyleFiles( array $styles, $flip ) {
+       public function readStyleFiles( array $styles, $flip ) {
                if ( empty( $styles ) ) {
                        return array();
                }