Merge "In ParserCache, respect $useOutdated"
[lhc/web/wiklou.git] / includes / media / MediaTransformOutput.php
index 1dd8519..9176b54 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'] ) ) {
@@ -413,7 +425,7 @@ class ThumbnailImage extends MediaTransformOutput {
                        $attribs['srcset'] = Html::srcSet( $this->responsiveUrls );
                }
 
-               Hooks::run( 'ThumbnailBeforeProduceHTML', array( $this, &$attribs, &$linkAttribs ) );
+               Hooks::run( 'ThumbnailBeforeProduceHTML', [ $this, &$attribs, &$linkAttribs ] );
 
                return $this->linkWrap( $linkAttribs, Xml::element( 'img', $attribs ) );
        }
@@ -444,7 +456,7 @@ class MediaTransformError extends MediaTransformOutput {
                $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 .