media: limit size of stderr being logged
[lhc/web/wiklou.git] / includes / media / MediaHandler.php
index 56dcae0..b002dfb 100644 (file)
@@ -32,9 +32,12 @@ 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
@@ -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];
        }
 
@@ -140,6 +145,7 @@ abstract class MediaHandler {
        static function getMetadataVersion() {
                $version = array( '2' ); // core metadata version
                wfRunHooks( 'GetMetadataVersion', array( &$version ) );
+
                return implode( ';', $version );
        }
 
@@ -160,8 +166,10 @@ abstract class MediaHandler {
                        wfSuppressWarnings();
                        $ret = unserialize( $metadata );
                        wfRestoreWarnings();
+
                        return $ret;
                }
+
                return $metadata;
        }
 
@@ -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 ) );
+       }
+
 }