Avoid dynamic call to static method in ResourceLoaderModule::buildContent()
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderModule.php
index c376fa7..b4a0f46 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 /**
- * Abstraction for ResourceLoader modules.
- *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -26,72 +24,74 @@ use MediaWiki\MediaWikiServices;
 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.
+ *
+ * @ingroup ResourceLoader
+ * @since 1.17
  */
 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
@@ -128,7 +128,7 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
         * @param ResourceLoaderContext $context
         * @return bool
         */
-       public function getFlip( $context ) {
+       public function getFlip( ResourceLoaderContext $context ) {
                return MediaWikiServices::getInstance()->getContentLanguage()->getDir() !==
                        $context->getDirection();
        }
@@ -136,9 +136,13 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
        /**
         * Get JS representing deprecation information for the current module if available
         *
+        * @param ResourceLoaderContext|null $context Missing $context is deprecated in 1.34
         * @return string JavaScript code
         */
-       public function getDeprecationInformation() {
+       public function getDeprecationInformation( ResourceLoaderContext $context = null ) {
+               if ( $context === null ) {
+                       wfDeprecated( __METHOD__ . ' without a ResourceLoader context', '1.34' );
+               }
                $deprecationInfo = $this->deprecated;
                if ( $deprecationInfo ) {
                        $name = $this->getName();
@@ -146,7 +150,10 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
                        if ( is_string( $deprecationInfo ) ) {
                                $warning .= "\n" . $deprecationInfo;
                        }
-                       return 'mw.log.warn(' . ResourceLoader::encodeJsonForScript( $warning ) . ');';
+                       if ( $context === null ) {
+                               return 'mw.log.warn(' . ResourceLoader::encodeJsonForScript( $warning ) . ');';
+                       }
+                       return 'mw.log.warn(' . $context->encodeJson( $warning ) . ');';
                } else {
                        return '';
                }
@@ -210,7 +217,6 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
        /**
         * @since 1.27
         * @param LoggerInterface $logger
-        * @return null
         */
        public function setLogger( LoggerInterface $logger ) {
                $this->logger = $logger;
@@ -691,7 +697,6 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
         * @return array
         */
        final protected function buildContent( ResourceLoaderContext $context ) {
-               $rl = $context->getResourceLoader();
                $stats = MediaWikiServices::getInstance()->getStatsdDataFactory();
                $statStart = microtime( true );
 
@@ -760,7 +765,7 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
                                }
                                // Wrap styles into @media groups as needed and flatten into a numerical array
                                $styles = [
-                                       'css' => $rl->makeCombinedStyles( $stylePairs )
+                                       'css' => ResourceLoader::makeCombinedStyles( $stylePairs )
                                ];
                        }
                }
@@ -950,12 +955,12 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
                                $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.
@@ -989,9 +994,9 @@ abstract class ResourceLoaderModule implements LoggerAwareInterface {
         * @return int UNIX timestamp
         */
        protected static function safeFilemtime( $filePath ) {
-               Wikimedia\suppressWarnings();
+               AtEase::suppressWarnings();
                $mtime = filemtime( $filePath ) ?: 1;
-               Wikimedia\restoreWarnings();
+               AtEase::restoreWarnings();
                return $mtime;
        }