From 5c4b57817b7f4870fb773b3eb19a7aef70a36389 Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Sat, 28 Jul 2007 01:15:35 +0000 Subject: [PATCH] Committing some work in progress -- abstraction of handler-specific metadata formatting. The return format of MediaHandler::formatMetadata() might still need some tweaking, but I believe there is feature parity at this point. --- includes/ImagePage.php | 49 +++++++++++-------------------------- includes/filerepo/File.php | 19 +++------------ includes/media/Bitmap.php | 50 ++++++++++++++++++++++++++++++++++++++ includes/media/Generic.php | 31 +++++++++++++++++++++++ 4 files changed, 99 insertions(+), 50 deletions(-) diff --git a/includes/ImagePage.php b/includes/ImagePage.php index d95e19f9de..d93436eed4 100644 --- a/includes/ImagePage.php +++ b/includes/ImagePage.php @@ -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( "\n" . "\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. * diff --git a/includes/filerepo/File.php b/includes/filerepo/File.php index 3b586da064..017acc0ad7 100644 --- a/includes/filerepo/File.php +++ b/includes/filerepo/File.php @@ -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() ); } /** diff --git a/includes/media/Bitmap.php b/includes/media/Bitmap.php index 8aa5fe7332..375028ab0d 100644 --- a/includes/media/Bitmap.php +++ b/includes/media/Bitmap.php @@ -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; + } } diff --git a/includes/media/Generic.php b/includes/media/Generic.php index fcf4751874..95e856817e 100644 --- a/includes/media/Generic.php +++ b/includes/media/Generic.php @@ -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 ) + ); + } + } /** -- 2.20.1