Merge "Remove use of MWException in MapCacheLRU"
[lhc/web/wiklou.git] / includes / media / MediaTransformOutput.php
index 1dd8519..5366c4f 100644 (file)
@@ -30,7 +30,7 @@ abstract class MediaTransformOutput {
        /** @var array Associative array mapping optional supplementary image files
         *  from pixel density (eg 1.5 or 2) to additional URLs.
         */
-       public $responsiveUrls = array();
+       public $responsiveUrls = [];
 
        /** @var File */
        protected $file;
@@ -131,7 +131,7 @@ abstract class MediaTransformOutput {
         *
         * @return string
         */
-       abstract public function toHtml( $options = array() );
+       abstract public function toHtml( $options = [] );
 
        /**
         * This will be overridden to return true in error classes
@@ -182,7 +182,7 @@ abstract class MediaTransformOutput {
                } elseif ( FileBackend::isStoragePath( $this->path ) ) {
                        $be = $this->file->getRepo()->getBackend();
                        // The temp file will be process cached by FileBackend
-                       $fsFile = $be->getLocalReference( array( 'src' => $this->path ) );
+                       $fsFile = $be->getLocalReference( [ 'src' => $this->path ] );
 
                        return $fsFile ? $fsFile->getPath() : false;
                } else {
@@ -194,20 +194,32 @@ abstract class MediaTransformOutput {
         * Stream the file if there were no errors
         *
         * @param array $headers Additional HTTP headers to send on success
-        * @return bool Success
+        * @return Status
+        * @since 1.27
         */
-       public function streamFile( $headers = array() ) {
+       public function streamFileWithStatus( $headers = [] ) {
                if ( !$this->path ) {
-                       return false;
+                       return Status::newFatal( 'backend-fail-stream', '<no path>' );
                } elseif ( FileBackend::isStoragePath( $this->path ) ) {
                        $be = $this->file->getRepo()->getBackend();
-
-                       return $be->streamFile( array( 'src' => $this->path, 'headers' => $headers ) )->isOK();
+                       return $be->streamFile( [ 'src' => $this->path, 'headers' => $headers ] );
                } else { // FS-file
-                       return StreamFile::stream( $this->getLocalCopyPath(), $headers );
+                       $success = StreamFile::stream( $this->getLocalCopyPath(), $headers );
+                       return $success ? Status::newGood() : Status::newFatal( 'backend-fail-stream', $this->path );
                }
        }
 
+       /**
+        * Stream the file if there were no errors
+        *
+        * @deprecated since 1.26, use streamFileWithStatus
+        * @param array $headers Additional HTTP headers to send on success
+        * @return bool Success
+        */
+       public function streamFile( $headers = [] ) {
+               $this->streamFileWithStatus( $headers )->isOK();
+       }
+
        /**
         * Wrap some XHTML text in an anchor tag with the given attributes
         *
@@ -228,11 +240,11 @@ abstract class MediaTransformOutput {
         * @param string|array $params Query parameters to add
         * @return array
         */
-       public function getDescLinkAttribs( $title = null, $params = array() ) {
+       public function getDescLinkAttribs( $title = null, $params = [] ) {
                if ( is_array( $params ) ) {
                        $query = $params;
                } else {
-                       $query = array();
+                       $query = [];
                }
                if ( $this->page && $this->page !== 1 ) {
                        $query['page'] = $this->page;
@@ -245,10 +257,10 @@ abstract class MediaTransformOutput {
                        $query = $params . '&' . wfArrayToCgi( $query );
                }
 
-               $attribs = array(
+               $attribs = [
                        'href' => $this->file->getTitle()->getLocalURL( $query ),
                        'class' => 'image',
-               );
+               ];
                if ( $title ) {
                        $attribs['title'] = $title;
                }
@@ -275,25 +287,25 @@ class ThumbnailImage extends MediaTransformOutput {
         * @param string|bool $path Filesystem path to the thumb
         * @param array $parameters Associative array of parameters
         */
-       function __construct( $file, $url, $path = false, $parameters = array() ) {
+       function __construct( $file, $url, $path = false, $parameters = [] ) {
                # Previous parameters:
                #   $file, $url, $width, $height, $path = false, $page = false
 
-               $defaults = array(
+               $defaults = [
                        'page' => false,
                        'lang' => false
-               );
+               ];
 
                if ( is_array( $parameters ) ) {
                        $actualParams = $parameters + $defaults;
                } else {
                        # Using old format, should convert. Later a warning could be added here.
                        $numArgs = func_num_args();
-                       $actualParams = array(
+                       $actualParams = [
                                'width' => $path,
                                'height' => $parameters,
                                'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false
-                       ) + $defaults;
+                       ] + $defaults;
                        $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false;
                }
 
@@ -343,7 +355,7 @@ class ThumbnailImage extends MediaTransformOutput {
         * @throws MWException
         * @return string
         */
-       function toHtml( $options = array() ) {
+       function toHtml( $options = [] ) {
                if ( count( func_get_args() ) == 2 ) {
                        throw new MWException( __METHOD__ . ' called in the old style' );
                }
@@ -352,13 +364,13 @@ class ThumbnailImage extends MediaTransformOutput {
 
                $query = isset( $options['desc-query'] ) ? $options['desc-query'] : '';
 
-               $attribs = array(
+               $attribs = [
                        'alt' => $alt,
                        'src' => $this->url,
-               );
+               ];
 
                if ( !empty( $options['custom-url-link'] ) ) {
-                       $linkAttribs = array( 'href' => $options['custom-url-link'] );
+                       $linkAttribs = [ 'href' => $options['custom-url-link'] ];
                        if ( !empty( $options['title'] ) ) {
                                $linkAttribs['title'] = $options['title'];
                        }
@@ -373,17 +385,17 @@ class ThumbnailImage extends MediaTransformOutput {
                } elseif ( !empty( $options['custom-title-link'] ) ) {
                        /** @var Title $title */
                        $title = $options['custom-title-link'];
-                       $linkAttribs = array(
+                       $linkAttribs = [
                                '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() );
+                       $linkAttribs = [ 'href' => $this->file->getUrl() ];
                } else {
                        $linkAttribs = false;
                        if ( !empty( $options['title'] ) ) {
@@ -409,11 +421,13 @@ class ThumbnailImage extends MediaTransformOutput {
                }
 
                // Additional densities for responsive images, if specified.
-               if ( !empty( $this->responsiveUrls ) ) {
-                       $attribs['srcset'] = Html::srcSet( $this->responsiveUrls );
+               // If any of these urls is the same as src url, it'll be excluded.
+               $responsiveUrls = array_diff( $this->responsiveUrls, [ $this->url ] );
+               if ( !empty( $responsiveUrls ) ) {
+                       $attribs['srcset'] = Html::srcSet( $responsiveUrls );
                }
 
-               Hooks::run( 'ThumbnailBeforeProduceHTML', array( $this, &$attribs, &$linkAttribs ) );
+               Hooks::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] );
 
                return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
        }
@@ -425,43 +439,44 @@ class ThumbnailImage extends MediaTransformOutput {
  * @ingroup Media
  */
 class MediaTransformError extends MediaTransformOutput {
-       /** @var string HTML formatted version of the error */
-       private $htmlMsg;
-
-       /** @var string Plain text formatted version of the error */
-       private $textMsg;
+       /** @var Message */
+       private $msg;
 
        function __construct( $msg, $width, $height /*, ... */ ) {
                $args = array_slice( func_get_args(), 3 );
-               $htmlArgs = array_map( 'htmlspecialchars', $args );
-               $htmlArgs = array_map( 'nl2br', $htmlArgs );
-
-               $this->htmlMsg = wfMessage( $msg )->rawParams( $htmlArgs )->escaped();
-               $this->textMsg = wfMessage( $msg )->rawParams( $htmlArgs )->text();
+               $this->msg = wfMessage( $msg )->params( $args );
                $this->width = intval( $width );
                $this->height = intval( $height );
                $this->url = false;
                $this->path = false;
        }
 
-       function toHtml( $options = array() ) {
+       function toHtml( $options = [] ) {
                return "<div class=\"MediaTransformError\" style=\"" .
                        "width: {$this->width}px; height: {$this->height}px; display:inline-block;\">" .
-                       $this->htmlMsg .
+                       $this->getHtmlMsg() .
                        "</div>";
        }
 
        function toText() {
-               return $this->textMsg;
+               return $this->msg->text();
        }
 
        function getHtmlMsg() {
-               return $this->htmlMsg;
+               return $this->msg->escaped();
+       }
+
+       function getMsg() {
+               return $this->msg;
        }
 
        function isError() {
                return true;
        }
+
+       function getHttpStatusCode() {
+               return 500;
+       }
 }
 
 /**
@@ -474,7 +489,12 @@ class TransformParameterError extends MediaTransformError {
                parent::__construct( 'thumbnail_error',
                        max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
                        max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
-                       wfMessage( 'thumbnail_invalid_params' )->text() );
+                       wfMessage( 'thumbnail_invalid_params' )
+               );
+       }
+
+       function getHttpStatusCode() {
+               return 400;
        }
 }
 
@@ -487,14 +507,18 @@ class TransformParameterError extends MediaTransformError {
 class TransformTooBigImageAreaError extends MediaTransformError {
        function __construct( $params, $maxImageArea ) {
                $msg = wfMessage( 'thumbnail_toobigimagearea' );
+               $msg->rawParams(
+                       $msg->getLanguage()->formatComputingNumbers( $maxImageArea, 1000, "size-$1pixel" )
+               );
 
                parent::__construct( 'thumbnail_error',
                        max( isset( $params['width'] ) ? $params['width'] : 0, 120 ),
                        max( isset( $params['height'] ) ? $params['height'] : 0, 120 ),
-                       $msg->rawParams(
-                               $msg->getLanguage()->formatComputingNumbers(
-                                       $maxImageArea, 1000, "size-$1pixel" )
-                               )->text()
-                       );
+                       $msg
+               );
+       }
+
+       function getHttpStatusCode() {
+               return 400;
        }
 }