3 * Resource loader module based on local JavaScript/CSS files.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Trevor Parscal
22 * @author Roan Kattouw
26 * ResourceLoader module based on local JavaScript/CSS files.
28 class ResourceLoaderFileModule
extends ResourceLoaderModule
{
29 /* Protected Members */
31 /** @var string Local base path, see __construct() */
32 protected $localBasePath = '';
34 /** @var string Remote base path, see __construct() */
35 protected $remoteBasePath = '';
37 /** @var array Saves a list of the templates named by the modules. */
38 protected $templates = array();
41 * @var array List of paths to JavaScript files to always include
44 * array( [file-path], [file-path], ... )
47 protected $scripts = array();
50 * @var array List of JavaScript files to include when using a specific language
53 * array( [language-code] => array( [file-path], [file-path], ... ), ... )
56 protected $languageScripts = array();
59 * @var array List of JavaScript files to include when using a specific skin
62 * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
65 protected $skinScripts = array();
68 * @var array List of paths to JavaScript files to include in debug mode
71 * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
74 protected $debugScripts = array();
77 * @var array List of paths to JavaScript files to include in the startup module
80 * array( [file-path], [file-path], ... )
83 protected $loaderScripts = array();
86 * @var array List of paths to CSS files to always include
89 * array( [file-path], [file-path], ... )
92 protected $styles = array();
95 * @var array List of paths to CSS files to include when using specific skins
98 * array( [file-path], [file-path], ... )
101 protected $skinStyles = array();
104 * @var array List of modules this module depends on
107 * array( [file-path], [file-path], ... )
110 protected $dependencies = array();
113 * @var string File name containing the body of the skip function
115 protected $skipFunction = null;
118 * @var array List of message keys used by this module
121 * array( [message-key], [message-key], ... )
124 protected $messages = array();
126 /** @var string Name of group to load this module in */
129 /** @var string Position on the page to load this module at */
130 protected $position = 'bottom';
132 /** @var bool Link to raw files in debug mode */
133 protected $debugRaw = true;
135 /** @var bool Whether mw.loader.state() call should be omitted */
136 protected $raw = false;
138 protected $targets = array( 'desktop' );
141 * @var bool Whether getStyleURLsForDebug should return raw file paths,
142 * or return load.php urls
144 protected $hasGeneratedStyles = false;
147 * @var array Place where readStyleFile() tracks file dependencies
150 * array( [file-path], [file-path], ... )
153 protected $localFileRefs = array();
156 * @var array Place where readStyleFile() tracks file dependencies for non-existent files.
157 * Used in tests to detect missing dependencies.
159 protected $missingLocalFileRefs = array();
164 * Constructs a new module from an options array.
166 * @param array $options List of options; if not given or empty, an empty module will be
168 * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
170 * @param string $remoteBasePath Base path to prepend to all remote paths in $options. Defaults
171 * to $wgResourceBasePath
173 * Below is a description for the $options array:
174 * @throws InvalidArgumentException
175 * @par Construction options:
178 * // Base path to prepend to all local paths in $options. Defaults to $IP
179 * 'localBasePath' => [base path],
180 * // Base path to prepend to all remote paths in $options. Defaults to $wgResourceBasePath
181 * 'remoteBasePath' => [base path],
182 * // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
183 * 'remoteExtPath' => [base path],
184 * // Equivalent of remoteBasePath, but relative to $wgStylePath
185 * 'remoteSkinPath' => [base path],
186 * // Scripts to always include
187 * 'scripts' => [file path string or array of file path strings],
188 * // Scripts to include in specific language contexts
189 * 'languageScripts' => array(
190 * [language code] => [file path string or array of file path strings],
192 * // Scripts to include in specific skin contexts
193 * 'skinScripts' => array(
194 * [skin name] => [file path string or array of file path strings],
196 * // Scripts to include in debug contexts
197 * 'debugScripts' => [file path string or array of file path strings],
198 * // Scripts to include in the startup module
199 * 'loaderScripts' => [file path string or array of file path strings],
200 * // Modules which must be loaded before this module
201 * 'dependencies' => [module name string or array of module name strings],
202 * 'templates' => array(
203 * [template alias with file.ext] => [file path to a template file],
205 * // Styles to always load
206 * 'styles' => [file path string or array of file path strings],
207 * // Styles to include in specific skin contexts
208 * 'skinStyles' => array(
209 * [skin name] => [file path string or array of file path strings],
211 * // Messages to always load
212 * 'messages' => [array of message key strings],
213 * // Group which this module should be loaded together with
214 * 'group' => [group name string],
215 * // Position on the page to load this module at
216 * 'position' => ['bottom' (default) or 'top']
217 * // Function that, if it returns true, makes the loader skip this module.
218 * // The file must contain valid JavaScript for execution in a private function.
219 * // The file must not contain the "function () {" and "}" wrapper though.
220 * 'skipFunction' => [file path]
224 public function __construct(
226 $localBasePath = null,
227 $remoteBasePath = null
229 // Flag to decide whether to automagically add the mediawiki.template module
230 $hasTemplates = false;
231 // localBasePath and remoteBasePath both have unbelievably long fallback chains
232 // and need to be handled separately.
233 list( $this->localBasePath
, $this->remoteBasePath
) =
234 self
::extractBasePaths( $options, $localBasePath, $remoteBasePath );
236 // Extract, validate and normalise remaining options
237 foreach ( $options as $member => $option ) {
239 // Lists of file paths
242 case 'loaderScripts':
244 $this->{$member} = (array)$option;
247 $hasTemplates = true;
248 $this->{$member} = (array)$option;
250 // Collated lists of file paths
251 case 'languageScripts':
254 if ( !is_array( $option ) ) {
255 throw new InvalidArgumentException(
256 "Invalid collated file path list error. " .
257 "'$option' given, array expected."
260 foreach ( $option as $key => $value ) {
261 if ( !is_string( $key ) ) {
262 throw new InvalidArgumentException(
263 "Invalid collated file path list key error. " .
264 "'$key' given, string expected."
267 $this->{$member}[$key] = (array)$value;
275 $option = array_values( array_unique( (array)$option ) );
278 $this->{$member} = $option;
282 $this->isPositionDefined
= true;
285 $this->{$member} = (string)$option;
290 $this->{$member} = (bool)$option;
294 if ( $hasTemplates ) {
295 $this->dependencies
[] = 'mediawiki.template';
296 // Ensure relevant template compiler module gets loaded
297 foreach ( $this->templates
as $alias => $templatePath ) {
298 if ( is_int( $alias ) ) {
299 $alias = $templatePath;
301 $suffix = explode( '.', $alias );
302 $suffix = end( $suffix );
303 $compilerModule = 'mediawiki.template.' . $suffix;
304 if ( $suffix !== 'html' && !in_array( $compilerModule, $this->dependencies
) ) {
305 $this->dependencies
[] = $compilerModule;
312 * Extract a pair of local and remote base paths from module definition information.
313 * Implementation note: the amount of global state used in this function is staggering.
315 * @param array $options Module definition
316 * @param string $localBasePath Path to use if not provided in module definition. Defaults
318 * @param string $remoteBasePath Path to use if not provided in module definition. Defaults
319 * to $wgResourceBasePath
320 * @return array Array( localBasePath, remoteBasePath )
322 public static function extractBasePaths(
324 $localBasePath = null,
325 $remoteBasePath = null
327 global $IP, $wgResourceBasePath;
329 // The different ways these checks are done, and their ordering, look very silly,
330 // but were preserved for backwards-compatibility just in case. Tread lightly.
332 if ( $localBasePath === null ) {
333 $localBasePath = $IP;
335 if ( $remoteBasePath === null ) {
336 $remoteBasePath = $wgResourceBasePath;
339 if ( isset( $options['remoteExtPath'] ) ) {
340 global $wgExtensionAssetsPath;
341 $remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
344 if ( isset( $options['remoteSkinPath'] ) ) {
346 $remoteBasePath = $wgStylePath . '/' . $options['remoteSkinPath'];
349 if ( array_key_exists( 'localBasePath', $options ) ) {
350 $localBasePath = (string)$options['localBasePath'];
353 if ( array_key_exists( 'remoteBasePath', $options ) ) {
354 $remoteBasePath = (string)$options['remoteBasePath'];
357 // Make sure the remote base path is a complete valid URL,
358 // but possibly protocol-relative to avoid cache pollution
359 $remoteBasePath = wfExpandUrl( $remoteBasePath, PROTO_RELATIVE
);
361 return array( $localBasePath, $remoteBasePath );
365 * Gets all scripts for a given context concatenated together.
367 * @param ResourceLoaderContext $context Context in which to generate script
368 * @return string JavaScript code for $context
370 public function getScript( ResourceLoaderContext
$context ) {
371 $files = $this->getScriptFiles( $context );
372 return $this->readScriptFiles( $files );
376 * @param ResourceLoaderContext $context
379 public function getScriptURLsForDebug( ResourceLoaderContext
$context ) {
381 foreach ( $this->getScriptFiles( $context ) as $file ) {
382 $urls[] = $this->getRemotePath( $file );
390 public function supportsURLLoading() {
391 return $this->debugRaw
;
397 * @return string|bool JavaScript code to be added to startup module
399 public function getLoaderScript() {
400 if ( count( $this->loaderScripts
) === 0 ) {
403 return $this->readScriptFiles( $this->loaderScripts
);
407 * Get all styles for a given context.
409 * @param ResourceLoaderContext $context
410 * @return array CSS code for $context as an associative array mapping media type to CSS text.
412 public function getStyles( ResourceLoaderContext
$context ) {
413 $styles = $this->readStyleFiles(
414 $this->getStyleFiles( $context ),
415 $this->getFlip( $context ),
418 // Collect referenced files
419 $this->localFileRefs
= array_unique( $this->localFileRefs
);
420 // If the list has been modified since last time we cached it, update the cache
422 if ( $this->localFileRefs
!== $this->getFileDependencies( $context->getSkin() ) ) {
423 $dbw = wfGetDB( DB_MASTER
);
424 $dbw->replace( 'module_deps',
425 array( array( 'md_module', 'md_skin' ) ), array(
426 'md_module' => $this->getName(),
427 'md_skin' => $context->getSkin(),
428 'md_deps' => FormatJson
::encode( $this->localFileRefs
),
432 } catch ( Exception
$e ) {
433 wfDebugLog( 'resourceloader', __METHOD__
. ": failed to update DB: $e" );
439 * @param ResourceLoaderContext $context
442 public function getStyleURLsForDebug( ResourceLoaderContext
$context ) {
443 if ( $this->hasGeneratedStyles
) {
444 // Do the default behaviour of returning a url back to load.php
445 // but with only=styles.
446 return parent
::getStyleURLsForDebug( $context );
448 // Our module consists entirely of real css files,
449 // in debug mode we can load those directly.
451 foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
452 $urls[$mediaType] = array();
453 foreach ( $list as $file ) {
454 $urls[$mediaType][] = $this->getRemotePath( $file );
461 * Gets list of message keys used by this module.
463 * @return array List of message keys
465 public function getMessages() {
466 return $this->messages
;
470 * Gets the name of the group this module should be loaded in.
472 * @return string Group name
474 public function getGroup() {
481 public function getPosition() {
482 return $this->position
;
486 * Gets list of names of modules this module depends on.
487 * @param ResourceLoaderContext context
488 * @return array List of module names
490 public function getDependencies( ResourceLoaderContext
$context = null ) {
491 return $this->dependencies
;
495 * Get the skip function.
496 * @return null|string
497 * @throws MWException
499 public function getSkipFunction() {
500 if ( !$this->skipFunction
) {
504 $localPath = $this->getLocalPath( $this->skipFunction
);
505 if ( !file_exists( $localPath ) ) {
506 throw new MWException( __METHOD__
. ": skip function file not found: \"$localPath\"" );
508 $contents = file_get_contents( $localPath );
509 if ( $this->getConfig()->get( 'ResourceLoaderValidateStaticJS' ) ) {
510 $contents = $this->validateScriptFile( $localPath, $contents );
518 public function isRaw() {
523 * Disable module content versioning.
525 * This class uses getDefinitionSummary() instead, to avoid filesystem overhead
526 * involved with building the full module content inside a startup request.
530 public function enableModuleContentVersion() {
535 * Helper method to gather file hashes for getDefinitionSummary.
537 * This function is context-sensitive, only computing hashes of files relevant to the
538 * given language, skin, etc.
540 * @see ResourceLoaderModule::getFileDependencies
541 * @param ResourceLoaderContext $context
544 protected function getFileHashes( ResourceLoaderContext
$context ) {
547 // Flatten style files into $files
548 $styles = self
::collateFilePathListByOption( $this->styles
, 'media', 'all' );
549 foreach ( $styles as $styleFiles ) {
550 $files = array_merge( $files, $styleFiles );
553 $skinFiles = self
::collateFilePathListByOption(
554 self
::tryForKey( $this->skinStyles
, $context->getSkin(), 'default' ),
558 foreach ( $skinFiles as $styleFiles ) {
559 $files = array_merge( $files, $styleFiles );
562 // Final merge, this should result in a master list of dependent files
563 $files = array_merge(
567 $context->getDebug() ?
$this->debugScripts
: array(),
568 $this->getLanguageScripts( $context->getLanguage() ),
569 self
::tryForKey( $this->skinScripts
, $context->getSkin(), 'default' ),
572 if ( $this->skipFunction
) {
573 $files[] = $this->skipFunction
;
575 $files = array_map( array( $this, 'getLocalPath' ), $files );
576 // File deps need to be treated separately because they're already prefixed
577 $files = array_merge( $files, $this->getFileDependencies( $context->getSkin() ) );
578 // Filter out any duplicates from getFileDependencies() and others.
579 // Most commonly introduced by compileLessFile(), which always includes the
580 // entry point Less file we already know about.
581 $files = array_values( array_unique( $files ) );
583 // Don't include keys or file paths here, only the hashes. Including that would needlessly
584 // cause global cache invalidation when files move or if e.g. the MediaWiki path changes.
585 // Any significant ordering is already detected by the definition summary.
586 return array_map( array( __CLASS__
, 'safeFileHash' ), $files );
590 * Get the definition summary for this module.
592 * @param ResourceLoaderContext $context
595 public function getDefinitionSummary( ResourceLoaderContext
$context ) {
596 $summary = parent
::getDefinitionSummary( $context );
600 // The following properties are omitted because they don't affect the module reponse:
601 // - localBasePath (Per T104950; Changes when absolute directory name changes. If
602 // this affects 'scripts' and other file paths, getFileHashes accounts for that.)
603 // - remoteBasePath (Per T104950)
604 // - dependencies (provided via startup module)
606 // - group (provided via startup module)
607 // - position (only used by OutputPage)
621 $options[$member] = $this->{$member};
625 'options' => $options,
626 'fileHashes' => $this->getFileHashes( $context ),
627 'msgBlobMtime' => $this->getMsgBlobMtime( $context->getLanguage() ),
633 * @param string|ResourceLoaderFilePath $path
636 protected function getLocalPath( $path ) {
637 if ( $path instanceof ResourceLoaderFilePath
) {
638 return $path->getLocalPath();
641 return "{$this->localBasePath}/$path";
645 * @param string|ResourceLoaderFilePath $path
648 protected function getRemotePath( $path ) {
649 if ( $path instanceof ResourceLoaderFilePath
) {
650 return $path->getRemotePath();
653 return "{$this->remoteBasePath}/$path";
657 * Infer the stylesheet language from a stylesheet file path.
660 * @param string $path
661 * @return string The stylesheet language name
663 public function getStyleSheetLang( $path ) {
664 return preg_match( '/\.less$/i', $path ) ?
'less' : 'css';
668 * Collates file paths by option (where provided).
670 * @param array $list List of file paths in any combination of index/path
671 * or path/options pairs
672 * @param string $option Option name
673 * @param mixed $default Default value if the option isn't set
674 * @return array List of file paths, collated by $option
676 protected static function collateFilePathListByOption( array $list, $option, $default ) {
677 $collatedFiles = array();
678 foreach ( (array)$list as $key => $value ) {
679 if ( is_int( $key ) ) {
680 // File name as the value
681 if ( !isset( $collatedFiles[$default] ) ) {
682 $collatedFiles[$default] = array();
684 $collatedFiles[$default][] = $value;
685 } elseif ( is_array( $value ) ) {
686 // File name as the key, options array as the value
687 $optionValue = isset( $value[$option] ) ?
$value[$option] : $default;
688 if ( !isset( $collatedFiles[$optionValue] ) ) {
689 $collatedFiles[$optionValue] = array();
691 $collatedFiles[$optionValue][] = $key;
694 return $collatedFiles;
698 * Get a list of element that match a key, optionally using a fallback key.
700 * @param array $list List of lists to select from
701 * @param string $key Key to look for in $map
702 * @param string $fallback Key to look for in $list if $key doesn't exist
703 * @return array List of elements from $map which matched $key or $fallback,
704 * or an empty list in case of no match
706 protected static function tryForKey( array $list, $key, $fallback = null ) {
707 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
709 } elseif ( is_string( $fallback )
710 && isset( $list[$fallback] )
711 && is_array( $list[$fallback] )
713 return $list[$fallback];
719 * Get a list of file paths for all scripts in this module, in order of proper execution.
721 * @param ResourceLoaderContext $context
722 * @return array List of file paths
724 protected function getScriptFiles( ResourceLoaderContext
$context ) {
725 $files = array_merge(
727 $this->getLanguageScripts( $context->getLanguage() ),
728 self
::tryForKey( $this->skinScripts
, $context->getSkin(), 'default' )
730 if ( $context->getDebug() ) {
731 $files = array_merge( $files, $this->debugScripts
);
734 return array_unique( $files, SORT_REGULAR
);
738 * Get the set of language scripts for the given language,
739 * possibly using a fallback language.
741 * @param string $lang
744 private function getLanguageScripts( $lang ) {
745 $scripts = self
::tryForKey( $this->languageScripts
, $lang );
749 $fallbacks = Language
::getFallbacksFor( $lang );
750 foreach ( $fallbacks as $lang ) {
751 $scripts = self
::tryForKey( $this->languageScripts
, $lang );
761 * Get a list of file paths for all styles in this module, in order of proper inclusion.
763 * @param ResourceLoaderContext $context
764 * @return array List of file paths
766 public function getStyleFiles( ResourceLoaderContext
$context ) {
767 return array_merge_recursive(
768 self
::collateFilePathListByOption( $this->styles
, 'media', 'all' ),
769 self
::collateFilePathListByOption(
770 self
::tryForKey( $this->skinStyles
, $context->getSkin(), 'default' ),
778 * Gets a list of file paths for all skin styles in the module used by
781 * @param string $skinName The name of the skin
782 * @return array A list of file paths collated by media type
784 protected function getSkinStyleFiles( $skinName ) {
785 return self
::collateFilePathListByOption(
786 self
::tryForKey( $this->skinStyles
, $skinName ),
793 * Gets a list of file paths for all skin style files in the module,
794 * for all available skins.
796 * @return array A list of file paths collated by media type
798 protected function getAllSkinStyleFiles() {
799 $styleFiles = array();
800 $internalSkinNames = array_keys( Skin
::getSkinNames() );
801 $internalSkinNames[] = 'default';
803 foreach ( $internalSkinNames as $internalSkinName ) {
804 $styleFiles = array_merge_recursive(
806 $this->getSkinStyleFiles( $internalSkinName )
814 * Returns all style files and all skin style files used by this module.
818 public function getAllStyleFiles() {
819 $collatedStyleFiles = array_merge_recursive(
820 self
::collateFilePathListByOption( $this->styles
, 'media', 'all' ),
821 $this->getAllSkinStyleFiles()
826 foreach ( $collatedStyleFiles as $media => $styleFiles ) {
827 foreach ( $styleFiles as $styleFile ) {
828 $result[] = $this->getLocalPath( $styleFile );
836 * Gets the contents of a list of JavaScript files.
838 * @param array $scripts List of file paths to scripts to read, remap and concetenate
839 * @throws MWException
840 * @return string Concatenated and remapped JavaScript data from $scripts
842 protected function readScriptFiles( array $scripts ) {
843 if ( empty( $scripts ) ) {
847 foreach ( array_unique( $scripts, SORT_REGULAR
) as $fileName ) {
848 $localPath = $this->getLocalPath( $fileName );
849 if ( !file_exists( $localPath ) ) {
850 throw new MWException( __METHOD__
. ": script file not found: \"$localPath\"" );
852 $contents = file_get_contents( $localPath );
853 if ( $this->getConfig()->get( 'ResourceLoaderValidateStaticJS' ) ) {
854 // Static files don't really need to be checked as often; unlike
855 // on-wiki module they shouldn't change unexpectedly without
856 // admin interference.
857 $contents = $this->validateScriptFile( $fileName, $contents );
859 $js .= $contents . "\n";
865 * Gets the contents of a list of CSS files.
867 * @param array $styles List of media type/list of file paths pairs, to read, remap and
870 * @param ResourceLoaderContext $context (optional)
872 * @throws MWException
873 * @return array List of concatenated and remapped CSS data from $styles,
874 * keyed by media type
876 public function readStyleFiles( array $styles, $flip, $context = null ) {
877 if ( empty( $styles ) ) {
880 foreach ( $styles as $media => $files ) {
881 $uniqueFiles = array_unique( $files, SORT_REGULAR
);
882 $styleFiles = array();
883 foreach ( $uniqueFiles as $file ) {
884 $styleFiles[] = $this->readStyleFile( $file, $flip, $context );
886 $styles[$media] = implode( "\n", $styleFiles );
892 * Reads a style file.
894 * This method can be used as a callback for array_map()
896 * @param string $path File path of style file to read
898 * @param ResourceLoaderContext $context (optional)
900 * @return string CSS data in script file
901 * @throws MWException If the file doesn't exist
903 protected function readStyleFile( $path, $flip, $context = null ) {
904 $localPath = $this->getLocalPath( $path );
905 $remotePath = $this->getRemotePath( $path );
906 if ( !file_exists( $localPath ) ) {
907 $msg = __METHOD__
. ": style file not found: \"$localPath\"";
908 wfDebugLog( 'resourceloader', $msg );
909 throw new MWException( $msg );
912 if ( $this->getStyleSheetLang( $localPath ) === 'less' ) {
913 $compiler = $this->getLessCompiler( $context );
914 $style = $this->compileLessFile( $localPath, $compiler );
915 $this->hasGeneratedStyles
= true;
917 $style = file_get_contents( $localPath );
921 $style = CSSJanus
::transform( $style, true, false );
923 $localDir = dirname( $localPath );
924 $remoteDir = dirname( $remotePath );
925 // Get and register local file references
926 $localFileRefs = CSSMin
::getAllLocalFileReferences( $style, $localDir );
927 foreach ( $localFileRefs as $file ) {
928 if ( file_exists( $file ) ) {
929 $this->localFileRefs
[] = $file;
931 $this->missingLocalFileRefs
[] = $file;
934 return CSSMin
::remap(
935 $style, $localDir, $remoteDir, true
940 * Get whether CSS for this module should be flipped
941 * @param ResourceLoaderContext $context
944 public function getFlip( $context ) {
945 return $context->getDirection() === 'rtl';
949 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
951 * @return array Array of strings
953 public function getTargets() {
954 return $this->targets
;
958 * Compile a LESS file into CSS.
960 * Keeps track of all used files and adds them to localFileRefs.
963 * @throws Exception If less.php encounters a parse error
964 * @param string $fileName File path of LESS source
965 * @param Less_Parser $parser Compiler to use, if not default
966 * @return string CSS source
968 protected function compileLessFile( $fileName, $compiler = null ) {
970 $compiler = $this->getLessCompiler();
972 $result = $compiler->parseFile( $fileName )->getCss();
973 $this->localFileRefs +
= array_keys( $compiler->AllParsedFiles() );
978 * Get a LESS compiler instance for this module in given context.
980 * Just calls ResourceLoader::getLessCompiler() by default to get a global compiler.
982 * @param ResourceLoaderContext $context
983 * @throws MWException
985 * @return Less_Parser
987 protected function getLessCompiler( ResourceLoaderContext
$context = null ) {
988 return ResourceLoader
::getLessCompiler( $this->getConfig() );
992 * Takes named templates by the module and returns an array mapping.
993 * @return array of templates mapping template alias to content
994 * @throws MWException
996 public function getTemplates() {
997 $templates = array();
999 foreach ( $this->templates
as $alias => $templatePath ) {
1000 // Alias is optional
1001 if ( is_int( $alias ) ) {
1002 $alias = $templatePath;
1004 $localPath = $this->getLocalPath( $templatePath );
1005 if ( file_exists( $localPath ) ) {
1006 $content = file_get_contents( $localPath );
1007 $templates[$alias] = $content;
1009 $msg = __METHOD__
. ": template file not found: \"$localPath\"";
1010 wfDebugLog( 'resourceloader', $msg );
1011 throw new MWException( $msg );