Rewrite the ThumbnailImage constructor to take parameter array
authorjarry1250 <jarry1250@gmail.com>
Wed, 29 Aug 2012 20:09:36 +0000 (21:09 +0100)
committerjarry1250 <jarry1250@gmail.com>
Wed, 29 Aug 2012 20:09:36 +0000 (21:09 +0100)
This is required for a full resolution of bug #32987. Per Brion's
commentary on that bug, for the long-term preservation of
sanity we can't just keep adding arguments to the constructor
whenever a new feature is added.

Instead, we can have an array of optional parameters. At the moment,
this situation is a bit iffy for width and height: we should really
specify either a default height / width or error if one is not found.
I'm open to ideas on that, but it's not a big deal since nothing
uses the new syntax yet. When that is agreed, I'll convert all usage
in core and maybe some extensions too.

Change-Id: I116f71aeb90ef7c786f0874b150c7bcca527e106

includes/media/MediaTransformOutput.php

index cee5bbf..773824c 100644 (file)
@@ -214,25 +214,46 @@ class ThumbnailImage extends MediaTransformOutput {
         * Get a thumbnail object from a file and parameters.
         * If $path is set to null, the output file is treated as a source copy.
         * If $path is set to false, no output file will be created.
+        * $parameters should include, as a minimum, (file) 'width' and 'height'.
+        * It may also include a 'page' parameter for multipage files.
         *
         * @param $file File object
         * @param $url String: URL path to the thumb
-        * @param $width Integer: file's width
-        * @param $height Integer: file's height
         * @param $path String|bool|null: filesystem path to the thumb
-        * @param $page Integer: page number, for multipage files
+        * @param $parameters Array: Associative array of parameters
         * @private
         */
-       function __construct( $file, $url, $width, $height, $path = false, $page = false ) {
+       function __construct( $file, $url, $path = false, $parameters = array() ) {
+               # Previous parameters:
+               #   $file, $url, $width, $height, $path = false, $page = false
+
+               if( is_array( $parameters ) ){
+                       $defaults = array(
+                               'page' => false
+                       );
+                       $actualParams = $parameters + $defaults;
+               } else {
+                       # Using old format, should convert. Later a warning could be added here.
+                       $numArgs = func_num_args();
+                       $actualParams = array(
+                               'width' => $path,
+                               'height' => $parameters,
+                               'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
+                       );
+                       $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
+               }
+
                $this->file = $file;
                $this->url = $url;
+               $this->path = $path;
+
                # These should be integers when they get here.
                # If not, there's a bug somewhere.  But let's at
                # least produce valid HTML code regardless.
-               $this->width = round( $width );
-               $this->height = round( $height );
-               $this->path = $path;
-               $this->page = $page;
+               $this->width = round( $actualParams['width'] );
+               $this->height = round( $actualParams['height'] );
+
+               $this->page = $actualParams['page'];
        }
 
        /**