Merge "Add oldRev parameter to DiffRevisionTools hook"
[lhc/web/wiklou.git] / includes / media / Bitmap.php
index f50c3e5..7f108c8 100644 (file)
@@ -28,7 +28,7 @@
  */
 class BitmapHandler extends ImageHandler {
        /**
-        * @param $image File
+        * @param File $image
         * @param array $params Transform parameters. Entries with the keys 'width'
         * and 'height' are the respective screen width and height, while the keys
         * 'physicalWidth' and 'physicalHeight' indicate the thumbnail dimensions.
@@ -57,13 +57,17 @@ class BitmapHandler extends ImageHandler {
 
                # Check if the file is smaller than the maximum image area for thumbnailing
                $checkImageAreaHookResult = null;
-               wfRunHooks( 'BitmapHandlerCheckImageArea', array( $image, &$params, &$checkImageAreaHookResult ) );
+               wfRunHooks(
+                       'BitmapHandlerCheckImageArea',
+                       array( $image, &$params, &$checkImageAreaHookResult )
+               );
+
                if ( is_null( $checkImageAreaHookResult ) ) {
                        global $wgMaxImageArea;
 
                        if ( $srcWidth * $srcHeight > $wgMaxImageArea
                                && !( $image->getMimeType() == 'image/jpeg'
-                               && self::getScalerType( false, false ) == 'im' )
+                                       && self::getScalerType( false, false ) == 'im' )
                        ) {
                                # Only ImageMagick can efficiently downsize jpg images without loading
                                # the entire file in memory
@@ -102,10 +106,10 @@ class BitmapHandler extends ImageHandler {
        }
 
        /**
-        * @param $image File
-        * @param  $dstPath
-        * @param  $dstUrl
-        * @param  $params
+        * @param File $image
+        * @param string $dstPath
+        * @param string $dstUrl
+        * @param array $params
         * @param int $flags
         * @return MediaTransformError|ThumbnailImage|TransformParameterError
         */
@@ -137,7 +141,8 @@ class BitmapHandler extends ImageHandler {
                # Determine scaler type
                $scaler = self::getScalerType( $dstPath );
 
-               wfDebug( __METHOD__ . ": creating {$scalerParams['physicalDimensions']} thumbnail at $dstPath using scaler $scaler\n" );
+               wfDebug( __METHOD__ . ": creating {$scalerParams['physicalDimensions']} " .
+                       "thumbnail at $dstPath using scaler $scaler\n" );
 
                if ( !$image->mustRender() &&
                        $scalerParams['physicalWidth'] == $scalerParams['srcWidth']
@@ -168,7 +173,8 @@ class BitmapHandler extends ImageHandler {
 
                # Try to make a target path for the thumbnail
                if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) {
-                       wfDebug( __METHOD__ . ": Unable to create thumbnail destination directory, falling back to client scaling\n" );
+                       wfDebug( __METHOD__ . ": Unable to create thumbnail destination " .
+                               "directory, falling back to client scaling\n" );
 
                        return $this->getClientScalingThumbnailImage( $image, $scalerParams );
                }
@@ -181,7 +187,9 @@ class BitmapHandler extends ImageHandler {
                                        wfHostname(), $image->getName() ) );
 
                        return new MediaTransformError( 'thumbnail_error',
-                               $scalerParams['clientWidth'], $scalerParams['clientHeight'] );
+                               $scalerParams['clientWidth'], $scalerParams['clientHeight'],
+                               wfMessage( 'filemissing' )->text()
+                       );
                }
 
                # Try a hook
