Remove the JPEG/TIFF specific metadata code from BitmapHandler and put it in JpegOrTi...
authorBrian Wolff <bawolff@users.mediawiki.org>
Wed, 20 Apr 2011 23:15:13 +0000 (23:15 +0000)
committerBrian Wolff <bawolff@users.mediawiki.org>
Wed, 20 Apr 2011 23:15:13 +0000 (23:15 +0000)
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.

includes/AutoLoader.php
includes/media/Bitmap.php
includes/media/Generic.php
includes/media/Jpeg.php
includes/media/JpegOrTiff.php [new file with mode: 0644]
includes/media/Tiff.php

index edae895..c34005e 100644 (file)
@@ -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',
index f230d41..25ad91d 100644 (file)
@@ -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
index c3bd102..6112140 100644 (file)
@@ -325,7 +325,7 @@ abstract class MediaHandler {
         * the metadata table is collapsed.
         *
         * @return array of strings
-        * @access private
+        * @access protected
         */
        function visibleMetadataFields() {
                $fields = array();
index 4d0d826..d8f713d 100644 (file)
@@ -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 (file)
index 0000000..767cb19
--- /dev/null
@@ -0,0 +1,123 @@
+<?php
+/**
+ * @file
+ * @ingroup Media
+ */
+
+/**
+ * Stuff specific to JPEG and (built-in) TIFF handler.
+ * All metadata related, since both JPEG and TIFF support Exif.
+ *
+ * @ingroup Media
+ */
+class JpegOrTiffHandler extends BitmapHandler {
+
+       const BROKEN_FILE = '-1'; // error extracting metadata
+       const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
+
+       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;
+               }
+               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';
+        }
+}
+
index 8773201..ef0621b 100644 (file)
@@ -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 '';
+                }
+        }
 }