media: limit size of stderr being logged
[lhc/web/wiklou.git] / includes / media / MediaHandler.php
index ddb8efd..b002dfb 100644 (file)
@@ -32,14 +32,17 @@ abstract class MediaHandler {
        const METADATA_BAD = false;
        const METADATA_COMPATIBLE = 2; // for old but backwards compatible.
        /**
-        * Instance cache
+        * Max length of error logged by logErrorForExternalProcess()
         */
-       static $handlers = array();
+       const MAX_ERR_LOG_SIZE = 65535;
+
+       /** @var MediaHandler[] Instance cache with array of MediaHandler */
+       protected static $handlers = array();
 
        /**
         * Get a MediaHandler for a given MIME type from the instance cache
         *
-        * @param $type string
+        * @param string $type
         *
         * @return MediaHandler
         */
@@ -47,6 +50,7 @@ abstract class MediaHandler {
                global $wgMediaHandlers;
                if ( !isset( $wgMediaHandlers[$type] ) ) {
                        wfDebug( __METHOD__ . ": no handler found for $type.\n" );
+
                        return false;
                }
                $class = $wgMediaHandlers[$type];
@@ -56,6 +60,7 @@ abstract class MediaHandler {
                                self::$handlers[$class] = false;
                        }
                }
+
                return self::$handlers[$class];
        }
 
