From ff84fbe8a0d7ddfd535bb38d11ede04b9fad039e Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Thu, 19 Apr 2018 15:45:44 +0200 Subject: [PATCH] resourceloader: Make various CSSMin performance optimizations and cleanups MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is called relatively often. Even small improvements might have an impact. I'm intentionally replacing method_exists with class_exists because the old check looked like it was done for backwards compatibility (MediaWiki before 1.27 did not contained the method), while in reality this code is meant to run without MediaWiki. This is much better reflected with a straight "if this class doesn't exist, there is no MediaWiki". I'm intentionally using the …::class feature. Yes, this works, even if the class is not there. Change-Id: I7f250a7cb000105bb751f68f25c6cc1c44c8f221 --- includes/libs/CSSMin.php | 15 ++++++--------- tests/phpunit/includes/libs/CSSMinTest.php | 6 ++++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/includes/libs/CSSMin.php b/includes/libs/CSSMin.php index 73825e82e3..ab4ae0081c 100644 --- a/includes/libs/CSSMin.php +++ b/includes/libs/CSSMin.php @@ -40,7 +40,7 @@ class CSSMin { const EMBED_REGEX = '\/\*\s*\@embed\s*\*\/'; const COMMENT_REGEX = '\/\*.*?\*\/'; - /** @var array List of common image files extensions and MIME-types */ + /** @var string[] List of common image files extensions and MIME-types */ protected static $mimeTypes = [ 'gif' => 'image/gif', 'jpe' => 'image/jpeg', @@ -58,7 +58,7 @@ class CSSMin { * * @param string $source CSS stylesheet source to process * @param string $path File path where the source was read from - * @return array List of local file references + * @return string[] List of local file references */ public static function getLocalFileReferences( $source, $path ) { $stripped = preg_replace( '/' . self::COMMENT_REGEX . '/s', '', $source ); @@ -100,7 +100,7 @@ class CSSMin { * @param bool $ie8Compat By default, a data URI will only be produced if it can be made short * enough to fit in Internet Explorer 8 (and earlier) URI length limit (32,768 bytes). Pass * `false` to remove this limitation. - * @return string|bool Image contents encoded as a data URI or false. + * @return string|false Image contents encoded as a data URI or false. */ public static function encodeImageAsDataURI( $file, $type = null, $ie8Compat = true ) { // Fast-fail for files that definitely exceed the maximum data URI length @@ -128,7 +128,7 @@ class CSSMin { * @param string $contents File contents to encode. * @param string $type File's MIME type. * @param bool $ie8Compat See encodeImageAsDataURI(). - * @return string|bool Image contents encoded as a data URI or false. + * @return string|false Image contents encoded as a data URI or false. */ public static function encodeStringAsDataURI( $contents, $type, $ie8Compat = true ) { // Try #1: Non-encoded data URI @@ -387,10 +387,7 @@ class CSSMin { * @return bool */ protected static function isLocalUrl( $maybeUrl ) { - if ( $maybeUrl !== '' && $maybeUrl[0] === '/' && !self::isRemoteUrl( $maybeUrl ) ) { - return true; - } - return false; + return isset( $maybeUrl[1] ) && $maybeUrl[0] === '/' && $maybeUrl[1] !== '/'; } /** @@ -511,7 +508,7 @@ class CSSMin { return $data; } } - if ( method_exists( 'OutputPage', 'transformFilePath' ) ) { + if ( class_exists( OutputPage::class ) ) { $url = OutputPage::transformFilePath( $remote, $local, $file ); } else { // Add version parameter as the first five hex digits diff --git a/tests/phpunit/includes/libs/CSSMinTest.php b/tests/phpunit/includes/libs/CSSMinTest.php index 02b3549a62..cc2fda9db0 100644 --- a/tests/phpunit/includes/libs/CSSMinTest.php +++ b/tests/phpunit/includes/libs/CSSMinTest.php @@ -199,6 +199,9 @@ class CSSMinTest extends MediaWikiTestCase { [ true, '//example.org/x.y.z/image.png' ], [ true, '//localhost/styles.css?query=yes' ], [ true, 'data:image/gif;base64,R0lGODlhAQABAIAAAP8AADAAACwAAAAAAQABAAACAkQBADs=' ], + [ false, '' ], + [ false, '/' ], + [ true, '//' ], [ false, 'x.gif' ], [ false, '/x.gif' ], [ false, './x.gif' ], @@ -217,6 +220,9 @@ class CSSMinTest extends MediaWikiTestCase { public static function provideIsLocalUrls() { return [ + [ false, '' ], + [ false, '/' ], + [ false, '//' ], [ false, 'x.gif' ], [ true, '/x.gif' ], [ false, './x.gif' ], -- 2.20.1