summaryrefslogtreecommitdiff
path: root/includes/resourceloader/ResourceLoaderFileModule.php
diff options
context:
space:
mode:
Diffstat (limited to 'includes/resourceloader/ResourceLoaderFileModule.php')
-rw-r--r--includes/resourceloader/ResourceLoaderFileModule.php270
1 files changed, 160 insertions, 110 deletions
diff --git a/includes/resourceloader/ResourceLoaderFileModule.php b/includes/resourceloader/ResourceLoaderFileModule.php
index 1c37eb07..f38c60ae 100644
--- a/includes/resourceloader/ResourceLoaderFileModule.php
+++ b/includes/resourceloader/ResourceLoaderFileModule.php
@@ -78,6 +78,8 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
protected $messages = array();
/** String: Name of group to load this module in */
protected $group;
+ /** String: Position on the page to load this module at */
+ protected $position = 'bottom';
/** Boolean: Link to raw files in debug mode */
protected $debugRaw = true;
/**
@@ -95,15 +97,16 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Constructs a new module from an options array.
- *
+ *
* @param $options Array: List of options; if not given or empty, an empty module will be
* constructed
* @param $localBasePath String: Base path to prepend to all local paths in $options. Defaults
* to $IP
* @param $remoteBasePath String: Base path to prepend to all remote paths in $options. Defaults
* to $wgScriptPath
- *
- * @example $options
+ *
+ * Below is a description for the $options array:
+ * @code
* array(
* // Base path to prepend to all local paths in $options. Defaults to $IP
* 'localBasePath' => [base path],
@@ -137,14 +140,21 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
* 'messages' => [array of message key strings],
* // Group which this module should be loaded together with
* 'group' => [group name string],
+ * // Position on the page to load this module at
+ * 'position' => ['bottom' (default) or 'top']
* )
+ * @endcode
*/
- public function __construct( $options = array(), $localBasePath = null,
- $remoteBasePath = null )
+ public function __construct( $options = array(), $localBasePath = null,
+ $remoteBasePath = null )
{
- global $IP, $wgScriptPath;
+ global $IP, $wgScriptPath, $wgResourceBasePath;
$this->localBasePath = $localBasePath === null ? $IP : $localBasePath;
- $this->remoteBasePath = $remoteBasePath === null ? $wgScriptPath : $remoteBasePath;
+ if ( $remoteBasePath !== null ) {
+ $this->remoteBasePath = $remoteBasePath;
+ } else {
+ $this->remoteBasePath = $wgResourceBasePath === null ? $wgScriptPath : $wgResourceBasePath;
+ }
if ( isset( $options['remoteExtPath'] ) ) {
global $wgExtensionAssetsPath;
@@ -166,14 +176,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
case 'skinStyles':
if ( !is_array( $option ) ) {
throw new MWException(
- "Invalid collated file path list error. " .
+ "Invalid collated file path list error. " .
"'$option' given, array expected."
);
}
foreach ( $option as $key => $value ) {
if ( !is_string( $key ) ) {
throw new MWException(
- "Invalid collated file path list key error. " .
+ "Invalid collated file path list key error. " .
"'$key' given, string expected."
);
}
@@ -187,6 +197,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
break;
// Single strings
case 'group':
+ case 'position':
case 'localBasePath':
case 'remoteBasePath':
$this->{$member} = (string) $option;
@@ -197,39 +208,37 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
break;
}
}
- // Make sure the remote base path is a complete valid url
- $this->remoteBasePath = wfExpandUrl( $this->remoteBasePath );
+ // Make sure the remote base path is a complete valid URL,
+ // but possibly protocol-relative to avoid cache pollution
+ $this->remoteBasePath = wfExpandUrl( $this->remoteBasePath, PROTO_RELATIVE );
}
/**
* Gets all scripts for a given context concatenated together.
- *
+ *
* @param $context ResourceLoaderContext: Context in which to generate script
* @return String: JavaScript code for $context
*/
public function getScript( 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 );
- if ( $this->debugRaw ) {
- $script = '';
- foreach ( $files as $file ) {
- $path = $this->getRemotePath( $file );
- $script .= "\n\t" . Xml::encodeJsCall( 'mediaWiki.loader.load', array( $path ) );
- }
- return $script;
- }
- }
+ $files = $this->getScriptFiles( $context );
return $this->readScriptFiles( $files );
}
+
+ public function getScriptURLsForDebug( ResourceLoaderContext $context ) {
+ $urls = array();
+ foreach ( $this->getScriptFiles( $context ) as $file ) {
+ $urls[] = $this->getRemotePath( $file );
+ }
+ return $urls;
+ }
+
+ public function supportsURLLoading() {
+ return $this->debugRaw;
+ }
/**
* Gets loader script.
- *
+ *
* @return String: JavaScript code to be added to startup module
*/
public function getLoaderScript() {
@@ -241,29 +250,19 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Gets all styles for a given context concatenated together.
- *
+ *
* @param $context ResourceLoaderContext: Context in which to generate styles
* @return String: CSS code for $context
*/
public function getStyles( ResourceLoaderContext $context ) {
- // Merge general styles and skin specific styles, retaining media type collation
- $styles = $this->readStyleFiles( $this->styles, $this->getFlip( $context ) );
- $skinStyles = $this->readStyleFiles(
- self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ),
+ $styles = $this->readStyleFiles(
+ $this->getStyleFiles( $context ),
$this->getFlip( $context )
);
-
- foreach ( $skinStyles as $media => $style ) {
- if ( isset( $styles[$media] ) ) {
- $styles[$media] .= $style;
- } else {
- $styles[$media] = $style;
- }
- }
// Collect referenced files
$this->localFileRefs = array_unique( $this->localFileRefs );
// If the list has been modified since last time we cached it, update the cache
- if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) ) {
+ if ( $this->localFileRefs !== $this->getFileDependencies( $context->getSkin() ) && !wfReadOnly() ) {
$dbw = wfGetDB( DB_MASTER );
$dbw->replace( 'module_deps',
array( array( 'md_module', 'md_skin' ) ), array(
@@ -276,9 +275,20 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
return $styles;
}
+ public function getStyleURLsForDebug( ResourceLoaderContext $context ) {
+ $urls = array();
+ foreach ( $this->getStyleFiles( $context ) as $mediaType => $list ) {
+ $urls[$mediaType] = array();
+ foreach ( $list as $file ) {
+ $urls[$mediaType][] = $this->getRemotePath( $file );
+ }
+ }
+ return $urls;
+ }
+
/**
* Gets list of message keys used by this module.
- *
+ *
* @return Array: List of message keys
*/
public function getMessages() {
@@ -287,7 +297,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Gets the name of the group this module should be loaded in.
- *
+ *
* @return String: Group name
*/
public function getGroup() {
@@ -295,8 +305,15 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
}
/**
+ * @return string
+ */
+ public function getPosition() {
+ return $this->position;
+ }
+
+ /**
* Gets list of names of modules this module depends on.
- *
+ *
* @return Array: List of module names
*/
public function getDependencies() {
@@ -305,14 +322,14 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Get the last modified timestamp of this module.
- *
- * 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
+ *
+ * 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
* mode.
- *
- * @param $context ResourceLoaderContext: Context in which to calculate
+ *
+ * @param $context ResourceLoaderContext: Context in which to calculate
* the modified time
* @return Integer: UNIX timestamp
* @see ResourceLoaderModule::getFileDependencies
@@ -322,23 +339,23 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
return $this->modifiedTime[$context->getHash()];
}
wfProfileIn( __METHOD__ );
-
+
$files = array();
-
+
// Flatten style files into $files
$styles = self::collateFilePathListByOption( $this->styles, 'media', 'all' );
foreach ( $styles as $styleFiles ) {
$files = array_merge( $files, $styleFiles );
}
$skinFiles = self::tryForKey(
- self::collateFilePathListByOption( $this->skinStyles, 'media', 'all' ),
- $context->getSkin(),
+ self::collateFilePathListByOption( $this->skinStyles, 'media', 'all' ),
+ $context->getSkin(),
'default'
);
foreach ( $skinFiles as $styleFiles ) {
$files = array_merge( $files, $styleFiles );
}
-
+
// Final merge, this should result in a master list of dependent files
$files = array_merge(
$files,
@@ -351,39 +368,47 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
$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() ) );
-
- // If a module is nothing but a list of dependencies, we need to avoid
+
+ // If a module is nothing but a list of dependencies, we need to avoid
// giving max() an empty array
if ( count( $files ) === 0 ) {
wfProfileOut( __METHOD__ );
return $this->modifiedTime[$context->getHash()] = 1;
}
-
+
wfProfileIn( __METHOD__.'-filemtime' );
- $filesMtime = max( array_map( array( __CLASS__, 'safeFilemtime' ), $files ) );
+ $filesMtime = max( array_map( 'filemtime', $files ) );
wfProfileOut( __METHOD__.'-filemtime' );
- $this->modifiedTime[$context->getHash()] = max(
- $filesMtime,
+ $this->modifiedTime[$context->getHash()] = max(
+ $filesMtime,
$this->getMsgBlobMtime( $context->getLanguage() ) );
wfProfileOut( __METHOD__ );
return $this->modifiedTime[$context->getHash()];
}
- /* Protected Members */
+ /* Protected Methods */
+ /**
+ * @param $path string
+ * @return string
+ */
protected function getLocalPath( $path ) {
return "{$this->localBasePath}/$path";
}
-
+
+ /**
+ * @param $path string
+ * @return string
+ */
protected function getRemotePath( $path ) {
return "{$this->remoteBasePath}/$path";
}
/**
* Collates file paths by option (where provided).
- *
- * @param $list Array: List of file paths in any combination of index/path
+ *
+ * @param $list Array: List of file paths in any combination of index/path
* or path/options pairs
* @param $option String: option name
* @param $default Mixed: default value if the option isn't set
@@ -398,7 +423,7 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
$collatedFiles[$default] = array();
}
$collatedFiles[$default][] = $value;
- } else if ( is_array( $value ) ) {
+ } elseif ( is_array( $value ) ) {
// File name as the key, options array as the value
$optionValue = isset( $value[$option] ) ? $value[$option] : $default;
if ( !isset( $collatedFiles[$optionValue] ) ) {
@@ -412,19 +437,19 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Gets a list of element that match a key, optionally using a fallback key.
- *
+ *
* @param $list Array: List of lists to select from
* @param $key String: Key to look for in $map
* @param $fallback String: Key to look for in $list if $key doesn't exist
- * @return Array: List of elements from $map which matched $key or $fallback,
+ * @return Array: List of elements from $map which matched $key or $fallback,
* or an empty list in case of no match
*/
protected static function tryForKey( array $list, $key, $fallback = null ) {
if ( isset( $list[$key] ) && is_array( $list[$key] ) ) {
return $list[$key];
- } else if ( is_string( $fallback )
- && isset( $list[$fallback] )
- && is_array( $list[$fallback] ) )
+ } elseif ( is_string( $fallback )
+ && isset( $list[$fallback] )
+ && is_array( $list[$fallback] ) )
{
return $list[$fallback];
}
@@ -432,23 +457,56 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
}
/**
+ * Gets a list of file paths for all scripts in this module, in order of propper execution.
+ *
+ * @param $context ResourceLoaderContext: Context
+ * @return Array: List of file paths
+ */
+ 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 );
+ }
+ return $files;
+ }
+
+ /**
+ * Gets a list of file paths for all styles in this module, in order of propper inclusion.
+ *
+ * @param $context ResourceLoaderContext: Context
+ * @return Array: List of file paths
+ */
+ protected function getStyleFiles( ResourceLoaderContext $context ) {
+ return array_merge_recursive(
+ self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
+ self::collateFilePathListByOption(
+ self::tryForKey( $this->skinStyles, $context->getSkin(), 'default' ), 'media', 'all'
+ )
+ );
+ }
+
+ /**
* Gets the contents of a list of JavaScript files.
- *
+ *
* @param $scripts Array: List of file paths to scripts to read, remap and concetenate
* @return String: Concatenated and remapped JavaScript data from $scripts
*/
protected function readScriptFiles( array $scripts ) {
+ global $wgResourceLoaderValidateStaticJS;
if ( empty( $scripts ) ) {
return '';
}
- global $wgResourceLoaderValidateStaticJS;
$js = '';
foreach ( array_unique( $scripts ) as $fileName ) {
$localPath = $this->getLocalPath( $fileName );
- if ( !file_exists( $localPath ) ) {
+ $contents = file_get_contents( $localPath );
+ if ( $contents === false ) {
throw new MWException( __METHOD__.": script file not found: \"$localPath\"" );
}
- $contents = file_get_contents( $localPath );
if ( $wgResourceLoaderValidateStaticJS ) {
// Static files don't really need to be checked as often; unlike
// on-wiki module they shouldn't change unexpectedly without
@@ -462,16 +520,19 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Gets the contents of a list of CSS files.
- *
- * @param $styles Array: List of file paths to styles to read, remap and concetenate
- * @return Array: List of concatenated and remapped CSS data from $styles,
+ *
+ * @param $styles Array: List of media type/list of file paths pairs, to read, remap and
+ * concetenate
+ *
+ * @param $flip bool
+ *
+ * @return Array: List of concatenated and remapped CSS data from $styles,
* keyed by media type
*/
protected function readStyleFiles( array $styles, $flip ) {
if ( empty( $styles ) ) {
return array();
}
- $styles = self::collateFilePathListByOption( $styles, 'media', 'all' );
foreach ( $styles as $media => $files ) {
$uniqueFiles = array_unique( $files );
$styles[$media] = implode(
@@ -488,49 +549,38 @@ class ResourceLoaderFileModule extends ResourceLoaderModule {
/**
* Reads a style file.
- *
+ *
* This method can be used as a callback for array_map()
- *
- * @param $path String: File path of style file to read
+ *
+ * @param $path String: File path of script file to read
+ * @param $flip bool
+ *
* @return String: CSS data in script file
- * @throws MWException if the file doesn't exist
*/
- protected function readStyleFile( $path, $flip ) {
+ protected function readStyleFile( $path, $flip ) {
$localPath = $this->getLocalPath( $path );
- if ( !file_exists( $localPath ) ) {
+ $style = file_get_contents( $localPath );
+ if ( $style === false ) {
throw new MWException( __METHOD__.": style file not found: \"$localPath\"" );
}
- $style = file_get_contents( $localPath );
if ( $flip ) {
$style = CSSJanus::transform( $style, true, false );
}
- $dir = $this->getLocalPath( dirname( $path ) );
- $remoteDir = $this->getRemotePath( dirname( $path ) );
+ $dirname = dirname( $path );
+ if ( $dirname == '.' ) {
+ // If $path doesn't have a directory component, don't prepend a dot
+ $dirname = '';
+ }
+ $dir = $this->getLocalPath( $dirname );
+ $remoteDir = $this->getRemotePath( $dirname );
// Get and register local file references
- $this->localFileRefs = array_merge(
- $this->localFileRefs,
+ $this->localFileRefs = array_merge(
+ $this->localFileRefs,
CSSMin::getLocalFileReferences( $style, $dir ) );
return CSSMin::remap(
$style, $dir, $remoteDir, true
);
}
-
- /**
- * Safe version of filemtime(), which doesn't throw a PHP warning if the file doesn't exist
- * but returns 1 instead.
- * @param $filename string File name
- * @return int UNIX timestamp, or 1 if the file doesn't exist
- */
- protected static function safeFilemtime( $filename ) {
- if ( file_exists( $filename ) ) {
- return filemtime( $filename );
- } else {
- // We only ever map this function on an array if we're gonna call max() after,
- // so return our standard minimum timestamps here. This is 1, not 0, because
- // wfTimestamp(0) == NOW
- return 1;
- }
- }
/**
* Get whether CSS for this module should be flipped