From 3bb6a37ae4c6bbe82e031c3a0a0cdc4ed978681d Mon Sep 17 00:00:00 2001 From: Jan Gerber Date: Mon, 23 Jul 2012 11:59:38 -0700 Subject: [PATCH] Split Generic into MediaHandler and ImageHandler media/Generic.php contains two classes, split it into MediaHandler.php and ImageHandler.php Change-Id: Id5027b397e3156cf70312b3ffa37227ca5c3b81c --- includes/AutoLoader.php | 4 +- includes/media/ImageHandler.php | 255 ++++++++++++++++++ .../media/{Generic.php => MediaHandler.php} | 233 ---------------- 3 files changed, 257 insertions(+), 235 deletions(-) create mode 100644 includes/media/ImageHandler.php rename includes/media/{Generic.php => MediaHandler.php} (73%) diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 6c37d1abcb..2987b31a6c 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -689,11 +689,11 @@ $wgAutoloadLocalClasses = array( 'FormatMetadata' => 'includes/media/FormatMetadata.php', 'GIFHandler' => 'includes/media/GIF.php', 'GIFMetadataExtractor' => 'includes/media/GIFMetadataExtractor.php', - 'ImageHandler' => 'includes/media/Generic.php', + 'ImageHandler' => 'includes/media/ImageHandler.php', 'IPTC' => 'includes/media/IPTC.php', 'JpegHandler' => 'includes/media/Jpeg.php', 'JpegMetadataExtractor' => 'includes/media/JpegMetadataExtractor.php', - 'MediaHandler' => 'includes/media/Generic.php', + 'MediaHandler' => 'includes/media/MediaHandler.php', 'MediaTransformError' => 'includes/media/MediaTransformOutput.php', 'MediaTransformOutput' => 'includes/media/MediaTransformOutput.php', 'PNGHandler' => 'includes/media/PNG.php', diff --git a/includes/media/ImageHandler.php b/includes/media/ImageHandler.php new file mode 100644 index 0000000000..69f51bea0e --- /dev/null +++ b/includes/media/ImageHandler.php @@ -0,0 +1,255 @@ +getWidth() && $file->getHeight() ); + } + + function getParamMap() { + return array( 'img_width' => 'width' ); + } + + function validateParam( $name, $value ) { + if ( in_array( $name, array( 'width', 'height' ) ) ) { + if ( $value <= 0 ) { + return false; + } else { + return true; + } + } else { + return false; + } + } + + function makeParamString( $params ) { + if ( isset( $params['physicalWidth'] ) ) { + $width = $params['physicalWidth']; + } elseif ( isset( $params['width'] ) ) { + $width = $params['width']; + } else { + throw new MWException( 'No width specified to '.__METHOD__ ); + } + # Removed for ProofreadPage + #$width = intval( $width ); + return "{$width}px"; + } + + function parseParamString( $str ) { + $m = false; + if ( preg_match( '/^(\d+)px$/', $str, $m ) ) { + return array( 'width' => $m[1] ); + } else { + return false; + } + } + + function getScriptParams( $params ) { + return array( 'width' => $params['width'] ); + } + + /** + * @param $image File + * @param $params + * @return bool + */ + function normaliseParams( $image, &$params ) { + $mimeType = $image->getMimeType(); + + if ( !isset( $params['width'] ) ) { + return false; + } + + if ( !isset( $params['page'] ) ) { + $params['page'] = 1; + } else { + if ( $params['page'] > $image->pageCount() ) { + $params['page'] = $image->pageCount(); + } + + if ( $params['page'] < 1 ) { + $params['page'] = 1; + } + } + + $srcWidth = $image->getWidth( $params['page'] ); + $srcHeight = $image->getHeight( $params['page'] ); + + if ( isset( $params['height'] ) && $params['height'] != -1 ) { + # Height & width were both set + if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) { + # Height is the relative smaller dimension, so scale width accordingly + $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] ); + + if ( $params['width'] == 0 ) { + # Very small image, so we need to rely on client side scaling :( + $params['width'] = 1; + } + + $params['physicalWidth'] = $params['width']; + } else { + # Height was crap, unset it so that it will be calculated later + unset( $params['height'] ); + } + } + + if ( !isset( $params['physicalWidth'] ) ) { + # Passed all validations, so set the physicalWidth + $params['physicalWidth'] = $params['width']; + } + + # Because thumbs are only referred to by width, the height always needs + # to be scaled by the width to keep the thumbnail sizes consistent, + # even if it was set inside the if block above + $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, + $params['physicalWidth'] ); + + # Set the height if it was not validated in the if block higher up + if ( !isset( $params['height'] ) || $params['height'] == -1 ) { + $params['height'] = $params['physicalHeight']; + } + + + if ( !$this->validateThumbParams( $params['physicalWidth'], + $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType ) ) { + return false; + } + return true; + } + + /** + * Validate thumbnail parameters and fill in the correct height + * + * @param $width Integer: specified width (input/output) + * @param $height Integer: height (output only) + * @param $srcWidth Integer: width of the source image + * @param $srcHeight Integer: height of the source image + * @param $mimeType + * @return bool False to indicate that an error should be returned to the user. + */ + function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) { + $width = intval( $width ); + + # Sanity check $width + if( $width <= 0) { + wfDebug( __METHOD__.": Invalid destination width: $width\n" ); + return false; + } + if ( $srcWidth <= 0 ) { + wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" ); + return false; + } + + $height = File::scaleHeight( $srcWidth, $srcHeight, $width ); + if ( $height == 0 ) { + # Force height to be at least 1 pixel + $height = 1; + } + return true; + } + + /** + * @param $image File + * @param $script + * @param $params + * @return bool|ThumbnailImage + */ + function getScriptedTransform( $image, $script, $params ) { + if ( !$this->normaliseParams( $image, $params ) ) { + return false; + } + $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) ); + $page = isset( $params['page'] ) ? $params['page'] : false; + + if( $image->mustRender() || $params['width'] < $image->getWidth() ) { + return new ThumbnailImage( $image, + $url, $params['width'], $params['height'], false, $page ); + } + } + + function getImageSize( $image, $path ) { + wfSuppressWarnings(); + $gis = getimagesize( $path ); + wfRestoreWarnings(); + return $gis; + } + + function isAnimatedImage( $image ) { + return false; + } + + /** + * @param $file File + * @return string + */ + function getShortDesc( $file ) { + global $wgLang; + $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ); + $widthheight = wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->escaped(); + + return "$widthheight ($nbytes)"; + } + + /** + * @param $file File + * @return string + */ + function getLongDesc( $file ) { + global $wgLang; + $pages = $file->pageCount(); + $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ); + if ( $pages === false || $pages <= 1 ) { + $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(), + $file->getHeight() )->params( $size, + $file->getMimeType() )->parse(); + } else { + $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(), + $file->getHeight() )->params( $size, + $file->getMimeType() )->numParams( $pages )->parse(); + } + return $msg; + } + + /** + * @param $file File + * @return string + */ + function getDimensionsString( $file ) { + $pages = $file->pageCount(); + if ( $pages > 1 ) { + return wfMessage( 'widthheightpage' )->numParams( $file->getWidth(), $file->getHeight(), $pages )->text(); + } else { + return wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->text(); + } + } +} diff --git a/includes/media/Generic.php b/includes/media/MediaHandler.php similarity index 73% rename from includes/media/Generic.php rename to includes/media/MediaHandler.php index bd4d8651e0..e883b7f8ec 100644 --- a/includes/media/Generic.php +++ b/includes/media/MediaHandler.php @@ -545,236 +545,3 @@ abstract class MediaHandler { // Do nothing } } - -/** - * Media handler abstract base class for images - * - * @ingroup Media - */ -abstract class ImageHandler extends MediaHandler { - - /** - * @param $file File - * @return bool - */ - function canRender( $file ) { - return ( $file->getWidth() && $file->getHeight() ); - } - - function getParamMap() { - return array( 'img_width' => 'width' ); - } - - function validateParam( $name, $value ) { - if ( in_array( $name, array( 'width', 'height' ) ) ) { - if ( $value <= 0 ) { - return false; - } else { - return true; - } - } else { - return false; - } - } - - function makeParamString( $params ) { - if ( isset( $params['physicalWidth'] ) ) { - $width = $params['physicalWidth']; - } elseif ( isset( $params['width'] ) ) { - $width = $params['width']; - } else { - throw new MWException( 'No width specified to '.__METHOD__ ); - } - # Removed for ProofreadPage - #$width = intval( $width ); - return "{$width}px"; - } - - function parseParamString( $str ) { - $m = false; - if ( preg_match( '/^(\d+)px$/', $str, $m ) ) { - return array( 'width' => $m[1] ); - } else { - return false; - } - } - - function getScriptParams( $params ) { - return array( 'width' => $params['width'] ); - } - - /** - * @param $image File - * @param $params - * @return bool - */ - function normaliseParams( $image, &$params ) { - $mimeType = $image->getMimeType(); - - if ( !isset( $params['width'] ) ) { - return false; - } - - if ( !isset( $params['page'] ) ) { - $params['page'] = 1; - } else { - if ( $params['page'] > $image->pageCount() ) { - $params['page'] = $image->pageCount(); - } - - if ( $params['page'] < 1 ) { - $params['page'] = 1; - } - } - - $srcWidth = $image->getWidth( $params['page'] ); - $srcHeight = $image->getHeight( $params['page'] ); - - if ( isset( $params['height'] ) && $params['height'] != -1 ) { - # Height & width were both set - if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) { - # Height is the relative smaller dimension, so scale width accordingly - $params['width'] = self::fitBoxWidth( $srcWidth, $srcHeight, $params['height'] ); - - if ( $params['width'] == 0 ) { - # Very small image, so we need to rely on client side scaling :( - $params['width'] = 1; - } - - $params['physicalWidth'] = $params['width']; - } else { - # Height was crap, unset it so that it will be calculated later - unset( $params['height'] ); - } - } - - if ( !isset( $params['physicalWidth'] ) ) { - # Passed all validations, so set the physicalWidth - $params['physicalWidth'] = $params['width']; - } - - # Because thumbs are only referred to by width, the height always needs - # to be scaled by the width to keep the thumbnail sizes consistent, - # even if it was set inside the if block above - $params['physicalHeight'] = File::scaleHeight( $srcWidth, $srcHeight, - $params['physicalWidth'] ); - - # Set the height if it was not validated in the if block higher up - if ( !isset( $params['height'] ) || $params['height'] == -1 ) { - $params['height'] = $params['physicalHeight']; - } - - - if ( !$this->validateThumbParams( $params['physicalWidth'], - $params['physicalHeight'], $srcWidth, $srcHeight, $mimeType ) ) { - return false; - } - return true; - } - - /** - * Validate thumbnail parameters and fill in the correct height - * - * @param $width Integer: specified width (input/output) - * @param $height Integer: height (output only) - * @param $srcWidth Integer: width of the source image - * @param $srcHeight Integer: height of the source image - * @param $mimeType - * @return bool False to indicate that an error should be returned to the user. - */ - function validateThumbParams( &$width, &$height, $srcWidth, $srcHeight, $mimeType ) { - $width = intval( $width ); - - # Sanity check $width - if( $width <= 0) { - wfDebug( __METHOD__.": Invalid destination width: $width\n" ); - return false; - } - if ( $srcWidth <= 0 ) { - wfDebug( __METHOD__.": Invalid source width: $srcWidth\n" ); - return false; - } - - $height = File::scaleHeight( $srcWidth, $srcHeight, $width ); - if ( $height == 0 ) { - # Force height to be at least 1 pixel - $height = 1; - } - return true; - } - - /** - * @param $image File - * @param $script - * @param $params - * @return bool|ThumbnailImage - */ - function getScriptedTransform( $image, $script, $params ) { - if ( !$this->normaliseParams( $image, $params ) ) { - return false; - } - $url = $script . '&' . wfArrayToCGI( $this->getScriptParams( $params ) ); - $page = isset( $params['page'] ) ? $params['page'] : false; - - if( $image->mustRender() || $params['width'] < $image->getWidth() ) { - return new ThumbnailImage( $image, - $url, $params['width'], $params['height'], false, $page ); - } - } - - function getImageSize( $image, $path ) { - wfSuppressWarnings(); - $gis = getimagesize( $path ); - wfRestoreWarnings(); - return $gis; - } - - function isAnimatedImage( $image ) { - return false; - } - - /** - * @param $file File - * @return string - */ - function getShortDesc( $file ) { - global $wgLang; - $nbytes = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ); - $widthheight = wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->escaped(); - - return "$widthheight ($nbytes)"; - } - - /** - * @param $file File - * @return string - */ - function getLongDesc( $file ) { - global $wgLang; - $pages = $file->pageCount(); - $size = htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ); - if ( $pages === false || $pages <= 1 ) { - $msg = wfMessage( 'file-info-size' )->numParams( $file->getWidth(), - $file->getHeight() )->params( $size, - $file->getMimeType() )->parse(); - } else { - $msg = wfMessage( 'file-info-size-pages' )->numParams( $file->getWidth(), - $file->getHeight() )->params( $size, - $file->getMimeType() )->numParams( $pages )->parse(); - } - return $msg; - } - - /** - * @param $file File - * @return string - */ - function getDimensionsString( $file ) { - $pages = $file->pageCount(); - if ( $pages > 1 ) { - return wfMessage( 'widthheightpage' )->numParams( $file->getWidth(), $file->getHeight(), $pages )->text(); - } else { - return wfMessage( 'widthheight' )->numParams( $file->getWidth(), $file->getHeight() )->text(); - } - } -} -- 2.20.1