Merge "Remove 6 unused revdelete messages"
[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'] )
201 && $array['text']['xmp']['x-default'] !== '' && $showXMP
202 ) {
203 $xmp = new XMPReader();
204 $xmp->parse( $array['text']['xmp']['x-default'] );
205 $xmpRes = $xmp->getResults();
206 foreach ( $xmpRes as $type => $xmpSection ) {
207 $meta->addMetadata( $xmpSection, $type );
208 }
209 }
210 unset( $array['text']['xmp'] );
211 $meta->addMetadata( $array['text'], 'native' );
212 unset( $array['text'] );
213 $array['metadata'] = $meta->getMetadataArray();
214 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
215
216 return $array;
217 }
218
219 /** function for gif images.
220 *
221 * They don't really have native metadata, so just merges together
222 * XMP and image comment.
223 *
224 * @param string $filename full path to file
225 * @return Array metadata array
226 */
227 public static function GIF( $filename ) {
228
229 $meta = new self();
230 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
231
232 if ( count( $baseArray['comment'] ) > 0 ) {
233 $meta->addMetadata( array( 'GIFFileComment' => $baseArray['comment'] ), 'native' );
234 }
235
236 if ( $baseArray['xmp'] !== '' && function_exists( 'xml_parser_create_ns' ) ) {
237 $xmp = new XMPReader();
238 $xmp->parse( $baseArray['xmp'] );
239 $xmpRes = $xmp->getResults();
240 foreach ( $xmpRes as $type => $xmpSection ) {
241 $meta->addMetadata( $xmpSection, $type );
242 }
243 }
244
245 unset( $baseArray['comment'] );
246 unset( $baseArray['xmp'] );
247
248 $baseArray['metadata'] = $meta->getMetadataArray();
249 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
250
251 return $baseArray;
252 }
253
254 /**
255 * This doesn't do much yet, but eventually I plan to add
256 * XMP support for Tiff. (PHP's exif support already extracts
257 * but needs some further processing because PHP's exif support
258 * is stupid...)
259 *
260 * @todo Add XMP support, so this function actually makes
261 * sense to put here.
262 *
263 * The various exceptions this throws are caught later.
264 * @param $filename String
265 * @throws MWException
266 * @return Array The metadata.
267 */
268 public static function Tiff( $filename ) {
269 if ( file_exists( $filename ) ) {
270 $byteOrder = self::getTiffByteOrder( $filename );
271 if ( !$byteOrder ) {
272 throw new MWException( "Error determining byte order of $filename" );
273 }
274 $exif = new Exif( $filename, $byteOrder );
275 $data = $exif->getFilteredData();
276 if ( $data ) {
277 $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
278
279 return $data;
280 } else {
281 throw new MWException( "Could not extract data from tiff file $filename" );
282 }
283 } else {
284 throw new MWException( "File doesn't exist - $filename" );
285 }
286 }
287
288 /**
289 * Read the first 2 bytes of a tiff file to figure out
290 * Little Endian or Big Endian. Needed for exif stuff.
291 *
292 * @param string $filename The filename
293 * @return String 'BE' or 'LE' or false
294 */
295 static function getTiffByteOrder( $filename ) {
296 $fh = fopen( $filename, 'rb' );
297 if ( !$fh ) {
298 return false;
299 }
300 $head = fread( $fh, 2 );
301 fclose( $fh );
302
303 switch ( $head ) {
304 case 'II':
305 return 'LE'; // II for intel.
306 case 'MM':
307 return 'BE'; // MM for motorla.
308 default:
309 return false; // Something went wrong.
310
311 }
312 }
313 }