Switch some HTMLForms in special pages to OOUI
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoaderStartUpModule.php
index 48b3576..a578ece 100644 (file)
 
 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
 
-       /* Protected Members */
-
-       protected $modifiedTime = array();
+       // Cache for getConfigSettings() as it's called by multiple methods
        protected $configVars = array();
        protected $targets = array( 'desktop', 'mobile' );
 
-       /* Protected Methods */
-
        /**
         * @param ResourceLoaderContext $context
         * @return array
@@ -82,6 +78,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                        'wgServerName' => $conf->get( 'ServerName' ),
                        'wgUserLanguage' => $context->getLanguage(),
                        'wgContentLanguage' => $wgContLang->getCode(),
+                       'wgTranslateNumerals' => $conf->get( 'TranslateNumerals' ),
                        'wgVersion' => $conf->get( 'Version' ),
                        'wgEnableAPI' => $conf->get( 'EnableAPI' ),
                        'wgEnableWriteAPI' => $conf->get( 'EnableWriteAPI' ),
@@ -158,7 +155,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
         * data send to the client.
         *
         * @param array &$registryData Modules keyed by name with properties:
-        *  - number 'version'
+        *  - string 'version'
         *  - array 'dependencies'
         *  - string|null 'group'
         *  - string 'source'
@@ -209,31 +206,27 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                                continue;
                        }
 
-                       // Coerce module timestamp to UNIX timestamp.
-                       // getModifiedTime() is supposed to return a UNIX timestamp, but custom implementations
-                       // might forget. TODO: Maybe emit warning?
-                       $moduleMtime = wfTimestamp( TS_UNIX, $module->getModifiedTime( $context ) );
+                       $versionHash = $module->getVersionHash( $context );
+                       if ( strlen( $versionHash ) !== 8 ) {
+                               // Module implementation either broken or deviated from ResourceLoader::makeHash
+                               // Asserted by tests/phpunit/structure/ResourcesTest.
+                               $versionHash = ResourceLoader::makeHash( $versionHash );
+                       }
 
                        $skipFunction = $module->getSkipFunction();
                        if ( $skipFunction !== null && !ResourceLoader::inDebugMode() ) {
                                $skipFunction = $resourceLoader->filter( 'minify-js',
                                        $skipFunction,
-                                       // There will potentially be lots of these little string in the registrations
+                                       // There will potentially be lots of these little strings in the registrations
                                        // manifest, we don't want to blow up the startup module with
-                                       // "/* cache key: ... */" all over it in non-debug mode.
+                                       // "/* cache key: ... */" all over it.
                                        /* cacheReport = */ false
                                );
                        }
 
-                       $mtime = max(
-                               $moduleMtime,
-                               wfTimestamp( TS_UNIX, $this->getConfig()->get( 'CacheEpoch' ) )
-                       );
-
                        $registryData[$name] = array(
-                               // Convert to numbers as wfTimestamp always returns a string, even for TS_UNIX
-                               'version' => (int) $mtime,
-                               'dependencies' => $module->getDependencies(),
+                               'version' => $versionHash,
+                               'dependencies' => $module->getDependencies( $context ),
                                'group' => $module->getGroup(),
                                'source' => $module->getSource(),
                                'loader' => $module->getLoaderScript(),
@@ -262,7 +255,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                                continue;
                        }
 
-                       // Call mw.loader.register(name, timestamp, dependencies, group, source, skip)
+                       // Call mw.loader.register(name, version, dependencies, group, source, skip)
                        $registrations[] = array(
                                $name,
                                $data['version'],
@@ -280,8 +273,6 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                return $out;
        }
 
-       /* Methods */
-
        /**
         * @return bool
         */
