summaryrefslogtreecommitdiff
path: root/includes/resourceloader/ResourceLoaderFileModule.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/resourceloader/ResourceLoaderFileModule.php')
-rw-r--r--includes/resourceloader/ResourceLoaderFileModule.php315
1 files changed, 209 insertions, 106 deletions
diff --git a/includes/resourceloader/ResourceLoaderFileModule.php b/includes/resourceloader/ResourceLoaderFileModule.php
index 8b9b7277..9ed181ed 100644
--- a/includes/resourceloader/ResourceLoaderFileModule.php
+++ b/includes/resourceloader/ResourceLoaderFileModule.php
@@ -113,6 +113,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
protected $debugRaw = true;
/** Boolean: Whether mw.loader.state() call should be omitted */
protected $raw = false;
+ protected $targets = array( 'desktop' );
+
+ /**
+ * Boolean: Whether getStyleURLsForDebug should return raw file paths,
+ * or return load.php urls
+ */
+ protected $hasGeneratedStyles = false;
+
/**
* Array: Cache for mtime
* @par Usage:
@@ -135,57 +143,58 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Constructs a new module from an options array.
*
- * @param $options Array: List of options; if not given or empty, an empty module will be
+ * @param array $options 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
+ * @param string $localBasePath 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
+ * @param string $remoteBasePath Base path to prepend to all remote paths in $options. Defaults
* to $wgScriptPath
*
* Below is a description for the $options array:
+ * @throws MWException
* @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
- * 'languageScripts' => array(
- * [language code] => [file path string or array of file path strings],
- * ),
- * // Scripts to include in specific skin contexts
- * 'skinScripts' => array(
- * [skin name] => [file path string or array of file path strings],
- * ),
- * // Scripts to include in debug contexts
- * 'debugScripts' => [file path string or array of file path strings],
- * // Scripts to include in the startup module
- * 'loaderScripts' => [file path string or array of file path strings],
- * // Modules which must be loaded before this module
- * 'dependencies' => [modile name string or array of module name strings],
- * // Styles to always load
- * 'styles' => [file path string or array of file path strings],
- * // Styles to include in specific skin contexts
- * 'skinStyles' => array(
- * [skin name] => [file path string or array of file path strings],
- * ),
- * // Messages to always load
- * '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']
- * )
+ * 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
+ * 'languageScripts' => array(
+ * [language code] => [file path string or array of file path strings],
+ * ),
+ * // Scripts to include in specific skin contexts
+ * 'skinScripts' => array(
+ * [skin name] => [file path string or array of file path strings],
+ * ),
+ * // Scripts to include in debug contexts
+ * 'debugScripts' => [file path string or array of file path strings],
+ * // Scripts to include in the startup module
+ * 'loaderScripts' => [file path string or array of file path strings],
+ * // Modules which must be loaded before this module
+ * 'dependencies' => [module name string or array of module name strings],
+ * // Styles to always load
+ * 'styles' => [file path string or array of file path strings],
+ * // Styles to include in specific skin contexts
+ * 'skinStyles' => array(
+ * [skin name] => [file path string or array of file path strings],
+ * ),
+ * // Messages to always load
+ * '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(), $localBasePath = null,
- $remoteBasePath = null )
- {
+ $remoteBasePath = null
+ ) {
global $IP, $wgScriptPath, $wgResourceBasePath;
$this->localBasePath = $localBasePath === null ? $IP : $localBasePath;
if ( $remoteBasePath !== null ) {
@@ -206,7 +215,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
case 'debugScripts':
case 'loaderScripts':
case 'styles':
- $this->{$member} = (array) $option;
+ $this->{$member} = (array)$option;
break;
// Collated lists of file paths
case 'languageScripts':
@@ -225,25 +234,26 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
"'$key' given, string expected."
);
}
- $this->{$member}[$key] = (array) $value;
+ $this->{$member}[$key] = (array)$value;
}
break;
// Lists of strings
case 'dependencies':
case 'messages':
- $this->{$member} = (array) $option;
+ case 'targets':
+ $this->{$member} = (array)$option;
break;
// Single strings
case 'group':
case 'position':
case 'localBasePath':
case 'remoteBasePath':
- $this->{$member} = (string) $option;
+ $this->{$member} = (string)$option;
break;
// Single booleans
case 'debugRaw':
case 'raw':
- $this->{$member} = (bool) $option;
+ $this->{$member} = (bool)$option;
break;
}
}
@@ -255,8 +265,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Gets all scripts for a given context concatenated together.
*
- * @param $context ResourceLoaderContext: Context in which to generate script
- * @return String: JavaScript code for $context
+ * @param ResourceLoaderContext $context Context in which to generate script
+ * @return string: JavaScript code for $context
*/
public function getScript( ResourceLoaderContext $context ) {
$files = $this->getScriptFiles( $context );
@@ -264,7 +274,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
}
/**
- * @param $context ResourceLoaderContext
+ * @param ResourceLoaderContext $context
* @return array
*/
public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
@@ -285,7 +295,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* 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 ) {
@@ -297,8 +307,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Gets all styles for a given context concatenated together.
*
- * @param $context ResourceLoaderContext: Context in which to generate styles
- * @return String: CSS code for $context
+ * @param ResourceLoaderContext $context Context in which to generate styles
+ * @return string: CSS code for $context
*/
public function getStyles( ResourceLoaderContext $context ) {
$styles = $this->readStyleFiles(
@@ -320,16 +330,23 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
);
}
} catch ( Exception $e ) {
- wfDebug( __METHOD__ . " failed to update DB: $e\n" );
+ wfDebugLog( 'resourceloader', __METHOD__ . ": failed to update DB: $e" );
}
return $styles;
}
/**
- * @param $context ResourceLoaderContext
+ * @param ResourceLoaderContext $context
* @return array
*/
public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
+ if ( $this->hasGeneratedStyles ) {
+ // Do the default behaviour of returning a url back to load.php
+ // but with only=styles.
+ return parent::getStyleURLsForDebug( $context );
+ }
+ // Our module consists entirely of real css files,
+ // in debug mode we can load those directly.
$urls = array();
foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
$urls[$mediaType] = array();
@@ -343,7 +360,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* 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;
@@ -352,7 +369,7 @@ 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;
@@ -368,7 +385,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* 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;
@@ -390,9 +407,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
* calculations on files relevant to the given language, skin and debug
* mode.
*
- * @param $context ResourceLoaderContext: Context in which to calculate
+ * @param ResourceLoaderContext $context Context in which to calculate
* the modified time
- * @return Integer: UNIX timestamp
+ * @return int: UNIX timestamp
* @see ResourceLoaderModule::getFileDependencies
*/
public function getModifiedTime( ResourceLoaderContext $context ) {
@@ -437,9 +454,9 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
return $this->modifiedTime[$context->getHash()] = 1;
}
- wfProfileIn( __METHOD__.'-filemtime' );
+ wfProfileIn( __METHOD__ . '-filemtime' );
$filesMtime = max( array_map( array( __CLASS__, 'safeFilemtime' ), $files ) );
- wfProfileOut( __METHOD__.'-filemtime' );
+ wfProfileOut( __METHOD__ . '-filemtime' );
$this->modifiedTime[$context->getHash()] = max(
$filesMtime,
$this->getMsgBlobMtime( $context->getLanguage() ) );
@@ -451,7 +468,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/* Protected Methods */
/**
- * @param $path string
+ * @param string $path
* @return string
*/
protected function getLocalPath( $path ) {
@@ -459,7 +476,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
}
/**
- * @param $path string
+ * @param string $path
* @return string
*/
protected function getRemotePath( $path ) {
@@ -467,17 +484,28 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
}
/**
+ * Infer the stylesheet language from a stylesheet file path.
+ *
+ * @since 1.22
+ * @param string $path
+ * @return string: the stylesheet language name
+ */
+ public function getStyleSheetLang( $path ) {
+ return preg_match( '/\.less$/i', $path ) ? 'less' : 'css';
+ }
+
+ /**
* Collates file paths by option (where provided).
*
- * @param $list Array: List of file paths in any combination of index/path
+ * @param array $list 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
+ * @param string $option option name
+ * @param mixed $default 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();
- foreach ( (array) $list as $key => $value ) {
+ foreach ( (array)$list as $key => $value ) {
if ( is_int( $key ) ) {
// File name as the value
if ( !isset( $collatedFiles[$default] ) ) {
@@ -499,10 +527,10 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Gets a list of element that match a key, optionally using a fallback key.
*
- * @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,
+ * @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
*/
protected static function tryForKey( array $list, $key, $fallback = null ) {
@@ -520,8 +548,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* 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
+ * @param ResourceLoaderContext $context
+ * @return array: List of file paths
*/
protected function getScriptFiles( ResourceLoaderContext $context ) {
$files = array_merge(
@@ -532,14 +560,15 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
if ( $context->getDebug() ) {
$files = array_merge( $files, $this->debugScripts );
}
- return $files;
+
+ return array_unique( $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
+ * @param ResourceLoaderContext $context
+ * @return array: List of file paths
*/
protected function getStyleFiles( ResourceLoaderContext $context ) {
return array_merge_recursive(
@@ -551,10 +580,28 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
}
/**
+ * Returns all style files used by this module
+ * @return array
+ */
+ public function getAllStyleFiles() {
+ $files = array();
+ foreach( (array)$this->styles as $key => $value ) {
+ if ( is_array( $value ) ) {
+ $path = $key;
+ } else {
+ $path = $value;
+ }
+ $files[] = $this->getLocalPath( $path );
+ }
+ return $files;
+ }
+
+ /**
* Gets the contents of a list of JavaScript files.
*
- * @param $scripts Array: List of file paths to scripts to read, remap and concetenate
- * @return String: Concatenated and remapped JavaScript data from $scripts
+ * @param array $scripts List of file paths to scripts to read, remap and concetenate
+ * @throws MWException
+ * @return string: Concatenated and remapped JavaScript data from $scripts
*/
protected function readScriptFiles( array $scripts ) {
global $wgResourceLoaderValidateStaticJS;
@@ -565,7 +612,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
foreach ( array_unique( $scripts ) as $fileName ) {
$localPath = $this->getLocalPath( $fileName );
if ( !file_exists( $localPath ) ) {
- throw new MWException( __METHOD__.": script file not found: \"$localPath\"" );
+ throw new MWException( __METHOD__ . ": script file not found: \"$localPath\"" );
}
$contents = file_get_contents( $localPath );
if ( $wgResourceLoaderValidateStaticJS ) {
@@ -582,12 +629,12 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Gets the contents of a list of CSS files.
*
- * @param $styles Array: List of media type/list of file paths pairs, to read, remap and
+ * @param array $styles List of media type/list of file paths pairs, to read, remap and
* concetenate
*
- * @param $flip bool
+ * @param bool $flip
*
- * @return Array: List of concatenated and remapped CSS data from $styles,
+ * @return array: List of concatenated and remapped CSS data from $styles,
* keyed by media type
*/
protected function readStyleFiles( array $styles, $flip ) {
@@ -613,18 +660,27 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
*
* This method can be used as a callback for array_map()
*
- * @param $path String: File path of style file to read
- * @param $flip bool
+ * @param string $path File path of style file to read
+ * @param bool $flip
*
- * @return String: CSS data in script file
+ * @return string: CSS data in script file
* @throws MWException if the file doesn't exist
*/
protected function readStyleFile( $path, $flip ) {
$localPath = $this->getLocalPath( $path );
if ( !file_exists( $localPath ) ) {
- throw new MWException( __METHOD__.": style file not found: \"$localPath\"" );
+ $msg = __METHOD__ . ": style file not found: \"$localPath\"";
+ wfDebugLog( 'resourceloader', $msg );
+ throw new MWException( $msg );
+ }
+
+ if ( $this->getStyleSheetLang( $path ) === 'less' ) {
+ $style = $this->compileLESSFile( $localPath );
+ $this->hasGeneratedStyles = true;
+ } else {
+ $style = file_get_contents( $localPath );
}
- $style = file_get_contents( $localPath );
+
if ( $flip ) {
$style = CSSJanus::transform( $style, true, false );
}
@@ -646,28 +702,75 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
}
/**
- * 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
+ * @param ResourceLoaderContext $context
* @return bool
*/
public function getFlip( $context ) {
return $context->getDirection() === 'rtl';
}
+
+ /**
+ * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
+ *
+ * @return array of strings
+ */
+ public function getTargets() {
+ return $this->targets;
+ }
+
+ /**
+ * Generate a cache key for a LESS file.
+ *
+ * The cache key varies on the file name and the names and values of global
+ * LESS variables.
+ *
+ * @since 1.22
+ * @param string $fileName File name of root LESS file.
+ * @return string: Cache key
+ */
+ protected static function getLESSCacheKey( $fileName ) {
+ $vars = json_encode( ResourceLoader::getLESSVars() );
+ $hash = md5( $fileName . $vars );
+ return wfMemcKey( 'resourceloader', 'less', $hash );
+ }
+
+ /**
+ * Compile a LESS file into CSS.
+ *
+ * If invalid, returns replacement CSS source consisting of the compilation
+ * error message encoded as a comment. To save work, we cache a result object
+ * which comprises the compiled CSS and the names & mtimes of the files
+ * that were processed. lessphp compares the cached & current mtimes and
+ * recompiles as necessary.
+ *
+ * @since 1.22
+ * @param string $fileName File path of LESS source
+ * @return string: CSS source
+ */
+ protected function compileLESSFile( $fileName ) {
+ $key = self::getLESSCacheKey( $fileName );
+ $cache = wfGetCache( CACHE_ANYTHING );
+
+ // The input to lessc. Either an associative array representing the
+ // cached results of a previous compilation, or the string file name if
+ // no cache result exists.
+ $source = $cache->get( $key );
+ if ( !is_array( $source ) || !isset( $source['root'] ) ) {
+ $source = $fileName;
+ }
+
+ $compiler = ResourceLoader::getLessCompiler();
+ $result = null;
+
+ $result = $compiler->cachedCompile( $source );
+
+ if ( !is_array( $result ) ) {
+ throw new MWException( 'LESS compiler result has type ' . gettype( $result ) . '; array expected.' );
+ }
+
+ $this->localFileRefs += array_keys( $result['files'] );
+ $cache->set( $key, $result );
+ return $result['compiled'];
+ }
}