Krinkle has uploaded a new change for review.
https://gerrit.wikimedia.org/r/207661
Change subject: [WIP] resourceloader: Implement version hashes instead of
timestamps
......................................................................
[WIP] resourceloader: Implement version hashes instead of timestamps
Bug: T94810
Change-Id: Ibb292d2416839327d1807a66c78fd96dac0637d0
---
M includes/resourceloader/ResourceLoaderModule.php
M includes/resourceloader/ResourceLoaderStartUpModule.php
2 files changed, 48 insertions(+), 28 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/61/207661/1
diff --git a/includes/resourceloader/ResourceLoaderModule.php
b/includes/resourceloader/ResourceLoaderModule.php
index d1b024f..f28c9bf 100644
--- a/includes/resourceloader/ResourceLoaderModule.php
+++ b/includes/resourceloader/ResourceLoaderModule.php
@@ -425,20 +425,36 @@
/* Abstract Methods */
/**
- * Get this module's last modification timestamp for a given
- * combination of language, skin and debug mode flag. This is typically
- * the highest of each of the relevant components' modification
- * timestamps. Whenever anything happens that changes the module's
- * contents for these parameters, the mtime should increase.
+ * Get a string identifying the current version of this module in a
given context.
*
- * NOTE: The mtime of the module's messages is NOT automatically
included.
- * If you want this to happen, you'll need to call getMsgBlobMtime()
- * yourself and take its result into consideration.
+ * Whenever anything happens that changes the module's response (e.g.
scripts, styles, and
+ * messages) this value must change. This value is used to store module
responses in cache.
+ * (Both client-side and server-side.)
*
- * NOTE: The mtime of the module's hash is NOT automatically included.
- * If your module provides a getModifiedHash() method, you'll need to
call getHashMtime()
- * yourself and take its result into consideration.
+ * This method should run fast because it is frequently run by
ResourceLoaderStartUpModule to
+ * propagate changes to the client and effectively invalidate cache.
*
+ * A number of utility methods are available to help you gathering data:
+ *
+ * - getMsgBlobMtime
+ *
+ * If modules have a hash or timestamp from another source, that should
be returned as-is.
+ *
+ * If modules need to collect data (e.g. file timestamps, definition
summaries etc.),
+ * return a serialised version of that data and defer hashing to
ResourceLoader.
+ * E.g. return `json_encode( .. )`, not `sha1( json_encode( .. ) )`.
+ *
+ * @param ResourceLoaderContext $context
+ * @return string 0 or more characters
+ */
+ public function getVersionHash( ResourceLoaderContext $context ) {
+ return '';
+ }
+
+ /**
+ * Get this module's last modification timestamp for a given context.
+ *
+ * @deprecated since 1.26 Use getVersionHash() directly.
* @param ResourceLoaderContext $context Context object
* @return int UNIX timestamp
*/
@@ -447,8 +463,13 @@
}
/**
- * Helper method for calculating when the module's hash (if it has one)
changed.
+ * Helper method for calculating when the custom hash changed.
*
+ * This method uses ObjectCache to track when a hash was first seen.
That principle stems from
+ * a time that ResourceLoader could only identify module versions by
timestamp.
+ * That is no longer the case. Use getVersionHash() directly.
+ *
+ * @deprecated since 1.26 Use getVersionHash() directly.
* @param ResourceLoaderContext $context
* @return int UNIX timestamp
*/
@@ -484,9 +505,7 @@
/**
* Get the hash for whatever this module may contain.
*
- * This is the method subclasses should implement if they want to make
- * use of getHashMTime() inside getModifiedTime().
- *
+ * @deprecated since 1.26 Use getVersionHash() directly.
* @param ResourceLoaderContext $context
* @return string|null Hash
*/
diff --git a/includes/resourceloader/ResourceLoaderStartUpModule.php
b/includes/resourceloader/ResourceLoaderStartUpModule.php
index 48b3576..13b4ec8 100644
--- a/includes/resourceloader/ResourceLoaderStartUpModule.php
+++ b/includes/resourceloader/ResourceLoaderStartUpModule.php
@@ -209,10 +209,17 @@
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 ( $versionHash === '' ) {
+ // Support: MediaWiki 1.25 and earlier
+ // Beware: Some getModifiedTime()
implementations return their timestamp in a format
+ // that is not an integer UNIX timestamp. This
used to convert it with wfTimestamp(),
+ // but since we're hashing, use it as-is.
+ $versionHash = strval(
$module->getModifiedTime( $context ) );
+ }
+
+ // Salt the hash with wgCacheEpoch
+ $versionHash = $versionHash . $this->getConfig()->get(
'CacheEpoch' );
$skipFunction = $module->getSkipFunction();
if ( $skipFunction !== null &&
!ResourceLoader::inDebugMode() ) {
@@ -225,14 +232,8 @@
);
}
- $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,
+ 'versionHash' => sha1( $versionHash ),
'dependencies' => $module->getDependencies(),
'group' => $module->getGroup(),
'source' => $module->getSource(),
@@ -253,7 +254,7 @@
if ( $data['loader'] !== false ) {
$out .= ResourceLoader::makeCustomLoaderScript(
$name,
- $data['version'],
+ $data['versionHash'],
$data['dependencies'],
$data['group'],
$data['source'],
@@ -265,7 +266,7 @@
// Call mw.loader.register(name, timestamp,
dependencies, group, source, skip)
$registrations[] = array(
$name,
- $data['version'],
+ $data['versionHash'],
$data['dependencies'],
$data['group'],
// Swap default (local) for null
--
To view, visit https://gerrit.wikimedia.org/r/207661
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibb292d2416839327d1807a66c78fd96dac0637d0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Krinkle <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits