*/
protected $dependencies = array();
+ /**
+ * @var string File name containing the body of the skip function
+ */
+ protected $skipFunction = null;
+
/**
* @var array List of message keys used by this module
* @par Usage:
* 'remoteBasePath' => [base path],
* // Equivalent of remoteBasePath, but relative to $wgExtensionAssetsPath
* 'remoteExtPath' => [base path],
+ * // Equivalent of remoteBasePath, but relative to $wgStylePath
+ * 'remoteSkinPath' => [base path],
* // Scripts to always include
* 'scripts' => [file path string or array of file path strings],
* // Scripts to include in specific language contexts
* 'group' => [group name string],
* // Position on the page to load this module at
* 'position' => ['bottom' (default) or 'top']
+ * // 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]
* )
* @endcode
*/
$this->remoteBasePath = $wgExtensionAssetsPath . '/' . $options['remoteExtPath'];
}
+ if ( isset( $options['remoteSkinPath'] ) ) {
+ global $wgStylePath;
+ $this->remoteBasePath = $wgStylePath . '/' . $options['remoteSkinPath'];
+ }
+
foreach ( $options as $member => $option ) {
switch ( $member ) {
// Lists of file paths
case 'position':
case 'localBasePath':
case 'remoteBasePath':
+ case 'skipFunction':
$this->{$member} = (string)$option;
break;
// Single booleans
}
/**
- * Gets loader script.
+ * Get loader script.
*
- * @return string JavaScript code to be added to startup module
+ * @return string|false JavaScript code to be added to startup module
*/
public function getLoaderScript() {
- if ( count( $this->loaderScripts ) == 0 ) {
+ if ( count( $this->loaderScripts ) === 0 ) {
return false;
}
return $this->readScriptFiles( $this->loaderScripts );
}
/**
- * Gets all styles for a given context concatenated together.
+ * Get all styles for a given context.
*
- * @param ResourceLoaderContext $context Context in which to generate styles
- * @return string CSS code for $context
+ * @param ResourceLoaderContext $context
+ * @return array CSS code for $context as an associative array mapping media type to CSS text.
*/
public function getStyles( ResourceLoaderContext $context ) {
$styles = $this->readStyleFiles(
return $this->dependencies;
}
+ /**
+ * Get the skip function.
+ *
+ * @return string|null
+ */
+ public function getSkipFunction() {
+ if ( !$this->skipFunction ) {
+ return null;
+ }
+
+ global $wgResourceLoaderValidateStaticJS;
+ $localPath = $this->getLocalPath( $this->skipFunction );
+ if ( !file_exists( $localPath ) ) {
+ throw new MWException( __METHOD__ . ": skip function file not found: \"$localPath\"" );
+ }
+ $contents = file_get_contents( $localPath );
+ if ( $wgResourceLoaderValidateStaticJS ) {
+ $contents = $this->validateScriptFile( $fileName, $contents );
+ }
+ return $contents;
+ }
+
/**
* @return bool
*/
self::tryForKey( $this->skinScripts, $context->getSkin(), 'default' ),
$this->loaderScripts
);
+ if ( $this->skipFunction ) {
+ $files[] = $this->skipFunction;
+ }
$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() ) );
'targets',
'group',
'position',
+ 'skipFunction',
'localBasePath',
'remoteBasePath',
'debugRaw',
}
/**
- * Gets a list of element that match a key, optionally using a fallback key.
+ * Get a list of element that match a key, optionally using a fallback key.
*
* @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
* @return array List of elements from $map which matched $key or $fallback,
- * or an empty list in case of no match
+ * 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] ) ) {
}
/**
- * Gets a list of file paths for all scripts in this module, in order of propper execution.
+ * Get a list of file paths for all scripts in this module, in order of proper execution.
*
* @param ResourceLoaderContext $context
* @return array List of file paths
}
/**
- * Gets a list of file paths for all styles in this module, in order of propper inclusion.
+ * Get a list of file paths for all styles in this module, in order of proper inclusion.
*
* @param ResourceLoaderContext $context
* @return array List of file paths
*/
- protected function getStyleFiles( ResourceLoaderContext $context ) {
+ public function getStyleFiles( ResourceLoaderContext $context ) {
return array_merge_recursive(
self::collateFilePathListByOption( $this->styles, 'media', 'all' ),
self::collateFilePathListByOption(
* @return array List of concatenated and remapped CSS data from $styles,
* keyed by media type
*/
- protected function readStyleFiles( array $styles, $flip ) {
+ public function readStyleFiles( array $styles, $flip ) {
if ( empty( $styles ) ) {
return array();
}