Fixed a E_DEPRECATED
[lhc/web/wiklou.git] / includes / MediaTransformOutput.php
index 8745bb5..a3fcc96 100644 (file)
@@ -1,17 +1,23 @@
 <?php
+/**
+ * @file
+ * @ingroup Media
+ */
 
 /**
  * Base class for the output of MediaHandler::doTransform() and File::transform().
  *
- * @addtogroup Media
+ * @ingroup Media
  */
 abstract class MediaTransformOutput {
+       var $file, $width, $height, $url, $page, $path;
+
        /**
         * Get the width of the output box
         */
        function getWidth() {
                return $this->width;
-       } 
+       }
 
        /**
         * Get the height of the output box
@@ -36,12 +42,25 @@ abstract class MediaTransformOutput {
 
        /**
         * Fetch HTML for this transform output
-        * @param array $attribs Advisory associative array of HTML attributes supplied 
-        *    by the linker. These can be incorporated into the output in any way.
-        * @param array $linkAttribs Attributes of a suggested enclosing <a> tag. 
-        *    May be ignored.
+        *
+        * @param array $options Associative array of options. Boolean options
+        *     should be indicated with a value of true for true, and false or
+        *     absent for false.
+        *
+        *     alt          Alternate text or caption
+        *     desc-link    Boolean, show a description link
+        *     file-link    Boolean, show a file download link
+        *     custom-url-link    Custom URL to link to
+        *     custom-title-link  Custom Title object to link to
+        *     valign       vertical-align property, if the output is an inline element
+        *     img-class    Class applied to the <img> tag, if there is such a tag
+        *
+        * For images, desc-link and file-link are implemented as a click-through. For
+        * sounds and videos, they may be displayed in other ways.
+        *
+        * @return string
         */
