From: Brian Wolff Date: Wed, 20 Apr 2011 23:15:13 +0000 (+0000) Subject: Remove the JPEG/TIFF specific metadata code from BitmapHandler and put it in JpegOrTi... X-Git-Tag: 1.31.0-rc.0~30666 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=19a66a3238367af15afb04778449df0ece71850f;p=lhc%2Fweb%2Fwiklou.git Remove the JPEG/TIFF specific metadata code from BitmapHandler and put it in JpegOrTiff handler to stop mostly irrelevent classes from getting it. Also remove a method that is an exact duplicate of a base class (not sure whats with that). This also coincidently fixes the issue with when a foreign file repo uses PagedTiffHandler and the local one does not, and the builtin Tiff handler tries to treat the metadata as if it was its own form. --- diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index edae895666..c34005e993 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -517,6 +517,7 @@ $wgAutoloadLocalClasses = array( 'IPTC' => 'includes/media/IPTC.php', 'JpegHandler' => 'includes/media/Jpeg.php', 'JpegMetadataExtractor' => 'includes/media/JpegMetadataExtractor.php', + 'JpegOrTiffHandler' => 'includes/media/JpegOrTiff.php', 'MediaHandler' => 'includes/media/Generic.php', 'MediaTransformError' => 'includes/media/MediaTransformOutput.php', 'MediaTransformOutput' => 'includes/media/MediaTransformOutput.php', diff --git a/includes/media/Bitmap.php b/includes/media/Bitmap.php index f230d417c1..25ad91df81 100644 --- a/includes/media/Bitmap.php +++ b/includes/media/Bitmap.php @@ -670,116 +670,6 @@ class BitmapHandler extends ImageHandler { imagejpeg( $dst_image, $thumbPath, 95 ); } - /** - * Its unclear if anything still uses this - * as jpeg is now in its own subclass. - * - * And really each media handler should use a - * different getMetadata, as the formats aren't - * all that similar and usually have different - * metadata needs. - * - * @deprecated - */ - function getMetadata( $image, $filename ) { - wfDeprecated( __METHOD__ ); - global $wgShowEXIF; - if ( $wgShowEXIF && file_exists( $filename ) ) { - $exif = new Exif( $filename ); - $data = $exif->getFilteredData(); - if ( $data ) { - $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); - return serialize( $data ); - } else { - return '0'; - } - } else { - return ''; - } - } - - function getMetadataType( $image ) { - return 'exif'; - } - - function isMetadataValid( $image, $metadata ) { - global $wgShowEXIF; - if ( !$wgShowEXIF ) { - # Metadata disabled and so an empty field is expected - return true; - } - if ( $metadata === '0' ) { - # Special value indicating that there is no EXIF data in the file - return true; - } - wfSuppressWarnings(); - $exif = unserialize( $metadata ); - wfRestoreWarnings(); - if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) || - $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() ) - { - # Wrong version - wfDebug( __METHOD__ . ": wrong version\n" ); - return false; - } - return true; - } - - /** - * Get a list of EXIF metadata items which should be displayed when - * the metadata table is collapsed. - * - * @return array of strings - * @access private - */ - 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; - } - - /** - * @param $image File - * @return array|bool - */ - function formatMetadata( $image ) { - $result = array( - 'visible' => array(), - 'collapsed' => array() - ); - $metadata = $image->getMetadata(); - if ( !$metadata ) { - return false; - } - $exif = unserialize( $metadata ); - if ( !$exif ) { - return false; - } - unset( $exif['MEDIAWIKI_EXIF_VERSION'] ); - $format = new FormatExif( $exif ); - - $formatted = $format->getFormattedData(); - // 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; - } - /** * Try to read out the orientation of the file and return the angle that * the file needs to be rotated to be viewed diff --git a/includes/media/Generic.php b/includes/media/Generic.php index c3bd102c7a..6112140dec 100644 --- a/includes/media/Generic.php +++ b/includes/media/Generic.php @@ -325,7 +325,7 @@ abstract class MediaHandler { * the metadata table is collapsed. * * @return array of strings - * @access private + * @access protected */ function visibleMetadataFields() { $fields = array(); diff --git a/includes/media/Jpeg.php b/includes/media/Jpeg.php index 4d0d82652d..d8f713db11 100644 --- a/includes/media/Jpeg.php +++ b/includes/media/Jpeg.php @@ -4,14 +4,15 @@ * @ingroup Media */ -/** JPEG specific handler. - * Inherits most stuff from BitmapHandler, just here to do the metadata handler differently +/** + * JPEG specific handler. + * Inherits most stuff from BitmapHandler, just here to do the metadata handler differently. + * + * Metadata stuff common to Jpeg and built-in Tiff (not PagedTiffHandler) is in JpegOrTiffHandler. + * * @ingroup Media */ -class JpegHandler extends BitmapHandler { - - const BROKEN_FILE = '-1'; // error extracting metadata - const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata. +class JpegHandler extends JpegOrTiffHandler { function getMetadata ( $image, $filename ) { try { @@ -27,7 +28,7 @@ class JpegHandler extends BitmapHandler { // BitmapMetadataHandler throws an exception in certain exceptional cases like if file does not exist. wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" ); - /* This used to use 0 (self::OLD_BROKEN_FILE) for the cases + /* This used to use 0 (JpegOrTiffHandler::OLD_BROKEN_FILE) for the cases * * No metadata in the file * * Something is broken in the file. * However, if the metadata support gets expanded then you can't tell if the 0 is from @@ -36,103 +37,9 @@ class JpegHandler extends BitmapHandler { * Thus switch to using -1 to denote only a broken file, and use an array with only * MEDIAWIKI_EXIF_VERSION to denote no props. */ - return self::BROKEN_FILE; - } - } - - function convertMetadataVersion( $metadata, $version = 1 ) { - // basically flattens arrays. - $version = explode(';', $version, 2); - $version = intval($version[0]); - if ( $version < 1 || $version >= 2 ) { - return $metadata; - } - - $avoidHtml = true; - - if ( !is_array( $metadata ) ) { - $metadata = unserialize( $metadata ); - } - if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) { - return $metadata; - } - - // Treat Software as a special case because in can contain - // an array of (SoftwareName, Version). - if (isset( $metadata['Software'] ) - && is_array( $metadata['Software'] ) - && is_array( $metadata['Software'][0]) - && isset( $metadata['Software'][0][0] ) - && isset( $metadata['Software'][0][1]) - ) { - $metadata['Software'] = $metadata['Software'][0][0] . ' (Version ' - . $metadata['Software'][0][1] . ')'; - } - - // ContactInfo also has to be dealt with specially - if ( isset( $metadata['Contact'] ) ) { - $metadata['Contact'] = - FormatMetadata::collapseContactInfo( - $metadata['Contact'] ); - } - - foreach ( $metadata as &$val ) { - if ( is_array( $val ) ) { - $val = FormatMetadata::flattenArray( $val, 'ul', $avoidHtml ); - } - } - $metadata['MEDIAWIKI_EXIF_VERSION'] = 1; - return $metadata; - } - - function isMetadataValid( $image, $metadata ) { - global $wgShowEXIF; - if ( !$wgShowEXIF ) { - # Metadata disabled and so an empty field is expected - return self::METADATA_GOOD; - } - if ( $metadata === self::OLD_BROKEN_FILE ) { - # Old special value indicating that there is no EXIF data in the file. - # or that there was an error well extracting the metadata. - wfDebug( __METHOD__ . ": back-compat version\n"); - return self::METADATA_COMPATIBLE; - } - if ( $metadata === self::BROKEN_FILE ) { - return self::METADATA_GOOD; + return JpegOrTiffHandler::BROKEN_FILE; } - wfSuppressWarnings(); - $exif = unserialize( $metadata ); - wfRestoreWarnings(); - if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) || - $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() ) - { - if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) && - $exif['MEDIAWIKI_EXIF_VERSION'] == 1 ) - { - //back-compatible but old - wfDebug( __METHOD__.": back-compat version\n" ); - return self::METADATA_COMPATIBLE; - } - # Wrong (non-compatible) version - wfDebug( __METHOD__.": wrong version\n" ); - return self::METADATA_BAD; - } - return self::METADATA_GOOD; } - function formatMetadata( $image ) { - $metadata = $image->getMetadata(); - if ( !$metadata || $metadata == self::BROKEN_FILE ) { - return false; - } - $exif = unserialize( $metadata ); - if ( !$exif ) { - return false; - } - unset( $exif['MEDIAWIKI_EXIF_VERSION'] ); - if ( count( $exif ) == 0 ) { - return false; - } - return $this->formatMetadataHelper( $exif ); - } } + diff --git a/includes/media/JpegOrTiff.php b/includes/media/JpegOrTiff.php new file mode 100644 index 0000000000..767cb19ea0 --- /dev/null +++ b/includes/media/JpegOrTiff.php @@ -0,0 +1,123 @@ += 2 ) { + return $metadata; + } + + $avoidHtml = true; + + if ( !is_array( $metadata ) ) { + $metadata = unserialize( $metadata ); + } + if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) { + return $metadata; + } + + // Treat Software as a special case because in can contain + // an array of (SoftwareName, Version). + if (isset( $metadata['Software'] ) + && is_array( $metadata['Software'] ) + && is_array( $metadata['Software'][0]) + && isset( $metadata['Software'][0][0] ) + && isset( $metadata['Software'][0][1]) + ) { + $metadata['Software'] = $metadata['Software'][0][0] . ' (Version ' + . $metadata['Software'][0][1] . ')'; + } + + // ContactInfo also has to be dealt with specially + if ( isset( $metadata['Contact'] ) ) { + $metadata['Contact'] = + FormatMetadata::collapseContactInfo( + $metadata['Contact'] ); + } + + foreach ( $metadata as &$val ) { + if ( is_array( $val ) ) { + $val = FormatMetadata::flattenArray( $val, 'ul', $avoidHtml ); + } + } + $metadata['MEDIAWIKI_EXIF_VERSION'] = 1; + return $metadata; + } + + function isMetadataValid( $image, $metadata ) { + global $wgShowEXIF; + if ( !$wgShowEXIF ) { + # Metadata disabled and so an empty field is expected + return self::METADATA_GOOD; + } + if ( $metadata === self::OLD_BROKEN_FILE ) { + # Old special value indicating that there is no EXIF data in the file. + # or that there was an error well extracting the metadata. + wfDebug( __METHOD__ . ": back-compat version\n"); + return self::METADATA_COMPATIBLE; + } + if ( $metadata === self::BROKEN_FILE ) { + return self::METADATA_GOOD; + } + wfSuppressWarnings(); + $exif = unserialize( $metadata ); + wfRestoreWarnings(); + if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) || + $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version() ) + { + if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] ) && + $exif['MEDIAWIKI_EXIF_VERSION'] == 1 ) + { + //back-compatible but old + wfDebug( __METHOD__.": back-compat version\n" ); + return self::METADATA_COMPATIBLE; + } + # Wrong (non-compatible) version + wfDebug( __METHOD__.": wrong version\n" ); + return self::METADATA_BAD; + } + return self::METADATA_GOOD; + } + + function formatMetadata( $image ) { + $metadata = $image->getMetadata(); + if ( !$metadata || + $this->isMetadataValid( $image, $metadata ) === self::METADATA_BAD ) + { + // So we don't try and display metadata from PagedTiffHandler + // for example when using InstantCommons. + return false; + } + + $exif = unserialize( $metadata ); + if ( !$exif ) { + return false; + } + unset( $exif['MEDIAWIKI_EXIF_VERSION'] ); + if ( count( $exif ) == 0 ) { + return false; + } + return $this->formatMetadataHelper( $exif ); + } + + function getMetadataType( $image ) { + return 'exif'; + } +} + diff --git a/includes/media/Tiff.php b/includes/media/Tiff.php index 8773201f76..ef0621b75a 100644 --- a/includes/media/Tiff.php +++ b/includes/media/Tiff.php @@ -11,7 +11,7 @@ * * @ingroup Media */ -class TiffHandler extends BitmapHandler { +class TiffHandler extends JpegOrTiffHandler { /** * Conversion to PNG for inline display can be disabled here... @@ -34,4 +34,20 @@ class TiffHandler extends BitmapHandler { global $wgTiffThumbnailType; return $wgTiffThumbnailType; } + + function getMetadata( $image, $filename ) { + global $wgShowEXIF; + if ( $wgShowEXIF && file_exists( $filename ) ) { + $exif = new Exif( $filename ); + $data = $exif->getFilteredData(); + if ( $data ) { + $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); + return serialize( $data ); + } else { + return JpegOrTiffHandler::BROKEN_FILE; + } + } else { + return ''; + } + } }