5bffad73d63dfd48842e1e09ec95926214f05233
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 * @author Trevor Parscal
20 * @author Roan Kattouw
23 defined( 'MEDIAWIKI' ) ||
die( 1 );
26 * ResourceLoader module based on local JavaScript/CSS files.
28 class ResourceLoaderFileModule
extends ResourceLoaderModule
{
30 /* Protected Members */
33 * @var {array} List of paths to JavaScript files to always include
34 * @format array( [file-path], [file-path], ... )
36 protected $scripts = array();
38 * @var {array} List of JavaScript files to include when using a specific language
39 * @format array( [language-code] => array( [file-path], [file-path], ... ), ... )
41 protected $languageScripts = array();
43 * @var {array} List of JavaScript files to include when using a specific skin
44 * @format array( [skin-name] => array( [file-path], [file-path], ... ), ... )
46 protected $skinScripts = array();
48 * @var {array} List of paths to JavaScript files to include in debug mode
49 * @format array( [skin-name] => array( [file-path], [file-path], ... ), ... )
51 protected $debugScripts = array();
53 * @var {array} List of paths to JavaScript files to include in the startup module
54 * @format array( [file-path], [file-path], ... )
56 protected $loaderScripts = array();
58 * @var {array} List of paths to CSS files to always include
59 * @format array( [file-path], [file-path], ... )
61 protected $styles = array();
63 * @var {array} List of paths to CSS files to include when using specific skins
64 * @format array( [file-path], [file-path], ... )
66 protected $skinStyles = array();
68 * @var {array} List of modules this module depends on
69 * @format array( [file-path], [file-path], ... )
71 protected $dependencies = array();
73 * @var {array} List of message keys used by this module
74 * @format array( [module-name], [module-name], ... )
76 protected $messages = array();
78 * @var {string} Name of group this module should be loaded in
79 * @format array( [message-key], [message-key], ... )
82 /** @var {boolean} Link to raw files in debug mode */
83 protected $debugRaw = true;
85 * @var {array} Cache for mtime
86 * @format array( [hash] => [mtime], [hash] => [mtime], ... )
88 protected $modifiedTime = array();
93 * Constructs a new module from an options array.
95 * @param {array} $options Options array. If not given or empty, an empty module will be constructed
96 * @param {string} $basePath base path to prepend to all paths in $options
100 * // Scripts to always include
101 * 'scripts' => [file path string or array of file path strings],
102 * // Scripts to include in specific language contexts
103 * 'languageScripts' => array(
104 * [language code] => [file path string or array of file path strings],
106 * // Scripts to include in specific skin contexts
107 * 'skinScripts' => array(
108 * [skin name] => [file path string or array of file path strings],
110 * // Scripts to include in debug contexts
111 * 'debugScripts' => [file path string or array of file path strings],
112 * // Scripts to include in the startup module
113 * 'loaderScripts' => [file path string or array of file path strings],
114 * // Modules which must be loaded before this module
115 * 'dependencies' => [modile name string or array of module name strings],
116 * // Styles to always load
117 * 'styles' => [file path string or array of file path strings],
118 * // Styles to include in specific skin contexts
119 * 'skinStyles' => array(
120 * [skin name] => [file path string or array of file path strings],
122 * // Messages to always load
123 * 'messages' => [array of message key strings],
124 * // Group which this module should be loaded together with
125 * 'group' => [group name string],
128 public function __construct( $options = array(), $basePath = null ) {
129 foreach ( $options as $member => $option ) {
131 // Lists of file paths
134 case 'loaderScripts':
136 $this->{$member} = self
::prefixFilePathList( (array) $option, $basePath );
138 // Collated lists of file paths
139 case 'languageScripts':
142 if ( !is_array( $option ) ) {
143 throw new MWException(
144 "Invalid collated file path list error. '$option' given, array expected."
147 foreach ( $option as $key => $value ) {
148 if ( !is_string( $key ) ) {
149 throw new MWException(
150 "Invalid collated file path list key error. '$key' given, string expected."
153 $this->{$member}[$key] = self
::prefixFilePathList( (array) $value, $basePath );
159 $this->{$member} = (array) $option;
163 $this->{$member} = (string) $option;
167 $this->{$member} = (bool) $option;
174 * Gets all scripts for a given context concatenated together.
176 * @param {ResourceLoaderContext} $context Context in which to generate script
177 * @return {string} JavaScript code for $context
179 public function getScript( ResourceLoaderContext
$context ) {
180 global $wgServer, $wgScriptPath;
182 $files = array_merge(
184 self
::tryForKey( $this->languageScripts
, $context->getLanguage() ),
185 self
::tryForKey( $this->skinScripts
, $context->getSkin(), 'default' )
187 if ( $context->getDebug() ) {
188 $files = array_merge( $files, $this->debugScripts
);
189 if ( $this->debugRaw
) {
191 foreach ( $files as $file ) {
192 $path = FormatJson
::encode( "$wgServer$wgScriptPath/$file" );
193 $script .= "\n\tmediaWiki.loader.load( $path );";
198 return self
::readScriptFiles( $files );
202 * Gets loader script.
204 * @return {string} JavaScript code to be added to startup module
206 public function getLoaderScript() {
207 if ( count( $this->loaderScripts
) == 0 ) {
210 return self
::readScriptFiles( $this->loaderScripts
);
214 * Gets all styles for a given context concatenated together.
216 * @param {ResourceLoaderContext} $context Context in which to generate styles
217 * @return {string} CSS code for $context
219 public function getStyles( ResourceLoaderContext
$context ) {
220 // Merge general styles and skin specific styles, retaining media type collation
221 $styles = self
::readStyleFiles( $this->styles
);
222 $skinStyles = self
::readStyleFiles( self
::tryForKey( $this->skinStyles
, $context->getSkin(), 'default' ) );
224 foreach ( $skinStyles as $media => $style ) {
225 if ( isset( $styles[$media] ) ) {
226 $styles[$media] .= $style;
228 $styles[$media] = $style;
231 // Collect referenced files
233 foreach ( $styles as /* $media => */ $style ) {
234 $files = array_merge( $files, CSSMin
::getLocalFileReferences( $style ) );
236 // If the list has been modified since last time we cached it, update the cache
237 if ( $files !== $this->getFileDependencies( $context->getSkin() ) ) {
238 $dbw = wfGetDB( DB_MASTER
);
239 $dbw->replace( 'module_deps',
240 array( array( 'md_module', 'md_skin' ) ), array(
241 'md_module' => $this->getName(),
242 'md_skin' => $context->getSkin(),
243 'md_deps' => FormatJson
::encode( $files ),
251 * Gets list of message keys used by this module.
253 * @return {array} List of message keys
255 public function getMessages() {
256 return $this->messages
;
260 * Gets the name of the group this module should be loaded in.
262 * @return {string} Group name
264 public function getGroup() {
269 * Gets list of names of modules this module depends on.
271 * @return {array} List of module names
273 public function getDependencies() {
274 return $this->dependencies
;
278 * Get the last modified timestamp of this module.
280 * Last modified timestamps are calculated from the highest last modified timestamp of this module's constituent
281 * files as well as the files it depends on. This function is context-sensitive, only performing calculations on
282 * files relevant to the given language, skin and debug mode.
284 * @param {ResourceLoaderContext} $context Context in which to calculate the modified time
285 * @return {integer} UNIX timestamp
286 * @see {ResourceLoaderModule::getFileDependencies}
288 public function getModifiedTime( ResourceLoaderContext
$context ) {
289 if ( isset( $this->modifiedTime
[$context->getHash()] ) ) {
290 return $this->modifiedTime
[$context->getHash()];
292 wfProfileIn( __METHOD__
);
296 // Flatten style files into $files
297 $styles = self
::collateFilePathListByOption( $this->styles
, 'media', 'all' );
298 foreach ( $styles as $styleFiles ) {
299 $files = array_merge( $files, $styleFiles );
301 $skinFiles = self
::tryForKey(
302 self
::collateFilePathListByOption( $this->skinStyles
, 'media', 'all' ), $context->getSkin(), 'default'
304 foreach ( $skinFiles as $styleFiles ) {
305 $files = array_merge( $files, $styleFiles );
308 // Final merge, this should result in a master list of dependent files
309 $files = array_merge(
312 $context->getDebug() ?
$this->debugScripts
: array(),
313 self
::tryForKey( $this->languageScripts
, $context->getLanguage() ),
314 self
::tryForKey( $this->skinScripts
, $context->getSkin(), 'default' ),
315 $this->loaderScripts
,
316 $this->getFileDependencies( $context->getSkin() )
319 // If a module is nothing but a list of dependencies, we need to avoid giving max() an empty array
320 if ( count( $files ) === 0 ) {
321 return $this->modifiedTime
[$context->getHash()] = 1;
324 wfProfileIn( __METHOD__
.'-filemtime' );
325 $filesMtime = max( array_map( 'filemtime', array_map( array( __CLASS__
, 'resolveFilePath' ), $files ) ) );
326 wfProfileOut( __METHOD__
.'-filemtime' );
327 $this->modifiedTime
[$context->getHash()] = max( $filesMtime, $this->getMsgBlobMtime( $context->getLanguage() ) );
328 wfProfileOut( __METHOD__
);
329 return $this->modifiedTime
[$context->getHash()];
332 /* Protected Members */
335 * Prefixes each file path in a list.
337 * @param {array} $list List of file paths in any combination of index/path or path/options pairs
338 * @param {string} $prefix String to prepend to each file path in $list
339 * @return {array} List of prefixed file paths
341 protected static function prefixFilePathList( array $list, $prefix ) {
343 foreach ( $list as $key => $value ) {
344 if ( is_string( $key ) && is_array( $value ) ) {
345 // array( [path] => array( [options] ) )
346 $prefixed[$prefix . $key] = $value;
347 } else if ( is_int( $key ) && is_string( $value ) ) {
349 $prefixed[$key] = $prefix . $value;
351 throw new MWException( "Invalid file path list error. '$key' => '$value' given." );
358 * Collates file paths by option (where provided).
360 * @param {array} $list List of file paths in any combination of index/path or path/options pairs
361 * @return {array} List of file paths, collated by $option
363 protected static function collateFilePathListByOption( array $list, $option, $default ) {
364 $collatedFiles = array();
365 foreach ( (array) $list as $key => $value ) {
366 if ( is_int( $key ) ) {
367 // File name as the value
368 if ( !isset( $collatedFiles[$default] ) ) {
369 $collatedFiles[$default] = array();
371 $collatedFiles[$default][] = $value;
372 } else if ( is_array( $value ) ) {
373 // File name as the key, options array as the value
374 $optionValue = isset( $value[$option] ) ?
$value[$option] : $default;
375 if ( !isset( $collatedFiles[$optionValue] ) ) {
376 $collatedFiles[$optionValue] = array();
378 $collatedFiles[$optionValue][] = $key;
381 return $collatedFiles;
385 * Gets a list of element that match a key, optionally using a fallback key.
387 * @param {array} $list List of lists to select from
388 * @param {string} $key Key to look for in $map
389 * @param {string} $fallback Key to look for in $list if $key doesn't exist
390 * @return {array} List of elements from $map which matched $key or $fallback, or an empty list in case of no match
392 protected static function tryForKey( array $list, $key, $fallback = null ) {
393 if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
395 } else if ( is_string( $fallback ) && isset( $list[$fallback] ) && is_array( $list[$fallback] ) ) {
396 return $list[$fallback];
402 * Gets the contents of a list of JavaScript files.
404 * @param {array} $scripts List of file paths to scripts to read, remap and concetenate
405 * @return {string} Concatenated and remapped JavaScript data from $scripts
407 protected static function readScriptFiles( array $scripts ) {
408 if ( empty( $scripts ) ) {
411 return implode( "\n", array_map( array( __CLASS__
, 'readScriptFile' ), array_unique( $scripts ) ) );
415 * Gets the contents of a list of CSS files.
417 * @param {array} $styles List of file paths to styles to read, remap and concetenate
418 * @return {array} List of concatenated and remapped CSS data from $styles, keyed by media type
420 protected static function readStyleFiles( array $styles ) {
421 if ( empty( $styles ) ) {
424 $styles = self
::collateFilePathListByOption( $styles, 'media', 'all' );
425 foreach ( $styles as $media => $files ) {
426 $styles[$media] = implode(
427 "\n", array_map( array( __CLASS__
, 'readStyleFile' ), array_unique( $files ) )
434 * Reads a script file.
436 * This method can be used as a callback for array_map()
438 * @param {string} $path File path of script file to read
439 * @return {string} JavaScript data in script file
441 protected static function readScriptFile( $path ) {
444 return file_get_contents( "$IP/$path" );
448 * Reads a style file.
450 * This method can be used as a callback for array_map()
452 * @param {string} $path File path of script file to read
453 * @return {string} CSS data in script file
455 protected static function readStyleFile( $path ) {
456 global $wgScriptPath, $IP;
458 return CSSMin
::remap(
459 file_get_contents( "$IP/$path" ), dirname( "$IP/$path" ), $wgScriptPath . '/' . dirname( $path ), true
464 * Resolves a file name.
466 * This method can be used as a callback for array_map()
468 * @param {string} $path File path to resolve
469 * @return {string} Absolute file path
471 protected static function resolveFilePath( $path ) {