Merge "Replace deprecated call."
[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
36 private $metadata = array();
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 private $iptcType = 'iptc-no-hash';
48
49 /**
50 * This does the photoshop image resource app13 block
51 * of interest, IPTC-IIM metadata is stored here.
52 *
53 * Mostly just calls doPSIR and doIPTC
54 *
55 * @param String $app13 String containing app13 block from jpeg file
56 */
57 private function doApp13 ( $app13 ) {
58 try {
59 $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
60 } catch ( MWException $e ) {
61 // Error reading the iptc hash information.
62 // This probably means the App13 segment is something other than what we expect.
63 // However, still try to read it, and treat it as if the hash didn't exist.
64 wfDebug( "Error parsing iptc data of file: " . $e->getMessage() . "\n" );
65 $this->iptcType = 'iptc-no-hash';
66 }
67
68 $iptc = IPTC::parse( $app13 );
69 $this->addMetadata( $iptc, $this->iptcType );
70 }
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 /** Add misc metadata. Warning: atm if the metadata category
94 * doesn't have a priority, it will be silently discarded.
95 *
96 * @param Array $metaArray array of metadata values
97 * @param string $type type. defaults to other. if two things have the same type they're merged
98 */
99 function addMetadata ( $metaArray, $type = 'other' ) {
100 if ( isset( $this->metadata[$type] ) ) {
101 /* merge with old data */
102 $metaArray = $metaArray + $this->metadata[$type];
103 }
104
105 $this->metadata[$type] = $metaArray;
106 }
107
108 /**
109 * Merge together the various types of metadata
110 * the different types have different priorites,
111 * and are merged in order.
112 *
113 * This function is generally called by the media handlers' getMetadata()
114 *
115 * @return Array metadata array
116 */
117 function getMetadataArray () {
118 // this seems a bit ugly... This is all so its merged in right order
119 // based on the MWG recomendation.
120 $temp = Array();
121 krsort( $this->metaPriority );
122 foreach ( $this->metaPriority as $pri ) {
123 foreach ( $pri as $type ) {
124 if ( isset( $this->metadata[$type] ) ) {
125 // Do some special casing for multilingual values.
126 // Don't discard translations if also as a simple value.
127 foreach ( $this->metadata[$type] as $itemName => $item ) {
128 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
129 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
130 $default = $temp[$itemName];
131 $temp[$itemName] = $item;
132 $temp[$itemName]['x-default'] = $default;
133 unset( $this->metadata[$type][$itemName] );
134 }
135 }
136 }
137
138 $temp = $temp + $this->metadata[$type];
139 }
140 }
141 }
142 return $temp;
143 }
144
145 /** Main entry point for jpeg's.
146 *
147 * @param $filename string filename (with full path)
148 * @return array metadata result array.
149 * @throws MWException on invalid file.
150 */
151 static function Jpeg ( $filename ) {
152 $showXMP = function_exists( 'xml_parser_create_ns' );
153 $meta = new self();
154
155 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
156 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
157 $meta->addMetadata( Array( 'JPEGFileComment' => $seg['COM'] ), 'native' );
158 }
159 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
160 foreach( $seg['PSIR'] as $curPSIRValue ) {
161 $meta->doApp13( $curPSIRValue );
162 }
163 }
164 if ( isset( $seg['XMP'] ) && $showXMP ) {
165 $xmp = new XMPReader();
166 $xmp->parse( $seg['XMP'] );
167 foreach ( $seg['XMP_ext'] as $xmpExt ) {
168 /* Support for extended xmp in jpeg files
169 * is not well tested and a bit fragile.
170 */
171 $xmp->parseExtended( $xmpExt );
172
173 }
174 $res = $xmp->getResults();
175 foreach ( $res as $type => $array ) {
176 $meta->addMetadata( $array, $type );
177 }
178 }
179 if ( isset( $seg['byteOrder'] ) ) {
180 $meta->getExif( $filename, $seg['byteOrder'] );
181 }
182 return $meta->getMetadataArray();
183 }
184
185 /** Entry point for png
186 * At some point in the future this might
187 * merge the png various tEXt chunks to that
188 * are interesting, but for now it only does XMP
189 *
190 * @param $filename String full path to file
191 * @return Array Array for storage in img_metadata.
192 */
193 static public function PNG ( $filename ) {
194 $showXMP = function_exists( 'xml_parser_create_ns' );
195
196 $meta = new self();
197 $array = PNGMetadataExtractor::getMetadata( $filename );
198 if ( isset( $array['text']['xmp']['x-default'] ) && $array['text']['xmp']['x-default'] !== '' && $showXMP ) {
199 $xmp = new XMPReader();
200 $xmp->parse( $array['text']['xmp']['x-default'] );
201 $xmpRes = $xmp->getResults();
202 foreach ( $xmpRes as $type => $xmpSection ) {
203 $meta->addMetadata( $xmpSection, $type );
204 }
205 }
206 unset( $array['text']['xmp'] );
207 $meta->addMetadata( $array['text'], 'native' );
208 unset( $array['text'] );
209 $array['metadata'] = $meta->getMetadataArray();
210 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
211 return $array;
212 }
213
214 /** function for gif images.
215 *
216 * They don't really have native metadata, so just merges together
217 * XMP and image comment.
218 *
219 * @param $filename string full path to file
220 * @return Array metadata array
221 */
222 static public function GIF ( $filename ) {
223
224 $meta = new self();
225 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
226
227 if ( count( $baseArray['comment'] ) > 0 ) {
228 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
229 }
230
231 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
232 $xmp = new XMPReader();
233 $xmp->parse( $baseArray['xmp'] );
234 $xmpRes = $xmp->getResults();
235 foreach ( $xmpRes as $type => $xmpSection ) {
236 $meta->addMetadata( $xmpSection, $type );
237 }
238
239 }
240
241 unset( $baseArray['comment'] );
242 unset( $baseArray['xmp'] );
243
244 $baseArray['metadata'] = $meta->getMetadataArray();
245 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
246 return $baseArray;
247 }
248
249 /**
250 * This doesn't do much yet, but eventually I plan to add
251 * XMP support for Tiff. (PHP's exif support already extracts
252 * but needs some further processing because PHP's exif support
253 * is stupid...)
254 *
255 * @todo Add XMP support, so this function actually makes
256 * sense to put here.
257 *
258 * The various exceptions this throws are caught later.
259 * @param $filename String
260 * @throws MWException
261 * @return Array The metadata.
262 */
263 static public function Tiff ( $filename ) {
264 if ( file_exists( $filename ) ) {
265 $byteOrder = self::getTiffByteOrder( $filename );
266 if ( !$byteOrder ) {
267 throw new MWException( "Error determining byte order of $filename" );
268 }
269 $exif = new Exif( $filename, $byteOrder );
270 $data = $exif->getFilteredData();
271 if ( $data ) {
272 $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
273 return $data;
274 } else {
275 throw new MWException( "Could not extract data from tiff file $filename" );
276 }
277 } else {
278 throw new MWException( "File doesn't exist - $filename" );
279 }
280 }
281 /**
282 * Read the first 2 bytes of a tiff file to figure out
283 * Little Endian or Big Endian. Needed for exif stuff.
284 *
285 * @param $filename String The filename
286 * @return String 'BE' or 'LE' or false
287 */
288 static function getTiffByteOrder( $filename ) {
289 $fh = fopen( $filename, 'rb' );
290 if ( !$fh ) return false;
291 $head = fread( $fh, 2 );
292 fclose( $fh );
293
294 switch( $head ) {
295 case 'II':
296 return 'LE'; // II for intel.
297 case 'MM':
298 return 'BE'; // MM for motorla.
299 default:
300 return false; // Something went wrong.
301
302 }
303 }
304
305
306 }