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
{
30 /* Protected Members */
32 /** String: Local base path, see __construct() */
33 protected $localBasePath = '';
34 /** String: Remote base path, see __construct() */
35 protected $remoteBasePath = '';
37 * Array: List of paths to JavaScript files to always include
40 * array( [file-path], [file-path], ... )
43 protected $scripts = array();
45 * Array: List of JavaScript files to include when using a specific language
48 * array( [language-code] => array( [file-path], [file-path], ... ), ... )
51 protected $languageScripts = array();
53 * Array: List of JavaScript files to include when using a specific skin
56 * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
59 protected $skinScripts = array();
61 * Array: List of paths to JavaScript files to include in debug mode
64 * array( [skin-name] => array( [file-path], [file-path], ... ), ... )
67 protected $debugScripts = array();
69 * Array: List of paths to JavaScript files to include in the startup module
72 * array( [file-path], [file-path], ... )
75 protected $loaderScripts = array();
77 * Array: List of paths to CSS files to always include
80 * array( [file-path], [file-path], ... )
83 protected $styles = array();
85 * Array: List of paths to CSS files to include when using specific skins
88 * array( [file-path], [file-path], ... )
91 protected $skinStyles = array();
93 * Array: List of modules this module depends on
96 * array( [file-path], [file-path], ... )
99 protected $dependencies = array();
101 * Array: List of message keys used by this module
104 * array( [message-key], [message-key], ... )
107 protected $messages = array();
108 /** String: Name of group to load this module in */
110 /** String: Position on the page to load this module at */
111 protected $position = 'bottom';
112 /** Boolean: Link to raw files in debug mode */
113 protected $debugRaw = true;
114 /** Boolean: Whether mw.loader.state() call should be omitted */
115 protected $raw = false;
116 protected $targets = array( 'desktop' );
119 * Boolean: Whether getStyleURLsForDebug should return raw file paths,
120 * or return load.php urls
122 protected $hasGeneratedStyles = false;
125 * Array: Cache for mtime
128 * array( [hash] => [mtime], [hash] => [mtime], ... )
131 protected $modifiedTime = array();
133 * Array: Place where readStyleFile() tracks file dependencies
136 * array( [file-path], [file-path], ... )
139 protected $localFileRefs = array();
144 * Constructs a new module from an options array.
146 * @param array $options List of options; if not given or empty, an empty module will be
148 * @param string $localBasePath Base path to prepend to all local paths in $options. Defaults
150 * @param string $remoteBasePath Base path to prepend to all remote paths in $options. Defaults
153 * Below is a description for the $options array:
154 * @throws MWException
155 * @par Construction options:
158 * // Base path to prepend to all local paths in $options. Defaults to $IP
159 * 'localBasePath' => [base path],
160 * // Base path to prepend to all remote paths in $options. Defaults to $wgScriptPath
161 * 'remoteBasePath' => [base path],
162 * // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
163 * 'remoteExtPath' => [base path],
164 * // Scripts to always include
165 * 'scripts' => [file path string or array of file path strings],
166 * // Scripts to include in specific language contexts
167 * 'languageScripts' => array(
168 * [language code] => [file path string or array of file path strings],
170 * // Scripts to include in specific skin contexts
171 * 'skinScripts' => array(
172 * [skin name] => [file path string or array of file path strings],
174 * // Scripts to include in debug contexts
175 * 'debugScripts' => [file path string or array of file path strings],
176 * // Scripts to include in the startup module
177 * 'loaderScripts' => [file path string or array of file path strings],
178 * // Modules which must be loaded before this module
179 * 'dependencies' => [module name string or array of module name strings],
180 * // Styles to always load
181 * 'styles' => [file path string or array of file path strings],
182 * // Styles to include in specific skin contexts
183 * 'skinStyles' => array(
184 * [skin name] => [file path string or array of file path strings],
186 * // Messages to always load
187 * 'messages' => [array of message key strings],
188 * // Group which this module should be loaded together with
189 * 'group' => [group name string],
190 * // Position on the page to load this module at
191 * 'position' => ['bottom' (default) or 'top']
195 public function __construct( $options = array(), $localBasePath = null,
196 $remoteBasePath = null
198 global $IP, $wgScriptPath, $wgResourceBasePath;
199 $this->localBasePath
= $localBasePath === null ?
$IP : $localBasePath;
200 if ( $remoteBasePath !== null ) {
201 $this->remoteBasePath
= $remoteBasePath;
203 $this->remoteBasePath
= $wgResourceBasePath === null ?
$wgScriptPath : $wgResourceBasePath;
206 if ( isset( $options['remoteExtPath'] ) ) {
207 global $wgExtensionAssetsPath;
208 $this->remoteBasePath
= $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
211 foreach ( $options as $member => $option ) {
213 // Lists of file paths
216 case 'loaderScripts':
218 $this->{$member} = (array)$option;
220 // Collated lists of file paths
221 case 'languageScripts':
224 if ( !is_array( $option ) ) {
225 throw new MWException(
226 "Invalid collated file path list error. " .
227 "'$option' given, array expected."
230 foreach ( $option as $key => $value ) {
231 if ( !is_string( $key ) ) {
232 throw new MWException(
233 "Invalid collated file path list key error. " .
234 "'$key' given, string expected."
237 $this->{$member}[$key] = (array)$value;
244 $this->{$member} = (array)$option;
249 case 'localBasePath':
250 case 'remoteBasePath':
251 $this->{$member} = (string)$option;
256 $this->{$member} = (bool)$option;
260 // Make sure the remote base path is a complete valid URL,
261 // but possibly protocol-relative to avoid cache pollution
262 $this->remoteBasePath
= wfExpandUrl( $this->remoteBasePath
, PROTO_RELATIVE
);
266 * Gets all scripts for a given context concatenated together.
268 * @param ResourceLoaderContext $context Context in which to generate script
269 * @return string: JavaScript code for $context
271 public function getScript( ResourceLoaderContext
$context ) {
272 $files = $this->getScriptFiles( $context );
273 return $this->readScriptFiles( $files );
277 * @param ResourceLoaderContext $context
280 public function getScriptURLsForDebug( ResourceLoaderContext
$context ) {
282 foreach ( $this->getScriptFiles( $context ) as $file ) {
283 $urls[] = $this->getRemotePath( $file );
291 public function supportsURLLoading() {
292 return $this->debugRaw
;
296 * Gets loader script.
298 * @return string: JavaScript code to be added to startup module
300 public function getLoaderScript() {
301 if ( count( $this->loaderScripts
) == 0 ) {
304 return $this->readScriptFiles( $this->loaderScripts
);
308 * Gets all styles for a given context concatenated together.
310 * @param ResourceLoaderContext $context Context in which to generate styles
311 * @return string: CSS code for $context
313 public function getStyles( ResourceLoaderContext
$context ) {
314 $styles = $this->readStyleFiles(
315 $this->getStyleFiles( $context ),
316 $this->getFlip( $context )
318 // Collect referenced files
319 $this->localFileRefs
= array_unique( $this->localFileRefs
);
320 // If the list has been modified since last time we cached it, update the cache
322 if ( $this->localFileRefs
!== $this->getFileDependencies( $context->getSkin() ) ) {
323 $dbw = wfGetDB( DB_MASTER
);
324 $dbw->replace( 'module_deps',
325 array( array( 'md_module', 'md_skin' ) ), array(
326 'md_module' => $this->getName(),
327 'md_skin' => $context->getSkin(),
328 'md_deps' => FormatJson
::encode( $this->localFileRefs
),
332 } catch ( Exception
$e ) {
333 wfDebugLog( 'resourceloader', __METHOD__
. ": failed to update DB: $e" );
339 * @param ResourceLoaderContext $context
342 public function getStyleURLsForDebug( ResourceLoaderContext
$context ) {
343 if ( $this->hasGeneratedStyles
) {
344 // Do the default behaviour of returning a url back to load.php
345 // but with only=styles.
346 return parent
::getStyleURLsForDebug( $context );
348 // Our module consists entirely of real css files,
349 // in debug mode we can load those directly.
351 foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
352 $urls[$mediaType] = array();
353 foreach ( $list as $file ) {
354 $urls[$mediaType][] = $this->getRemotePath( $file );
361 * Gets list of message keys used by this module.
363 * @return array: List of message keys
365 public function getMessages() {
366 return $this->messages
;
370 * Gets the name of the group this module should be loaded in.
372 * @return string: Group name
374 public function getGroup() {
381 public function getPosition() {
382 return $this->position
;
386 * Gets list of names of modules this module depends on.
388 * @return array: List of module names
390 public function getDependencies() {
391 return $this->dependencies
;
397 public function isRaw() {
402 * Get the last modified timestamp of this module.
404 * Last modified timestamps are calculated from the highest last modified
405 * timestamp of this module's constituent files as well as the files it
406 * depends on. This function is context-sensitive, only performing
407 * calculations on files relevant to the given language, skin and debug
410 * @param ResourceLoaderContext $context Context in which to calculate
412 * @return int: UNIX timestamp
413 * @see ResourceLoaderModule::getFileDependencies
415 public function getModifiedTime( ResourceLoaderContext
$context ) {
416 if ( isset( $this->modifiedTime
[$context->getHash()] ) ) {
417 return $this->modifiedTime
[$context->getHash()];
419 wfProfileIn( __METHOD__
);
423 // Flatten style files into $files
424 $styles = self
::collateFilePathListByOption( $this->styles
, 'media', 'all' );
425 foreach ( $styles as $styleFiles ) {
426 $files = array_merge( $files, $styleFiles );
428 $skinFiles = self
::tryForKey(
429 self
::collateFilePathListByOption( $this->skinStyles
, 'media', 'all' ),
433 foreach ( $skinFiles as $styleFiles ) {
434 $files = array_merge( $files, $styleFiles );
437 // Final merge, this should result in a master list of dependent files
438 $files = array_merge(
441 $context->getDebug() ?
$this->debugScripts
: array(),
442 self
::tryForKey( $this->languageScripts
, $context->getLanguage() ),
443 self
::tryForKey( $this->skinScripts
, $context->getSkin(), 'default' ),
446 $files = array_map( array( $this, 'getLocalPath' ), $files );
447 // File deps need to be treated separately because they're already prefixed
448 $files = array_merge( $files, $this->getFileDependencies( $context->getSkin() ) );
450 // If a module is nothing but a list of dependencies, we need to avoid
451 // giving max() an empty array
452 if ( count( $files ) === 0 ) {
453 wfProfileOut( __METHOD__
);
454 return $this->modifiedTime
[$context->getHash()] = 1;
457 wfProfileIn( __METHOD__
. '-filemtime' );
458 $filesMtime = max( array_map( array( __CLASS__
, 'safeFilemtime' ), $files ) );
459 wfProfileOut( __METHOD__
. '-filemtime' );
460 $this->modifiedTime
[$context->getHash()] = max(
462 $this->getMsgBlobMtime( $context->getLanguage() ) );
464 wfProfileOut( __METHOD__
);
465 return $this->modifiedTime
[$context->getHash()];
468 /* Protected Methods */
471 * @param string $path
474 protected function getLocalPath( $path ) {
475 return "{$this->localBasePath}/$path";
479 * @param string $path
482 protected function getRemotePath( $path ) {
483 return "{$this->remoteBasePath}/$path";
487 * Infer the stylesheet language from a stylesheet file path.
489 * @param string $path
490 * @return string: the stylesheet language name
492 protected function getStyleSheetLang( $path ) {
493 return preg_match( '/\.less$/i', $path ) ?
'less' : 'css';
497 * Collates file paths by option (where provided).
499 * @param array $list List of file paths in any combination of index/path
500 * or path/options pairs
501 * @param string $option option name
502 * @param mixed $default default value if the option isn't set
503 * @return array: List of file paths, collated by $option
505 protected static function collateFilePathListByOption( array $list, $option, $default ) {
506 $collatedFiles = array();
507 foreach ( (array)$list as $key => $value ) {
508 if ( is_int( $key ) ) {
509 // File name as the value
510 if ( !isset( $collatedFiles[$default] ) ) {
511 $collatedFiles[$default] = array();
513 $collatedFiles[$default][] = $value;
514 } elseif ( is_array( $value ) ) {
515 // File name as the key, options array as the value
516 $optionValue = isset( $value[$option] ) ?
$value[$option] : $default;
517 if ( !isset( $collatedFiles[$optionValue] ) ) {
518 $collatedFiles[$optionValue] = array();
520 $collatedFiles[$optionValue][] = $key;
523 return $collatedFiles;
527 * Gets a list of element that match a key, optionally using a fallback key.
529 * @param array $list List of lists to select from
530 * @param string $key Key to look for in $map
531 * @param string $fallback Key to look for in $list if $key doesn't exist
532 * @return array: List of elements from $map which matched $key or $fallback,
533 * or an empty list in case of no match
535 protected static function tryForKey( array $list, $key, $fallback = null ) {
536 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
538 } elseif ( is_string( $fallback )
539 && isset( $list[$fallback] )
540 && is_array( $list[$fallback] ) )
542 return $list[$fallback];
548 * Gets a list of file paths for all scripts in this module, in order of propper execution.
550 * @param ResourceLoaderContext $context
551 * @return array: List of file paths
553 protected function getScriptFiles( ResourceLoaderContext
$context ) {
554 $files = array_merge(
556 self
::tryForKey( $this->languageScripts
, $context->getLanguage() ),
557 self
::tryForKey( $this->skinScripts
, $context->getSkin(), 'default' )
559 if ( $context->getDebug() ) {
560 $files = array_merge( $files, $this->debugScripts
);
563 return array_unique( $files );
567 * Gets a list of file paths for all styles in this module, in order of propper inclusion.
569 * @param ResourceLoaderContext $context
570 * @return array: List of file paths
572 protected function getStyleFiles( ResourceLoaderContext
$context ) {
573 return array_merge_recursive(
574 self
::collateFilePathListByOption( $this->styles
, 'media', 'all' ),
575 self
::collateFilePathListByOption(
576 self
::tryForKey( $this->skinStyles
, $context->getSkin(), 'default' ), 'media', 'all'
582 * Gets the contents of a list of JavaScript files.
584 * @param array $scripts List of file paths to scripts to read, remap and concetenate
585 * @throws MWException
586 * @return string: Concatenated and remapped JavaScript data from $scripts
588 protected function readScriptFiles( array $scripts ) {
589 global $wgResourceLoaderValidateStaticJS;
590 if ( empty( $scripts ) ) {
594 foreach ( array_unique( $scripts ) as $fileName ) {
595 $localPath = $this->getLocalPath( $fileName );
596 if ( !file_exists( $localPath ) ) {
597 throw new MWException( __METHOD__
. ": script file not found: \"$localPath\"" );
599 $contents = file_get_contents( $localPath );
600 if ( $wgResourceLoaderValidateStaticJS ) {
601 // Static files don't really need to be checked as often; unlike
602 // on-wiki module they shouldn't change unexpectedly without
603 // admin interference.
604 $contents = $this->validateScriptFile( $fileName, $contents );
606 $js .= $contents . "\n";
612 * Gets the contents of a list of CSS files.
614 * @param array $styles List of media type/list of file paths pairs, to read, remap and
619 * @return array: List of concatenated and remapped CSS data from $styles,
620 * keyed by media type
622 protected function readStyleFiles( array $styles, $flip ) {
623 if ( empty( $styles ) ) {
626 foreach ( $styles as $media => $files ) {
627 $uniqueFiles = array_unique( $files );
628 $styles[$media] = implode(
631 array( $this, 'readStyleFile' ),
633 array_fill( 0, count( $uniqueFiles ), $flip )
641 * Reads a style file.
643 * This method can be used as a callback for array_map()
645 * @param string $path File path of style file to read
648 * @return string: CSS data in script file
649 * @throws MWException if the file doesn't exist
651 protected function readStyleFile( $path, $flip ) {
652 $localPath = $this->getLocalPath( $path );
653 if ( !file_exists( $localPath ) ) {
654 $msg = __METHOD__
. ": style file not found: \"$localPath\"";
655 wfDebugLog( 'resourceloader', $msg );
656 throw new MWException( $msg );
659 if ( $this->getStyleSheetLang( $path ) === 'less' ) {
660 $style = $this->compileLESSFile( $localPath );
661 $this->hasGeneratedStyles
= true;
663 $style = file_get_contents( $localPath );
667 $style = CSSJanus
::transform( $style, true, false );
669 $dirname = dirname( $path );
670 if ( $dirname == '.' ) {
671 // If $path doesn't have a directory component, don't prepend a dot
674 $dir = $this->getLocalPath( $dirname );
675 $remoteDir = $this->getRemotePath( $dirname );
676 // Get and register local file references
677 $this->localFileRefs
= array_merge(
678 $this->localFileRefs
,
679 CSSMin
::getLocalFileReferences( $style, $dir )
681 return CSSMin
::remap(
682 $style, $dir, $remoteDir, true
687 * Get whether CSS for this module should be flipped
688 * @param ResourceLoaderContext $context
691 public function getFlip( $context ) {
692 return $context->getDirection() === 'rtl';
696 * Get target(s) for the module, eg ['desktop'] or ['desktop', 'mobile']
698 * @return array of strings
700 public function getTargets() {
701 return $this->targets
;
705 * Generate a cache key for a LESS file.
706 * The cache key varies on the file name, the names and values of global
707 * LESS variables, and the value of $wgShowExceptionDetails. Varying on
708 * $wgShowExceptionDetails ensures the CSS comment indicating compilation
709 * failure shows the right level of detail.
711 * @param string $fileName File name of root LESS file.
712 * @return string: Cache key
714 protected static function getLESSCacheKey( $fileName ) {
715 global $wgShowExceptionDetails;
717 $vars = json_encode( self
::getLESSVars() );
718 $hash = md5( $fileName . $vars );
719 return wfMemcKey( 'resourceloader', 'less', (string)$wgShowExceptionDetails, $hash );
723 * Compile a LESS file into CSS.
725 * If invalid, returns replacement CSS source consisting of the compilation
726 * error message encoded as a comment. To save work, we cache a result object
727 * which comprises the compiled CSS and the names & mtimes of the files
728 * that were processed. lessphp compares the cached & current mtimes and
729 * recompiles as necessary.
731 * @param string $fileName File path of LESS source
732 * @return string: CSS source
734 protected function compileLESSFile( $fileName ) {
735 global $wgShowExceptionDetails;
737 $key = self
::getLESSCacheKey( $fileName );
738 $cache = wfGetCache( CACHE_ANYTHING
);
740 // The input to lessc. Either an associative array representing the
741 // cached results of a previous compilation, or the string file name if
742 // no cache result exists.
743 $source = $cache->get( $key );
744 if ( !is_array( $source ) ||
!isset( $source['root'] ) ) {
748 $compiler = self
::lessCompiler();
751 $result = $compiler->cachedCompile( $source );
752 if ( !is_array( $result ) ) {
753 throw new Exception( 'LESS compiler result has type ' . gettype( $result ) . '; array expected.' );
755 } catch ( Exception
$e ) {
756 // The exception might have been caused by an imported file rather
757 // than the root node. But we don't know which files were imported,
758 // because compilation failed; we thus cannot rely on file mtime to
759 // know when to reattempt compilation. Expire in 5 mins. instead.
761 wfDebugLog( 'resourceloader', __METHOD__
. ": $e" );
763 $result['root'] = $fileName;
765 if ( $wgShowExceptionDetails ) {
766 $result['compiled'] = ResourceLoader
::makeComment( 'LESS error: ' . $e->getMessage() );
768 $result['compiled'] = ResourceLoader
::makeComment( 'LESS stylesheet compilation failed. ' .
769 'Set "$wgShowExceptionDetails = true;" to show detailed debugging information.' );
772 $result['files'] = array( $fileName => self
::safeFilemtime( $fileName ) );
773 $result['updated'] = time();
775 // Tie cache expiry to the names and mtimes of image files that were
776 // embedded in the generated CSS source.
777 $result['files'] +
= $compiler->embeddedImages
;
778 $this->localFileRefs +
= array_keys( $result['files'] );
779 $cache->set( $key, $result, $expire );
780 return $result['compiled'];