MediaWiki REL1_31
BitmapMetadataHandler.php
Go to the documentation of this file.
1<?php
25
38 private $metadata = [];
39
41 private $metaPriority = [
42 20 => [ 'other' ],
43 40 => [ 'native' ],
44 60 => [ 'iptc-good-hash', 'iptc-no-hash' ],
45 70 => [ 'xmp-deprecated' ],
46 80 => [ 'xmp-general' ],
47 90 => [ 'xmp-exif' ],
48 100 => [ 'iptc-bad-hash' ],
49 120 => [ 'exif' ],
50 ];
51
53 private $iptcType = 'iptc-no-hash';
54
63 private function doApp13( $app13 ) {
64 try {
65 $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
66 } catch ( Exception $e ) {
67 // Error reading the iptc hash information.
68 // This probably means the App13 segment is something other than what we expect.
69 // However, still try to read it, and treat it as if the hash didn't exist.
70 wfDebug( "Error parsing iptc data of file: " . $e->getMessage() . "\n" );
71 $this->iptcType = 'iptc-no-hash';
72 }
73
74 $iptc = IPTC::parse( $app13 );
75 $this->addMetadata( $iptc, $this->iptcType );
76 }
77
88 function getExif( $filename, $byteOrder ) {
90 if ( file_exists( $filename ) && $wgShowEXIF ) {
91 $exif = new Exif( $filename, $byteOrder );
92 $data = $exif->getFilteredData();
93 if ( $data ) {
94 $this->addMetadata( $data, 'exif' );
95 }
96 }
97 }
98
105 function addMetadata( $metaArray, $type = 'other' ) {
106 if ( isset( $this->metadata[$type] ) ) {
107 /* merge with old data */
108 $metaArray = $metaArray + $this->metadata[$type];
109 }
110
111 $this->metadata[$type] = $metaArray;
112 }
113
123 function getMetadataArray() {
124 // this seems a bit ugly... This is all so its merged in right order
125 // based on the MWG recomendation.
126 $temp = [];
127 krsort( $this->metaPriority );
128 foreach ( $this->metaPriority as $pri ) {
129 foreach ( $pri as $type ) {
130 if ( isset( $this->metadata[$type] ) ) {
131 // Do some special casing for multilingual values.
132 // Don't discard translations if also as a simple value.
133 foreach ( $this->metadata[$type] as $itemName => $item ) {
134 if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
135 if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
136 $default = $temp[$itemName];
137 $temp[$itemName] = $item;
138 $temp[$itemName]['x-default'] = $default;
139 unset( $this->metadata[$type][$itemName] );
140 }
141 }
142 }
143
144 $temp = $temp + $this->metadata[$type];
145 }
146 }
147 }
148
149 return $temp;
150 }
151
158 static function Jpeg( $filename ) {
159 $showXMP = XMPReader::isSupported();
160 $meta = new self();
161
162 $seg = JpegMetadataExtractor::segmentSplitter( $filename );
163
164 if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
165 $meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' );
166 }
167 if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
168 foreach ( $seg['PSIR'] as $curPSIRValue ) {
169 $meta->doApp13( $curPSIRValue );
170 }
171 }
172 if ( isset( $seg['XMP'] ) && $showXMP ) {
173 $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
174 $xmp->parse( $seg['XMP'] );
175 foreach ( $seg['XMP_ext'] as $xmpExt ) {
176 /* Support for extended xmp in jpeg files
177 * is not well tested and a bit fragile.
178 */
179 $xmp->parseExtended( $xmpExt );
180 }
181 $res = $xmp->getResults();
182 foreach ( $res as $type => $array ) {
183 $meta->addMetadata( $array, $type );
184 }
185 }
186
187 $meta->getExif( $filename, isset( $seg['byteOrder'] ) ? $seg['byteOrder'] : 'BE' );
188
189 return $meta->getMetadataArray();
190 }
191
200 public static function PNG( $filename ) {
201 $showXMP = XMPReader::isSupported();
202
203 $meta = new self();
204 $array = PNGMetadataExtractor::getMetadata( $filename );
205 if ( isset( $array['text']['xmp']['x-default'] )
206 && $array['text']['xmp']['x-default'] !== '' && $showXMP
207 ) {
208 $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
209 $xmp->parse( $array['text']['xmp']['x-default'] );
210 $xmpRes = $xmp->getResults();
211 foreach ( $xmpRes as $type => $xmpSection ) {
212 $meta->addMetadata( $xmpSection, $type );
213 }
214 }
215 unset( $array['text']['xmp'] );
216 $meta->addMetadata( $array['text'], 'native' );
217 unset( $array['text'] );
218 $array['metadata'] = $meta->getMetadataArray();
219 $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
220
221 return $array;
222 }
223
232 public static function GIF( $filename ) {
233 $meta = new self();
234 $baseArray = GIFMetadataExtractor::getMetadata( $filename );
235
236 if ( count( $baseArray['comment'] ) > 0 ) {
237 $meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' );
238 }
239
240 if ( $baseArray['xmp'] !== '' && XMPReader::isSupported() ) {
241 $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
242 $xmp->parse( $baseArray['xmp'] );
243 $xmpRes = $xmp->getResults();
244 foreach ( $xmpRes as $type => $xmpSection ) {
245 $meta->addMetadata( $xmpSection, $type );
246 }
247 }
248
249 unset( $baseArray['comment'] );
250 unset( $baseArray['xmp'] );
251
252 $baseArray['metadata'] = $meta->getMetadataArray();
253 $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
254
255 return $baseArray;
256 }
257
271 public static function Tiff( $filename ) {
272 if ( file_exists( $filename ) ) {
273 $byteOrder = self::getTiffByteOrder( $filename );
274 if ( !$byteOrder ) {
275 throw new MWException( "Error determining byte order of $filename" );
276 }
277 $exif = new Exif( $filename, $byteOrder );
278 $data = $exif->getFilteredData();
279 if ( $data ) {
280 $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
281
282 return $data;
283 } else {
284 throw new MWException( "Could not extract data from tiff file $filename" );
285 }
286 } else {
287 throw new MWException( "File doesn't exist - $filename" );
288 }
289 }
290
298 static function getTiffByteOrder( $filename ) {
299 $fh = fopen( $filename, 'rb' );
300 if ( !$fh ) {
301 return false;
302 }
303 $head = fread( $fh, 2 );
304 fclose( $fh );
305
306 switch ( $head ) {
307 case 'II':
308 return 'LE'; // II for intel.
309 case 'MM':
310 return 'BE'; // MM for motorla.
311 default:
312 return false; // Something went wrong.
313
314 }
315 }
316}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgShowEXIF
Show Exif data, on by default if available.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Class to deal with reconciling and extracting metadata from bitmap images.
getExif( $filename, $byteOrder)
Get exif info using exif class.
addMetadata( $metaArray, $type='other')
Add misc metadata.
static getTiffByteOrder( $filename)
Read the first 2 bytes of a tiff file to figure out Little Endian or Big Endian.
static Tiff( $filename)
This doesn't do much yet, but eventually I plan to add XMP support for Tiff.
array $metaPriority
Metadata priority.
doApp13( $app13)
This does the photoshop image resource app13 block of interest, IPTC-IIM metadata is stored here.
static Jpeg( $filename)
Main entry point for jpeg's.
getMetadataArray()
Merge together the various types of metadata the different types have different priorites,...
static PNG( $filename)
Entry point for png At some point in the future this might merge the png various tEXt chunks to that ...
static GIF( $filename)
function for gif images.
Class to extract and validate Exif data from jpeg (and possibly tiff) files.
Definition Exif.php:32
static version()
#-
Definition Exif.php:582
static getMetadata( $filename)
static parse( $rawData)
This takes the results of iptcparse() and puts it into a form that can be handled by mediawiki.
Definition IPTC.php:40
static doPSIR( $app13)
This reads the photoshop image resource.
static segmentSplitter( $filename)
Function to extract metadata segments of interest from jpeg files based on GIFMetadataExtractor.
MediaWiki exception.
PSR-3 logger instance factory.
static getMetadata( $filename)
Class for reading xmp data containing properties relevant to images, and spitting out an array that F...
Definition XMP.php:53
static isSupported()
Check if this instance supports using this class.
Definition XMP.php:202
$res
Definition database.txt:21
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
the array() calling protocol came about after MediaWiki 1.4rc1.
returning false will NOT prevent logging $e
Definition hooks.txt:2176
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37