@@ -195,6 +203,7 @@ class BitmapHandler extends ImageHandler {
                switch ( $scaler ) {
                        case 'hookaborted':
                                # Handled by the hook above
+                               /** @var MediaTransformOutput $mto */
                                $err = $mto->isError() ? $mto : false;
                                break;
                        case 'im':
@@ -220,7 +229,9 @@ class BitmapHandler extends ImageHandler {
                } elseif ( $removed ) {
                        # Thumbnail was zero-byte and had to be removed
                        return new MediaTransformError( 'thumbnail_error',
-                               $scalerParams['clientWidth'], $scalerParams['clientHeight'] );
+                               $scalerParams['clientWidth'], $scalerParams['clientHeight'],
+                               wfMessage( 'unknown-error' )->text()
+                       );
                } elseif ( $mto ) {
                        return $mto;
                } else {
@@ -237,7 +248,9 @@ class BitmapHandler extends ImageHandler {
         * Returns which scaler type should be used. Creates parent directories
         * for $dstPath and returns 'client' on error
         *
-        * @return string client,im,custom,gd
+        * @param string $dstPath
+        * @param bool $checkDstPath
+        * @return string One of client, im, custom, gd, imext
         */
        protected static function getScalerType( $dstPath, $checkDstPath = true ) {
                global $wgUseImageResize, $wgUseImageMagick, $wgCustomConvertCommand;
@@ -266,11 +279,11 @@ class BitmapHandler extends ImageHandler {
         * Get a ThumbnailImage that respresents an image that will be scaled
         * client side
         *
-        * @param $image File File associated with this thumbnail
+        * @param File $image File associated with this thumbnail
         * @param array $scalerParams Array with scaler params
         * @return ThumbnailImage
         *
-        * @todo fixme: no rotation support
+        * @todo FIXME: No rotation support
         */
        protected function getClientScalingThumbnailImage( $image, $scalerParams ) {
                $params = array(
@@ -284,7 +297,7 @@ class BitmapHandler extends ImageHandler {
        /**
         * Transform an image using ImageMagick
         *
-        * @param $image File File associated with this thumbnail
+        * @param File $image File associated with this thumbnail
         * @param array $params Array with scaler params
         *
         * @return MediaTransformError Error object if error occurred, false (=no error) otherwise
@@ -373,7 +386,7 @@ class BitmapHandler extends ImageHandler {
                if ( $retval !== 0 ) {
                        $this->logErrorForExternalProcess( $retval, $err, $cmd );
 
-                       return $this->getMediaTransformError( $params, $err );
+                       return $this->getMediaTransformError( $params, "$err\nError code: $retval" );
                }
 
                return false; # No error
@@ -382,7 +395,7 @@ class BitmapHandler extends ImageHandler {
        /**
         * Transform an image using the Imagick PHP extension
         *
-        * @param $image File File associated with this thumbnail
+        * @param File $image File associated with this thumbnail
         * @param array $params Array with scaler params
         *
         * @return MediaTransformError Error object if error occurred, false (=no error) otherwise
@@ -458,7 +471,7 @@ class BitmapHandler extends ImageHandler {
        /**
         * Transform an image using a custom command
         *
-        * @param $image File File associated with this thumbnail
+        * @param File $image File associated with this thumbnail
         * @param array $params Array with scaler params
         *
         * @return MediaTransformError Error object if error occurred, false (=no error) otherwise
@@ -489,19 +502,6 @@ class BitmapHandler extends ImageHandler {
                return false; # No error
        }
 
-       /**
-        * Log an error that occurred in an external process
-        *
-        * @param $retval int
-        * @param $err int
-        * @param $cmd string
-        */
-       protected function logErrorForExternalProcess( $retval, $err, $cmd ) {
-               wfDebugLog( 'thumbnail',
-                       sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
-                               wfHostname(), $retval, trim( $err ), $cmd ) );
-       }
-
        /**
         * Get a MediaTransformError with error 'thumbnail_error'
         *
@@ -517,7 +517,7 @@ class BitmapHandler extends ImageHandler {
        /**
         * Transform an image using the built in GD library
         *
-        * @param $image File File associated with this thumbnail
+        * @param File $image File associated with this thumbnail
         * @param array $params Array with scaler params
         *
         * @return MediaTransformError Error object if error occurred, false (=no error) otherwise
@@ -530,7 +530,8 @@ class BitmapHandler extends ImageHandler {
 
                $typemap = array(
                        'image/gif' => array( 'imagecreatefromgif', 'palette', 'imagegif' ),
-                       'image/jpeg' => array( 'imagecreatefromjpeg', 'truecolor', array( __CLASS__, 'imageJpegWrapper' ) ),
+                       'image/jpeg' => array( 'imagecreatefromjpeg', 'truecolor',
+                               array( __CLASS__, 'imageJpegWrapper' ) ),
                        'image/png' => array( 'imagecreatefrompng', 'bits', 'imagepng' ),
                        'image/vnd.wap.wbmp' => array( 'imagecreatefromwbmp', 'palette', 'imagewbmp' ),
                        'image/xbm' => array( 'imagecreatefromxbm', 'palette', 'imagexbm' ),
@@ -604,7 +605,8 @@ class BitmapHandler extends ImageHandler {
        /**
         * Escape a string for ImageMagick's property input (e.g. -set -comment)
         * See InterpretImageProperties() in magick/property.c
-        * @return mixed|string
+        * @param string $s
+        * @return string
         */
        function escapeMagickProperty( $s ) {
                // Double the backslashes
@@ -652,6 +654,8 @@ class BitmapHandler extends ImageHandler {
        /**
         * Escape a string for ImageMagick's output filename. See
         * InterpretImageFilename() in magick/image.c.
+        * @param string $path The file path
+        * @param bool|string $scene The scene specification, or false if there is none
         * @return string
         */
        function escapeMagickOutput( $path, $scene = false ) {
@@ -698,7 +702,7 @@ class BitmapHandler extends ImageHandler {
         * Retrieve the version of the installed ImageMagick
         * You can use PHPs version_compare() to use this value
         * Value is cached for one hour.
-        * @return String representing the IM version.
+        * @return string Representing the IM version.
         */
        protected function getMagickVersion() {
                global $wgMemc;
@@ -754,9 +758,24 @@ class BitmapHandler extends ImageHandler {
        }
 
        /**
-        * @param $file File
+        * @see $wgEnableAutoRotation
+        * @return bool Whether auto rotation is enabled
+        */
+       public static function autoRotateEnabled() {
+               global $wgEnableAutoRotation;
+
+               if ( $wgEnableAutoRotation === null ) {
+                       // Only enable auto-rotation when the bitmap handler can rotate
+                       $wgEnableAutoRotation = BitmapHandler::canRotate();
+               }
+
+               return $wgEnableAutoRotation;
+       }
+
+       /**
+        * @param File $file
         * @param array $params Rotate parameters.
-        *    'rotation' clockwise rotation in degrees, allowed are multiples of 90
+        *   'rotation' clockwise rotation in degrees, allowed are multiples of 90
         * @since 1.21
         * @return bool
         */
@@ -776,6 +795,7 @@ class BitmapHandler extends ImageHandler {
                                wfDebug( __METHOD__ . ": running ImageMagick: $cmd\n" );
                                wfProfileIn( 'convert' );
                                $retval = 0;
+                               // @todo FIXME: Undefined variable %env
                                $err = wfShellExecWithStderr( $cmd, $retval, $env );
                                wfProfileOut( 'convert' );
                                if ( $retval !== 0 ) {
@@ -809,7 +829,7 @@ class BitmapHandler extends ImageHandler {
         * Rerurns whether the file needs to be rendered. Returns true if the
         * file requires rotation and we are able to rotate it.
         *
-        * @param $file File
+        * @param File $file
         * @return bool
         */
        public function mustRender( $file ) {