@@ -308,24 +299,16 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
         * @return string
         */
        public static function getStartupModulesUrl( ResourceLoaderContext $context ) {
+               $rl = $context->getResourceLoader();
                $moduleNames = self::getStartupModules();
 
-               // Get the latest version
-               $loader = $context->getResourceLoader();
-               $version = 1;
-               foreach ( $moduleNames as $moduleName ) {
-                       $version = max( $version,
-                               $loader->getModule( $moduleName )->getModifiedTime( $context )
-                       );
-               }
-
                $query = array(
                        'modules' => ResourceLoader::makePackedModulesString( $moduleNames ),
                        'only' => 'scripts',
                        'lang' => $context->getLanguage(),
                        'skin' => $context->getSkin(),
                        'debug' => $context->getDebug() ? 'true' : 'false',
-                       'version' => wfTimestamp( TS_ISO_8601_BASIC, $version )
+                       'version' => $rl->getCombinedVersion( $context, $moduleNames ),
                );
                // Ensure uniform query order
                ksort( $query );
@@ -382,59 +365,48 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
        }
 
        /**
+        * Get the definition summary for this module.
+        *
         * @param ResourceLoaderContext $context
-        * @return array|mixed
+        * @return array
         */
-       public function getModifiedTime( ResourceLoaderContext $context ) {
+       public function getDefinitionSummary( ResourceLoaderContext $context ) {
                global $IP;
+               $summary = parent::getDefinitionSummary( $context );
+               $summary[] = array(
+                       // Detect changes to variables exposed in mw.config (T30899).
+                       'vars' => $this->getConfigSettings( $context ),
+                       // Changes how getScript() creates mw.Map for mw.config
+                       'wgLegacyJavaScriptGlobals' => $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
+                       // Detect changes to the module registrations
+                       'moduleHashes' => $this->getAllModuleHashes( $context ),
 
-               $hash = $context->getHash();
-               if ( isset( $this->modifiedTime[$hash] ) ) {
-                       return $this->modifiedTime[$hash];
-               }
-
-               // Call preloadModuleInfo() on ALL modules as we're about
-               // to call getModifiedTime() on all of them
-               $loader = $context->getResourceLoader();
-               $loader->preloadModuleInfo( $loader->getModuleNames(), $context );
-
-               $time = max(
-                       wfTimestamp( TS_UNIX, $this->getConfig()->get( 'CacheEpoch' ) ),
-                       filemtime( "$IP/resources/src/startup.js" ),
-                       $this->getHashMtime( $context )
+                       'fileMtimes' => array(
+                               filemtime( "$IP/resources/src/startup.js" ),
+                       ),
                );
-
-               // ATTENTION!: Because of the line below, this is not going to cause
-               // infinite recursion - think carefully before making changes to this
-               // code!
-               // Pre-populate modifiedTime with something because the loop over
-               // all modules below includes the startup module (this module).
-               $this->modifiedTime[$hash] = 1;
-
-               foreach ( $loader->getModuleNames() as $name ) {
-                       $module = $loader->getModule( $name );
-                       $time = max( $time, $module->getModifiedTime( $context ) );
-               }
-
-               $this->modifiedTime[$hash] = $time;
-               return $this->modifiedTime[$hash];
+               return $summary;
        }
 
        /**
-        * Hash of all dynamic data embedded in getScript().
-        *
-        * Detect changes to mw.config settings embedded in #getScript (bug 28899).
+        * Helper method for getDefinitionSummary().
         *
         * @param ResourceLoaderContext $context
-        * @return string Hash
+        * @return string SHA-1
         */
-       public function getModifiedHash( ResourceLoaderContext $context ) {
-               $data = array(
-                       'vars' => $this->getConfigSettings( $context ),
-                       'wgLegacyJavaScriptGlobals' => $this->getConfig()->get( 'LegacyJavaScriptGlobals' ),
-               );
-
-               return md5( serialize( $data ) );
+       protected function getAllModuleHashes( ResourceLoaderContext $context ) {
+               $rl = $context->getResourceLoader();
+               // Preload for getCombinedVersion()
+               $rl->preloadModuleInfo( $rl->getModuleNames(), $context );
+
+               // ATTENTION: Because of the line below, this is not going to cause infinite recursion.
+               // Think carefully before making changes to this code!
+               // Pre-populate versionHash with something because the loop over all modules below includes
+               // the startup module (this module).
+               // See ResourceLoaderModule::getVersionHash() for usage of this cache.
+               $this->versionHash[$context->getHash()] = null;
+
+               return $rl->getCombinedVersion( $context, $rl->getModuleNames() );
        }
 
        /**