Allow iiurlheight to be used without iiurlwidth
authorCatrope <roan.kattouw@gmail.com>
Tue, 23 Apr 2013 03:46:15 +0000 (20:46 -0700)
committerCatrope <roan.kattouw@gmail.com>
Fri, 3 May 2013 00:34:07 +0000 (17:34 -0700)
Wanting to get a thumbnail with only the height constrained is a
reasonable use case, the only reason we don't support it in the
API is because the backend doesn't support it directly. This is
easy enough to emulate by setting the width to the full width of
the image if only the height is specified. This makes the transform
function use a bounding box of size w x h, and if w is the width of
the original image that's equivalent to just bounding the height.

Change-Id: I28c7c22ee91669469cbd9e7d25f09100933582de

RELEASE-NOTES-1.22
includes/api/ApiQueryImageInfo.php

index 850b43d..7f0e8b2 100644 (file)
@@ -98,6 +98,7 @@ production.
   apply the new LanguageLinks hook, and thus only consider language links
   stored in the database.
 * (bug 47219) Allow specifying change type of Wikipedia feed items
+* prop=imageinfo now allows setting iiurlheight without setting iiurlwidth
 
 === Languages updated in 1.22===
 
index c36821b..5ed9d38 100644 (file)
@@ -202,21 +202,20 @@ class ApiQueryImageInfo extends ApiQueryBase {
        public function getScale( $params ) {
                $p = $this->getModulePrefix();
 
-               // Height and width.
-               if ( $params['urlheight'] != -1 && $params['urlwidth'] == -1 ) {
-                       $this->dieUsage( "{$p}urlheight cannot be used without {$p}urlwidth", "{$p}urlwidth" );
-               }
-
                if ( $params['urlwidth'] != -1 ) {
                        $scale = array();
                        $scale['width'] = $params['urlwidth'];
                        $scale['height'] = $params['urlheight'];
+               } elseif ( $params['urlheight'] != -1 ) {
+                       // Height is specified but width isn't
+                       // Don't set $scale['width']; this signals mergeThumbParams() to fill it with the image's width
+                       $scale = array();
+                       $scale['height'] = $params['urlheight'];
                } else {
                        $scale = null;
                        if ( $params['urlparam'] ) {
                                $this->dieUsage( "{$p}urlparam requires {$p}urlwidth", "urlparam_no_width" );
                        }
-                       return $scale;
                }
 
                return $scale;
@@ -232,6 +231,12 @@ class ApiQueryImageInfo extends ApiQueryBase {
         * @return Array of parameters for transform.
         */
        protected function mergeThumbParams( $image, $thumbParams, $otherParams ) {
+
+               if ( !isset( $thumbParams['width'] ) && isset( $thumbParams['height'] ) ) {
+                       // Populate the width with the image's width, so only the height restriction applies
+                       $thumbParams['width'] = $image->getWidth();
+               }
+
                if ( !$otherParams ) {
                        return $thumbParams;
                }
@@ -257,8 +262,8 @@ class ApiQueryImageInfo extends ApiQueryBase {
 
                if ( isset( $paramList['width'] ) ) {
                        if ( intval( $paramList['width'] ) != intval( $thumbParams['width'] ) ) {
-                               $this->dieUsage( "{$p}urlparam had width of {$paramList['width']} but "
-                                       . "{$p}urlwidth was {$thumbParams['width']}", "urlparam_urlwidth_mismatch" );
+                               $this->setWarning( "Ignoring width value set in {$p}urlparam ({$paramList['width']}) "
+                                       . "in favor of width value derived from {$p}urlwidth/{$p}urlheight ({$thumbParams['width']})" );
                        }
                }
 
@@ -571,7 +576,7 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        'urlwidth' => array( "If {$p}prop=url is set, a URL to an image scaled to this width will be returned.",
                                'For performance reasons if this option is used, ' .
                                        'no more than ' . self::TRANSFORM_LIMIT . ' scaled images will be returned.' ),
-                       'urlheight' => "Similar to {$p}urlwidth. Cannot be used without {$p}urlwidth",
+                       'urlheight' => "Similar to {$p}urlwidth.",
                        'urlparam' => array( "A handler specific parameter string. For example, pdf's ",
                                "might use 'page15-100px'. {$p}urlwidth must be used and be consistent with {$p}urlparam" ),
                        'limit' => 'How many image revisions to return per image',
@@ -718,8 +723,6 @@ class ApiQueryImageInfo extends ApiQueryBase {
                        array( 'code' => "{$p}urlwidth", 'info' => "{$p}urlheight cannot be used without {$p}urlwidth" ),
                        array( 'code' => 'urlparam', 'info' => "Invalid value for {$p}urlparam" ),
                        array( 'code' => 'urlparam_no_width', 'info' => "{$p}urlparam requires {$p}urlwidth" ),
-                       array( 'code' => 'urlparam_urlwidth_mismatch', 'info' => "The width set in {$p}urlparm doesn't " .
-                               "match the one in {$p}urlwidth" ),
                ) );
        }