From ef3c0a92b4a64e6a960d2c44c7378817755ee6b8 Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Fri, 18 Mar 2011 22:28:19 +0000 Subject: [PATCH] Added hook BitmapHandlerTransform to allow extension to transform a file without overriding the entire handler. --- RELEASE-NOTES | 2 ++ docs/hooks.txt | 7 +++++++ includes/media/Bitmap.php | 19 +++++++++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 54b60b2ff1..52d60a8886 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -116,6 +116,8 @@ PHP if you have not done so prior to upgrading MediaWiki. file description page. The sizes are set by $wgImageLimits. * (bug 28031) Add pageCount support to ArchivedFile * (bug 27924) PhpHttpRequest doesn't return response body if HTTP != 200 +* Added hook BitmapHandlerTransform to allow extension to transform a file + without overriding the entire handler. === Bug fixes in 1.18 === * (bug 23119) WikiError class and subclasses are now marked as deprecated diff --git a/docs/hooks.txt b/docs/hooks.txt index 9349acb8ed..a30bf9f52b 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -578,6 +578,13 @@ $mediaWiki: Mediawiki object &$parser: Parser object &$ig: ImageGallery object +'BitmapHandlerTransform': before a file is transformed, gives extension the +possibility to transform it themselves +$handler: BitmapHandler +$image: File +&$scalerParams: Array with scaler parameters +&$mto: null, set to a MediaTransformOutput + 'BlockIp': before an IP address or user is blocked $block: the Block object about to be saved $user: the user _doing_ the block (not the one being blocked) diff --git a/includes/media/Bitmap.php b/includes/media/Bitmap.php index f001333340..ff2e56d988 100644 --- a/includes/media/Bitmap.php +++ b/includes/media/Bitmap.php @@ -101,6 +101,7 @@ class BitmapHandler extends ImageHandler { 'mimeType' => $image->getMimeType(), 'srcPath' => $image->getPath(), 'dstPath' => $dstPath, + 'dstUrl' => $dstUrl, ); wfDebug( __METHOD__ . ": creating {$scalerParams['physicalDimensions']} thumbnail at $dstPath\n" ); @@ -135,8 +136,20 @@ class BitmapHandler extends ImageHandler { wfDebug( __METHOD__ . ": Unable to create thumbnail destination directory, falling back to client scaling\n" ); return $this->getClientScalingThumbnailImage( $image, $scalerParams ); } - + + # Try a hook + $mto = null; + wfRunHooks( 'BitmapHandlerTransform', array( $this, $image, &$scalerParams, &$mto ) ); + if ( !is_null( $mto ) ) { + wfDebug( __METHOD__ . ": Hook to BitmapHandlerTransform created an mto\n" ); + $scaler = 'hookaborted'; + } + switch ( $scaler ) { + case 'hookaborted': + # Handled by the hook above + $err = $mto->isError() ? $mto : false; + break; case 'im': $err = $this->transformImageMagick( $image, $scalerParams ); break; @@ -161,6 +174,8 @@ class BitmapHandler extends ImageHandler { # Thumbnail was zero-byte and had to be removed return new MediaTransformError( 'thumbnail_error', $scalerParams['clientWidth'], $scalerParams['clientHeight'] ); + } elseif ( $mto ) { + return $mto; } else { return new ThumbnailImage( $image, $dstUrl, $scalerParams['clientWidth'], $scalerParams['clientHeight'], $dstPath ); @@ -443,7 +458,7 @@ class BitmapHandler extends ImageHandler { * @param $errMsg string Error message * @return MediaTransformError */ - protected function getMediaTransformError( $params, $errMsg ) { + public function getMediaTransformError( $params, $errMsg ) { return new MediaTransformError( 'thumbnail_error', $params['clientWidth'], $params['clientHeight'], $errMsg ); } -- 2.20.1