use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
+use Wikimedia\AtEase\AtEase;
use Wikimedia\RelPath;
use Wikimedia\ScopedCallback;
* Abstraction for ResourceLoader modules, with name registration and maxage functionality.
*/
abstract class ResourceLoaderModule implements LoggerAwareInterface {
- # Type of resource
- const TYPE_SCRIPTS = 'scripts';
- const TYPE_STYLES = 'styles';
- const TYPE_COMBINED = 'combined';
-
- # Desired load type
- // Module only has styles (loaded via <style> or <link rel=stylesheet>)
- const LOAD_STYLES = 'styles';
- // Module may have other resources (loaded via mw.loader from a script)
- const LOAD_GENERAL = 'general';
-
- # sitewide core module like a skin file or jQuery component
- const ORIGIN_CORE_SITEWIDE = 1;
-
- # per-user module generated by the software
- const ORIGIN_CORE_INDIVIDUAL = 2;
-
- # sitewide module generated from user-editable files, like MediaWiki:Common.js, or
- # modules accessible to multiple users, such as those generated by the Gadgets extension.
- const ORIGIN_USER_SITEWIDE = 3;
-
- # per-user module generated from user-editable files, like User:Me/vector.js
- const ORIGIN_USER_INDIVIDUAL = 4;
-
- # an access constant; make sure this is kept as the largest number in this group
- const ORIGIN_ALL = 10;
+ /** @var Config */
+ protected $config;
+ /** @var LoggerInterface */
+ protected $logger;
- # script and style modules form a hierarchy of trustworthiness, with core modules like
- # skins and jQuery as most trustworthy, and user scripts as least trustworthy. We can
- # limit the types of scripts and styles we allow to load on, say, sensitive special
- # pages like Special:UserLogin and Special:Preferences
+ /**
+ * Script and style modules form a hierarchy of trustworthiness, with core modules
+ * like skins and jQuery as most trustworthy, and user scripts as least trustworthy. We can
+ * limit the types of scripts and styles we allow to load on, say, sensitive special
+ * pages like Special:UserLogin and Special:Preferences
+ * @var int
+ */
protected $origin = self::ORIGIN_CORE_SITEWIDE;
+ /** @var string|null Module name */
protected $name = null;
+ /** @var string[] What client platforms the module targets (e.g. desktop, mobile) */
protected $targets = [ 'desktop' ];
- // In-object cache for file dependencies
+ /** @var array Map of (variant => indirect file dependencies) */
protected $fileDeps = [];
- // In-object cache for message blob (keyed by language)
+ /** @var array Map of (language => in-object cache for message blob) */
protected $msgBlobs = [];
- // In-object cache for version hash
+ /** @var array Map of (context hash => cached module version hash) */
protected $versionHash = [];
- // In-object cache for module content
+ /** @var array Map of (context hash => cached module content) */
protected $contents = [];
- /**
- * @var Config
- */
- protected $config;
-
- /**
- * @var array|bool
- */
+ /** @var string|bool Deprecation string or true if deprecated; false otherwise */
protected $deprecated = false;
+ /** @var string Scripts only */
+ const TYPE_SCRIPTS = 'scripts';
+ /** @var string Styles only */
+ const TYPE_STYLES = 'styles';
+ /** @var string Scripts and styles */
+ const TYPE_COMBINED = 'combined';
+
+ /** @var string Module only has styles (loaded via <style> or <link rel=stylesheet>) */
+ const LOAD_STYLES = 'styles';
+ /** @var string Module may have other resources (loaded via mw.loader from a script) */
+ const LOAD_GENERAL = 'general';
+
+ /** @var int Sitewide core module like a skin file or jQuery component */
+ const ORIGIN_CORE_SITEWIDE = 1;
+ /** @var int Per-user module generated by the software */
+ const ORIGIN_CORE_INDIVIDUAL = 2;
/**
- * @var LoggerInterface
+ * Sitewide module generated from user-editable files, like MediaWiki:Common.js,
+ * or modules accessible to multiple users, such as those generated by the Gadgets extension.
+ * @var int
*/
- protected $logger;
+ const ORIGIN_USER_SITEWIDE = 3;
+ /** @var int Per-user module generated from user-editable files, like User:Me/vector.js */
+ const ORIGIN_USER_INDIVIDUAL = 4;
+ /** @var int An access constant; make sure this is kept as the largest number in this group */
+ const ORIGIN_ALL = 10;
/**
* Get this module's name. This is set when the module is registered
/**
* @since 1.27
* @param LoggerInterface $logger
- * @return null
*/
public function setLogger( LoggerInterface $logger ) {
$this->logger = $logger;
return 'local';
}
- /**
- * Whether this module's JS expects to work without the client-side ResourceLoader module.
- * Returning true from this function will prevent mw.loader.state() call from being
- * appended to the bottom of the script.
- *
- * @return bool
- */
- public function isRaw() {
- return false;
- }
-
/**
* Get a list of modules this module depends on.
*
* @return array List of files
*/
protected function getFileDependencies( ResourceLoaderContext $context ) {
- $vary = $context->getSkin() . '|' . $context->getLanguage();
+ $vary = self::getVary( $context );
// Try in-object cache first
if ( !isset( $this->fileDeps[$vary] ) ) {
* @param string[] $files Array of file names
*/
public function setFileDependencies( ResourceLoaderContext $context, $files ) {
- $vary = $context->getSkin() . '|' . $context->getLanguage();
+ $vary = self::getVary( $context );
$this->fileDeps[$vary] = $files;
}
}
// The file deps list has changed, we want to update it.
- $vary = $context->getSkin() . '|' . $context->getLanguage();
+ $vary = self::getVary( $context );
$cache = ObjectCache::getLocalClusterInstance();
$key = $cache->makeKey( __METHOD__, $this->getName(), $vary );
$scopeLock = $cache->getScopedLock( $key, 0 );
$parser = self::javaScriptParser();
$err = null;
try {
- Wikimedia\suppressWarnings();
+ AtEase::suppressWarnings();
$parser->parse( $contents, $fileName, 1 );
} catch ( Exception $e ) {
$err = $e;
} finally {
- Wikimedia\restoreWarnings();
+ AtEase::restoreWarnings();
}
if ( $err ) {
// Send the error to the browser console client-side.
* @return int UNIX timestamp
*/
protected static function safeFilemtime( $filePath ) {
- Wikimedia\suppressWarnings();
+ AtEase::suppressWarnings();
$mtime = filemtime( $filePath ) ?: 1;
- Wikimedia\restoreWarnings();
+ AtEase::restoreWarnings();
return $mtime;
}
protected static function safeFileHash( $filePath ) {
return FileContentsHasher::getFileContentsHash( $filePath );
}
+
+ /**
+ * Get vary string.
+ *
+ * @internal For internal use only.
+ * @param ResourceLoaderContext $context
+ * @return string Vary string
+ */
+ public static function getVary( ResourceLoaderContext $context ) {
+ return implode( '|', [
+ $context->getSkin(),
+ $context->getLanguage(),
+ ] );
+ }
}