2010-10-19 18:25:42 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-04-30 07:16:10 +00:00
|
|
|
* Resource loader module based on local JavaScript/CSS files.
|
|
|
|
|
*
|
2010-10-19 18:25:42 +00:00
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* ResourceLoader module based on local JavaScript/CSS files.
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
|
|
|
|
class ResourceLoaderFileModule extends ResourceLoaderModule {
|
|
|
|
|
/* Protected Members */
|
2010-10-19 23:02:15 +00:00
|
|
|
|
2014-04-20 21:33:05 +00:00
|
|
|
/** @var string Local base path, see __construct() */
|
2010-10-27 22:22:10 +00:00
|
|
|
protected $localBasePath = '';
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2014-04-20 21:33:05 +00:00
|
|
|
/** @var string Remote base path, see __construct() */
|
2010-10-27 22:22:10 +00:00
|
|
|
protected $remoteBasePath = '';
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of paths to JavaScript files to always include
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [file-path], [file-path], ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
protected $scripts = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of JavaScript files to include when using a specific language
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [language-code] => array( [file-path], [file-path], ... ), ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
protected $languageScripts = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of JavaScript files to include when using a specific skin
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [skin-name] => array( [file-path], [file-path], ... ), ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
protected $skinScripts = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of paths to JavaScript files to include in debug mode
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [skin-name] => array( [file-path], [file-path], ... ), ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 22:48:52 +00:00
|
|
|
protected $debugScripts = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of paths to JavaScript files to include in the startup module
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [file-path], [file-path], ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 22:48:52 +00:00
|
|
|
protected $loaderScripts = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of paths to CSS files to always include
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [file-path], [file-path], ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 22:48:52 +00:00
|
|
|
protected $styles = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of paths to CSS files to include when using specific skins
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [file-path], [file-path], ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
protected $skinStyles = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of modules this module depends on
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [file-path], [file-path], ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 22:48:52 +00:00
|
|
|
protected $dependencies = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2014-04-30 21:06:51 +00:00
|
|
|
/**
|
|
|
|
|
* @var string File name containing the body of the skip function
|
|
|
|
|
*/
|
|
|
|
|
protected $skipFunction = null;
|
|
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array List of message keys used by this module
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [message-key], [message-key], ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-26 20:17:32 +00:00
|
|
|
protected $messages = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2014-04-20 21:33:05 +00:00
|
|
|
/** @var string Name of group to load this module in */
|
2010-10-19 22:48:52 +00:00
|
|
|
protected $group;
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2014-04-20 21:33:05 +00:00
|
|
|
/** @var string Position on the page to load this module at */
|
2011-04-07 12:07:25 +00:00
|
|
|
protected $position = 'bottom';
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2014-04-20 21:33:05 +00:00
|
|
|
/** @var bool Link to raw files in debug mode */
|
2010-10-21 01:03:46 +00:00
|
|
|
protected $debugRaw = true;
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2014-04-20 21:33:05 +00:00
|
|
|
/** @var bool Whether mw.loader.state() call should be omitted */
|
2012-05-11 19:16:29 +00:00
|
|
|
protected $raw = false;
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2012-10-11 22:37:59 +00:00
|
|
|
protected $targets = array( 'desktop' );
|
|
|
|
|
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var bool Whether getStyleURLsForDebug should return raw file paths,
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
* or return load.php urls
|
|
|
|
|
*/
|
|
|
|
|
protected $hasGeneratedStyles = false;
|
|
|
|
|
|
2010-10-20 20:43:30 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array Cache for mtime
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [hash] => [mtime], [hash] => [mtime], ... )
|
|
|
|
|
* @endcode
|
2010-10-20 20:43:30 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
protected $modifiedTime = array();
|
2014-05-10 08:39:37 +00:00
|
|
|
|
2010-10-27 19:59:10 +00:00
|
|
|
/**
|
2014-04-20 21:33:05 +00:00
|
|
|
* @var array Place where readStyleFile() tracks file dependencies
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Usage:
|
|
|
|
|
* @code
|
|
|
|
|
* array( [file-path], [file-path], ... )
|
|
|
|
|
* @endcode
|
2010-10-27 19:59:10 +00:00
|
|
|
*/
|
|
|
|
|
protected $localFileRefs = array();
|
2010-10-19 18:25:42 +00:00
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Constructs a new module from an options array.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $options List of options; if not given or empty, an empty module will be
|
2010-11-05 18:33:50 +00:00
|
|
|
* constructed
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
|
2010-11-05 18:33:50 +00:00
|
|
|
* to $IP
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $remoteBasePath Base path to prepend to all remote paths in $options. Defaults
|
2010-11-05 18:33:50 +00:00
|
|
|
* to $wgScriptPath
|
2011-03-02 20:28:32 +00:00
|
|
|
*
|
2011-06-17 16:05:05 +00:00
|
|
|
* Below is a description for the $options array:
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2012-02-01 17:05:15 +00:00
|
|
|
* @par Construction options:
|
2011-03-02 20:28:32 +00:00
|
|
|
* @code
|
2012-10-07 23:35:26 +00:00
|
|
|
* 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],
|
2014-06-01 17:25:11 +00:00
|
|
|
* // Equivalent of remoteBasePath, but relative to $wgStylePath
|
|
|
|
|
* 'remoteSkinPath' => [base path],
|
2012-10-07 23:35:26 +00:00
|
|
|
* // 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
|
2012-12-27 18:05:53 +00:00
|
|
|
* 'dependencies' => [module name string or array of module name strings],
|
2012-10-07 23:35:26 +00:00
|
|
|
* // 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']
|
2014-04-30 21:06:51 +00:00
|
|
|
* // 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]
|
2012-10-07 23:35:26 +00:00
|
|
|
* )
|
2011-03-02 20:28:32 +00:00
|
|
|
* @endcode
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
2014-06-26 14:29:31 +00:00
|
|
|
public function __construct(
|
|
|
|
|
$options = array(),
|
|
|
|
|
$localBasePath = null,
|
2013-04-02 20:28:40 +00:00
|
|
|
$remoteBasePath = null
|
|
|
|
|
) {
|
2014-06-26 14:29:31 +00:00
|
|
|
// localBasePath and remoteBasePath both have unbelievably long fallback chains
|
|
|
|
|
// and need to be handled separately.
|
|
|
|
|
list( $this->localBasePath, $this->remoteBasePath ) =
|
|
|
|
|
self::extractBasePaths( $options, $localBasePath, $remoteBasePath );
|
2014-06-01 17:25:11 +00:00
|
|
|
|
2014-06-26 14:29:31 +00:00
|
|
|
// Extract, validate and normalise remaining options
|
2010-10-19 20:59:23 +00:00
|
|
|
foreach ( $options as $member => $option ) {
|
|
|
|
|
switch ( $member ) {
|
|
|
|
|
// Lists of file paths
|
2010-10-19 18:25:42 +00:00
|
|
|
case 'scripts':
|
|
|
|
|
case 'debugScripts':
|
2010-10-19 22:48:52 +00:00
|
|
|
case 'loaderScripts':
|
2010-10-19 18:25:42 +00:00
|
|
|
case 'styles':
|
2013-04-02 20:28:40 +00:00
|
|
|
$this->{$member} = (array)$option;
|
2010-10-19 20:59:23 +00:00
|
|
|
break;
|
|
|
|
|
// Collated lists of file paths
|
|
|
|
|
case 'languageScripts':
|
|
|
|
|
case 'skinScripts':
|
2010-10-19 18:25:42 +00:00
|
|
|
case 'skinStyles':
|
2010-10-21 22:34:26 +00:00
|
|
|
if ( !is_array( $option ) ) {
|
|
|
|
|
throw new MWException(
|
2011-06-17 16:05:05 +00:00
|
|
|
"Invalid collated file path list error. " .
|
2010-11-05 06:53:14 +00:00
|
|
|
"'$option' given, array expected."
|
2010-10-21 22:34:26 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
foreach ( $option as $key => $value ) {
|
|
|
|
|
if ( !is_string( $key ) ) {
|
|
|
|
|
throw new MWException(
|
2011-06-17 16:05:05 +00:00
|
|
|
"Invalid collated file path list key error. " .
|
2010-11-05 06:53:14 +00:00
|
|
|
"'$key' given, string expected."
|
2010-10-21 22:34:26 +00:00
|
|
|
);
|
|
|
|
|
}
|
2013-04-02 20:28:40 +00:00
|
|
|
$this->{$member}[$key] = (array)$value;
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2010-10-21 22:34:26 +00:00
|
|
|
break;
|
2010-10-19 20:59:23 +00:00
|
|
|
// Lists of strings
|
2010-10-19 18:25:42 +00:00
|
|
|
case 'dependencies':
|
|
|
|
|
case 'messages':
|
2012-10-11 22:37:59 +00:00
|
|
|
case 'targets':
|
2013-10-18 13:34:50 +00:00
|
|
|
// Normalise
|
|
|
|
|
$option = array_values( array_unique( (array)$option ) );
|
|
|
|
|
sort( $option );
|
|
|
|
|
|
|
|
|
|
$this->{$member} = $option;
|
2010-10-19 18:25:42 +00:00
|
|
|
break;
|
2010-10-19 20:59:23 +00:00
|
|
|
// Single strings
|
2010-10-19 18:25:42 +00:00
|
|
|
case 'group':
|
2011-04-07 12:07:25 +00:00
|
|
|
case 'position':
|
2014-04-30 21:06:51 +00:00
|
|
|
case 'skipFunction':
|
2013-04-02 20:28:40 +00:00
|
|
|
$this->{$member} = (string)$option;
|
2010-10-21 01:03:46 +00:00
|
|
|
break;
|
|
|
|
|
// Single booleans
|
|
|
|
|
case 'debugRaw':
|
2012-05-11 19:16:29 +00:00
|
|
|
case 'raw':
|
2013-04-02 20:28:40 +00:00
|
|
|
$this->{$member} = (bool)$option;
|
2010-10-19 18:25:42 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-26 14:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extract a pair of local and remote base paths from module definition information.
|
|
|
|
|
* Implementation note: the amount of global state used in this function is staggering.
|
|
|
|
|
*
|
|
|
|
|
* @param array $options Module definition
|
|
|
|
|
* @param string $localBasePath Path to use if not provided in module definition. Defaults
|
|
|
|
|
* to $IP
|
|
|
|
|
* @param string $remoteBasePath Path to use if not provided in module definition. Defaults
|
|
|
|
|
* to $wgScriptPath
|
2014-08-04 10:00:15 +00:00
|
|
|
* @return array Array( localBasePath, remoteBasePath )
|
2014-06-26 14:29:31 +00:00
|
|
|
*/
|
|
|
|
|
public static function extractBasePaths(
|
|
|
|
|
$options = array(),
|
|
|
|
|
$localBasePath = null,
|
|
|
|
|
$remoteBasePath = null
|
|
|
|
|
) {
|
|
|
|
|
global $IP, $wgScriptPath, $wgResourceBasePath;
|
|
|
|
|
|
|
|
|
|
// The different ways these checks are done, and their ordering, look very silly,
|
|
|
|
|
// but were preserved for backwards-compatibility just in case. Tread lightly.
|
|
|
|
|
|
|
|
|
|
$localBasePath = $localBasePath === null ? $IP : $localBasePath;
|
2014-08-07 10:27:36 +00:00
|
|
|
if ( $remoteBasePath === null ) {
|
2014-06-26 14:29:31 +00:00
|
|
|
$remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $options['remoteExtPath'] ) ) {
|
|
|
|
|
global $wgExtensionAssetsPath;
|
|
|
|
|
$remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( isset( $options['remoteSkinPath'] ) ) {
|
|
|
|
|
global $wgStylePath;
|
|
|
|
|
$remoteBasePath = $wgStylePath . '/' . $options['remoteSkinPath'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( array_key_exists( 'localBasePath', $options ) ) {
|
|
|
|
|
$localBasePath = (string)$options['localBasePath'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( array_key_exists( 'remoteBasePath', $options ) ) {
|
|
|
|
|
$remoteBasePath = (string)$options['remoteBasePath'];
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-03 12:58:21 +00:00
|
|
|
// Make sure the remote base path is a complete valid URL,
|
|
|
|
|
// but possibly protocol-relative to avoid cache pollution
|
2014-06-26 14:29:31 +00:00
|
|
|
$remoteBasePath = wfExpandUrl( $remoteBasePath, PROTO_RELATIVE );
|
|
|
|
|
|
|
|
|
|
return array( $localBasePath, $remoteBasePath );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Gets all scripts for a given context concatenated together.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param ResourceLoaderContext $context Context in which to generate script
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string JavaScript code for $context
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
2010-10-19 22:48:52 +00:00
|
|
|
public function getScript( ResourceLoaderContext $context ) {
|
2011-05-14 12:15:58 +00:00
|
|
|
$files = $this->getScriptFiles( $context );
|
2010-10-27 22:22:10 +00:00
|
|
|
return $this->readScriptFiles( $files );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2011-10-14 08:06:54 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
Fix the fixme on r88053: dependency handling was broken in debug mode in certain cases. More specifically, if A is a file module that depends on B, B is a wiki module that depends on C and C is a file module, the loading order is CBA (correct) in production mode but was BCA (wrong) in debug mode. Fixed this by URL-ifying scripts and styles for those modules in debug mode, as I said to on CR. What this means is that the initial debug=true request for a module will now always return arrays of URLs, never the JS or CSS itself. This was already the case for file modules (which returned arrays of URLs to the raw files), but not for other modules (which returned the JS and CSS itself). So for non-file modules, load.php?modules=foo&debug=true now returns some JS that instructs the loader to fetch the module's JS from load.php?modules=foo&debug=true&only=scripts and the CSS from ...&only=styles .
* Removed the magic behavior where ResourceLoaderModule::getScripts() and getStyles() could return an array of URLs where the documentation said they should return a JS/CSS string. Because I didn't restructure the calling code too much, the old magical behavior should still work.
* Instead, move this behavior to getScriptURLsForDebug() and getStyleURLsForDebug(). The default implementation constructs a single URL for a load.php request for the module with debug=true&only=scripts (or styles). The URL building code duplicates some things from OutputPage::makeResourceLoaderLink(), I'll clean that up later. ResourceLoaderFileModule overrides this method to return URLs to the raw files, using code that I removed from getScripts()/getStyles()
* Add ResourceLoaderModule::supportsURLLoading(), which returns true by default but may return false to indicate that a module does not support loading via a URL. This is needed to respect $this->debugRaw in ResourceLoaderFileModule (set to true for jquery and mediawiki), and obviously for the startup module as well, because we get bootstrapping problems otherwise (can't call mw.loader.implement() when the code for mw.loader isn't loaded yet)
2011-09-13 17:13:53 +00:00
|
|
|
public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
|
|
|
|
|
$urls = array();
|
|
|
|
|
foreach ( $this->getScriptFiles( $context ) as $file ) {
|
|
|
|
|
$urls[] = $this->getRemotePath( $file );
|
|
|
|
|
}
|
|
|
|
|
return $urls;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-14 08:06:54 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
Fix the fixme on r88053: dependency handling was broken in debug mode in certain cases. More specifically, if A is a file module that depends on B, B is a wiki module that depends on C and C is a file module, the loading order is CBA (correct) in production mode but was BCA (wrong) in debug mode. Fixed this by URL-ifying scripts and styles for those modules in debug mode, as I said to on CR. What this means is that the initial debug=true request for a module will now always return arrays of URLs, never the JS or CSS itself. This was already the case for file modules (which returned arrays of URLs to the raw files), but not for other modules (which returned the JS and CSS itself). So for non-file modules, load.php?modules=foo&debug=true now returns some JS that instructs the loader to fetch the module's JS from load.php?modules=foo&debug=true&only=scripts and the CSS from ...&only=styles .
* Removed the magic behavior where ResourceLoaderModule::getScripts() and getStyles() could return an array of URLs where the documentation said they should return a JS/CSS string. Because I didn't restructure the calling code too much, the old magical behavior should still work.
* Instead, move this behavior to getScriptURLsForDebug() and getStyleURLsForDebug(). The default implementation constructs a single URL for a load.php request for the module with debug=true&only=scripts (or styles). The URL building code duplicates some things from OutputPage::makeResourceLoaderLink(), I'll clean that up later. ResourceLoaderFileModule overrides this method to return URLs to the raw files, using code that I removed from getScripts()/getStyles()
* Add ResourceLoaderModule::supportsURLLoading(), which returns true by default but may return false to indicate that a module does not support loading via a URL. This is needed to respect $this->debugRaw in ResourceLoaderFileModule (set to true for jquery and mediawiki), and obviously for the startup module as well, because we get bootstrapping problems otherwise (can't call mw.loader.implement() when the code for mw.loader isn't loaded yet)
2011-09-13 17:13:53 +00:00
|
|
|
public function supportsURLLoading() {
|
|
|
|
|
return $this->debugRaw;
|
|
|
|
|
}
|
2010-10-19 18:25:42 +00:00
|
|
|
|
|
|
|
|
/**
|
2014-06-04 19:30:14 +00:00
|
|
|
* Get loader script.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2014-07-24 17:43:25 +00:00
|
|
|
* @return string|bool JavaScript code to be added to startup module
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
2010-10-19 22:48:52 +00:00
|
|
|
public function getLoaderScript() {
|
2014-06-04 19:30:14 +00:00
|
|
|
if ( count( $this->loaderScripts ) === 0 ) {
|
2010-10-19 22:48:52 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-10-27 22:22:10 +00:00
|
|
|
return $this->readScriptFiles( $this->loaderScripts );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-04 19:30:14 +00:00
|
|
|
* Get all styles for a given context.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2014-06-04 19:30:14 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
|
|
|
|
* @return array CSS code for $context as an associative array mapping media type to CSS text.
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
|
|
|
|
public function getStyles( ResourceLoaderContext $context ) {
|
2011-05-14 12:15:58 +00:00
|
|
|
$styles = $this->readStyleFiles(
|
|
|
|
|
$this->getStyleFiles( $context ),
|
2010-12-16 19:31:48 +00:00
|
|
|
$this->getFlip( $context )
|
|
|
|
|
);
|
2010-10-19 18:25:42 +00:00
|
|
|
// Collect referenced files
|
2010-10-27 19:59:10 +00:00
|
|
|
$this->localFileRefs = array_unique( $this->localFileRefs );
|
2010-10-19 22:48:52 +00:00
|
|
|
// If the list has been modified since last time we cached it, update the cache
|
2013-01-15 23:32:10 +00:00
|
|
|
try {
|
|
|
|
|
if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) ) {
|
|
|
|
|
$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( $this->localFileRefs ),
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch ( Exception $e ) {
|
2013-05-02 02:13:13 +00:00
|
|
|
wfDebugLog( 'resourceloader', __METHOD__ . ": failed to update DB: $e" );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
return $styles;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-14 08:06:54 +00:00
|
|
|
/**
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2011-10-14 08:06:54 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
Fix the fixme on r88053: dependency handling was broken in debug mode in certain cases. More specifically, if A is a file module that depends on B, B is a wiki module that depends on C and C is a file module, the loading order is CBA (correct) in production mode but was BCA (wrong) in debug mode. Fixed this by URL-ifying scripts and styles for those modules in debug mode, as I said to on CR. What this means is that the initial debug=true request for a module will now always return arrays of URLs, never the JS or CSS itself. This was already the case for file modules (which returned arrays of URLs to the raw files), but not for other modules (which returned the JS and CSS itself). So for non-file modules, load.php?modules=foo&debug=true now returns some JS that instructs the loader to fetch the module's JS from load.php?modules=foo&debug=true&only=scripts and the CSS from ...&only=styles .
* Removed the magic behavior where ResourceLoaderModule::getScripts() and getStyles() could return an array of URLs where the documentation said they should return a JS/CSS string. Because I didn't restructure the calling code too much, the old magical behavior should still work.
* Instead, move this behavior to getScriptURLsForDebug() and getStyleURLsForDebug(). The default implementation constructs a single URL for a load.php request for the module with debug=true&only=scripts (or styles). The URL building code duplicates some things from OutputPage::makeResourceLoaderLink(), I'll clean that up later. ResourceLoaderFileModule overrides this method to return URLs to the raw files, using code that I removed from getScripts()/getStyles()
* Add ResourceLoaderModule::supportsURLLoading(), which returns true by default but may return false to indicate that a module does not support loading via a URL. This is needed to respect $this->debugRaw in ResourceLoaderFileModule (set to true for jquery and mediawiki), and obviously for the startup module as well, because we get bootstrapping problems otherwise (can't call mw.loader.implement() when the code for mw.loader isn't loaded yet)
2011-09-13 17:13:53 +00:00
|
|
|
public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
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.
|
Fix the fixme on r88053: dependency handling was broken in debug mode in certain cases. More specifically, if A is a file module that depends on B, B is a wiki module that depends on C and C is a file module, the loading order is CBA (correct) in production mode but was BCA (wrong) in debug mode. Fixed this by URL-ifying scripts and styles for those modules in debug mode, as I said to on CR. What this means is that the initial debug=true request for a module will now always return arrays of URLs, never the JS or CSS itself. This was already the case for file modules (which returned arrays of URLs to the raw files), but not for other modules (which returned the JS and CSS itself). So for non-file modules, load.php?modules=foo&debug=true now returns some JS that instructs the loader to fetch the module's JS from load.php?modules=foo&debug=true&only=scripts and the CSS from ...&only=styles .
* Removed the magic behavior where ResourceLoaderModule::getScripts() and getStyles() could return an array of URLs where the documentation said they should return a JS/CSS string. Because I didn't restructure the calling code too much, the old magical behavior should still work.
* Instead, move this behavior to getScriptURLsForDebug() and getStyleURLsForDebug(). The default implementation constructs a single URL for a load.php request for the module with debug=true&only=scripts (or styles). The URL building code duplicates some things from OutputPage::makeResourceLoaderLink(), I'll clean that up later. ResourceLoaderFileModule overrides this method to return URLs to the raw files, using code that I removed from getScripts()/getStyles()
* Add ResourceLoaderModule::supportsURLLoading(), which returns true by default but may return false to indicate that a module does not support loading via a URL. This is needed to respect $this->debugRaw in ResourceLoaderFileModule (set to true for jquery and mediawiki), and obviously for the startup module as well, because we get bootstrapping problems otherwise (can't call mw.loader.implement() when the code for mw.loader isn't loaded yet)
2011-09-13 17:13:53 +00:00
|
|
|
$urls = array();
|
|
|
|
|
foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
|
|
|
|
|
$urls[$mediaType] = array();
|
|
|
|
|
foreach ( $list as $file ) {
|
|
|
|
|
$urls[$mediaType][] = $this->getRemotePath( $file );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $urls;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 22:48:52 +00:00
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Gets list of message keys used by this module.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array List of message keys
|
2010-10-19 22:48:52 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
public function getMessages() {
|
|
|
|
|
return $this->messages;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 22:48:52 +00:00
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Gets the name of the group this module should be loaded in.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string Group name
|
2010-10-19 22:48:52 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
public function getGroup() {
|
|
|
|
|
return $this->group;
|
|
|
|
|
}
|
2011-05-21 17:45:20 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2011-04-07 12:07:25 +00:00
|
|
|
public function getPosition() {
|
|
|
|
|
return $this->position;
|
|
|
|
|
}
|
2010-10-19 18:25:42 +00:00
|
|
|
|
2010-10-19 22:48:52 +00:00
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Gets list of names of modules this module depends on.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array List of module names
|
2010-10-19 22:48:52 +00:00
|
|
|
*/
|
2010-10-19 18:25:42 +00:00
|
|
|
public function getDependencies() {
|
|
|
|
|
return $this->dependencies;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-30 21:06:51 +00:00
|
|
|
/**
|
|
|
|
|
* Get the skip function.
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
*/
|
|
|
|
|
public function getSkipFunction() {
|
|
|
|
|
if ( !$this->skipFunction ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$localPath = $this->getLocalPath( $this->skipFunction );
|
|
|
|
|
if ( !file_exists( $localPath ) ) {
|
|
|
|
|
throw new MWException( __METHOD__ . ": skip function file not found: \"$localPath\"" );
|
|
|
|
|
}
|
|
|
|
|
$contents = file_get_contents( $localPath );
|
2014-08-07 10:25:56 +00:00
|
|
|
if ( $this->getConfig()->get( 'ResourceLoaderValidateStaticJS' ) ) {
|
2014-04-30 21:06:51 +00:00
|
|
|
$contents = $this->validateScriptFile( $fileName, $contents );
|
|
|
|
|
}
|
|
|
|
|
return $contents;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-11 19:16:29 +00:00
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isRaw() {
|
|
|
|
|
return $this->raw;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Get the last modified timestamp of this module.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
2010-11-05 06:53:14 +00:00
|
|
|
* mode.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param ResourceLoaderContext $context Context in which to calculate
|
2010-11-05 06:53:14 +00:00
|
|
|
* the modified time
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return int UNIX timestamp
|
2010-11-05 18:33:50 +00:00
|
|
|
* @see ResourceLoaderModule::getFileDependencies
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
|
|
|
|
public function getModifiedTime( ResourceLoaderContext $context ) {
|
|
|
|
|
if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
|
|
|
|
|
return $this->modifiedTime[$context->getHash()];
|
|
|
|
|
}
|
|
|
|
|
wfProfileIn( __METHOD__ );
|
2011-06-17 16:05:05 +00:00
|
|
|
|
2010-10-19 23:47:56 +00:00
|
|
|
$files = array();
|
2011-06-17 16:05:05 +00:00
|
|
|
|
2010-10-19 23:47:56 +00:00
|
|
|
// Flatten style files into $files
|
|
|
|
|
$styles = self::collateFilePathListByOption( $this->styles, 'media', 'all' );
|
|
|
|
|
foreach ( $styles as $styleFiles ) {
|
|
|
|
|
$files = array_merge( $files, $styleFiles );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2014-02-28 19:23:11 +00:00
|
|
|
|
|
|
|
|
$skinFiles = self::collateFilePathListByOption(
|
|
|
|
|
self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ),
|
|
|
|
|
'media',
|
|
|
|
|
'all'
|
2010-10-19 18:25:42 +00:00
|
|
|
);
|
|
|
|
|
foreach ( $skinFiles as $styleFiles ) {
|
2010-10-19 23:47:56 +00:00
|
|
|
$files = array_merge( $files, $styleFiles );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
2011-06-17 16:05:05 +00:00
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
// Final merge, this should result in a master list of dependent files
|
|
|
|
|
$files = array_merge(
|
2010-10-19 23:47:56 +00:00
|
|
|
$files,
|
2010-10-19 18:25:42 +00:00
|
|
|
$this->scripts,
|
|
|
|
|
$context->getDebug() ? $this->debugScripts : array(),
|
2010-10-19 22:48:52 +00:00
|
|
|
self::tryForKey( $this->languageScripts, $context->getLanguage() ),
|
|
|
|
|
self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ),
|
2010-10-27 22:22:10 +00:00
|
|
|
$this->loaderScripts
|
2010-10-19 18:25:42 +00:00
|
|
|
);
|
2014-04-30 21:06:51 +00:00
|
|
|
if ( $this->skipFunction ) {
|
|
|
|
|
$files[] = $this->skipFunction;
|
|
|
|
|
}
|
2010-10-27 22:22:10 +00:00
|
|
|
$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() ) );
|
2011-06-17 16:05:05 +00:00
|
|
|
|
|
|
|
|
// If a module is nothing but a list of dependencies, we need to avoid
|
2010-11-05 06:53:14 +00:00
|
|
|
// giving max() an empty array
|
2010-10-20 20:43:30 +00:00
|
|
|
if ( count( $files ) === 0 ) {
|
2014-01-01 18:56:08 +00:00
|
|
|
$this->modifiedTime[$context->getHash()] = 1;
|
2011-02-10 16:39:53 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2014-01-01 18:56:08 +00:00
|
|
|
return $this->modifiedTime[$context->getHash()];
|
2010-10-20 20:43:30 +00:00
|
|
|
}
|
2011-06-17 16:05:05 +00:00
|
|
|
|
2013-02-03 18:30:03 +00:00
|
|
|
wfProfileIn( __METHOD__ . '-filemtime' );
|
2011-08-13 21:25:02 +00:00
|
|
|
$filesMtime = max( array_map( array( __CLASS__, 'safeFilemtime' ), $files ) );
|
2013-02-03 18:30:03 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-filemtime' );
|
2013-10-18 13:34:50 +00:00
|
|
|
|
2011-06-17 16:05:05 +00:00
|
|
|
$this->modifiedTime[$context->getHash()] = max(
|
|
|
|
|
$filesMtime,
|
2013-10-18 13:34:50 +00:00
|
|
|
$this->getMsgBlobMtime( $context->getLanguage() ),
|
|
|
|
|
$this->getDefinitionMtime( $context )
|
|
|
|
|
);
|
2011-02-10 16:39:53 +00:00
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $this->modifiedTime[$context->getHash()];
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-18 13:34:50 +00:00
|
|
|
/**
|
|
|
|
|
* Get the definition summary for this module.
|
|
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array
|
2013-10-18 13:34:50 +00:00
|
|
|
*/
|
|
|
|
|
public function getDefinitionSummary( ResourceLoaderContext $context ) {
|
|
|
|
|
$summary = array(
|
|
|
|
|
'class' => get_class( $this ),
|
|
|
|
|
);
|
|
|
|
|
foreach ( array(
|
|
|
|
|
'scripts',
|
|
|
|
|
'debugScripts',
|
|
|
|
|
'loaderScripts',
|
|
|
|
|
'styles',
|
|
|
|
|
'languageScripts',
|
|
|
|
|
'skinScripts',
|
|
|
|
|
'skinStyles',
|
|
|
|
|
'dependencies',
|
|
|
|
|
'messages',
|
|
|
|
|
'targets',
|
|
|
|
|
'group',
|
|
|
|
|
'position',
|
2014-04-30 21:06:51 +00:00
|
|
|
'skipFunction',
|
2013-10-18 13:34:50 +00:00
|
|
|
'localBasePath',
|
|
|
|
|
'remoteBasePath',
|
|
|
|
|
'debugRaw',
|
|
|
|
|
'raw',
|
|
|
|
|
) as $member ) {
|
|
|
|
|
$summary[$member] = $this->{$member};
|
|
|
|
|
};
|
|
|
|
|
return $summary;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-14 12:15:58 +00:00
|
|
|
/* Protected Methods */
|
2010-10-19 18:25:42 +00:00
|
|
|
|
2011-05-21 17:45:20 +00:00
|
|
|
/**
|
2014-06-26 14:29:31 +00:00
|
|
|
* @param string|ResourceLoaderFilePath $path
|
2011-05-21 17:45:20 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-10-27 22:22:10 +00:00
|
|
|
protected function getLocalPath( $path ) {
|
2014-06-26 14:29:31 +00:00
|
|
|
if ( $path instanceof ResourceLoaderFilePath ) {
|
|
|
|
|
return $path->getLocalPath();
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-27 22:22:10 +00:00
|
|
|
return "{$this->localBasePath}/$path";
|
|
|
|
|
}
|
2011-05-21 17:45:20 +00:00
|
|
|
|
|
|
|
|
/**
|
2014-06-26 14:29:31 +00:00
|
|
|
* @param string|ResourceLoaderFilePath $path
|
2011-05-21 17:45:20 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2010-10-27 22:22:10 +00:00
|
|
|
protected function getRemotePath( $path ) {
|
2014-06-26 14:29:31 +00:00
|
|
|
if ( $path instanceof ResourceLoaderFilePath ) {
|
|
|
|
|
return $path->getRemotePath();
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-27 22:22:10 +00:00
|
|
|
return "{$this->remoteBasePath}/$path";
|
2010-10-19 20:59:23 +00:00
|
|
|
}
|
|
|
|
|
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
/**
|
|
|
|
|
* Infer the stylesheet language from a stylesheet file path.
|
|
|
|
|
*
|
2013-09-27 09:32:41 +00:00
|
|
|
* @since 1.22
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
* @param string $path
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string The stylesheet language name
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
*/
|
2013-09-28 07:40:03 +00:00
|
|
|
public function getStyleSheetLang( $path ) {
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
return preg_match( '/\.less$/i', $path ) ? 'less' : 'css';
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 20:59:23 +00:00
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Collates file paths by option (where provided).
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $list List of file paths in any combination of index/path
|
2010-11-05 06:53:14 +00:00
|
|
|
* or path/options pairs
|
2014-04-20 21:33:05 +00:00
|
|
|
* @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
|
2010-10-19 20:59:23 +00:00
|
|
|
*/
|
|
|
|
|
protected static function collateFilePathListByOption( array $list, $option, $default ) {
|
|
|
|
|
$collatedFiles = array();
|
2013-04-02 20:28:40 +00:00
|
|
|
foreach ( (array)$list as $key => $value ) {
|
2010-10-19 20:59:23 +00:00
|
|
|
if ( is_int( $key ) ) {
|
|
|
|
|
// File name as the value
|
|
|
|
|
if ( !isset( $collatedFiles[$default] ) ) {
|
|
|
|
|
$collatedFiles[$default] = array();
|
|
|
|
|
}
|
|
|
|
|
$collatedFiles[$default][] = $value;
|
2011-06-17 16:05:05 +00:00
|
|
|
} elseif ( is_array( $value ) ) {
|
2010-10-19 20:59:23 +00:00
|
|
|
// File name as the key, options array as the value
|
2010-10-20 20:43:30 +00:00
|
|
|
$optionValue = isset( $value[$option] ) ? $value[$option] : $default;
|
|
|
|
|
if ( !isset( $collatedFiles[$optionValue] ) ) {
|
|
|
|
|
$collatedFiles[$optionValue] = array();
|
2010-10-19 20:59:23 +00:00
|
|
|
}
|
2010-10-20 20:43:30 +00:00
|
|
|
$collatedFiles[$optionValue][] = $key;
|
2010-10-19 20:59:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $collatedFiles;
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/**
|
2014-06-04 19:30:14 +00:00
|
|
|
* Get a list of element that match a key, optionally using a fallback key.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @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
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array List of elements from $map which matched $key or $fallback,
|
2014-06-04 19:30:14 +00:00
|
|
|
* or an empty list in case of no match
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
2010-10-20 20:56:33 +00:00
|
|
|
protected static function tryForKey( array $list, $key, $fallback = null ) {
|
2010-10-19 22:48:52 +00:00
|
|
|
if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
|
2010-10-20 20:56:33 +00:00
|
|
|
return $list[$key];
|
2011-06-17 16:05:05 +00:00
|
|
|
} elseif ( is_string( $fallback )
|
|
|
|
|
&& isset( $list[$fallback] )
|
2013-12-01 20:39:00 +00:00
|
|
|
&& is_array( $list[$fallback] )
|
|
|
|
|
) {
|
2010-10-20 20:56:33 +00:00
|
|
|
return $list[$fallback];
|
2010-10-19 22:48:52 +00:00
|
|
|
}
|
|
|
|
|
return array();
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
2011-05-14 12:15:58 +00:00
|
|
|
/**
|
2014-06-04 19:30:14 +00:00
|
|
|
* Get a list of file paths for all scripts in this module, in order of proper execution.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array List of file paths
|
2011-05-14 12:15:58 +00:00
|
|
|
*/
|
|
|
|
|
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 );
|
|
|
|
|
}
|
2012-10-21 21:06:00 +00:00
|
|
|
|
2014-06-26 14:29:31 +00:00
|
|
|
return array_unique( $files, SORT_REGULAR );
|
2011-05-14 12:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-06-04 19:30:14 +00:00
|
|
|
* Get a list of file paths for all styles in this module, in order of proper inclusion.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array List of file paths
|
2011-05-14 12:15:58 +00:00
|
|
|
*/
|
2014-06-04 19:30:14 +00:00
|
|
|
public function getStyleFiles( ResourceLoaderContext $context ) {
|
2011-05-14 12:15:58 +00:00
|
|
|
return array_merge_recursive(
|
|
|
|
|
self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
|
|
|
|
|
self::collateFilePathListByOption(
|
2014-02-28 19:23:11 +00:00
|
|
|
self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ),
|
|
|
|
|
'media',
|
|
|
|
|
'all'
|
2011-05-14 12:15:58 +00:00
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-28 07:40:03 +00:00
|
|
|
/**
|
2014-04-01 14:28:46 +00:00
|
|
|
* Gets a list of file paths for all skin styles in the module used by
|
|
|
|
|
* the skin.
|
|
|
|
|
*
|
|
|
|
|
* @param string $skinName The name of the skin
|
|
|
|
|
* @return array A list of file paths collated by media type
|
|
|
|
|
*/
|
|
|
|
|
protected function getSkinStyleFiles( $skinName ) {
|
|
|
|
|
return self::collateFilePathListByOption(
|
|
|
|
|
self::tryForKey( $this->skinStyles, $skinName ),
|
|
|
|
|
'media',
|
|
|
|
|
'all'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a list of file paths for all skin style files in the module,
|
|
|
|
|
* for all available skins.
|
|
|
|
|
*
|
|
|
|
|
* @return array A list of file paths collated by media type
|
|
|
|
|
*/
|
|
|
|
|
protected function getAllSkinStyleFiles() {
|
|
|
|
|
$styleFiles = array();
|
|
|
|
|
$internalSkinNames = array_keys( Skin::getSkinNames() );
|
|
|
|
|
$internalSkinNames[] = 'default';
|
|
|
|
|
|
|
|
|
|
foreach ( $internalSkinNames as $internalSkinName ) {
|
|
|
|
|
$styleFiles = array_merge_recursive(
|
|
|
|
|
$styleFiles,
|
|
|
|
|
$this->getSkinStyleFiles( $internalSkinName )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $styleFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns all style files and all skin style files used by this module.
|
|
|
|
|
*
|
2013-09-28 07:40:03 +00:00
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getAllStyleFiles() {
|
2014-04-01 14:28:46 +00:00
|
|
|
$collatedStyleFiles = array_merge_recursive(
|
|
|
|
|
self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
|
|
|
|
|
$this->getAllSkinStyleFiles()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
|
|
foreach ( $collatedStyleFiles as $media => $styleFiles ) {
|
|
|
|
|
foreach ( $styleFiles as $styleFile ) {
|
|
|
|
|
$result[] = $this->getLocalPath( $styleFile );
|
2013-09-28 07:40:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-04-01 14:28:46 +00:00
|
|
|
|
|
|
|
|
return $result;
|
2013-09-28 07:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-19 18:25:42 +00:00
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Gets the contents of a list of JavaScript files.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $scripts List of file paths to scripts to read, remap and concetenate
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string Concatenated and remapped JavaScript data from $scripts
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
2010-10-27 22:22:10 +00:00
|
|
|
protected function readScriptFiles( array $scripts ) {
|
2010-10-19 22:48:52 +00:00
|
|
|
if ( empty( $scripts ) ) {
|
2010-10-19 18:25:42 +00:00
|
|
|
return '';
|
|
|
|
|
}
|
2010-11-05 06:53:14 +00:00
|
|
|
$js = '';
|
2014-06-26 14:29:31 +00:00
|
|
|
foreach ( array_unique( $scripts, SORT_REGULAR ) as $fileName ) {
|
2010-11-05 06:53:14 +00:00
|
|
|
$localPath = $this->getLocalPath( $fileName );
|
2011-08-13 21:25:02 +00:00
|
|
|
if ( !file_exists( $localPath ) ) {
|
2013-02-03 18:30:03 +00:00
|
|
|
throw new MWException( __METHOD__ . ": script file not found: \"$localPath\"" );
|
2010-11-05 06:53:14 +00:00
|
|
|
}
|
2011-08-13 21:25:02 +00:00
|
|
|
$contents = file_get_contents( $localPath );
|
2014-08-07 10:25:56 +00:00
|
|
|
if ( $this->getConfig()->get( 'ResourceLoaderValidateStaticJS' ) ) {
|
2011-07-11 21:39:06 +00:00
|
|
|
// 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 );
|
|
|
|
|
}
|
2010-11-05 06:53:14 +00:00
|
|
|
$js .= $contents . "\n";
|
|
|
|
|
}
|
|
|
|
|
return $js;
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Gets the contents of a list of CSS files.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param array $styles List of media type/list of file paths pairs, to read, remap and
|
2011-05-14 12:15:58 +00:00
|
|
|
* concetenate
|
2011-05-21 17:45:20 +00:00
|
|
|
*
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param bool $flip
|
2011-05-21 17:45:20 +00:00
|
|
|
*
|
2014-01-30 18:39:51 +00:00
|
|
|
* @throws MWException
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array List of concatenated and remapped CSS data from $styles,
|
2010-11-05 06:53:14 +00:00
|
|
|
* keyed by media type
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
2014-06-04 19:30:14 +00:00
|
|
|
public function readStyleFiles( array $styles, $flip ) {
|
2010-10-19 22:48:52 +00:00
|
|
|
if ( empty( $styles ) ) {
|
|
|
|
|
return array();
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
foreach ( $styles as $media => $files ) {
|
2014-06-26 14:29:31 +00:00
|
|
|
$uniqueFiles = array_unique( $files, SORT_REGULAR );
|
2014-01-30 18:39:51 +00:00
|
|
|
$styleFiles = array();
|
|
|
|
|
foreach ( $uniqueFiles as $file ) {
|
|
|
|
|
$styleFiles[] = $this->readStyleFile( $file, $flip );
|
|
|
|
|
}
|
|
|
|
|
$styles[$media] = implode( "\n", $styleFiles );
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|
|
|
|
|
return $styles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2010-10-20 00:22:25 +00:00
|
|
|
* Reads a style file.
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2010-10-19 22:48:52 +00:00
|
|
|
* This method can be used as a callback for array_map()
|
2011-06-17 16:05:05 +00:00
|
|
|
*
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $path File path of style file to read
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param bool $flip
|
2011-05-21 17:45:20 +00:00
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string CSS data in script file
|
2014-07-24 17:43:25 +00:00
|
|
|
* @throws MWException If the file doesn't exist
|
2010-10-19 18:25:42 +00:00
|
|
|
*/
|
2011-06-17 16:05:05 +00:00
|
|
|
protected function readStyleFile( $path, $flip ) {
|
2010-11-05 06:53:14 +00:00
|
|
|
$localPath = $this->getLocalPath( $path );
|
2014-06-26 14:29:31 +00:00
|
|
|
$remotePath = $this->getRemotePath( $path );
|
2011-08-13 21:25:02 +00:00
|
|
|
if ( !file_exists( $localPath ) ) {
|
2013-02-03 18:30:03 +00:00
|
|
|
$msg = __METHOD__ . ": style file not found: \"$localPath\"";
|
2012-10-15 20:32:20 +00:00
|
|
|
wfDebugLog( 'resourceloader', $msg );
|
2012-10-15 17:12:10 +00:00
|
|
|
throw new MWException( $msg );
|
2010-11-05 06:53:14 +00:00
|
|
|
}
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
|
2014-06-26 14:29:31 +00:00
|
|
|
if ( $this->getStyleSheetLang( $localPath ) === 'less' ) {
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
$style = $this->compileLESSFile( $localPath );
|
|
|
|
|
$this->hasGeneratedStyles = true;
|
|
|
|
|
} else {
|
|
|
|
|
$style = file_get_contents( $localPath );
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-16 19:31:48 +00:00
|
|
|
if ( $flip ) {
|
|
|
|
|
$style = CSSJanus::transform( $style, true, false );
|
|
|
|
|
}
|
2014-06-26 14:29:31 +00:00
|
|
|
$localDir = dirname( $localPath );
|
|
|
|
|
$remoteDir = dirname( $remotePath );
|
2010-10-27 19:59:10 +00:00
|
|
|
// Get and register local file references
|
2011-06-17 16:05:05 +00:00
|
|
|
$this->localFileRefs = array_merge(
|
|
|
|
|
$this->localFileRefs,
|
2014-06-26 14:29:31 +00:00
|
|
|
CSSMin::getLocalFileReferences( $style, $localDir )
|
ResourceLoader: Refactor style loading
Fixes:
* bug 31676: Work around IE stylesheet limit.
* bug 35562: @import styles broken in modules that combine
multiple stylesheets.
* bug 40498: Don't output empty "@media print { }" blocks.
* bug 40500: Don't ignore media-type for urls in debug mode.
Approach:
* Re-use the same <style> tag so that we stay under the 31
stylesheet limit in IE. Unless the to-be-added css text from
the being-loaded module contains @import, in which case we do
create a new <style> tag and then re-use that one from that
point on (bug 31676).
* Return stylesheets as arrays, instead of a concatenated string.
This fixes bug 35562, because @import only works when at the
top of a stylesheet. By not unconditionally concatenating files
within a module on the server side already, @import will work
in e.g. module 'site' that contains 2 wiki pages.
This is normalized in ResourceLoader::makeCombinedStyles(),
so far only ResourceLoaderWikiModule makes use of this.
Misc. clean up and bug fixes:
* Reducing usage of jQuery() and mw.html.element() where
native DOM would be very simple and faster. Aside from
simplicity and speed, this is also working towards a more
stand-alone ResourceLoader.
* Trim server output a little bit more
- Redundant new line after minify-css (it is now an array, so
no need to keep space afterwards)
- Redundant semi-colon after minify-js if it ends in a colon
* Allow space in styleTest.css.php
* Clean up and extend unit tests to cover for these features
and bug fixes.
* Don't set styleEl.rel = 'stylesheet'; that has no business
on a <style> tag.
* Fix bug in mw.loader's addStyleTag(). It turns out IE6
has an odd security measure that does not allow manipulation
of elements (at least style tags) that are created by a
different script (even if that script was served from the same
domain/origin etc.). We didn't ran into this before because
we only created new style tags, never appended to them. Now
that we do, this came up. Took a while to figure out because
it was created by mediawiki.js but it calls jQuery which did
the actual dom insertion. Odd thing is, we load jquery.js and
mediawiki.js in the same request even...
Without this all css-url related mw.loader tests would fail
in IE6.
* mediawiki.js and mediawiki.test.js now pass jshint again.
Tested (and passing qunit/?module=mediawiki; 123 of 123):
* Chrome 14, 21
* Firefox 3.0, 3.6, 4, 7, 14, 15, 16beta
* IE 6, 7, 8, 9
* Safari 4.0, 5.0, 5.1
* Opera 10.0, 11.1, 11.5, 11.6, 12.0, 12.5beta
* iPhone 3GS / iOS 3.0 / Mobile Safari 4.0
iPhone 4 / iOS 4.0.1 / Mobile Safari 4.0.5
iPhone 4S / iOS 6.0 Beta / Mobile Safari 6.0
Change-Id: I3e8227ddb87fd9441071ca935439fc6467751dab
2012-07-25 21:20:21 +00:00
|
|
|
);
|
2010-10-19 18:25:42 +00:00
|
|
|
return CSSMin::remap(
|
2014-06-26 14:29:31 +00:00
|
|
|
$style, $localDir, $remoteDir, true
|
2010-10-19 18:25:42 +00:00
|
|
|
);
|
|
|
|
|
}
|
2011-10-14 08:06:54 +00:00
|
|
|
|
2011-03-25 11:15:40 +00:00
|
|
|
/**
|
|
|
|
|
* Get whether CSS for this module should be flipped
|
2013-09-13 04:50:45 +00:00
|
|
|
* @param ResourceLoaderContext $context
|
2011-03-25 11:15:40 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function getFlip( $context ) {
|
|
|
|
|
return $context->getDirection() === 'rtl';
|
|
|
|
|
}
|
2012-10-11 22:37:59 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
|
|
|
|
|
*
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return array Array of strings
|
2012-10-11 22:37:59 +00:00
|
|
|
*/
|
|
|
|
|
public function getTargets() {
|
|
|
|
|
return $this->targets;
|
|
|
|
|
}
|
|
|
|
|
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
/**
|
|
|
|
|
* Generate a cache key for a LESS file.
|
2013-09-27 09:32:41 +00:00
|
|
|
*
|
2013-10-08 21:47:42 +00:00
|
|
|
* The cache key varies on the file name and the names and values of global
|
|
|
|
|
* LESS variables.
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
*
|
2013-09-27 09:32:41 +00:00
|
|
|
* @since 1.22
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
* @param string $fileName File name of root LESS file.
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string Cache key
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
*/
|
2014-08-07 10:25:56 +00:00
|
|
|
protected function getLESSCacheKey( $fileName ) {
|
|
|
|
|
$vars = json_encode( ResourceLoader::getLESSVars( $this->getConfig() ) );
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
$hash = md5( $fileName . $vars );
|
2013-10-08 21:47:42 +00:00
|
|
|
return wfMemcKey( 'resourceloader', 'less', $hash );
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2013-09-27 09:32:41 +00:00
|
|
|
* @since 1.22
|
2013-10-17 10:33:26 +00:00
|
|
|
* @throws Exception If Less encounters a parse error
|
|
|
|
|
* @throws MWException If Less compilation returns unexpection result
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
* @param string $fileName File path of LESS source
|
2014-04-20 21:33:05 +00:00
|
|
|
* @return string CSS source
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
*/
|
|
|
|
|
protected function compileLESSFile( $fileName ) {
|
2014-08-07 10:25:56 +00:00
|
|
|
$key = $this->getLESSCacheKey( $fileName );
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-07 10:25:56 +00:00
|
|
|
$compiler = ResourceLoader::getLessCompiler( $this->getConfig() );
|
2013-10-08 21:47:42 +00:00
|
|
|
$result = null;
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
|
2013-10-08 21:47:42 +00:00
|
|
|
$result = $compiler->cachedCompile( $source );
|
|
|
|
|
|
|
|
|
|
if ( !is_array( $result ) ) {
|
2014-05-10 08:39:37 +00:00
|
|
|
throw new MWException( 'LESS compiler result has type '
|
|
|
|
|
. gettype( $result ) . '; array expected.' );
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
}
|
2013-10-08 21:47:42 +00:00
|
|
|
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
$this->localFileRefs += array_keys( $result['files'] );
|
2013-10-08 21:47:42 +00:00
|
|
|
$cache->set( $key, $result );
|
Support LESS stylesheets in ResourceLoader
This patch adds support for the LESS stylesheet language to ResourceLoader.
LESS is a stylesheet language that compiles into CSS. The patch includes
lessphp, a LESS compiler implemented in PHP. The rationale for choosing LESS is
explained in a MediaWiki RFC which accompanies this patch, available at
<https://www.mediawiki.org/wiki/Requests_for_comment/LESS>.
LESS support is provided for ResourceLoader file modules. It is triggered by
the presence of the '.less' extension in stylesheet filenames. LESS files are
compiled by lessc, and the resultant CSS is subjected to the standard set of
transformations (CSSJanus & CSSMin). The immediate result of LESS compilation
is encoded as an array, which includes the list of LESS files that were
compiled and their mtimes. This array is cached. Cache invalidation is
performed by comparing the cached mtimes with the mtimes of the files on disk.
If the compiler itself throws an exception, ResourceLoader constructs a
compilation result which consists of the error message encoded as a CSS
comment. Failed compilation results are cached too, but with an expiration time
of five minutes. The expiration time is required because the full list of
referenced files is not known.
Three configuration variables configure the global environment for LESS
modules: $wgResourceLoaderLESSVars, $wgResourceLoaderLESSFunctions, and
$wgResourceLoaderLESSImportPaths. $wgResourceLoaderLESSVars maps variable names
to CSS values, specified as strings. Variables declared in this array are
available in all LESS files. $wgResourceLoaderLESSFunctions is similar, except
it maps custom function names to PHP callables. These functions can be called
from within LESS to transform values. Read more about custom functions at
<http://leafo.net/lessphp/docs/#custom_functions>. Finally,
$wgResourceLoaderLESSImportPaths specifies file system paths in addition to the
current module's path where the LESS compiler should look up files referenced
in @import statements.
The issue of handling of /* @embed */ and /* @noflip */ annotations is left
unresolved. Earlier versions of this patch included an @embed analog
implemented as a LESS custom function, but there was enough ambiguity about
whether the strategy it took was optimal to merit discussing it in a separate,
follow-up patch.
Bug: 40964
Change-Id: Id052a04dd2f76a1f4aef39fbd454bd67f5fd282f
2013-08-11 16:40:20 +00:00
|
|
|
return $result['compiled'];
|
|
|
|
|
}
|
2010-10-19 18:25:42 +00:00
|
|
|
}
|