-       abstract function toHtml( $attribs = array() , $linkAttribs = false );
+       abstract function toHtml( $options = array() );
 
        /**
         * This will be overridden to return true in error classes
@@ -60,13 +79,28 @@ abstract class MediaTransformOutput {
                        return $contents;
                }
        }
+
+       function getDescLinkAttribs( $title = null, $params = '' ) {
+               $query = $this->page ? ( 'page=' . urlencode( $this->page ) ) : '';
+               if( $params ) {
+                       $query .= $query ? '&'.$params : $params;
+               }
+               $attribs = array(
+                       'href' => $this->file->getTitle()->getLocalURL( $query ),
+                       'class' => 'image',
+               );
+               if ( $title ) {
+                       $attribs['title'] = $title;
+               }
+               return $attribs;
+       }
 }
 
 
 /**
  * Media transform output for images
  *
- * @addtogroup Media
+ * @ingroup Media
  */
 class ThumbnailImage extends MediaTransformOutput {
        /**
@@ -74,7 +108,8 @@ class ThumbnailImage extends MediaTransformOutput {
         * @param string $url URL path to the thumb
         * @private
         */
-       function ThumbnailImage( $url, $width, $height, $path = false ) {
+       function ThumbnailImage( $file, $url, $width, $height, $path = false, $page = false ) {
+               $this->file = $file;
                $this->url = $url;
                # These should be integers when they get here.
                # If not, there's a bug somewhere.  But let's at
@@ -82,28 +117,73 @@ class ThumbnailImage extends MediaTransformOutput {
                $this->width = round( $width );
                $this->height = round( $height );
                $this->path = $path;
+               $this->page = $page;
        }
 
        /**
         * Return HTML <img ... /> tag for the thumbnail, will include
         * width and height attributes and a blank alt text (as required).
         *
-        * You can set or override additional attributes by passing an
-        * associative array of name => data pairs. The data will be escaped
-        * for HTML output, so should be in plaintext.
+        * @param array $options Associative array of options. Boolean options
+        *     should be indicated with a value of true for true, and false or
+        *     absent for false.
+        *
+        *     alt          HTML alt attribute
+        *     title        HTML title attribute
+        *     desc-link    Boolean, show a description link
+        *     file-link    Boolean, show a file download link
+        *     valign       vertical-align property, if the output is an inline element
+        *     img-class    Class applied to the <img> tag, if there is such a tag
+        *     desc-query   String, description link query params
+        *     custom-url-link    Custom URL to link to
+        *     custom-title-link  Custom Title object to link to
         *
-        * If $linkAttribs is given, the image will be enclosed in an <a> tag.
+        * For images, desc-link and file-link are implemented as a click-through. For
+        * sounds and videos, they may be displayed in other ways.
         *
-        * @param array $attribs
-        * @param array $linkAttribs
         * @return string
         * @public
         */
-       function toHtml( $attribs = array(), $linkAttribs = false ) {
-               $attribs['src'] = $this->url;
-               $attribs['width'] = $this->width;
-               $attribs['height'] = $this->height;
-               if( !isset( $attribs['alt'] ) ) $attribs['alt'] = '';
+       function toHtml( $options = array() ) {
+               if ( count( func_get_args() ) == 2 ) {
+                       throw new MWException( __METHOD__ .' called in the old style' );
+               }
+
+               $alt = empty( $options['alt'] ) ? '' : $options['alt'];
+
+               $query = empty( $options['desc-query'] )  ? '' : $options['desc-query'];
+
+               if ( !empty( $options['custom-url-link'] ) ) {
+                       $linkAttribs = array( 'href' => $options['custom-url-link'] );
+                       if ( !empty( $options['title'] ) ) {
+                               $linkAttribs['title'] = $options['title'];
+                       }
+               } elseif ( !empty( $options['custom-title-link'] ) ) {
+                       $title = $options['custom-title-link'];
+                       $linkAttribs = array(
+                               'href' => $title->getLinkUrl(),
+                               'title' => empty( $options['title'] ) ? $title->getFullText() : $options['title']
+                       );
+               } elseif ( !empty( $options['desc-link'] ) ) {
+                       $linkAttribs = $this->getDescLinkAttribs( empty( $options['title'] ) ? null : $options['title'], $query );
+               } elseif ( !empty( $options['file-link'] ) ) {
+                       $linkAttribs = array( 'href' => $this->file->getURL() );
+               } else {
+                       $linkAttribs = false;
+               }
+
+               $attribs = array(
+                       'alt' => $alt,
+                       'src' => $this->url,
+                       'width' => $this->width,
+                       'height' => $this->height,
+               );
+               if ( !empty( $options['valign'] ) ) {
+                       $attribs['style'] = "vertical-align: {$options['valign']}";
+               }
+               if ( !empty( $options['img-class'] ) ) {
+                       $attribs['class'] = $options['img-class'];
+               }
                return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
        }
 
@@ -112,7 +192,7 @@ class ThumbnailImage extends MediaTransformOutput {
 /**
  * Basic media transform error class
  *
- * @addtogroup Media
+ * @ingroup Media
  */
 class MediaTransformError extends MediaTransformOutput {
        var $htmlMsg, $textMsg, $width, $height, $url, $path;
@@ -130,7 +210,7 @@ class MediaTransformError extends MediaTransformOutput {
                $this->path = false;
        }
 
-       function toHtml( $attribs = array(), $linkAttribs = false ) {
+       function toHtml( $options = array() ) {
                return "<table class=\"MediaTransformError\" style=\"" .
                        "width: {$this->width}px; height: {$this->height}px;\"><tr><td>" .
                        $this->htmlMsg .
@@ -153,15 +233,13 @@ class MediaTransformError extends MediaTransformOutput {
 /**
  * Shortcut class for parameter validation errors
  *
- * @addtogroup Media
+ * @ingroup Media
  */
 class TransformParameterError extends MediaTransformError {
        function __construct( $params ) {
-               parent::__construct( 'thumbnail_error', 
-                       max( isset( $params['width']  ) ? $params['width']  : 0, 180 ), 
-                       max( isset( $params['height'] ) ? $params['height'] : 0, 180 ), 
+               parent::__construct( 'thumbnail_error',
+                       max( isset( $params['width']  ) ? $params['width']  : 0, 180 ),
+                       max( isset( $params['height'] ) ? $params['height'] : 0, 180 ),
                        wfMsg( 'thumbnail_invalid_params' ) );
        }
 }
-
-