Fixed undefined defines warnings introduced in change 5131
[mediawiki.git] / includes / media / BitmapMetadataHandler.php
blobd158fe49878b52e56faa92dd4a86c4e4017d6b82
1 <?php
2 /**
3 Class to deal with reconciling and extracting metadata from bitmap images.
4 This is meant to comply with http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
6 This sort of acts as an intermediary between MediaHandler::getMetadata
7 and the various metadata extractors.
9 @todo other image formats.
11 class BitmapMetadataHandler {
13 private $metadata = array();
14 private $metaPriority = array(
15 20 => array( 'other' ),
16 40 => array( 'native' ),
17 60 => array( 'iptc-good-hash', 'iptc-no-hash' ),
18 70 => array( 'xmp-deprecated' ),
19 80 => array( 'xmp-general' ),
20 90 => array( 'xmp-exif' ),
21 100 => array( 'iptc-bad-hash' ),
22 120 => array( 'exif' ),
24 private $iptcType = 'iptc-no-hash';
26 /**
27 * This does the photoshop image resource app13 block
28 * of interest, IPTC-IIM metadata is stored here.
30 * Mostly just calls doPSIR and doIPTC
32 * @param String $app13 String containing app13 block from jpeg file
34 private function doApp13 ( $app13 ) {
35 try {
36 $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
37 } catch ( MWException $e ) {
38 // Error reading the iptc hash information.
39 // This probably means the App13 segment is something other than what we expect.
40 // However, still try to read it, and treat it as if the hash didn't exist.
41 wfDebug( "Error parsing iptc data of file: " . $e->getMessage() . "\n" );
42 $this->iptcType = 'iptc-no-hash';
45 $iptc = IPTC::parse( $app13 );
46 $this->addMetadata( $iptc, $this->iptcType );
50 /**
51 * Get exif info using exif class.
52 * Basically what used to be in BitmapHandler::getMetadata().
53 * Just calls stuff in the Exif class.
55 * Parameters are passed to the Exif class.
57 * @param $filename string
58 * @param $byteOrder string
60 function getExif ( $filename, $byteOrder ) {
61 global $wgShowEXIF;
62 if ( file_exists( $filename ) && $wgShowEXIF ) {
63 $exif = new Exif( $filename, $byteOrder );
64 $data = $exif->getFilteredData();
65 if ( $data ) {
66 $this->addMetadata( $data, 'exif' );
70 /** Add misc metadata. Warning: atm if the metadata category
71 * doesn't have a priority, it will be silently discarded.
73 * @param Array $metaArray array of metadata values
74 * @param string $type type. defaults to other. if two things have the same type they're merged
76 function addMetadata ( $metaArray, $type = 'other' ) {
77 if ( isset( $this->metadata[$type] ) ) {
78 /* merge with old data */
79 $metaArray = $metaArray + $this->metadata[$type];
82 $this->metadata[$type] = $metaArray;
85 /**
86 * Merge together the various types of metadata
87 * the different types have different priorites,
88 * and are merged in order.
90 * This function is generally called by the media handlers' getMetadata()
92 * @return Array metadata array
94 function getMetadataArray () {
95 // this seems a bit ugly... This is all so its merged in right order
96 // based on the MWG recomendation.
97 $temp = Array();
98 krsort( $this->metaPriority );
99 foreach ( $this->metaPriority as $pri ) {
100 foreach ( $pri as $type ) {
101 if ( isset( $this->metadata[$type] ) ) {
102 // Do some special casing for multilingual values.
103 // Don't discard translations if also as a simple value.
104 foreach ( $this->metadata[$type] as $itemName => $item ) {
105 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
106 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
107 $default = $temp[$itemName];
108 $temp[$itemName] = $item;
109 $temp[$itemName]['x-default'] = $default;
110 unset( $this->metadata[$type][$itemName] );
115 $temp = $temp + $this->metadata[$type];
119 return $temp;
122 /** Main entry point for jpeg's.
124 * @param $filename string filename (with full path)
125 * @return array metadata result array.
126 * @throws MWException on invalid file.
128 static function Jpeg ( $filename ) {
129 $showXMP = function_exists( 'xml_parser_create_ns' );
130 $meta = new self();
132 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
133 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
134 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
136 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
137 foreach( $seg['PSIR'] as $curPSIRValue ) {
138 $meta->doApp13( $curPSIRValue );
141 if ( isset( $seg['XMP'] ) && $showXMP ) {
142 $xmp = new XMPReader();
143 $xmp->parse( $seg['XMP'] );
144 foreach ( $seg['XMP_ext'] as $xmpExt ) {
145 /* Support for extended xmp in jpeg files
146 * is not well tested and a bit fragile.
148 $xmp->parseExtended( $xmpExt );
151 $res = $xmp->getResults();
152 foreach ( $res as $type => $array ) {
153 $meta->addMetadata( $array, $type );
156 if ( isset( $seg['byteOrder'] ) ) {
157 $meta->getExif( $filename, $seg['byteOrder'] );
159 return $meta->getMetadataArray();
162 /** Entry point for png
163 * At some point in the future this might
164 * merge the png various tEXt chunks to that
165 * are interesting, but for now it only does XMP
167 * @param $filename String full path to file
168 * @return Array Array for storage in img_metadata.
170 static public function PNG ( $filename ) {
171 $showXMP = function_exists( 'xml_parser_create_ns' );
173 $meta = new self();
174 $array = PNGMetadataExtractor::getMetadata( $filename );
175 if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
176 $xmp = new XMPReader();
177 $xmp->parse( $array['text']['xmp']['x-default'] );
178 $xmpRes = $xmp->getResults();
179 foreach ( $xmpRes as $type => $xmpSection ) {
180 $meta->addMetadata( $xmpSection, $type );
183 unset( $array['text']['xmp'] );
184 $meta->addMetadata( $array['text'], 'native' );
185 unset( $array['text'] );
186 $array['metadata'] = $meta->getMetadataArray();
187 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
188 return $array;
191 /** function for gif images.
193 * They don't really have native metadata, so just merges together
194 * XMP and image comment.
196 * @param $filename string full path to file
197 * @return Array metadata array
199 static public function GIF ( $filename ) {
201 $meta = new self();
202 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
204 if ( count( $baseArray['comment'] ) > 0 ) {
205 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
208 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
209 $xmp = new XMPReader();
210 $xmp->parse( $baseArray['xmp'] );
211 $xmpRes = $xmp->getResults();
212 foreach ( $xmpRes as $type => $xmpSection ) {
213 $meta->addMetadata( $xmpSection, $type );
218 unset( $baseArray['comment'] );
219 unset( $baseArray['xmp'] );
221 $baseArray['metadata'] = $meta->getMetadataArray();
222 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
223 return $baseArray;
227 * This doesn't do much yet, but eventually I plan to add
228 * XMP support for Tiff. (PHP's exif support already extracts
229 * but needs some further processing because PHP's exif support
230 * is stupid...)
232 * @todo Add XMP support, so this function actually makes
233 * sense to put here.
235 * The various exceptions this throws are caught later.
236 * @param $filename String
237 * @return Array The metadata.
239 static public function Tiff ( $filename ) {
240 if ( file_exists( $filename ) ) {
241 $byteOrder = self::getTiffByteOrder( $filename );
242 if ( !$byteOrder ) {
243 throw new MWException( "Error determining byte order of $filename" );
245 $exif = new Exif( $filename, $byteOrder );
246 $data = $exif->getFilteredData();
247 if ( $data ) {
248 $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
249 return $data;
250 } else {
251 throw new MWException( "Could not extract data from tiff file $filename" );
253 } else {
254 throw new MWException( "File doesn't exist - $filename" );
258 * Read the first 2 bytes of a tiff file to figure out
259 * Little Endian or Big Endian. Needed for exif stuff.
261 * @param $filename String The filename
262 * @return String 'BE' or 'LE' or false
264 static function getTiffByteOrder( $filename ) {
265 $fh = fopen( $filename, 'rb' );
266 if ( !$fh ) return false;
267 $head = fread( $fh, 2 );
268 fclose( $fh );
270 switch( $head ) {
271 case 'II':
272 return 'LE'; // II for intel.
273 case 'MM':
274 return 'BE'; // MM for motorla.
275 default:
276 return false; // Something went wrong.