Merge "Improve usage of which and that"
[lhc/web/wiklou.git] / includes / media / MediaTransformOutput.php
index cee5bbf..97a2d1d 100644 (file)
@@ -33,6 +33,13 @@ abstract class MediaTransformOutput {
        var $file;
 
        var $width, $height, $url, $page, $path;
+
+       /**
+        * @var array Associative array mapping optional supplementary image files
+        * from pixel density (eg 1.5 or 2) to additional URLs.
+        */
+       public $responsiveUrls = array();
+
        protected $storagePath = false;
 
        /**
@@ -189,7 +196,10 @@ abstract class MediaTransformOutput {
         * @return array
         */
        public function getDescLinkAttribs( $title = null, $params = '' ) {
-               $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
+               $query = '';
+               if ( $this->page && $this->page !== 1 ) {
+                         $query = 'page=' . urlencode( $this->page );
+               }
                if( $params ) {
                        $query .= $query ? '&'.$params : $params;
                }
@@ -214,25 +224,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'];
        }
 
        /**
@@ -260,6 +291,7 @@ class ThumbnailImage extends MediaTransformOutput {
         * For images, desc-link and file-link are implemented as a click-through. For
         * sounds and videos, they may be displayed in other ways.
         *
+        * @throws MWException
         * @return string
         */
        function toHtml( $options = array() ) {
@@ -302,7 +334,7 @@ class ThumbnailImage extends MediaTransformOutput {
                        'alt' => $alt,
                        'src' => $this->url,
                        'width' => $this->width,
-                       'height' => $this->height,
+                       'height' => $this->height
                );
                if ( !empty( $options['valign'] ) ) {
                        $attribs['style'] = "vertical-align: {$options['valign']}";
@@ -310,6 +342,11 @@ class ThumbnailImage extends MediaTransformOutput {
                if ( !empty( $options['img-class'] ) ) {
                        $attribs['class'] = $options['img-class'];
                }
+
+               // Additional densities for responsive images, if specified.
+               if ( !empty( $this->responsiveUrls ) ) {
+                       $attribs['srcset'] = Html::srcSet( $this->responsiveUrls );
+               }
                return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
        }