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