jenkins-bot has submitted this change and it was merged. Change subject: Centralise url handling for urls to static resources ......................................................................
Centralise url handling for urls to static resources Keep in CSSMin as-is for back-compat and to ensure library remains independent of MediaWiki. Moved down a few lines as there is no need to compute the md5 hash when we're returning a data URI. Previously md5_file was called twice during module builds (once for the fallback url, and another time when producing the embedded data uri). Applied to logo in SkinModule as example. To be applied elsewhere as needed. Without it, fallback is current behaviour (no cache invalidation). Bug: T99096 Change-Id: I7f38bfc1bea5c241bc4f8ec4f4b640fd65f2c04f --- M includes/OutputPage.php M includes/libs/CSSMin.php M includes/resourceloader/ResourceLoaderSkinModule.php 3 files changed, 70 insertions(+), 8 deletions(-) Approvals: Krinkle: Looks good to me, but someone else must approve Ori.livneh: Looks good to me, approved jenkins-bot: Verified diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 97165b4..e06fad9 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -3810,6 +3810,58 @@ } /** + * Transform path to web-accessible static resource. + * + * This is used to add a validation hash as query string. + * This aids various behaviors: + * + * - Put long Cache-Control max-age headers on responses for improved + * cache performance. + * - Get the correct version of a file as expected by the current page. + * - Instantly get the updated version of a file after deployment. + * + * Avoid using this for urls included in HTML as otherwise clients may get different + * versions of a resource when navigating the site depending on when the page was cached. + * If changes to the url propagate, this is not a problem (e.g. if the url is in + * an external stylesheet). + * + * @since 1.27 + * @param Config $config + * @param string $path Path-absolute URL to file (from document root, must start with "/") + * @return string URL + */ + public static function transformResourcePath( Config $config, $path ) { + global $IP; + $remotePath = $config->get( 'ResourceBasePath' ); + if ( strpos( $path, $remotePath ) !== 0 ) { + // Path is outside wgResourceBasePath, ignore. + return $path; + } + $path = RelPath\getRelativePath( $path, $remotePath ); + return self::transformFilePath( $remotePath, $IP, $path ); + } + + /** + * Utility method for transformResourceFilePath(). + * + * Caller is responsible for ensuring the file exists. Emits a PHP warning otherwise. + * + * @since 1.27 + * @param string $remotePath URL path that points to $localPath + * @param string $localPath File directory exposed at $remotePath + * @param string $file Path to target file relative to $localPath + * @return string URL + */ + public static function transformFilePath( $remotePath, $localPath, $file ) { + $hash = md5_file( "$localPath/$file" ); + if ( $hash === false ) { + wfLogWarning( __METHOD__ . ": Failed to hash $localPath/$file" ); + $hash = ''; + } + return "$remotePath/$file?" . substr( $hash, 0, 5 ); + } + + /** * Transform "media" attribute based on request parameters * * @param string $media Current value of the "media" attribute diff --git a/includes/libs/CSSMin.php b/includes/libs/CSSMin.php index 6ca0fed..246de75 100644 --- a/includes/libs/CSSMin.php +++ b/includes/libs/CSSMin.php @@ -451,15 +451,19 @@ // Path to the actual file on the filesystem $localFile = "{$local}/{$file}"; if ( file_exists( $localFile ) ) { - // Add version parameter as the first five hex digits - // of the MD5 hash of the file's contents. - $url .= '?' . substr( md5_file( $localFile ), 0, 5 ); if ( $embed ) { $data = self::encodeImageAsDataURI( $localFile ); if ( $data !== false ) { return $data; } } + if ( method_exists( 'OutputPage', 'transformFilePath' ) ) { + $url = OutputPage::transformFilePath( $remote, $local, $file ); + } else { + // Add version parameter as the first five hex digits + // of the MD5 hash of the file's contents. + $url .= '?' . substr( md5_file( $localFile ), 0, 5 ); + } } // If any of these conditions failed (file missing, we don't want to embed it // or it's not embeddable), return the URL (possibly with ?timestamp part) diff --git a/includes/resourceloader/ResourceLoaderSkinModule.php b/includes/resourceloader/ResourceLoaderSkinModule.php index e1df6d9..490a4ab 100644 --- a/includes/resourceloader/ResourceLoaderSkinModule.php +++ b/includes/resourceloader/ResourceLoaderSkinModule.php @@ -30,11 +30,17 @@ * @return array */ public function getStyles( ResourceLoaderContext $context ) { - $logo = $this->getConfig()->get( 'Logo' ); - $logoHD = $this->getConfig()->get( 'LogoHD' ); + $conf = $this->getConfig(); + $logo = $conf->get( 'Logo' ); + $logoHD = $conf->get( 'LogoHD' ); + + $logo1 = OutputPage::transformResourcePath( $conf, $logo ); + $logo15 = OutputPage::transformResourcePath( $conf, $logoHD['1.5x'] ); + $logo2 = OutputPage::transformResourcePath( $conf, $logoHD['2x'] ); + $styles = parent::getStyles( $context ); $styles['all'][] = '.mw-wiki-logo { background-image: ' . - CSSMin::buildUrlValue( $logo ) . + CSSMin::buildUrlValue( $logo1 ) . '; }'; if ( $logoHD ) { if ( isset( $logoHD['1.5x'] ) ) { @@ -44,7 +50,7 @@ '(min-resolution: 1.5dppx), ' . '(min-resolution: 144dpi)' ][] = '.mw-wiki-logo { background-image: ' . - CSSMin::buildUrlValue( $logoHD['1.5x'] ) . ';' . + CSSMin::buildUrlValue( $logo15 ) . ';' . 'background-size: 135px auto; }'; } if ( isset( $logoHD['2x'] ) ) { @@ -54,7 +60,7 @@ '(min-resolution: 2dppx), ' . '(min-resolution: 192dpi)' ][] = '.mw-wiki-logo { background-image: ' . - CSSMin::buildUrlValue( $logoHD['2x'] ) . ';' . + CSSMin::buildUrlValue( $logo2 ) . ';' . 'background-size: 135px auto; }'; } } -- To view, visit https://gerrit.wikimedia.org/r/265868 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I7f38bfc1bea5c241bc4f8ec4f4b640fd65f2c04f Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Fomafix Gerrit-Reviewer: Krinkle <krinklem...@gmail.com> Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com> Gerrit-Reviewer: Ori.livneh <o...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits