FontRepoCompiler now adds hash suffix to font paths

This is now new standard way of doing this in MediaWiki.

Since this makes the font repo bit longer, compensated by no
longer providing version information, which also simplifies the code.
Uncompressed increase is 542 bytes.

Depends on https://github.com/wikimedia/jquery.webfonts/pull/45

Bug: T135806
Change-Id: I1921115fc1abc20e6e756044c0a84a9fe4133884
This commit is contained in:
Niklas Laxström
2016-05-20 14:02:47 +02:00
parent 1e3b85b1ca
commit e054af704c
2 changed files with 18 additions and 26 deletions

View File

@@ -24,12 +24,10 @@ class FontRepoCompiler {
$conf = $this->parseFile( $file ); $conf = $this->parseFile( $file );
$fontPath = dirname( $file ); $fontPath = dirname( $file );
// Ugly hack to populate version to all fonts in a set
$version = null;
foreach ( $conf as $fontname => $font ) { foreach ( $conf as $fontname => $font ) {
$fontLanguages = $this->getLanguages( $font ); $fontLanguages = $this->getLanguages( $font );
$this->appendLanguages( $languages, $fontLanguages, $fontname ); $this->appendLanguages( $languages, $fontLanguages, $fontname );
$fonts[$fontname] = $this->getFontInfo( $font, $fontPath, $version ); $fonts[$fontname] = $this->getFontInfo( $font, $fontPath );
} }
} }
@@ -81,12 +79,10 @@ class FontRepoCompiler {
} }
} }
public function getFontInfo( $font, $fontpath, &$version ) { public function getFontInfo( $font, $fontpath ) {
$info = []; $info = [];
$fontdir = basename( $fontpath ); $fontdir = basename( $fontpath );
$version = $info['version'] = isset( $font['version'] ) ? $font['version'] : $version;
if ( isset( $font['fontweight'] ) ) { if ( isset( $font['fontweight'] ) ) {
$info['fontweight'] = $font['fontweight']; $info['fontweight'] = $font['fontweight'];
} }
@@ -95,31 +91,17 @@ class FontRepoCompiler {
$info['fontstyle'] = $font['fontstyle']; $info['fontstyle'] = $font['fontstyle'];
} }
if ( isset( $font['ttf'] ) ) { foreach ( [ 'ttf', 'svg', 'eot', 'woff', 'woff2' ] as $format ) {
$info['ttf'] = $fontdir . '/' . $font['ttf']; if ( isset( $font[$format] ) ) {
} $info[$format] = $this->getFontWebPath( $fontpath, $fontdir, $font[$format] );
}
if ( isset( $font['svg'] ) ) {
$info['svg'] = $fontdir . '/' . $font['svg'];
}
if ( isset( $font['eot'] ) ) {
$info['eot'] = $fontdir . '/' . $font['eot'];
}
if ( isset( $font['woff'] ) ) {
$info['woff'] = $fontdir . '/' . $font['woff'];
}
if ( isset( $font['woff2'] ) ) {
$info['woff2'] = $fontdir . '/' . $font['woff2'];
} }
// If font formats are not explicitly defined, scan the directory. // If font formats are not explicitly defined, scan the directory.
if ( !isset( $info['ttf'] ) ) { if ( !isset( $info['ttf'] ) ) {
foreach ( glob( "$fontpath/*.{eot,ttf,woff,woff2,svg}", GLOB_BRACE ) as $fontfile ) { foreach ( glob( "$fontpath/*.{eot,ttf,woff,woff2,svg}", GLOB_BRACE ) as $fontfile ) {
$type = substr( $fontfile, strrpos( $fontfile, '.' ) + 1 ); $type = substr( $fontfile, strrpos( $fontfile, '.' ) + 1 );
$info[$type] = $fontdir . '/' . basename( $fontfile ); $info[$type] = $this->getFontWebPath( $fontpath, $fontdir, basename( $fontfile ) );
} }
} }
@@ -138,4 +120,14 @@ class FontRepoCompiler {
return $info; return $info;
} }
private function getFontWebPath( $path, $fontdir, $filename ) {
if ( method_exists( 'OutputPage', 'transformFilePath' ) ) {
return OutputPage::transformFilePath( $fontdir, $path, $filename );
}
// BC for MediaWiki <= 1.27
$hash = md5_file( "$path/$filename" );
return "$fontdir/$filename?" . substr( $hash, 0, 5 );
}
} }

File diff suppressed because one or more lines are too long