From 2922b7f34f084ed64348fc94cf9cd745a48928b4 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 3 Feb 2015 12:27:05 -0800 Subject: [PATCH] Account for HiDPI variants in thumb.php rate limiting If $wgResponsiveImages is on, include the 1.5x and 2x multipliers in the wfThumbIsStandard() standard image size comparisons. Change-Id: I79d866387730cdd4d7c7a976e691192b2335b7ce --- includes/GlobalFunctions.php | 33 +++++++++++++++++-- .../GlobalFunctions/wfThumbIsStandardTest.php | 21 ++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 04958644d6..5232413fdd 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -4214,7 +4214,15 @@ function wfIsConfiguredProxy( $ip ) { * @since 1.24 Moved from thumb.php to GlobalFunctions in 1.25 */ function wfThumbIsStandard( File $file, array $params ) { - global $wgThumbLimits, $wgImageLimits; + global $wgThumbLimits, $wgImageLimits, $wgResponsiveImages; + + $multipliers = array( 1 ); + if ( $wgResponsiveImages ) { + // These available sizes are hardcoded currently elsewhere in MediaWiki. + // @see Linker::processResponsiveImages + $multipliers[] = 1.5; + $multipliers[] = 2; + } $handler = $file->getHandler(); if ( !$handler || !isset( $params['width'] ) ) { @@ -4226,15 +4234,34 @@ function wfThumbIsStandard( File $file, array $params ) { $basicParams['page'] = $params['page']; } + $thumbLimits = array(); + $imageLimits = array(); + // Expand limits to account for multipliers + foreach ( $multipliers as $multiplier ) { + $thumbLimits = array_merge( $thumbLimits, array_map( + function ( $width ) use ( $multiplier ) { + return round( $width * $multiplier ); + }, $wgThumbLimits ) + ); + $imageLimits = array_merge( $imageLimits, array_map( + function ( $pair ) use ( $multiplier ) { + return array( + round( $pair[0] * $multiplier ), + round( $pair[1] * $multiplier ), + ); + }, $wgImageLimits ) + ); + } + // Check if the width matches one of $wgThumbLimits - if ( in_array( $params['width'], $wgThumbLimits ) ) { + if ( in_array( $params['width'], $thumbLimits ) ) { $normalParams = $basicParams + array( 'width' => $params['width'] ); // Append any default values to the map (e.g. "lossy", "lossless", ...) $handler->normaliseParams( $file, $normalParams ); } else { // If not, then check if the width matchs one of $wgImageLimits $match = false; - foreach ( $wgImageLimits as $pair ) { + foreach ( $imageLimits as $pair ) { $normalParams = $basicParams + array( 'width' => $pair[0], 'height' => $pair[1] ); // Decide whether the thumbnail should be scaled on width or height. // Also append any default values to the map (e.g. "lossy", "lossless", ...) diff --git a/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php b/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php index dc1dea1b57..448250a681 100644 --- a/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php +++ b/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php @@ -12,7 +12,7 @@ class WfThumbIsStandardTest extends MediaWikiTestCase { $this->setMwGlobals( array( 'wgThumbLimits' => array( 100, - 400 + 401 ), 'wgImageLimits' => array( array( 300, 225 ), @@ -35,7 +35,24 @@ class WfThumbIsStandardTest extends MediaWikiTestCase { array( 'Standard thumb width', true, - array( 'width' => 400 ), + array( 'width' => 401 ), + ), + // wfThumbIsStandard should match Linker::processResponsiveImages + // in its rounding behaviour. + array( + 'Standard thumb width (HiDPI 1.5x) - incorrect rounding', + false, + array( 'width' => 601 ), + ), + array( + 'Standard thumb width (HiDPI 1.5x)', + true, + array( 'width' => 602 ), + ), + array( + 'Standard thumb width (HiDPI 2x)', + true, + array( 'width' => 802 ), ), array( 'Non-standard thumb width', -- 2.20.1