Committing some work in progress -- abstraction of handler-specific metadata formatti...
authorTim Starling <tstarling@users.mediawiki.org>
Sat, 28 Jul 2007 01:15:35 +0000 (01:15 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Sat, 28 Jul 2007 01:15:35 +0000 (01:15 +0000)
includes/ImagePage.php
includes/filerepo/File.php
includes/media/Bitmap.php
includes/media/Generic.php

index d95e19f..d93436e 100644 (file)
@@ -46,10 +46,9 @@ class ImagePage extends Article {
                        return Article::view();
 
                if ($wgShowEXIF && $this->img->exists()) {
-                       $exif = $this->img->getExifData();
-                       $showmeta = count($exif) ? true : false;
+                       $formattedMetadata = $this->img->formatMetadata();
+                       $showmeta = $formattedMetadata !== false;
                } else {
-                       $exif = false;
                        $showmeta = false;
                }
 
@@ -82,12 +81,12 @@ class ImagePage extends Article {
                $this->imageHistory();
                $this->imageLinks();
 
-               if ( $exif ) {
+               if ( $showmeta ) {
                        global $wgStylePath, $wgStyleVersion;
                        $expand = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-expand' ) ) );
                        $collapse = htmlspecialchars( wfEscapeJsString( wfMsg( 'metadata-collapse' ) ) );
                        $wgOut->addHTML( Xml::element( 'h2', array( 'id' => 'metadata' ), wfMsg( 'metadata' ) ). "\n" );
-                       $wgOut->addWikiText( $this->makeMetadataTable( $exif ) );
+                       $wgOut->addWikiText( $this->makeMetadataTable( $formattedMetadata ) );
                        $wgOut->addHTML(
                                "<script type=\"text/javascript\" src=\"$wgStylePath/common/metadata.js?$wgStyleVersion\"></script>\n" .
                                "<script type=\"text/javascript\">attachMetadataToggle('mw_metadata', '$expand', '$collapse');</script>\n" );
@@ -121,44 +120,24 @@ class ImagePage extends Article {
         * @param array $exif The array containing the EXIF data
         * @return string
         */
-       function makeMetadataTable( $exif ) {
+       function makeMetadataTable( $metadata ) {
                $r = wfMsg( 'metadata-help' ) . "\n\n";
                $r .= "{| id=mw_metadata class=mw_metadata\n";
-               $visibleFields = $this->visibleMetadataFields();
-               foreach( $exif as $k => $v ) {
-                       $tag = strtolower( $k );
-                       $msg = wfMsg( "exif-$tag" );
-                       $class = "exif-$tag";
-                       if( !in_array( $tag, $visibleFields ) ) {
-                               $class .= ' collapsable';
+               foreach ( $metadata as $type => $stuff ) {
+                       foreach ( $stuff as $k => $v ) {
+                               $class = Sanitizer::escapeId( $v['id'] );
+                               if( $type == 'collapsed' ) {
+                                       $class .= ' collapsable';
+                               }
+                               $r .= "|- class=\"$class\"\n";
+                               $r .= "!| {$v['name']}\n";
+                               $r .= "|| {$v['value']}\n";
                        }
-                       $r .= "|- class=\"$class\"\n";
-                       $r .= "!| $msg\n";
-                       $r .= "|| $v\n";
                }
                $r .= '|}';
                return $r;
        }
 
-       /**
-        * 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];
-                       }
-               }
-               return $fields;
-       }
-
        /**
         * Overloading Article's getContent method.
         * 
index 3b586da..017acc0 100644 (file)
@@ -814,22 +814,11 @@ abstract class File {
                return $retVal;
        }
 
-       function getExifData() {
-               if ( !$this->getHandler() || $this->handler->getMetadataType( $this ) != 'exif' ) {
-                       return array();
-               }
-               $metadata = $this->getMetadata();
-               if ( !$metadata ) {
-                       return array();
-               }
-               $exif = unserialize( $metadata );
-               if ( !$exif ) {
-                       return array();
+       function formatMetadata() {
+               if ( !$this->getHandler() ) {
+                       return false;
                }
-               unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
-               $format = new FormatExif( $exif );
-
-               return $format->getFormattedData();
+               return $this->getHandler()->formatMetadata( $this, $this->getMetadata() );
        }
 
        /**
index 8aa5fe7..375028a 100644 (file)
@@ -234,6 +234,56 @@ class BitmapHandler extends ImageHandler {
                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;
+       }
+
+       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( $k );
+                       self::addMeta( $result,
+                               in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
+                               'exif',
+                               "exif-$tag",
+                               $value
+                       );
+               }
+               return $result;
+       }
 }
 
 
index fcf4751..95e8568 100644 (file)
@@ -158,6 +158,37 @@ abstract class MediaHandler {
                        'height' => $gis[1]
                );
        }
+
+       /**
+        * 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.
+        */
+       function formatMetadata( $image, $metadata ) {
+               return false;
+       }
+       
+       protected static function addMeta( &$array, $visibility, $type, $id, $value, $param = false ) {
+               $array[$visibility][] = array(
+                       'id' => "$type-$id",
+                       'name' => wfMsg( "$type-$id", $param ),
+                       'value' => wfEscapeWikiText( $value )
+               );
+       }
+
 }
 
 /**