X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fmedia%2FSVG.php;h=10be97a5cd5b8c796dd9d0d51ff1cbce1ccbdb70;hb=8f4ae0d51c8b4c4f77753e157312a91176f5a719;hp=0cea6d899f2dc74aba79b4ffa156a0c5ce2a0f97;hpb=b8da5c83743ea31c6f73c063508384f114748537;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/media/SVG.php b/includes/media/SVG.php index 0cea6d899f..10be97a5cd 100644 --- a/includes/media/SVG.php +++ b/includes/media/SVG.php @@ -97,19 +97,50 @@ class SvgHandler extends ImageHandler { if ( isset( $metadata['translations'] ) ) { foreach ( $metadata['translations'] as $lang => $langType ) { if ( $langType === SVGReader::LANG_FULL_MATCH ) { - $langList[] = $lang; + $langList[] = strtolower( $lang ); } } } } - return $langList; + return array_unique( $langList ); } /** - * What language to render file in if none selected. + * SVG's systemLanguage matching rules state: + * 'The `systemLanguage` attribute ... [e]valuates to "true" if one of the languages indicated + * by user preferences exactly equals one of the languages given in the value of this parameter, + * or if one of the languages indicated by user preferences exactly equals a prefix of one of + * the languages given in the value of this parameter such that the first tag character + * following the prefix is "-".' * - * @param File $file - * @return string Language code. + * Return the first element of $svgLanguages that matches $userPreferredLanguage + * + * @see https://www.w3.org/TR/SVG/struct.html#SystemLanguageAttribute + * @param string $userPreferredLanguage + * @param array $svgLanguages + * @return string|null + */ + public function getMatchedLanguage( $userPreferredLanguage, array $svgLanguages ) { + foreach ( $svgLanguages as $svgLang ) { + if ( strcasecmp( $svgLang, $userPreferredLanguage ) === 0 ) { + return $svgLang; + } + $trimmedSvgLang = $svgLang; + while ( strpos( $trimmedSvgLang, '-' ) !== false ) { + $trimmedSvgLang = substr( $trimmedSvgLang, 0, strrpos( $trimmedSvgLang, '-' ) ); + if ( strcasecmp( $trimmedSvgLang, $userPreferredLanguage ) === 0 ) { + return $svgLang; + } + } + } + return null; + } + + /** + * What language to render file in if none selected + * + * @param File $file Language code + * @return string */ public function getDefaultRenderLanguage( File $file ) { return 'en'; @@ -126,7 +157,7 @@ class SvgHandler extends ImageHandler { /** * @param File $image - * @param array $params + * @param array &$params * @return bool */ function normaliseParams( $image, &$params ) { @@ -205,7 +236,17 @@ class SvgHandler extends ImageHandler { // https://git.gnome.org/browse/librsvg/commit/?id=f01aded72c38f0e18bc7ff67dee800e380251c8e $tmpDir = wfTempDir() . '/svg_' . wfRandomString( 24 ); $lnPath = "$tmpDir/" . basename( $srcPath ); - $ok = mkdir( $tmpDir, 0771 ) && symlink( $srcPath, $lnPath ); + $ok = mkdir( $tmpDir, 0771 ); + if ( !$ok ) { + wfDebugLog( 'thumbnail', + sprintf( 'Thumbnail failed on %s: could not create temporary directory %s', + wfHostname(), $tmpDir ) ); + return new MediaTransformError( 'thumbnail_error', + $params['width'], $params['height'], + wfMessage( 'thumbnail-temp-create' )->text() + ); + } + $ok = symlink( $srcPath, $lnPath ); /** @noinspection PhpUnusedLocalVariableInspection */ $cleaner = new ScopedCallback( function () use ( $tmpDir, $lnPath ) { MediaWiki\suppressWarnings(); @@ -469,9 +510,7 @@ class SvgHandler extends ImageHandler { return ( $value > 0 ); } elseif ( $name == 'lang' ) { // Validate $code - if ( $value === '' || !Language::isValidBuiltInCode( $value ) ) { - wfDebug( "Invalid user language code\n" ); - + if ( $value === '' || !Language::isValidCode( $value ) ) { return false; } @@ -489,8 +528,7 @@ class SvgHandler extends ImageHandler { public function makeParamString( $params ) { $lang = ''; if ( isset( $params['lang'] ) && $params['lang'] !== 'en' ) { - $params['lang'] = strtolower( $params['lang'] ); - $lang = "lang{$params['lang']}-"; + $lang = 'lang' . strtolower( $params['lang'] ) . '-'; } if ( !isset( $params['width'] ) ) { return false; @@ -501,7 +539,7 @@ class SvgHandler extends ImageHandler { public function parseParamString( $str ) { $m = false; - if ( preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/', $str, $m ) ) { + if ( preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/i', $str, $m ) ) { return [ 'width' => array_pop( $m ), 'lang' => $m[1] ]; } elseif ( preg_match( '/^(\d+)px$/', $str, $m ) ) { return [ 'width' => $m[1], 'lang' => 'en' ];