Account for HiDPI variants in thumb.php rate limiting
authorBrion Vibber <brion@pobox.com>
Tue, 3 Feb 2015 20:27:05 +0000 (12:27 -0800)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 5 Feb 2015 03:55:44 +0000 (19:55 -0800)
If $wgResponsiveImages is on, include the 1.5x and 2x multipliers
in the wfThumbIsStandard() standard image size comparisons.

Change-Id: I79d866387730cdd4d7c7a976e691192b2335b7ce

includes/GlobalFunctions.php
tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php

index 0495864..5232413 100644 (file)
@@ -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", ...)
index dc1dea1..448250a 100644 (file)
@@ -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',