From 63601400e476c6cf43d985f3e7b9864681695ed4 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Fri, 18 Jan 2013 16:46:04 +0100 Subject: Update to MediaWiki 1.20.2 this update includes: * adjusted Arch Linux skin * updated FluxBBAuthPlugin * patch for https://bugzilla.wikimedia.org/show_bug.cgi?id=44024 --- includes/media/BMP.php | 17 +- includes/media/Bitmap.php | 64 ++- includes/media/BitmapMetadataHandler.php | 41 +- includes/media/Bitmap_ClientOnly.php | 20 +- includes/media/DjVu.php | 50 +- includes/media/DjVuImage.php | 4 +- includes/media/Exif.php | 9 + includes/media/ExifBitmap.php | 20 +- includes/media/FormatMetadata.php | 125 ++--- includes/media/GIF.php | 30 +- includes/media/GIFMetadataExtractor.php | 17 +- includes/media/Generic.php | 751 ------------------------------- includes/media/IPTC.php | 31 +- includes/media/ImageHandler.php | 249 ++++++++++ includes/media/Jpeg.php | 17 + includes/media/JpegMetadataExtractor.php | 44 +- includes/media/MediaHandler.php | 560 +++++++++++++++++++++++ includes/media/MediaTransformOutput.php | 99 +++- includes/media/PNG.php | 29 +- includes/media/PNGMetadataExtractor.php | 16 + includes/media/SVG.php | 79 +++- includes/media/SVGMetadataExtractor.php | 33 +- includes/media/Tiff.php | 15 + includes/media/XCF.php | 17 +- includes/media/XMP.php | 62 ++- includes/media/XMPInfo.php | 22 + includes/media/XMPValidate.php | 22 + 27 files changed, 1524 insertions(+), 919 deletions(-) delete mode 100644 includes/media/Generic.php create mode 100644 includes/media/ImageHandler.php create mode 100644 includes/media/MediaHandler.php (limited to 'includes/media') diff --git a/includes/media/BMP.php b/includes/media/BMP.php index 6886e950..a515c635 100644 --- a/includes/media/BMP.php +++ b/includes/media/BMP.php @@ -1,6 +1,21 @@ $scalerParams['clientWidth'], + 'height' => $scalerParams['clientHeight'] + ); + return new ThumbnailImage( $image, $dstUrl, false, $params ); } # Try to make a target path for the thumbnail @@ -205,8 +223,11 @@ class BitmapHandler extends ImageHandler { } elseif ( $mto ) { return $mto; } else { - return new ThumbnailImage( $image, $dstUrl, $scalerParams['clientWidth'], - $scalerParams['clientHeight'], $dstPath ); + $params = array( + 'width' => $scalerParams['clientWidth'], + 'height' => $scalerParams['clientHeight'] + ); + return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); } } @@ -243,14 +264,17 @@ class BitmapHandler extends ImageHandler { * client side * * @param $image File File associated with this thumbnail - * @param $params array Array with scaler params + * @param $scalerParams array Array with scaler params * @return ThumbnailImage * - * @fixme no rotation support + * @todo fixme: no rotation support */ - protected function getClientScalingThumbnailImage( $image, $params ) { - return new ThumbnailImage( $image, $image->getURL(), - $params['clientWidth'], $params['clientHeight'], null ); + protected function getClientScalingThumbnailImage( $image, $scalerParams ) { + $params = array( + 'width' => $scalerParams['clientWidth'], + 'height' => $scalerParams['clientHeight'] + ); + return new ThumbnailImage( $image, $image->getURL(), null, $params ); } /** @@ -259,7 +283,7 @@ class BitmapHandler extends ImageHandler { * @param $image File File associated with this thumbnail * @param $params array Array with scaler params * - * @return MediaTransformError Error object if error occured, false (=no error) otherwise + * @return MediaTransformError Error object if error occurred, false (=no error) otherwise */ protected function transformImageMagick( $image, $params ) { # use ImageMagick @@ -358,7 +382,7 @@ class BitmapHandler extends ImageHandler { * @param $image File File associated with this thumbnail * @param $params array Array with scaler params * - * @return MediaTransformError Error object if error occured, false (=no error) otherwise + * @return MediaTransformError Error object if error occurred, false (=no error) otherwise */ protected function transformImageMagickExt( $image, $params ) { global $wgSharpenReductionThreshold, $wgSharpenParameter, $wgMaxAnimatedGifArea; @@ -435,7 +459,7 @@ class BitmapHandler extends ImageHandler { * @param $image File File associated with this thumbnail * @param $params array Array with scaler params * - * @return MediaTransformError Error object if error occured, false (=no error) otherwise + * @return MediaTransformError Error object if error occurred, false (=no error) otherwise */ protected function transformCustom( $image, $params ) { # Use a custom convert command @@ -462,7 +486,7 @@ class BitmapHandler extends ImageHandler { } /** - * Log an error that occured in an external process + * Log an error that occurred in an external process * * @param $retval int * @param $err int @@ -491,7 +515,7 @@ class BitmapHandler extends ImageHandler { * @param $image File File associated with this thumbnail * @param $params array Array with scaler params * - * @return MediaTransformError Error object if error occured, false (=no error) otherwise + * @return MediaTransformError Error object if error occurred, false (=no error) otherwise */ protected function transformGd( $image, $params ) { # Use PHP's builtin GD library functions. @@ -509,7 +533,7 @@ class BitmapHandler extends ImageHandler { if ( !isset( $typemap[$params['mimeType']] ) ) { $err = 'Image type not supported'; wfDebug( "$err\n" ); - $errMsg = wfMsg( 'thumbnail_image-type' ); + $errMsg = wfMessage( 'thumbnail_image-type' )->text(); return $this->getMediaTransformError( $params, $errMsg ); } list( $loader, $colorStyle, $saveType ) = $typemap[$params['mimeType']]; @@ -517,14 +541,14 @@ class BitmapHandler extends ImageHandler { if ( !function_exists( $loader ) ) { $err = "Incomplete GD library configuration: missing function $loader"; wfDebug( "$err\n" ); - $errMsg = wfMsg( 'thumbnail_gd-library', $loader ); + $errMsg = wfMessage( 'thumbnail_gd-library', $loader )->text(); return $this->getMediaTransformError( $params, $errMsg ); } if ( !file_exists( $params['srcPath'] ) ) { $err = "File seems to be missing: {$params['srcPath']}"; wfDebug( "$err\n" ); - $errMsg = wfMsg( 'thumbnail_image-missing', $params['srcPath'] ); + $errMsg = wfMessage( 'thumbnail_image-missing', $params['srcPath'] )->text(); return $this->getMediaTransformError( $params, $errMsg ); } @@ -572,6 +596,7 @@ 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 */ function escapeMagickProperty( $s ) { // Double the backslashes @@ -599,6 +624,7 @@ class BitmapHandler extends ImageHandler { * * @param $path string The file path * @param $scene string The scene specification, or false if there is none + * @return string */ function escapeMagickInput( $path, $scene = false ) { # Die on initial metacharacters (caller should prepend path) @@ -616,6 +642,7 @@ class BitmapHandler extends ImageHandler { /** * Escape a string for ImageMagick's output filename. See * InterpretImageFilename() in magick/image.c. + * @return string */ function escapeMagickOutput( $path, $scene = false ) { $path = str_replace( '%', '%%', $path ); @@ -628,6 +655,7 @@ class BitmapHandler extends ImageHandler { * * @param $path string The file path * @param $scene string The scene specification, or false if there is none + * @return string */ protected function escapeMagickPath( $path, $scene = false ) { # Die on format specifiers (other than drive letters). The regex is diff --git a/includes/media/BitmapMetadataHandler.php b/includes/media/BitmapMetadataHandler.php index 746dddda..0a195547 100644 --- a/includes/media/BitmapMetadataHandler.php +++ b/includes/media/BitmapMetadataHandler.php @@ -1,13 +1,36 @@ normaliseParams( $image, $params ) ) { return new TransformParameterError( $params ); } - return new ThumbnailImage( $image, $image->getURL(), $params['width'], - $params['height'], $image->getLocalRefPath() ); + return new ThumbnailImage( $image, $image->getURL(), $image->getLocalRefPath(), $params ); } } diff --git a/includes/media/DjVu.php b/includes/media/DjVu.php index dedbee0d..84672e05 100644 --- a/includes/media/DjVu.php +++ b/includes/media/DjVu.php @@ -1,6 +1,21 @@ text() ); } if ( !$this->normaliseParams( $image, $params ) ) { @@ -131,20 +146,35 @@ class DjVuHandler extends ImageHandler { } $width = $params['width']; $height = $params['height']; - $srcPath = $image->getLocalRefPath(); $page = $params['page']; if ( $page > $this->pageCount( $image ) ) { - return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) ); + return new MediaTransformError( + 'thumbnail_error', + $width, + $height, + wfMessage( 'djvu_page_error' )->text() + ); } if ( $flags & self::TRANSFORM_LATER ) { - return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page ); + $params = array( + 'width' => $width, + 'height' => $height, + 'page' => $page + ); + return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); } if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) { - return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) ); + return new MediaTransformError( + 'thumbnail_error', + $width, + $height, + wfMessage( 'thumbnail_dest_directory' )->text() + ); } + $srcPath = $image->getLocalRefPath(); # Use a subshell (brackets) to aggregate stderr from both pipeline commands # before redirecting it to the overall stdout. This works in both Linux and Windows XP. $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page}" . @@ -167,7 +197,12 @@ class DjVuHandler extends ImageHandler { wfHostname(), $retval, trim($err), $cmd ) ); return new MediaTransformError( 'thumbnail_error', $width, $height, $err ); } else { - return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page ); + $params = array( + 'width' => $width, + 'height' => $height, + 'page' => $page + ); + return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); } } @@ -191,6 +226,7 @@ class DjVuHandler extends ImageHandler { * Cache a document tree for the DjVu XML metadata * @param $image File * @param $gettext Boolean: DOCUMENT (Default: false) + * @return bool */ function getMetaTree( $image , $gettext = false ) { if ( isset( $image->dejaMetaTree ) ) { diff --git a/includes/media/DjVuImage.php b/includes/media/DjVuImage.php index 80b7408c..6aef562b 100644 --- a/includes/media/DjVuImage.php +++ b/includes/media/DjVuImage.php @@ -1,6 +1,6 @@ * http://www.mediawiki.org/ @@ -21,6 +21,7 @@ * http://www.gnu.org/copyleft/gpl.html * * @file + * @ingroup Media */ /** @@ -284,6 +285,7 @@ EOR; /** * Hack to temporarily work around djvutoxml bug + * @return bool|string */ function convertDumpToXML( $dump ) { if ( strval( $dump ) == '' ) { diff --git a/includes/media/Exif.php b/includes/media/Exif.php index a4acdfe0..784a6018 100644 --- a/includes/media/Exif.php +++ b/includes/media/Exif.php @@ -1,5 +1,7 @@ exifGPStoNumber( 'GPSDestLongitude' ); if ( isset( $this->mFilteredExifData['GPSAltitude'] ) && isset( $this->mFilteredExifData['GPSAltitudeRef'] ) ) { + + // We know altitude data is a / from the validation functions ran earlier. + // But multiplying such a string by -1 doesn't work well, so convert. + list( $num, $denom ) = explode( '/', $this->mFilteredExifData['GPSAltitude'] ); + $this->mFilteredExifData['GPSAltitude'] = $num / $denom; + if ( $this->mFilteredExifData['GPSAltitudeRef'] === "\1" ) { $this->mFilteredExifData['GPSAltitude'] *= - 1; } @@ -549,6 +557,7 @@ class Exif { */ /** * Get $this->mRawExifData + * @return array */ function getData() { return $this->mRawExifData; diff --git a/includes/media/ExifBitmap.php b/includes/media/ExifBitmap.php index 7b9867f7..34a1f511 100644 --- a/includes/media/ExifBitmap.php +++ b/includes/media/ExifBitmap.php @@ -1,5 +1,22 @@ 0 ) { - $tags[$tag] = $wgLang->time( $time ); + try { + $time = wfTimestamp( TS_MW, '1971:01:01 ' . $tags[$tag] ); + // the 1971:01:01 is just a placeholder, and not shown to user. + if ( $time && intval( $time ) > 0 ) { + $tags[$tag] = $wgLang->time( $time ); + } + } catch ( TimestampException $e ) { + // This shouldn't happen, but we've seen bad formats + // such as 4-digit seconds in the wild. + // leave $tags[$tag] as-is } continue; } @@ -231,7 +239,7 @@ class FormatMetadata { case 'dc-date': case 'DateTimeMetadata': if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) { - $val = wfMsg( 'exif-unknowndate' ); + $val = wfMessage( 'exif-unknowndate' )->text(); } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) { // Full date. $time = wfTimestamp( TS_MW, $val ); @@ -307,7 +315,7 @@ class FormatMetadata { 'redeye' => ( $val & bindec( '01000000' ) ) >> 6, // 'reserved' => ($val & bindec( '10000000' )) >> 7, ); - + $flashMsgs = array(); # We do not need to handle unknown values since all are used. foreach ( $flashDecode as $subTag => $subValue ) { # We do not need any message for zeroed values. @@ -589,7 +597,7 @@ class FormatMetadata { case 'Software': if ( is_array( $val ) ) { //if its a software, version array. - $val = wfMsg( 'exif-software-version-value', $val[0], $val[1] ); + $val = wfMessage( 'exif-software-version-value', $val[0], $val[1] )->text(); } else { $val = self::msg( $tag, '', $val ); } @@ -597,8 +605,8 @@ class FormatMetadata { case 'ExposureTime': // Show the pretty fraction as well as decimal version - $val = wfMsg( 'exif-exposuretime-format', - self::formatFraction( $val ), self::formatNum( $val ) ); + $val = wfMessage( 'exif-exposuretime-format', + self::formatFraction( $val ), self::formatNum( $val ) )->text(); break; case 'ISOSpeedRatings': // If its = 65535 that means its at the @@ -611,13 +619,13 @@ class FormatMetadata { } break; case 'FNumber': - $val = wfMsg( 'exif-fnumber-format', - self::formatNum( $val ) ); + $val = wfMessage( 'exif-fnumber-format', + self::formatNum( $val ) )->text(); break; case 'FocalLength': case 'FocalLengthIn35mmFilm': - $val = wfMsg( 'exif-focallength-format', - self::formatNum( $val ) ); + $val = wfMessage( 'exif-focallength-format', + self::formatNum( $val ) )->text(); break; case 'MaxApertureValue': @@ -631,14 +639,14 @@ class FormatMetadata { if ( is_numeric( $val ) ) { $fNumber = pow( 2, $val / 2 ); if ( $fNumber !== false ) { - $val = wfMsg( 'exif-maxaperturevalue-value', + $val = wfMessage( 'exif-maxaperturevalue-value', self::formatNum( $val ), self::formatNum( $fNumber, 2 ) - ); + )->text(); } } break; - + case 'iimCategory': switch( strtolower( $val ) ) { // See pg 29 of IPTC photo @@ -694,7 +702,7 @@ class FormatMetadata { case 'PixelYDimension': case 'ImageWidth': case 'ImageLength': - $val = self::formatNum( $val ) . ' ' . wfMsg( 'unit-pixel' ); + $val = self::formatNum( $val ) . ' ' . wfMessage( 'unit-pixel' )->text(); break; // Do not transform fields with pure text. @@ -800,7 +808,7 @@ class FormatMetadata { break; case 'LanguageCode': - $lang = $wgLang->getLanguageName( strtolower( $val ) ); + $lang = Language::fetchLanguageName( strtolower( $val ), $wgLang->getCode() ); if ($lang) { $val = htmlspecialchars( $lang ); } else { @@ -825,14 +833,14 @@ class FormatMetadata { * This turns an array of (for example) authors into a bulleted list. * * This is public on the basis it might be useful outside of this class. - * + * * @param $vals Array array of values * @param $type String Type of array (either lang, ul, ol). * lang = language assoc array with keys being the lang code * ul = unordered list, ol = ordered list * type can also come from the '_type' member of $vals. * @param $noHtml Boolean If to avoid returning anything resembling - * html. (Ugly hack for backwards compatibility with old mediawiki). + * html. (Ugly hack for backwards compatibility with old mediawiki). * @return String single value (in wiki-syntax). */ public static function flattenArray( $vals, $type = 'ul', $noHtml = false ) { @@ -874,7 +882,7 @@ class FormatMetadata { // If default is set, save it for later, // as we don't know if it's equal to // one of the lang codes. (In xmp - // you specify the language for a + // you specify the language for a // default property by having both // a default prop, and one in the language // that are identical) @@ -937,11 +945,11 @@ class FormatMetadata { * @param $lang String lang code of item or false * @param $default Boolean if it is default value. * @param $noHtml Boolean If to avoid html (for back-compat) - * @return language item (Note: despite how this looks, - * this is treated as wikitext not html). + * @throws MWException + * @return string language item (Note: despite how this looks, + * this is treated as wikitext not html). */ private static function langItem( $value, $lang, $default = false, $noHtml = false ) { - global $wgContLang; if ( $lang === false && $default === false) { throw new MWException('$lang and $default cannot both ' . 'be false.'); @@ -956,21 +964,21 @@ class FormatMetadata { if ( $lang === false ) { if ( $noHtml ) { - return wfMsg( 'metadata-langitem-default', - $wrappedValue ) . "\n\n"; + return wfMessage( 'metadata-langitem-default', + $wrappedValue )->text() . "\n\n"; } /* else */ return '\n"; } $lowLang = strtolower( $lang ); - $langName = $wgContLang->getLanguageName( $lowLang ); + $langName = Language::fetchLanguageName( $lowLang ); if ( $langName === '' ) { //try just the base language name. (aka en-US -> en ). list( $langPrefix ) = explode( '-', $lowLang, 2 ); - $langName = $wgContLang->getLanguageName( $langPrefix ); + $langName = Language::fetchLanguageName( $langPrefix ); if ( $langName === '' ) { // give up. $langName = $lang; @@ -979,8 +987,8 @@ class FormatMetadata { // else we have a language specified if ( $noHtml ) { - return '*' . wfMsg( 'metadata-langitem', - $wrappedValue, $langName, $lang ); + return '*' . wfMessage( 'metadata-langitem', + $wrappedValue, $langName, $lang )->text(); } /* else: */ $item = '\n"; return $item; } @@ -1004,24 +1012,22 @@ class FormatMetadata { * @param $val String: the value of the tag * @param $arg String: an argument to pass ($1) * @param $arg2 String: a 2nd argument to pass ($2) - * @return string A wfMsg of "exif-$tag-$val" in lower case + * @return string A wfMessage of "exif-$tag-$val" in lower case */ static function msg( $tag, $val, $arg = null, $arg2 = null ) { global $wgContLang; if ($val === '') $val = 'value'; - return wfMsg( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 ); + return wfMessage( $wgContLang->lc( "exif-$tag-$val" ), $arg, $arg2 )->text(); } /** * Format a number, convert numbers from fractions into floating point * numbers, joins arrays of numbers with commas. * - * @private - * * @param $num Mixed: the value to format - * @param $round digits to round to or false. + * @param $round float|int|bool digits to round to or false. * @return mixed A floating point number or whatever we were fed */ static function formatNum( $num, $round = false ) { @@ -1102,8 +1108,9 @@ class FormatMetadata { return $a; } - /** Fetch the human readable version of a news code. - * A news code is an 8 digit code. The first two + /** + * Fetch the human readable version of a news code. + * A news code is an 8 digit code. The first two * digits are a general classification, so we just * translate that. * @@ -1111,7 +1118,7 @@ class FormatMetadata { * a string, not an int. * * @param $val String: The 8 digit news code. - * @return The human readable form + * @return string The human readable form */ static private function convertNewsCode( $val ) { if ( !preg_match( '/^\d{8}$/D', $val ) ) { @@ -1183,7 +1190,7 @@ class FormatMetadata { * Format a coordinate value, convert numbers from floating point * into degree minute second representation. * - * @param $coord Array: degrees, minutes and seconds + * @param $coord int degrees, minutes and seconds * @param $type String: latitude or longitude (for if its a NWS or E) * @return mixed A floating point number or whatever we were fed */ @@ -1193,17 +1200,14 @@ class FormatMetadata { $nCoord = -$coord; if ( $type === 'latitude' ) { $ref = 'S'; - } - elseif ( $type === 'longitude' ) { + } elseif ( $type === 'longitude' ) { $ref = 'W'; } - } - else { + } else { $nCoord = $coord; if ( $type === 'latitude' ) { $ref = 'N'; - } - elseif ( $type === 'longitude' ) { + } elseif ( $type === 'longitude' ) { $ref = 'E'; } } @@ -1216,7 +1220,7 @@ class FormatMetadata { $min = self::formatNum( $min ); $sec = self::formatNum( $sec ); - return wfMsg( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord ); + return wfMessage( 'exif-coordinate-format', $deg, $min, $sec, $ref, $coord )->text(); } /** @@ -1274,7 +1278,7 @@ class FormatMetadata { // Todo: This can potentially be multi-line. // Need to check how that works in XMP. $street = '' - . htmlspecialchars( + . htmlspecialchars( $vals['CiAdrExtadr'] ) . ''; } @@ -1321,7 +1325,7 @@ class FormatMetadata { } if ( isset( $vals['CiAdrPcode'] ) ) { $postal = '' - . htmlspecialchars( + . htmlspecialchars( $vals['CiAdrPcode'] ) . ''; } @@ -1337,9 +1341,9 @@ class FormatMetadata { . htmlspecialchars( $vals['CiUrlWork'] ) . ''; } - return wfMsg( 'exif-contact-value', $email, $url, + return wfMessage( 'exif-contact-value', $email, $url, $street, $city, $region, $postal, $country, - $tel ); + $tel )->text(); } } } @@ -1352,12 +1356,19 @@ class FormatMetadata { **/ class FormatExif { var $meta; - function FormatExif ( $meta ) { + + /** + * @param $meta array + */ + function FormatExif( $meta ) { wfDeprecated(__METHOD__); $this->meta = $meta; } - function getFormattedData ( ) { + /** + * @return array + */ + function getFormattedData() { return FormatMetadata::getFormattedData( $this->meta ); } } diff --git a/includes/media/GIF.php b/includes/media/GIF.php index 32618e94..84b9b8ca 100644 --- a/includes/media/GIF.php +++ b/includes/media/GIF.php @@ -2,6 +2,21 @@ /** * Handler for GIF images. * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * * @file * @ingroup Media */ @@ -78,6 +93,17 @@ class GIFHandler extends BitmapHandler { return false; } + /** + * We cannot animate thumbnails that are bigger than a particular size + * @param File $file + * @return bool + */ + function canAnimateThumbnail( $file ) { + global $wgMaxAnimatedGifArea; + $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea; + return $answer; + } + function getMetadataType( $image ) { return 'parsed-gif'; } @@ -127,11 +153,11 @@ class GIFHandler extends BitmapHandler { $info[] = $original; if ( $metadata['looped'] ) { - $info[] = wfMsgExt( 'file-info-gif-looped', 'parseinline' ); + $info[] = wfMessage( 'file-info-gif-looped' )->parse(); } if ( $metadata['frameCount'] > 1 ) { - $info[] = wfMsgExt( 'file-info-gif-frames', 'parseinline', $metadata['frameCount'] ); + $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse(); } if ( $metadata['duration'] ) { diff --git a/includes/media/GIFMetadataExtractor.php b/includes/media/GIFMetadataExtractor.php index 5dbeb8f8..5fc5c1a7 100644 --- a/includes/media/GIFMetadataExtractor.php +++ b/includes/media/GIFMetadataExtractor.php @@ -7,6 +7,21 @@ * Deliberately not using MWExceptions to avoid external dependencies, encouraging * redistribution. * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * * @file * @ingroup Media */ @@ -286,7 +301,7 @@ class GIFMetadataExtractor { * sub-blocks in the returned value. Normally this is false, * except XMP is weird and does a hack where you need to keep * these length bytes. - * @return The data. + * @return string The data. */ static function readBlock( $fh, $includeLengths = false ) { $data = ''; diff --git a/includes/media/Generic.php b/includes/media/Generic.php deleted file mode 100644 index 271d3a8d..00000000 --- a/includes/media/Generic.php +++ /dev/null @@ -1,751 +0,0 @@ -isEnabled() ) { - self::$handlers[$class] = false; - } - } - return self::$handlers[$class]; - } - - /** - * Get an associative array mapping magic word IDs to parameter names. - * Will be used by the parser to identify parameters. - */ - abstract function getParamMap(); - - /** - * Validate a thumbnail parameter at parse time. - * 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 $value - */ - abstract function validateParam( $name, $value ); - - /** - * Merge a parameter array into a string appropriate for inclusion in filenames - * - * @param $params array - */ - abstract function makeParamString( $params ); - - /** - * Parse a param string made with makeParamString back into an array - * - * @param $str string - */ - abstract function parseParamString( $str ); - - /** - * Changes the parameter array as necessary, ready for transformation. - * Should be idempotent. - * Returns false if the parameters are unacceptable and the transform should fail - * @param $image - * @param $params - */ - abstract function normaliseParams( $image, &$params ); - - /** - * 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 $path String: the filename - * @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. - * Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!) - * @param $path String: the filename - * @return String - */ - function getMetadata( $image, $path ) { return ''; } - - /** - * Get metadata version. - * - * This is not used for validating metadata, this is used for the api when returning - * metadata, since api content formats should stay the same over time, and so things - * using ForiegnApiRepo can keep backwards compatibility - * - * All core media handlers share a common version number, and extensions can - * use the GetMetadataVersion hook to append to the array (they should append a unique - * string so not to get confusing). If there was a media handler named 'foo' with metadata - * 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 - */ - static function getMetadataVersion () { - $version = Array( '2' ); // core metadata version - wfRunHooks('GetMetadataVersion', Array(&$version)); - return implode( ';', $version); - } - - /** - * Convert metadata version. - * - * 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. - */ - function convertMetadataVersion( $metadata, $version = 1 ) { - if ( !is_array( $metadata ) ) { - - //unserialize to keep return parameter consistent. - wfSuppressWarnings(); - $ret = unserialize( $metadata ); - wfRestoreWarnings(); - return $ret; - } - return $metadata; - } - - /** - * Get a string describing the type of metadata, for display purposes. - * - * @return string - */ - function getMetadataType( $image ) { return false; } - - /** - * Check if the metadata string is valid for this handler. - * If it returns MediaHandler::METADATA_BAD (or false), Image - * will reload the metadata from the file and update the database. - * MediaHandler::METADATA_GOOD for if the metadata is a-ok, - * MediaHanlder::METADATA_COMPATIBLE if metadata is old but backwards - * compatible (which may or may not trigger a metadata reload). - */ - function isMetadataValid( $image, $metadata ) { - return self::METADATA_GOOD; - } - - - /** - * Get a MediaTransformOutput object representing an alternate of the transformed - * output which will call an intermediary thumbnail assist script. - * - * Used when the repository has a thumbnailScriptUrl option configured. - * - * Return false to fall back to the regular getTransform(). - */ - function getScriptedTransform( $image, $script, $params ) { - return false; - } - - /** - * Get a MediaTransformOutput object representing the transformed output. Does not - * actually do the transform. - * - * @param $image File: the image object - * @param $dstPath String: filesystem destination path - * @param $dstUrl String: Destination URL to use in output HTML - * @param $params Array: Arbitrary set of parameters validated by $this->validateParam() - */ - final function getTransform( $image, $dstPath, $dstUrl, $params ) { - return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER ); - } - - /** - * Get a MediaTransformOutput object representing the transformed output. Does the - * transform unless $flags contains self::TRANSFORM_LATER. - * - * @param $image File: the image object - * @param $dstPath String: filesystem destination path - * @param $dstUrl String: destination URL to use in output HTML - * @param $params Array: arbitrary set of parameters validated by $this->validateParam() - * @param $flags Integer: a bitfield, may contain self::TRANSFORM_LATER - * - * @return MediaTransformOutput - */ - abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ); - - /** - * Get the thumbnail extension and MIME type for a given source MIME type - * @return array thumbnail extension and MIME type - */ - function getThumbType( $ext, $mime, $params = null ) { - $magic = MimeMagic::singleton(); - if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) { - // The extension is not valid for this mime type and we do - // recognize the mime type - $extensions = $magic->getExtensionsForType( $mime ); - if ( $extensions ) { - return array( strtok( $extensions, ' ' ), $mime ); - } - } - - // The extension is correct (true) or the mime type is unknown to - // MediaWiki (null) - return array( $ext, $mime ); - } - - /** - * True if the handled types can be transformed - */ - function canRender( $file ) { return true; } - /** - * True if handled types cannot be displayed directly in a browser - * but can be rendered - */ - function mustRender( $file ) { return false; } - /** - * True if the type has multi-page capabilities - */ - function isMultiPage( $file ) { return false; } - /** - * Page count for a multi-page document, false if unsupported or unknown - */ - function pageCount( $file ) { return false; } - /** - * The material is vectorized and thus scaling is lossless - */ - function isVectorized( $file ) { return false; } - /** - * False if the handler is disabled for all files - */ - function isEnabled() { return true; } - - /** - * Get an associative array of page dimensions - * Currently "width" and "height" are understood, but this might be - * expanded in the future. - * Returns false if unknown or if the document is not multi-page. - * - * @param $image File - */ - function getPageDimensions( $image, $page ) { - $gis = $this->getImageSize( $image, $image->getLocalRefPath() ); - return array( - 'width' => $gis[0], - 'height' => $gis[1] - ); - } - - /** - * Generic getter for text layer. - * Currently overloaded by PDF and DjVu handlers - */ - function getPageText( $image, $page ) { - return false; - } - - /** - * Get an array structure that looks like this: - * - * array( - * 'visible' => array( - * 'Human-readable name' => 'Human readable value', - * ... - * ), - * 'collapsed' => array( - * 'Human-readable name' => 'Human readable value', - * ... - * ) - * ) - * The UI will format this into a table where the visible fields are always - * visible, and the collapsed fields are optionally visible. - * - * The function should return false if there is no metadata to display. - */ - - /** - * @todo FIXME: I don't really like this interface, it's not very flexible - * I think the media handler should generate HTML instead. It can do - * all the formatting according to some standard. That makes it possible - * to do things like visual indication of grouped and chained streams - * in ogg container files. - */ - function formatMetadata( $image ) { - return false; - } - - /** sorts the visible/invisible field. - * Split off from ImageHandler::formatMetadata, as used by more than - * one type of handler. - * - * This is used by the media handlers that use the FormatMetadata class - * - * @param $metadataArray Array metadata array - * @return array for use displaying metadata. - */ - function formatMetadataHelper( $metadataArray ) { - $result = array( - 'visible' => array(), - 'collapsed' => array() - ); - - $formatted = FormatMetadata::getFormattedData( $metadataArray ); - // Sort fields into visible and collapsed - $visibleFields = $this->visibleMetadataFields(); - foreach ( $formatted as $name => $value ) { - $tag = strtolower( $name ); - self::addMeta( $result, - in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed', - 'exif', - $tag, - $value - ); - } - return $result; - } - - /** - * Get a list of metadata items which should be displayed when - * the metadata table is collapsed. - * - * @return array of strings - * @access protected - */ - function visibleMetadataFields() { - $fields = array(); - $lines = explode( "\n", wfMsgForContent( 'metadata-fields' ) ); - foreach( $lines as $line ) { - $matches = array(); - if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) { - $fields[] = $matches[1]; - } - } - $fields = array_map( 'strtolower', $fields ); - return $fields; - } - - - /** - * This is used to generate an array element for each metadata value - * That array is then used to generate the table of metadata values - * on the image page - * - * @param &$array Array An array containing elements for each type of visibility - * and each of those elements being an array of metadata items. This function adds - * a value to that array. - * @param $visibility string ('visible' or 'collapsed') if this value is hidden - * by default. - * @param $type String type of metadata tag (currently always 'exif') - * @param $id String the name of the metadata tag (like 'artist' for example). - * its name in the table displayed is the message "$type-$id" (Ex exif-artist ). - * @param $value String thingy goes into a wikitext table; it used to be escaped but - * that was incompatible with previous practise of customized display - * with wikitext formatting via messages such as 'exif-model-value'. - * So the escaping is taken back out, but generally this seems a confusing - * interface. - * @param $param String value to pass to the message for the name of the field - * as $1. Currently this parameter doesn't seem to ever be used. - * - * Note, everything here is passed through the parser later on (!) - */ - protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) { - $msg = wfMessage( "$type-$id", $param ); - if ( $msg->exists() ) { - $name = $msg->text(); - } else { - // This is for future compatibility when using instant commons. - // So as to not display as ugly a name if a new metadata - // property is defined that we don't know about - // (not a major issue since such a property would be collapsed - // by default). - wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" ); - $name = wfEscapeWikiText( $id ); - } - $array[$visibility][] = array( - 'id' => "$type-$id", - 'name' => $name, - 'value' => $value - ); - } - - /** - * @param $file File - * @return string - */ - function getShortDesc( $file ) { - global $wgLang; - return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ); - } - - /** - * @param $file File - * @return string - */ - function getLongDesc( $file ) { - global $wgLang; - return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ), - $file->getMimeType() )->parse(); - } - - /** - * @param $file File - * @return string - */ - static function getGeneralShortDesc( $file ) { - global $wgLang; - return $wgLang->formatSize( $file->getSize() ); - } - - /** - * @param $file File - * @return string - */ - static function getGeneralLongDesc( $file ) { - global $wgLang; - return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ), - $file->getMimeType() )->parse(); - } - - /** - * Calculate the largest thumbnail width for a given original file size - * such that the thumbnail's height is at most $maxHeight. - * @param $boxWidth Integer Width of the thumbnail box. - * @param $boxHeight Integer Height of the thumbnail box. - * @param $maxHeight Integer Maximum height expected for the thumbnail. - * @return Integer. - */ - public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) { - $idealWidth = $boxWidth * $maxHeight / $boxHeight; - $roundedUp = ceil( $idealWidth ); - if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) { - return floor( $idealWidth ); - } else { - return $roundedUp; - } - } - - function getDimensionsString( $file ) { - return ''; - } - - /** - * Modify the parser object post-transform - */ - function parserTransformHook( $parser, $file ) {} - - /** - * File validation hook called on upload. - * - * If the file at the given local path is not valid, or its MIME type does not - * match the handler class, a Status object should be returned containing - * relevant errors. - * - * @param $fileName The local path to the file. - * @return Status object - */ - function verifyUpload( $fileName ) { - return Status::newGood(); - } - - /** - * Check for zero-sized thumbnails. These can be generated when - * no disk space is available or some other error occurs - * - * @param $dstPath The location of the suspect file - * @param $retval Return value of some shell process, file will be deleted if this is non-zero - * @return true if removed, false otherwise - */ - function removeBadFile( $dstPath, $retval = 0 ) { - if( file_exists( $dstPath ) ) { - $thumbstat = stat( $dstPath ); - if( $thumbstat['size'] == 0 || $retval != 0 ) { - $result = unlink( $dstPath ); - - if ( $result ) { - wfDebugLog( 'thumbnail', - sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded', - $thumbstat['size'], $dstPath ) ); - } else { - wfDebugLog( 'thumbnail', - sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed', - $thumbstat['size'], $dstPath ) ); - } - return true; - } - } - return false; - } - - /** - * Remove files from the purge list - * - * @param array $files - * @param array $options - */ - public function filterThumbnailPurgeList( &$files, $options ) { - // 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 Unused - * @return 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'], $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/IPTC.php b/includes/media/IPTC.php index 1d19791c..8fd3552f 100644 --- a/includes/media/IPTC.php +++ b/includes/media/IPTC.php @@ -1,8 +1,31 @@ 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 ) ); + + if( $image->mustRender() || $params['width'] < $image->getWidth() ) { + return new ThumbnailImage( $image, $url, false, $params ); + } + } + + function getImageSize( $image, $path ) { + wfSuppressWarnings(); + $gis = getimagesize( $path ); + wfRestoreWarnings(); + return $gis; + } + + /** + * @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/Jpeg.php b/includes/media/Jpeg.php index 7033409b..a15b6524 100644 --- a/includes/media/Jpeg.php +++ b/includes/media/Jpeg.php @@ -1,5 +1,22 @@ isEnabled() ) { + self::$handlers[$class] = false; + } + } + return self::$handlers[$class]; + } + + /** + * Get an associative array mapping magic word IDs to parameter names. + * Will be used by the parser to identify parameters. + */ + abstract function getParamMap(); + + /** + * Validate a thumbnail parameter at parse time. + * 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 $value + */ + abstract function validateParam( $name, $value ); + + /** + * Merge a parameter array into a string appropriate for inclusion in filenames + * + * @param $params array + */ + abstract function makeParamString( $params ); + + /** + * Parse a param string made with makeParamString back into an array + * + * @param $str string + */ + abstract function parseParamString( $str ); + + /** + * Changes the parameter array as necessary, ready for transformation. + * Should be idempotent. + * Returns false if the parameters are unacceptable and the transform should fail + * @param $image + * @param $params + */ + abstract function normaliseParams( $image, &$params ); + + /** + * 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 $path String: the filename + * @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. + * Warning, FSFile::getPropsFromPath might pass an (object)array() instead (!) + * @param $path String: the filename + * @return String + */ + function getMetadata( $image, $path ) { return ''; } + + /** + * Get metadata version. + * + * This is not used for validating metadata, this is used for the api when returning + * metadata, since api content formats should stay the same over time, and so things + * using ForiegnApiRepo can keep backwards compatibility + * + * All core media handlers share a common version number, and extensions can + * use the GetMetadataVersion hook to append to the array (they should append a unique + * string so not to get confusing). If there was a media handler named 'foo' with metadata + * 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 + */ + static function getMetadataVersion () { + $version = Array( '2' ); // core metadata version + wfRunHooks('GetMetadataVersion', Array(&$version)); + return implode( ';', $version); + } + + /** + * Convert metadata version. + * + * 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. + */ + function convertMetadataVersion( $metadata, $version = 1 ) { + if ( !is_array( $metadata ) ) { + + //unserialize to keep return parameter consistent. + wfSuppressWarnings(); + $ret = unserialize( $metadata ); + wfRestoreWarnings(); + return $ret; + } + return $metadata; + } + + /** + * Get a string describing the type of metadata, for display purposes. + * + * @return string + */ + function getMetadataType( $image ) { return false; } + + /** + * Check if the metadata string is valid for this handler. + * If it returns MediaHandler::METADATA_BAD (or false), Image + * will reload the metadata from the file and update the database. + * MediaHandler::METADATA_GOOD for if the metadata is a-ok, + * MediaHanlder::METADATA_COMPATIBLE if metadata is old but backwards + * compatible (which may or may not trigger a metadata reload). + * @return bool + */ + function isMetadataValid( $image, $metadata ) { + return self::METADATA_GOOD; + } + + + /** + * Get a MediaTransformOutput object representing an alternate of the transformed + * output which will call an intermediary thumbnail assist script. + * + * Used when the repository has a thumbnailScriptUrl option configured. + * + * Return false to fall back to the regular getTransform(). + * @return bool + */ + function getScriptedTransform( $image, $script, $params ) { + return false; + } + + /** + * Get a MediaTransformOutput object representing the transformed output. Does not + * actually do the transform. + * + * @param $image File: the image object + * @param $dstPath String: filesystem destination path + * @param $dstUrl String: Destination URL to use in output HTML + * @param $params Array: Arbitrary set of parameters validated by $this->validateParam() + * @return MediaTransformOutput + */ + final function getTransform( $image, $dstPath, $dstUrl, $params ) { + return $this->doTransform( $image, $dstPath, $dstUrl, $params, self::TRANSFORM_LATER ); + } + + /** + * Get a MediaTransformOutput object representing the transformed output. Does the + * transform unless $flags contains self::TRANSFORM_LATER. + * + * @param $image File: the image object + * @param $dstPath String: filesystem destination path + * @param $dstUrl String: destination URL to use in output HTML + * @param $params Array: arbitrary set of parameters validated by $this->validateParam() + * @param $flags Integer: a bitfield, may contain self::TRANSFORM_LATER + * + * @return MediaTransformOutput + */ + abstract function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ); + + /** + * Get the thumbnail extension and MIME type for a given source MIME type + * @return array thumbnail extension and MIME type + */ + function getThumbType( $ext, $mime, $params = null ) { + $magic = MimeMagic::singleton(); + if ( !$ext || $magic->isMatchingExtension( $ext, $mime ) === false ) { + // The extension is not valid for this mime type and we do + // recognize the mime type + $extensions = $magic->getExtensionsForType( $mime ); + if ( $extensions ) { + return array( strtok( $extensions, ' ' ), $mime ); + } + } + + // The extension is correct (true) or the mime type is unknown to + // MediaWiki (null) + return array( $ext, $mime ); + } + + /** + * True if the handled types can be transformed + * @return bool + */ + function canRender( $file ) { return true; } + /** + * True if handled types cannot be displayed directly in a browser + * but can be rendered + * @return bool + */ + function mustRender( $file ) { return false; } + /** + * True if the type has multi-page capabilities + * @return bool + */ + function isMultiPage( $file ) { return false; } + /** + * Page count for a multi-page document, false if unsupported or unknown + * @return bool + */ + function pageCount( $file ) { return false; } + /** + * The material is vectorized and thus scaling is lossless + * @return bool + */ + function isVectorized( $file ) { return false; } + /** + * The material is an image, and is animated. + * In particular, video material need not return true. + * @note Before 1.20, this was a method of ImageHandler only + * @return bool + */ + function isAnimatedImage( $file ) { return false; } + /** + * If the material is animated, we can animate the thumbnail + * @since 1.20 + * @return bool If material is not animated, handler may return any value. + */ + function canAnimateThumbnail( $file ) { return true; } + /** + * False if the handler is disabled for all files + * @return bool + */ + function isEnabled() { return true; } + + /** + * Get an associative array of page dimensions + * Currently "width" and "height" are understood, but this might be + * expanded in the future. + * Returns false if unknown or if the document is not multi-page. + * + * @param $image File + * @param $page Unused, left for backcompatibility? + * @return array + */ + function getPageDimensions( $image, $page ) { + $gis = $this->getImageSize( $image, $image->getLocalRefPath() ); + return array( + 'width' => $gis[0], + 'height' => $gis[1] + ); + } + + /** + * Generic getter for text layer. + * Currently overloaded by PDF and DjVu handlers + * @return bool + */ + function getPageText( $image, $page ) { + return false; + } + + /** + * Get an array structure that looks like this: + * + * array( + * 'visible' => array( + * 'Human-readable name' => 'Human readable value', + * ... + * ), + * 'collapsed' => array( + * 'Human-readable name' => 'Human readable value', + * ... + * ) + * ) + * The UI will format this into a table where the visible fields are always + * visible, and the collapsed fields are optionally visible. + * + * The function should return false if there is no metadata to display. + */ + + /** + * @todo FIXME: I don't really like this interface, it's not very flexible + * I think the media handler should generate HTML instead. It can do + * all the formatting according to some standard. That makes it possible + * to do things like visual indication of grouped and chained streams + * in ogg container files. + * @return bool + */ + function formatMetadata( $image ) { + return false; + } + + /** sorts the visible/invisible field. + * Split off from ImageHandler::formatMetadata, as used by more than + * one type of handler. + * + * This is used by the media handlers that use the FormatMetadata class + * + * @param $metadataArray Array metadata array + * @return array for use displaying metadata. + */ + function formatMetadataHelper( $metadataArray ) { + $result = array( + 'visible' => array(), + 'collapsed' => array() + ); + + $formatted = FormatMetadata::getFormattedData( $metadataArray ); + // Sort fields into visible and collapsed + $visibleFields = $this->visibleMetadataFields(); + foreach ( $formatted as $name => $value ) { + $tag = strtolower( $name ); + self::addMeta( $result, + in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed', + 'exif', + $tag, + $value + ); + } + return $result; + } + + /** + * Get a list of metadata items which should be displayed when + * the metadata table is collapsed. + * + * @return array of strings + * @access protected + */ + function visibleMetadataFields() { + $fields = array(); + $lines = explode( "\n", wfMessage( 'metadata-fields' )->inContentLanguage()->text() ); + foreach( $lines as $line ) { + $matches = array(); + if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) { + $fields[] = $matches[1]; + } + } + $fields = array_map( 'strtolower', $fields ); + return $fields; + } + + + /** + * This is used to generate an array element for each metadata value + * That array is then used to generate the table of metadata values + * on the image page + * + * @param &$array Array An array containing elements for each type of visibility + * and each of those elements being an array of metadata items. This function adds + * a value to that array. + * @param $visibility string ('visible' or 'collapsed') if this value is hidden + * by default. + * @param $type String type of metadata tag (currently always 'exif') + * @param $id String the name of the metadata tag (like 'artist' for example). + * its name in the table displayed is the message "$type-$id" (Ex exif-artist ). + * @param $value String thingy goes into a wikitext table; it used to be escaped but + * that was incompatible with previous practise of customized display + * with wikitext formatting via messages such as 'exif-model-value'. + * So the escaping is taken back out, but generally this seems a confusing + * interface. + * @param $param String value to pass to the message for the name of the field + * as $1. Currently this parameter doesn't seem to ever be used. + * + * Note, everything here is passed through the parser later on (!) + */ + protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) { + $msg = wfMessage( "$type-$id", $param ); + if ( $msg->exists() ) { + $name = $msg->text(); + } else { + // This is for future compatibility when using instant commons. + // So as to not display as ugly a name if a new metadata + // property is defined that we don't know about + // (not a major issue since such a property would be collapsed + // by default). + wfDebug( __METHOD__ . ' Unknown metadata name: ' . $id . "\n" ); + $name = wfEscapeWikiText( $id ); + } + $array[$visibility][] = array( + 'id' => "$type-$id", + 'name' => $name, + 'value' => $value + ); + } + + /** + * @param $file File + * @return string + */ + function getShortDesc( $file ) { + global $wgLang; + return htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ); + } + + /** + * @param $file File + * @return string + */ + function getLongDesc( $file ) { + global $wgLang; + return wfMessage( 'file-info', htmlspecialchars( $wgLang->formatSize( $file->getSize() ) ), + $file->getMimeType() )->parse(); + } + + /** + * @param $file File + * @return string + */ + static function getGeneralShortDesc( $file ) { + global $wgLang; + return $wgLang->formatSize( $file->getSize() ); + } + + /** + * @param $file File + * @return string + */ + static function getGeneralLongDesc( $file ) { + global $wgLang; + return wfMessage( 'file-info', $wgLang->formatSize( $file->getSize() ), + $file->getMimeType() )->parse(); + } + + /** + * Calculate the largest thumbnail width for a given original file size + * such that the thumbnail's height is at most $maxHeight. + * @param $boxWidth Integer Width of the thumbnail box. + * @param $boxHeight Integer Height of the thumbnail box. + * @param $maxHeight Integer Maximum height expected for the thumbnail. + * @return Integer. + */ + public static function fitBoxWidth( $boxWidth, $boxHeight, $maxHeight ) { + $idealWidth = $boxWidth * $maxHeight / $boxHeight; + $roundedUp = ceil( $idealWidth ); + if( round( $roundedUp * $boxHeight / $boxWidth ) > $maxHeight ) { + return floor( $idealWidth ); + } else { + return $roundedUp; + } + } + + function getDimensionsString( $file ) { + return ''; + } + + /** + * Modify the parser object post-transform + */ + function parserTransformHook( $parser, $file ) {} + + /** + * File validation hook called on upload. + * + * If the file at the given local path is not valid, or its MIME type does not + * match the handler class, a Status object should be returned containing + * relevant errors. + * + * @param $fileName string The local path to the file. + * @return Status object + */ + function verifyUpload( $fileName ) { + return Status::newGood(); + } + + /** + * Check for zero-sized thumbnails. These can be generated when + * no disk space is available or some other error occurs + * + * @param $dstPath string The location of the suspect file + * @param $retval int Return value of some shell process, file will be deleted if this is non-zero + * @return bool True if removed, false otherwise + */ + function removeBadFile( $dstPath, $retval = 0 ) { + if( file_exists( $dstPath ) ) { + $thumbstat = stat( $dstPath ); + if( $thumbstat['size'] == 0 || $retval != 0 ) { + $result = unlink( $dstPath ); + + if ( $result ) { + wfDebugLog( 'thumbnail', + sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() succeeded', + $thumbstat['size'], $dstPath ) ); + } else { + wfDebugLog( 'thumbnail', + sprintf( 'Removing bad %d-byte thumbnail "%s". unlink() failed', + $thumbstat['size'], $dstPath ) ); + } + return true; + } + } + return false; + } + + /** + * Remove files from the purge list + * + * @param array $files + * @param array $options + */ + public function filterThumbnailPurgeList( &$files, $options ) { + // Do nothing + } +} diff --git a/includes/media/MediaTransformOutput.php b/includes/media/MediaTransformOutput.php index fcfb2f45..773824cb 100644 --- a/includes/media/MediaTransformOutput.php +++ b/includes/media/MediaTransformOutput.php @@ -2,6 +2,21 @@ /** * Base class for the output of file transformation methods. * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * * @file * @ingroup Media */ @@ -21,28 +36,37 @@ abstract class MediaTransformOutput { protected $storagePath = false; /** - * Get the width of the output box + * @return integer Width of the output box */ public function getWidth() { return $this->width; } /** - * Get the height of the output box + * @return integer Height of the output box */ public function getHeight() { return $this->height; } /** - * @return string The thumbnail URL + * Get the final extension of the thumbnail. + * Returns false for scripted transformations. + * @return string|false + */ + public function getExtension() { + return $this->path ? FileBackend::extensionFromPath( $this->path ) : false; + } + + /** + * @return string|false The thumbnail URL */ public function getUrl() { return $this->url; } /** - * @return string|false The permanent thumbnail storage path + * @return string|bool The permanent thumbnail storage path */ public function getStoragePath() { return $this->storagePath; @@ -69,7 +93,7 @@ abstract class MediaTransformOutput { * custom-url-link Custom URL to link to * custom-title-link Custom Title object to link to * valign vertical-align property, if the output is an inline element - * img-class Class applied to the tag, if there is such a tag + * img-class Class applied to the "" tag, if there is such a tag * * For images, desc-link and file-link are implemented as a click-through. For * sounds and videos, they may be displayed in other ways. @@ -80,6 +104,7 @@ abstract class MediaTransformOutput { /** * This will be overridden to return true in error classes + * @return bool */ public function isError() { return false; @@ -90,7 +115,7 @@ abstract class MediaTransformOutput { * This will return false if there was an error, the * thumbnail is to be handled client-side only, or if * transformation was deferred via TRANSFORM_LATER. - * + * * @return Bool */ public function hasFile() { @@ -113,7 +138,7 @@ abstract class MediaTransformOutput { * Get the path of a file system copy of the thumbnail. * Callers should never write to this path. * - * @return string|false Returns false if there isn't one + * @return string|bool Returns false if there isn't one */ public function getLocalCopyPath() { if ( $this->isError() ) { @@ -132,7 +157,14 @@ abstract class MediaTransformOutput { * @return Bool success */ public function streamFile( $headers = array() ) { - return $this->path && StreamFile::stream( $this->getLocalCopyPath(), $headers ); + if ( !$this->path ) { + return false; + } elseif ( FileBackend::isStoragePath( $this->path ) ) { + $be = $this->file->getRepo()->getBackend(); + return $be->streamFile( array( 'src' => $this->path, 'headers' => $headers ) )->isOK(); + } else { // FS-file + return StreamFile::stream( $this->getLocalCopyPath(), $headers ); + } } /** @@ -182,25 +214,46 @@ class ThumbnailImage extends MediaTransformOutput { * Get a thumbnail object from a file and parameters. * If $path is set to null, the output file is treated as a source copy. * If $path is set to false, no output file will be created. - * + * $parameters should include, as a minimum, (file) 'width' and 'height'. + * It may also include a 'page' parameter for multipage files. + * * @param $file File object * @param $url String: URL path to the thumb - * @param $width Integer: file's width - * @param $height Integer: file's height - * @param $path String|false|null: filesystem path to the thumb - * @param $page Integer: page number, for multipage files + * @param $path String|bool|null: filesystem path to the thumb + * @param $parameters Array: Associative array of parameters * @private */ - function __construct( $file, $url, $width, $height, $path = false, $page = false ) { + function __construct( $file, $url, $path = false, $parameters = array() ) { + # Previous parameters: + # $file, $url, $width, $height, $path = false, $page = false + + if( is_array( $parameters ) ){ + $defaults = array( + 'page' => false + ); + $actualParams = $parameters + $defaults; + } else { + # Using old format, should convert. Later a warning could be added here. + $numArgs = func_num_args(); + $actualParams = array( + 'width' => $path, + 'height' => $parameters, + 'page' => ( $numArgs > 5 ) ? func_get_arg( 5 ) : false + ); + $path = ( $numArgs > 4 ) ? func_get_arg( 4 ) : false; + } + $this->file = $file; $this->url = $url; + $this->path = $path; + # These should be integers when they get here. # If not, there's a bug somewhere. But let's at # least produce valid HTML code regardless. - $this->width = round( $width ); - $this->height = round( $height ); - $this->path = $path; - $this->page = $page; + $this->width = round( $actualParams['width'] ); + $this->height = round( $actualParams['height'] ); + + $this->page = $actualParams['page']; } /** @@ -221,6 +274,9 @@ class ThumbnailImage extends MediaTransformOutput { * custom-url-link Custom URL to link to * custom-title-link Custom Title object to link to * custom target-link Value of the target attribute, for custom-target-link + * parser-extlink-* Attributes added by parser for external links: + * parser-extlink-rel: add rel="nofollow" + * parser-extlink-target: link target, but overridden by custom-target-link * * For images, desc-link and file-link are implemented as a click-through. For * sounds and videos, they may be displayed in other ways. @@ -243,6 +299,11 @@ class ThumbnailImage extends MediaTransformOutput { } if ( !empty( $options['custom-target-link'] ) ) { $linkAttribs['target'] = $options['custom-target-link']; + } elseif ( !empty( $options['parser-extlink-target'] ) ) { + $linkAttribs['target'] = $options['parser-extlink-target']; + } + if ( !empty( $options['parser-extlink-rel'] ) ) { + $linkAttribs['rel'] = $options['parser-extlink-rel']; } } elseif ( !empty( $options['custom-title-link'] ) ) { $title = $options['custom-title-link']; @@ -326,6 +387,6 @@ class TransformParameterError extends MediaTransformError { parent::__construct( 'thumbnail_error', max( isset( $params['width'] ) ? $params['width'] : 0, 120 ), max( isset( $params['height'] ) ? $params['height'] : 0, 120 ), - wfMsg( 'thumbnail_invalid_params' ) ); + wfMessage( 'thumbnail_invalid_params' )->text() ); } } diff --git a/includes/media/PNG.php b/includes/media/PNG.php index 8fe9ecb4..1b329e57 100644 --- a/includes/media/PNG.php +++ b/includes/media/PNG.php @@ -2,6 +2,21 @@ /** * Handler for PNG images. * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * * @file * @ingroup Media */ @@ -65,6 +80,14 @@ class PNGHandler extends BitmapHandler { } return false; } + /** + * We do not support making APNG thumbnails, so always false + * @param $image File + * @return bool false + */ + function canAnimateThumbnail( $image ) { + return false; + } function getMetadataType( $image ) { return 'parsed-png'; @@ -113,13 +136,13 @@ class PNGHandler extends BitmapHandler { $info[] = $original; if ( $metadata['loopCount'] == 0 ) { - $info[] = wfMsgExt( 'file-info-png-looped', 'parseinline' ); + $info[] = wfMessage( 'file-info-png-looped' )->parse(); } elseif ( $metadata['loopCount'] > 1 ) { - $info[] = wfMsgExt( 'file-info-png-repeat', 'parseinline', $metadata['loopCount'] ); + $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse(); } if ( $metadata['frameCount'] > 0 ) { - $info[] = wfMsgExt( 'file-info-png-frames', 'parseinline', $metadata['frameCount'] ); + $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse(); } if ( $metadata['duration'] ) { diff --git a/includes/media/PNGMetadataExtractor.php b/includes/media/PNGMetadataExtractor.php index d3c44d4f..9dcde406 100644 --- a/includes/media/PNGMetadataExtractor.php +++ b/includes/media/PNGMetadataExtractor.php @@ -1,10 +1,26 @@ getLocalRefPath(); if ( $flags & self::TRANSFORM_LATER ) { - return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath ); + return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); } if ( !wfMkdirParents( dirname( $dstPath ), null, __METHOD__ ) ) { return new MediaTransformError( 'thumbnail_error', $clientWidth, $clientHeight, - wfMsg( 'thumbnail_dest_directory' ) ); + wfMessage( 'thumbnail_dest_directory' )->text() ); } + $srcPath = $image->getLocalRefPath(); $status = $this->rasterize( $srcPath, $dstPath, $physicalWidth, $physicalHeight ); if( $status === true ) { - return new ThumbnailImage( $image, $dstUrl, $clientWidth, $clientHeight, $dstPath ); + return new ThumbnailImage( $image, $dstUrl, $dstPath, $params ); } else { return $status; // MediaTransformError } @@ -119,7 +141,7 @@ class SvgHandler extends ImageHandler { * @param string $dstPath * @param string $width * @param string $height - * @return true|MediaTransformError + * @return bool|MediaTransformError */ public function rasterize( $srcPath, $dstPath, $width, $height ) { global $wgSVGConverters, $wgSVGConverter, $wgSVGConverterPath; @@ -199,15 +221,30 @@ class SvgHandler extends ImageHandler { } /** + * Subtitle for the image. Different from the base + * class so it can be denoted that SVG's have + * a "nominal" resolution, and not a fixed one, + * as well as so animation can be denoted. + * * @param $file File * @return string */ function getLongDesc( $file ) { global $wgLang; - return wfMsgExt( 'svg-long-desc', 'parseinline', - $wgLang->formatNum( $file->getWidth() ), - $wgLang->formatNum( $file->getHeight() ), - $wgLang->formatSize( $file->getSize() ) ); + $size = $wgLang->formatSize( $file->getSize() ); + + if ( $this->isAnimatedImage( $file ) ) { + $msg = wfMessage( 'svg-long-desc-animated' ); + } else { + $msg = wfMessage( 'svg-long-desc' ); + } + + $msg->numParams( + $file->getWidth(), + $file->getHeight() + ); + $msg->Params( $size ); + return $msg->parse(); } function getMetadata( $file, $filename ) { @@ -238,11 +275,19 @@ class SvgHandler extends ImageHandler { } function isMetadataValid( $image, $metadata ) { - return $this->unpackMetadata( $metadata ) !== false; + $meta = $this->unpackMetadata( $metadata ); + if ( $meta === false ) { + return self::METADATA_BAD; + } + if ( !isset( $meta['originalWidth'] ) ) { + // Old but compatible + return self::METADATA_COMPATIBLE; + } + return self::METADATA_GOOD; } function visibleMetadataFields() { - $fields = array( 'title', 'description', 'animated' ); + $fields = array( 'objectname', 'imagedescription' ); return $fields; } @@ -263,8 +308,6 @@ class SvgHandler extends ImageHandler { if ( !$metadata ) { return false; } - unset( $metadata['version'] ); - unset( $metadata['metadata'] ); /* non-formatted XML */ /* TODO: add a formatter $format = new FormatSVG( $metadata ); @@ -275,9 +318,10 @@ class SvgHandler extends ImageHandler { $visibleFields = $this->visibleMetadataFields(); // Rename fields to be compatible with exif, so that - // the labels for these fields work. - $conversion = array( 'width' => 'imagewidth', - 'height' => 'imagelength', + // the labels for these fields work and reuse existing messages. + $conversion = array( + 'originalwidth' => 'imagewidth', + 'originalheight' => 'imagelength', 'description' => 'imagedescription', 'title' => 'objectname', ); @@ -285,6 +329,9 @@ class SvgHandler extends ImageHandler { $tag = strtolower( $name ); if ( isset( $conversion[$tag] ) ) { $tag = $conversion[$tag]; + } else { + // Do not output other metadata not in list + continue; } self::addMeta( $result, in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed', diff --git a/includes/media/SVGMetadataExtractor.php b/includes/media/SVGMetadataExtractor.php index db9f05fd..851fe428 100644 --- a/includes/media/SVGMetadataExtractor.php +++ b/includes/media/SVGMetadataExtractor.php @@ -1,6 +1,6 @@ + * @author "Derk-Jan Hartman " * @author Brion Vibber * @copyright Copyright © 2010-2010 Brion Vibber, Derk-Jan Hartman * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License */ +/** + * @ingroup Media + */ class SVGMetadataExtractor { static function getMetadata( $filename ) { $svg = new SVGReader( $filename ); @@ -32,6 +35,9 @@ class SVGMetadataExtractor { } } +/** + * @ingroup Media + */ class SVGReader { const DEFAULT_WIDTH = 512; const DEFAULT_HEIGHT = 512; @@ -77,6 +83,12 @@ class SVGReader { $this->metadata['width'] = self::DEFAULT_WIDTH; $this->metadata['height'] = self::DEFAULT_HEIGHT; + // The size in the units specified by the SVG file + // (for the metadata box) + // Per the SVG spec, if unspecified, default to '100%' + $this->metadata['originalWidth'] = '100%'; + $this->metadata['originalHeight'] = '100%'; + // Because we cut off the end of the svg making an invalid one. Complicated // try catch thing to make sure warnings get restored. Seems like there should // be a better way. @@ -84,6 +96,8 @@ class SVGReader { try { $this->read(); } catch( Exception $e ) { + // Note, if this happens, the width/height will be taken to be 0x0. + // Should we consider it the default 512x512 instead? wfRestoreWarnings(); throw $e; } @@ -99,6 +113,7 @@ class SVGReader { /** * Read the SVG + * @return bool */ public function read() { $keepReading = $this->reader->read(); @@ -132,6 +147,11 @@ class SVGReader { $this->readField( $tag, 'description' ); } elseif ( $isSVG && $tag == 'metadata' && $type == XmlReader::ELEMENT ) { $this->readXml( $tag, 'metadata' ); + } elseif ( $isSVG && $tag == 'script' ) { + // We normally do not allow scripted svgs. + // However its possible to configure MW to let them + // in, and such files should be considered animated. + $this->metadata['animated'] = true; } elseif ( $tag !== '#text' ) { $this->debug( "Unhandled top-level XML tag $tag" ); @@ -212,6 +232,11 @@ class SVGReader { break; } elseif ( $this->reader->namespaceURI == self::NS_SVG && $this->reader->nodeType == XmlReader::ELEMENT ) { switch( $this->reader->localName ) { + case 'script': + // Normally we disallow files with + //