@@ -70,7 +75,7 @@ abstract class MediaHandler {
         * Return true to accept the parameter, and false to reject it.
         * If you return false, the parser will do something quiet and forgiving.
         *
-        * @param $name
+        * @param string $name
         * @param $value
         */
        abstract function validateParam( $name, $value );
@@ -78,16 +83,16 @@ abstract class MediaHandler {
        /**
         * Merge a parameter array into a string appropriate for inclusion in filenames
         *
-        * @param $params array Array of parameters that have been through normaliseParams.
-        * @return String
+        * @param array $params Array of parameters that have been through normaliseParams.
+        * @return string
         */
        abstract function makeParamString( $params );
 
        /**
         * Parse a param string made with makeParamString back into an array
         *
-        * @param $str string The parameter string without file name (e.g. 122px)
-        * @return Array|Boolean Array of parameters or false on failure.
+        * @param string $str The parameter string without file name (e.g. 122px)
+        * @return array|bool Array of parameters or false on failure.
         */
        abstract function parseParamString( $str );
 
@@ -104,19 +109,19 @@ abstract class MediaHandler {
         * Get an image size array like that returned by getimagesize(), or false if it
         * can't be determined.
         *
-        * @param $image File: the image object, or false if there isn't one
+        * @param File $image The image object, or false if there isn't one
         * @param string $path the filename
-        * @return Array Follow the format of PHP getimagesize() internal function. See http://www.php.net/getimagesize
+        * @return array Follow the format of PHP getimagesize() internal function. See http://www.php.net/getimagesize
         */
        abstract function getImageSize( $image, $path );
 
        /**
         * Get handler-specific metadata which will be saved in the img_metadata field.
         *
-        * @param $image File: the image object, or false if there isn't one.
+        * @param File $image The image object, or false if there isn't one.
         *   Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!)
-        * @param string $path the filename
-        * @return String
+        * @param string $path The filename
+        * @return string
         */
        function getMetadata( $image, $path ) {
                return '';
@@ -135,11 +140,12 @@ abstract class MediaHandler {
         * version 3 it might add to the end of the array the element 'foo=3'. if the core metadata
         * version is 2, the end version string would look like '2;foo=3'.
         *
-        * @return string version string
+        * @return string Version string
         */
        static function getMetadataVersion() {
-               $version = Array( '2' ); // core metadata version
-               wfRunHooks( 'GetMetadataVersion', Array( &$version ) );
+               $version = array( '2' ); // core metadata version
+               wfRunHooks( 'GetMetadataVersion', array( &$version ) );
+
                return implode( ';', $version );
        }
 
@@ -149,9 +155,9 @@ abstract class MediaHandler {
         * By default just returns $metadata, but can be used to allow
         * media handlers to convert between metadata versions.
         *
-        * @param $metadata Mixed String or Array metadata array (serialized if string)
-        * @param $version Integer target version
-        * @return Array serialized metadata in specified version, or $metadata on fail.
+        * @param mixed|string|array $metadata Metadata array (serialized if string)
+        * @param int $version Target version
+        * @return array Serialized metadata in specified version, or $metadata on fail.
         */
        function convertMetadataVersion( $metadata, $version = 1 ) {
                if ( !is_array( $metadata ) ) {
@@ -160,14 +166,16 @@ abstract class MediaHandler {
                        wfSuppressWarnings();
                        $ret = unserialize( $metadata );
                        wfRestoreWarnings();
+
                        return $ret;
                }
+
                return $metadata;
        }
 
        /**
         * Get a string describing the type of metadata, for display purposes.
-        *
+        * @param $image
         * @return string
         */
        function getMetadataType( $image ) {
@@ -217,7 +225,7 @@ abstract class MediaHandler {
         *
         * @param File $file
         *
-        * @return Array or false if interface not supported
+        * @return array|bool False if interface not supported
         * @since 1.23
         */
        public function getCommonMetaArray( File $file ) {
@@ -241,7 +249,7 @@ abstract class MediaHandler {
         * Get a MediaTransformOutput object representing the transformed output. Does not
         * actually do the transform.
         *
-        * @param $image File: the image object
+        * @param File $image The image object
         * @param string $dstPath filesystem destination path
         * @param string $dstUrl Destination URL to use in output HTML
         * @param array $params Arbitrary set of parameters validated by $this->validateParam()
@@ -255,12 +263,12 @@ abstract class MediaHandler {
         * Get a MediaTransformOutput object representing the transformed output. Does the
         * transform unless $flags contains self::TRANSFORM_LATER.
         *
-        * @param $image File: the image object
+        * @param File $image The image object
         * @param string $dstPath filesystem destination path
         * @param string $dstUrl destination URL to use in output HTML
         * @param array $params arbitrary set of parameters validated by $this->validateParam()
         *   Note: These parameters have *not* gone through $this->normaliseParams()
-        * @param $flags Integer: a bitfield, may contain self::TRANSFORM_LATER
+        * @param int $flags A bitfield, may contain self::TRANSFORM_LATER
         *
         * @return MediaTransformOutput
         */
@@ -269,9 +277,9 @@ abstract class MediaHandler {
        /**
         * Get the thumbnail extension and MIME type for a given source MIME type
         *
-        * @param String $ext Extension of original file
-        * @param String $mime Mime type of original file
-        * @param Array $params Handler specific rendering parameters
+        * @param string $ext Extension of original file
+        * @param string $mime Mime type of original file
+        * @param array $params Handler specific rendering parameters
         * @return array thumbnail extension and MIME type
         */
        function getThumbType( $ext, $mime, $params = null ) {
@@ -292,8 +300,8 @@ abstract class MediaHandler {
 
        /**
         * Get useful response headers for GET/HEAD requests for a file with the given metadata
-        * @param $metadata mixed Result of the getMetadata() function of this handler for a file
-        * @return Array
+        * @param mixed $metadata Result of the getMetadata() function of this handler for a file
+        * @return array
         */
        public function getStreamHeaders( $metadata ) {
                return array();
@@ -379,8 +387,8 @@ abstract class MediaHandler {
         *
         * @note For non-paged media, use getImageSize.
         *
-        * @param $image File
-        * @param $page What page to get dimensions of
+        * @param File $image
+        * @param int $page What page to get dimensions of
         * @return array|bool
         */
        function getPageDimensions( $image, $page ) {
@@ -462,6 +470,7 @@ abstract class MediaHandler {
                                $value
                        );
                }
+
                return $result;
        }
 
@@ -527,6 +536,7 @@ abstract class MediaHandler {
         */
        function getShortDesc( $file ) {
                global $wgLang;
+
                return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) );
        }
 
@@ -538,6 +548,7 @@ abstract class MediaHandler {
         */
        function getLongDesc( $file ) {
                global $wgLang;
+
                return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ),
                        $file->getMimeType() )->parse();
        }
@@ -550,6 +561,7 @@ abstract class MediaHandler {
         */
        static function getGeneralShortDesc( $file ) {
                global $wgLang;
+
                return $wgLang->formatSize( $file->getSize() );
        }
 
@@ -561,6 +573,7 @@ abstract class MediaHandler {
         */
        static function getGeneralLongDesc( $file ) {
                global $wgLang;
+
                return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ),
                        $file->getMimeType() )->parse();
        }
@@ -603,7 +616,8 @@ abstract class MediaHandler {
         * @param Parser $parser
         * @param File $file
         */
-       function parserTransformHook( $parser, $file ) {}
+       function parserTransformHook( $parser, $file ) {
+       }
 
        /**
         * File validation hook called on upload.
@@ -642,9 +656,11 @@ abstract class MediaHandler {
                                                sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed',
                                                        $thumbstat['size'], $dstPath ) );
                                }
+
                                return true;
                        }
                }
+
                return false;
        }
 
@@ -692,4 +708,24 @@ abstract class MediaHandler {
                return 0;
        }
 
+       /**
+        * Log an error that occurred in an external process
+        *
+        * Moved from BitmapHandler to MediaHandler with MediaWiki 1.23
+        *
+        * @since 1.23
+        * @param $retval int
+        * @param $err string Error reported by command. Anything longer than
+        * MediaHandler::MAX_ERR_LOG_SIZE is stripped off.
+        * @param $cmd string
+        */
+       protected function logErrorForExternalProcess( $retval, $err, $cmd ) {
+               # Keep error output limited (bug 57985)
+               $errMessage = trim( substr( $err, 0, self::MAX_ERR_LOG_SIZE ) );
+
+               wfDebugLog( 'thumbnail',
+                       sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
+                                       wfHostname(), $retval, $errMessage, $cmd ) );
+       }
+
 }