follow-up r86169 - 2 minor issues found while writing unit tests
[lhc/web/wiklou.git] / includes / media / BitmapMetadataHandler.php
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
5
6 This sort of acts as an intermediary between MediaHandler::getMetadata
7 and the various metadata extractors.
8
9 @todo other image formats.
10 */
11 class BitmapMetadataHandler {
12
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' ),
23 );
24 private $iptcType = 'iptc-no-hash';
25
26 /**
27 * This does the photoshop image resource app13 block
28 * of interest, IPTC-IIM metadata is stored here.
29 *
30 * Mostly just calls doPSIR and doIPTC
31 *
32 * @param String $app13 String containing app13 block from jpeg file
33 */
34 private function doApp13 ( $app13 ) {
35 $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
36
37 $iptc = IPTC::parse( $app13 );
38 $this->addMetadata( $iptc, $this->iptcType );
39 }
40
41
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.
46 *
47 * @param $filename string
48 */
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' );
56 }
57 }
58 }
59 /** Add misc metadata. Warning: atm if the metadata category
60 * doesn't have a priority, it will be silently discarded.
61 *
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
64 */
65 function addMetadata ( $metaArray, $type = 'other' ) {
66 if ( isset( $this->metadata[$type] ) ) {
67 /* merge with old data */
68 $metaArray = $metaArray + $this->metadata[$type];
69 }
70
71 $this->metadata[$type] = $metaArray;
72 }
73
74 /**
75 * Merge together the various types of metadata
76 * the different types have different priorites,
77 * and are merged in order.
78 *
79 * This function is generally called by the media handlers' getMetadata()
80 *
81 * @return Array metadata array
82 */
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] );
100 }
101 }
102 }
103
104 $temp = $temp + $this->metadata[$type];
105 }
106 }
107 }
108 return $temp;
109 }
110
111 /** Main entry point for jpeg's.
112 *
113 * @param $filename string filename (with full path)
114 * @return metadata result array.
115 * @throws MWException on invalid file.
116 */
117 static function Jpeg ( $filename ) {
118 $showXMP = function_exists( 'xml_parser_create_ns' );
119 $meta = new self();
120
121 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
122 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
123 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
124 }
125 if ( isset( $seg['PSIR'] ) ) {
126 $meta->doApp13( $seg['PSIR'] );
127 }
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.
134 */
135 $xmp->parseExtended( $xmpExt );
136
137 }
138 $res = $xmp->getResults();
139 foreach ( $res as $type => $array ) {
140 $meta->addMetadata( $array, $type );
141 }
142 }
143 if ( isset( $seg['byteOrder'] ) ) {
144 $meta->getExif( $filename, $seg['byteOrder'] );
145 }
146 return $meta->getMetadataArray();
147 }
148
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
153 *
154 * @param $filename String full path to file
155 * @return Array Array for storage in img_metadata.
156 */
157 static public function PNG ( $filename ) {
158 $showXMP = function_exists( 'xml_parser_create_ns' );
159
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 );
168 }
169 }
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;
176 }
177
178 /** function for gif images.
179 *
180 * They don't really have native metadata, so just merges together
181 * XMP and image comment.
182 *
183 * @param $filename full path to file
184 * @return Array metadata array
185 */
186 static public function GIF ( $filename ) {
187
188 $meta = new self();
189 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
190
191 if ( count( $baseArray['comment'] ) > 0 ) {
192 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
193 }
194
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 );
201 }
202
203 }
204
205 unset( $baseArray['comment'] );
206 unset( $baseArray['xmp'] );
207
208 $baseArray['metadata'] = $meta->getMetadataArray();
209 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
210 return $baseArray;
211 }
212
213 /**
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...)
218 *
219 * @todo Add XMP support, so this function actually makes
220 * sense to put here.
221 *
222 * The various exceptions this throws are caught later.
223 * @param $filename String
224 * @return Array The metadata.
225 */
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" );
231 }
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" );
239 }
240 } else {
241 throw new MWException( "File doesn't exist - $filename" );
242 }
243 }
244 /**
245 * Read the first 2 bytes of a tiff file to figure out
246 * Little Endian or Big Endian. Needed for exif stuff.
247 *
248 * @param $filename String The filename
249 * @return String 'BE' or 'LE' or false
250 */
251 static function getTiffByteOrder( $filename ) {
252 $fh = fopen( $filename, 'rb' );
253 if ( !$fh ) return false;
254 $head = fread( $fh, 2 );
255 fclose( $fh );
256
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.
264
265 }
266 }
267
268
269 }