Merge branch 'master' into Wikidata
[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 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';
43 }
44
45 $iptc = IPTC::parse( $app13 );
46 $this->addMetadata( $iptc, $this->iptcType );
47 }
48
49
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.
54 *
55 * Parameters are passed to the Exif class.
56 *
57 * @param $filename string
58 * @param $byteOrder string
59 */
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' );
67 }
68 }
69 }
70 /** Add misc metadata. Warning: atm if the metadata category
71 * doesn't have a priority, it will be silently discarded.
72 *
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
75 */
76 function addMetadata ( $metaArray, $type = 'other' ) {
77 if ( isset( $this->metadata[$type] ) ) {
78 /* merge with old data */
79 $metaArray = $metaArray + $this->metadata[$type];
80 }
81
82 $this->metadata[$type] = $metaArray;
83 }
84
85 /**
86 * Merge together the various types of metadata
87 * the different types have different priorites,
88 * and are merged in order.
89 *
90 * This function is generally called by the media handlers' getMetadata()
91 *
92 * @return Array metadata array
93 */
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] );
111 }
112 }
113 }
114
115 $temp = $temp + $this->metadata[$type];
116 }
117 }
118 }
119 return $temp;
120 }
121
122 /** Main entry point for jpeg's.
123 *
124 * @param $filename string filename (with full path)
125 * @return array metadata result array.
126 * @throws MWException on invalid file.
127 */
128 static function Jpeg ( $filename ) {
129 $showXMP = function_exists( 'xml_parser_create_ns' );
130 $meta = new self();
131
132 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
133 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
134 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
135 }
136 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
137 foreach( $seg['PSIR'] as $curPSIRValue ) {
138 $meta->doApp13( $curPSIRValue );
139 }
140 }
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.
147 */
148 $xmp->parseExtended( $xmpExt );
149
150 }
151 $res = $xmp->getResults();
152 foreach ( $res as $type => $array ) {
153 $meta->addMetadata( $array, $type );
154 }
155 }
156 if ( isset( $seg['byteOrder'] ) ) {
157 $meta->getExif( $filename, $seg['byteOrder'] );
158 }
159 return $meta->getMetadataArray();
160 }
161
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
166 *
167 * @param $filename String full path to file
168 * @return Array Array for storage in img_metadata.
169 */
170 static public function PNG ( $filename ) {
171 $showXMP = function_exists( 'xml_parser_create_ns' );
172
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 );
181 }
182 }
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;
189 }
190
191 /** function for gif images.
192 *
193 * They don't really have native metadata, so just merges together
194 * XMP and image comment.
195 *
196 * @param $filename string full path to file
197 * @return Array metadata array
198 */
199 static public function GIF ( $filename ) {
200
201 $meta = new self();
202 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
203
204 if ( count( $baseArray['comment'] ) > 0 ) {
205 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
206 }
207
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 );
214 }
215
216 }
217
218 unset( $baseArray['comment'] );
219 unset( $baseArray['xmp'] );
220
221 $baseArray['metadata'] = $meta->getMetadataArray();
222 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
223 return $baseArray;
224 }
225
226 /**
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...)
231 *
232 * @todo Add XMP support, so this function actually makes
233 * sense to put here.
234 *
235 * The various exceptions this throws are caught later.
236 * @param $filename String
237 * @return Array The metadata.
238 */
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" );
244 }
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" );
252 }
253 } else {
254 throw new MWException( "File doesn't exist - $filename" );
255 }
256 }
257 /**
258 * Read the first 2 bytes of a tiff file to figure out
259 * Little Endian or Big Endian. Needed for exif stuff.
260 *
261 * @param $filename String The filename
262 * @return String 'BE' or 'LE' or false
263 */
264 static function getTiffByteOrder( $filename ) {
265 $fh = fopen( $filename, 'rb' );
266 if ( !$fh ) return false;
267 $head = fread( $fh, 2 );
268 fclose( $fh );
269
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.
277
278 }
279 }
280
281
282 }