Update formatting for media related classes
[lhc/web/wiklou.git] / includes / media / BitmapMetadataHandler.php
1 <?php
2 /**
3 * Extraction of metadata from different bitmap image types.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Class to deal with reconciling and extracting metadata from bitmap images.
26 * This is meant to comply with http://www.metadataworkinggroup.org/pdf/mwg_guidance.pdf
27 *
28 * This sort of acts as an intermediary between MediaHandler::getMetadata
29 * and the various metadata extractors.
30 *
31 * @todo other image formats.
32 * @ingroup Media
33 */
34 class BitmapMetadataHandler {
35 private $metadata = array();
36
37 private $metaPriority = array(
38 20 => array( 'other' ),
39 40 => array( 'native' ),
40 60 => array( 'iptc-good-hash', 'iptc-no-hash' ),
41 70 => array( 'xmp-deprecated' ),
42 80 => array( 'xmp-general' ),
43 90 => array( 'xmp-exif' ),
44 100 => array( 'iptc-bad-hash' ),
45 120 => array( 'exif' ),
46 );
47
48 private $iptcType = 'iptc-no-hash';
49
50 /**
51 * This does the photoshop image resource app13 block
52 * of interest, IPTC-IIM metadata is stored here.
53 *
54 * Mostly just calls doPSIR and doIPTC
55 *
56 * @param string $app13 String containing app13 block from jpeg file
57 */
58 private function doApp13( $app13 ) {
59 try {
60 $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
61 } catch ( MWException $e ) {
62 // Error reading the iptc hash information.
63 // This probably means the App13 segment is something other than what we expect.
64 // However, still try to read it, and treat it as if the hash didn't exist.
65 wfDebug( "Error parsing iptc data of file: " . $e->getMessage() . "\n" );
66 $this->iptcType = 'iptc-no-hash';
67 }
68
69 $iptc = IPTC::parse( $app13 );
70 $this->addMetadata( $iptc, $this->iptcType );
71 }
72
73 /**
74 * Get exif info using exif class.
75 * Basically what used to be in BitmapHandler::getMetadata().
76 * Just calls stuff in the Exif class.
77 *
78 * Parameters are passed to the Exif class.
79 *
80 * @param $filename string
81 * @param $byteOrder string
82 */
83 function getExif( $filename, $byteOrder ) {
84 global $wgShowEXIF;
85 if ( file_exists( $filename ) && $wgShowEXIF ) {
86 $exif = new Exif( $filename, $byteOrder );
87 $data = $exif->getFilteredData();
88 if ( $data ) {
89 $this->addMetadata( $data, 'exif' );
90 }
91 }
92 }
93
94 /** Add misc metadata. Warning: atm if the metadata category
95 * doesn't have a priority, it will be silently discarded.
96 *
97 * @param array $metaArray array of metadata values
98 * @param string $type type. defaults to other. if two things have the same type they're merged
99 */
100 function addMetadata( $metaArray, $type = 'other' ) {
101 if ( isset( $this->metadata[$type] ) ) {
102 /* merge with old data */
103 $metaArray = $metaArray + $this->metadata[$type];
104 }
105
106 $this->metadata[$type] = $metaArray;
107 }
108
109 /**
110 * Merge together the various types of metadata
111 * the different types have different priorites,
112 * and are merged in order.
113 *
114 * This function is generally called by the media handlers' getMetadata()
115 *
116 * @return Array metadata array
117 */
118 function getMetadataArray() {
119 // this seems a bit ugly... This is all so its merged in right order
120 // based on the MWG recomendation.
121 $temp = array();
122 krsort( $this->metaPriority );
123 foreach ( $this->metaPriority as $pri ) {
124 foreach ( $pri as $type ) {
125 if ( isset( $this->metadata[$type] ) ) {
126 // Do some special casing for multilingual values.
127 // Don't discard translations if also as a simple value.
128 foreach ( $this->metadata[$type] as $itemName => $item ) {
129 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
130 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
131 $default = $temp[$itemName];
132 $temp[$itemName] = $item;
133 $temp[$itemName]['x-default'] = $default;
134 unset( $this->metadata[$type][$itemName] );
135 }
136 }
137 }
138
139 $temp = $temp + $this->metadata[$type];
140 }
141 }
142 }
143
144 return $temp;
145 }
146
147 /** Main entry point for jpeg's.
148 *
149 * @param string $filename filename (with full path)
150 * @return array metadata result array.
151 * @throws MWException on invalid file.
152 */
153 static function Jpeg( $filename ) {
154 $showXMP = function_exists( 'xml_parser_create_ns' );
155 $meta = new self();
156
157 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
158 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
159 $meta->addMetadata( array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
160 }
161 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
162 foreach ( $seg['PSIR'] as $curPSIRValue ) {
163 $meta->doApp13( $curPSIRValue );
164 }
165 }
166 if ( isset( $seg['XMP'] ) && $showXMP ) {
167 $xmp = new XMPReader();
168 $xmp->parse( $seg['XMP'] );
169 foreach ( $seg['XMP_ext'] as $xmpExt ) {
170 /* Support for extended xmp in jpeg files
171 * is not well tested and a bit fragile.
172 */
173 $xmp->parseExtended( $xmpExt );
174 }
175 $res = $xmp->getResults();
176 foreach ( $res as $type => $array ) {
177 $meta->addMetadata( $array, $type );
178 }
179 }
180 if ( isset( $seg['byteOrder'] ) ) {
181 $meta->getExif( $filename, $seg['byteOrder'] );
182 }
183
184 return $meta->getMetadataArray();
185 }
186
187 /** Entry point for png
188 * At some point in the future this might
189 * merge the png various tEXt chunks to that
190 * are interesting, but for now it only does XMP
191 *
192 * @param string $filename full path to file
193 * @return Array Array for storage in img_metadata.
194 */
195 public static function PNG( $filename ) {
196 $showXMP = function_exists( 'xml_parser_create_ns' );
197
198 $meta = new self();
199 $array = PNGMetadataExtractor::getMetadata( $filename );
200 if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
201 $xmp = new XMPReader();
202 $xmp->parse( $array['text']['xmp']['x-default'] );
203 $xmpRes = $xmp->getResults();
204 foreach ( $xmpRes as $type => $xmpSection ) {
205 $meta->addMetadata( $xmpSection, $type );
206 }
207 }
208 unset( $array['text']['xmp'] );
209 $meta->addMetadata( $array['text'], 'native' );
210 unset( $array['text'] );
211 $array['metadata'] = $meta->getMetadataArray();
212 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
213
214 return $array;
215 }
216
217 /** function for gif images.
218 *
219 * They don't really have native metadata, so just merges together
220 * XMP and image comment.
221 *
222 * @param string $filename full path to file
223 * @return Array metadata array
224 */
225 public static function GIF( $filename ) {
226
227 $meta = new self();
228 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
229
230 if ( count( $baseArray['comment'] ) > 0 ) {
231 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
232 }
233
234 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
235 $xmp = new XMPReader();
236 $xmp->parse( $baseArray['xmp'] );
237 $xmpRes = $xmp->getResults();
238 foreach ( $xmpRes as $type => $xmpSection ) {
239 $meta->addMetadata( $xmpSection, $type );
240 }
241 }
242
243 unset( $baseArray['comment'] );
244 unset( $baseArray['xmp'] );
245
246 $baseArray['metadata'] = $meta->getMetadataArray();
247 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
248
249 return $baseArray;
250 }
251
252 /**
253 * This doesn't do much yet, but eventually I plan to add
254 * XMP support for Tiff. (PHP's exif support already extracts
255 * but needs some further processing because PHP's exif support
256 * is stupid...)
257 *
258 * @todo Add XMP support, so this function actually makes
259 * sense to put here.
260 *
261 * The various exceptions this throws are caught later.
262 * @param $filename String
263 * @throws MWException
264 * @return Array The metadata.
265 */
266 public static function Tiff( $filename ) {
267 if ( file_exists( $filename ) ) {
268 $byteOrder = self::getTiffByteOrder( $filename );
269 if ( !$byteOrder ) {
270 throw new MWException( "Error determining byte order of $filename" );
271 }
272 $exif = new Exif( $filename, $byteOrder );
273 $data = $exif->getFilteredData();
274 if ( $data ) {
275 $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
276
277 return $data;
278 } else {
279 throw new MWException( "Could not extract data from tiff file $filename" );
280 }
281 } else {
282 throw new MWException( "File doesn't exist - $filename" );
283 }
284 }
285
286 /**
287 * Read the first 2 bytes of a tiff file to figure out
288 * Little Endian or Big Endian. Needed for exif stuff.
289 *
290 * @param string $filename The filename
291 * @return String 'BE' or 'LE' or false
292 */
293 static function getTiffByteOrder( $filename ) {
294 $fh = fopen( $filename, 'rb' );
295 if ( !$fh ) {
296 return false;
297 }
298 $head = fread( $fh, 2 );
299 fclose( $fh );
300
301 switch ( $head ) {
302 case 'II':
303 return 'LE'; // II for intel.
304 case 'MM':
305 return 'BE'; // MM for motorla.
306 default:
307 return false; // Something went wrong.
308
309 }
310 }
311 }