Revert r101492, broken, see CR. Also revert followup r101496.
[mediawiki.git] / includes / media / BitmapMetadataHandler.php
blobd1caa67a389e01c603a6fe5d78756363a5425817
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 $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
37 $iptc = IPTC::parse( $app13 );
38 $this->addMetadata( $iptc, $this->iptcType );
42 /**
43 * Get exif info using exif class.
44 * Basically what used to be in BitmapHandler::getMetadata().
45 * Just calls stuff in the Exif class.
47 * @param $filename string
49 function getExif ( $filename, $byteOrder ) {
50 global $wgShowEXIF;
51 if ( file_exists( $filename ) && $wgShowEXIF ) {
52 $exif = new Exif( $filename, $byteOrder );
53 $data = $exif->getFilteredData();
54 if ( $data ) {
55 $this->addMetadata( $data, 'exif' );
59 /** Add misc metadata. Warning: atm if the metadata category
60 * doesn't have a priority, it will be silently discarded.
62 * @param Array $metaArray array of metadata values
63 * @param string $type type. defaults to other. if two things have the same type they're merged
65 function addMetadata ( $metaArray, $type = 'other' ) {
66 if ( isset( $this->metadata[$type] ) ) {
67 /* merge with old data */
68 $metaArray = $metaArray + $this->metadata[$type];
71 $this->metadata[$type] = $metaArray;
74 /**
75 * Merge together the various types of metadata
76 * the different types have different priorites,
77 * and are merged in order.
79 * This function is generally called by the media handlers' getMetadata()
81 * @return Array metadata array
83 function getMetadataArray () {
84 // this seems a bit ugly... This is all so its merged in right order
85 // based on the MWG recomendation.
86 $temp = Array();
87 krsort( $this->metaPriority );
88 foreach ( $this->metaPriority as $pri ) {
89 foreach ( $pri as $type ) {
90 if ( isset( $this->metadata[$type] ) ) {
91 // Do some special casing for multilingual values.
92 // Don't discard translations if also as a simple value.
93 foreach ( $this->metadata[$type] as $itemName => $item ) {
94 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
95 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
96 $default = $temp[$itemName];
97 $temp[$itemName] = $item;
98 $temp[$itemName]['x-default'] = $default;
99 unset( $this->metadata[$type][$itemName] );
104 $temp = $temp + $this->metadata[$type];
108 return $temp;
111 /** Main entry point for jpeg's.
113 * @param $filename string filename (with full path)
114 * @return metadata result array.
115 * @throws MWException on invalid file.
117 static function Jpeg ( $filename ) {
118 $showXMP = function_exists( 'xml_parser_create_ns' );
119 $meta = new self();
121 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
122 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
123 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
125 if ( isset( $seg['PSIR'] ) ) {
126 $meta->doApp13( $seg['PSIR'] );
128 if ( isset( $seg['XMP'] ) && $showXMP ) {
129 $xmp = new XMPReader();
130 $xmp->parse( $seg['XMP'] );
131 foreach ( $seg['XMP_ext'] as $xmpExt ) {
132 /* Support for extended xmp in jpeg files
133 * is not well tested and a bit fragile.
135 $xmp->parseExtended( $xmpExt );
138 $res = $xmp->getResults();
139 foreach ( $res as $type => $array ) {
140 $meta->addMetadata( $array, $type );
143 if ( isset( $seg['byteOrder'] ) ) {
144 $meta->getExif( $filename, $seg['byteOrder'] );
146 return $meta->getMetadataArray();
149 /** Entry point for png
150 * At some point in the future this might
151 * merge the png various tEXt chunks to that
152 * are interesting, but for now it only does XMP
154 * @param $filename String full path to file
155 * @return Array Array for storage in img_metadata.
157 static public function PNG ( $filename ) {
158 $showXMP = function_exists( 'xml_parser_create_ns' );
160 $meta = new self();
161 $array = PNGMetadataExtractor::getMetadata( $filename );
162 if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
163 $xmp = new XMPReader();
164 $xmp->parse( $array['text']['xmp']['x-default'] );
165 $xmpRes = $xmp->getResults();
166 foreach ( $xmpRes as $type => $xmpSection ) {
167 $meta->addMetadata( $xmpSection, $type );
170 unset( $array['text']['xmp'] );
171 $meta->addMetadata( $array['text'], 'native' );
172 unset( $array['text'] );
173 $array['metadata'] = $meta->getMetadataArray();
174 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
175 return $array;
178 /** function for gif images.
180 * They don't really have native metadata, so just merges together
181 * XMP and image comment.
183 * @param $filename full path to file
184 * @return Array metadata array
186 static public function GIF ( $filename ) {
188 $meta = new self();
189 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
191 if ( count( $baseArray['comment'] ) > 0 ) {
192 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
195 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
196 $xmp = new XMPReader();
197 $xmp->parse( $baseArray['xmp'] );
198 $xmpRes = $xmp->getResults();
199 foreach ( $xmpRes as $type => $xmpSection ) {
200 $meta->addMetadata( $xmpSection, $type );
205 unset( $baseArray['comment'] );
206 unset( $baseArray['xmp'] );
208 $baseArray['metadata'] = $meta->getMetadataArray();
209 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
210 return $baseArray;
214 * This doesn't do much yet, but eventually I plan to add
215 * XMP support for Tiff. (PHP's exif support already extracts
216 * but needs some further processing because PHP's exif support
217 * is stupid...)
219 * @todo Add XMP support, so this function actually makes
220 * sense to put here.
222 * The various exceptions this throws are caught later.
223 * @param $filename String
224 * @return Array The metadata.
226 static public function Tiff ( $filename ) {
227 if ( file_exists( $filename ) ) {
228 $byteOrder = self::getTiffByteOrder( $filename );
229 if ( !$byteOrder ) {
230 throw new MWException( "Error determining byte order of $filename" );
232 $exif = new Exif( $filename, $byteOrder );
233 $data = $exif->getFilteredData();
234 if ( $data ) {
235 $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
236 return $data;
237 } else {
238 throw new MWException( "Could not extract data from tiff file $filename" );
240 } else {
241 throw new MWException( "File doesn't exist - $filename" );
245 * Read the first 2 bytes of a tiff file to figure out
246 * Little Endian or Big Endian. Needed for exif stuff.
248 * @param $filename String The filename
249 * @return String 'BE' or 'LE' or false
251 static function getTiffByteOrder( $filename ) {
252 $fh = fopen( $filename, 'rb' );
253 if ( !$fh ) return false;
254 $head = fread( $fh, 2 );
255 fclose( $fh );
257 switch( $head ) {
258 case 'II':
259 return 'LE'; // II for intel.
260 case 'MM':
261 return 'BE'; // MM for motorla.
262 default:
263 return false; // Something went wrong.