MediaWiki  1.32.5
BitmapMetadataHandler.php
Go to the documentation of this file.
1 <?php
25 use Wikimedia\XMPReader\Reader as XMPReader;
26 
39  private $metadata = [];
40 
42  private $metaPriority = [
43  20 => [ 'other' ],
44  40 => [ 'native' ],
45  60 => [ 'iptc-good-hash', 'iptc-no-hash' ],
46  70 => [ 'xmp-deprecated' ],
47  80 => [ 'xmp-general' ],
48  90 => [ 'xmp-exif' ],
49  100 => [ 'iptc-bad-hash' ],
50  120 => [ 'exif' ],
51  ];
52 
54  private $iptcType = 'iptc-no-hash';
55 
64  private function doApp13( $app13 ) {
65  try {
66  $this->iptcType = JpegMetadataExtractor::doPSIR( $app13 );
67  } catch ( Exception $e ) {
68  // Error reading the iptc hash information.
69  // This probably means the App13 segment is something other than what we expect.
70  // However, still try to read it, and treat it as if the hash didn't exist.
71  wfDebug( "Error parsing iptc data of file: " . $e->getMessage() . "\n" );
72  $this->iptcType = 'iptc-no-hash';
73  }
74 
75  $iptc = IPTC::parse( $app13 );
76  $this->addMetadata( $iptc, $this->iptcType );
77  }
78 
89  function getExif( $filename, $byteOrder ) {
90  global $wgShowEXIF;
91  if ( file_exists( $filename ) && $wgShowEXIF ) {
92  $exif = new Exif( $filename, $byteOrder );
93  $data = $exif->getFilteredData();
94  if ( $data ) {
95  $this->addMetadata( $data, 'exif' );
96  }
97  }
98  }
99 
106  function addMetadata( $metaArray, $type = 'other' ) {
107  if ( isset( $this->metadata[$type] ) ) {
108  /* merge with old data */
109  $metaArray = $metaArray + $this->metadata[$type];
110  }
111 
112  $this->metadata[$type] = $metaArray;
113  }
114 
124  function getMetadataArray() {
125  // this seems a bit ugly... This is all so its merged in right order
126  // based on the MWG recommendation.
127  $temp = [];
128  krsort( $this->metaPriority );
129  foreach ( $this->metaPriority as $pri ) {
130  foreach ( $pri as $type ) {
131  if ( isset( $this->metadata[$type] ) ) {
132  // Do some special casing for multilingual values.
133  // Don't discard translations if also as a simple value.
134  foreach ( $this->metadata[$type] as $itemName => $item ) {
135  if ( is_array( $item ) && isset( $item['_type'] ) && $item['_type'] === 'lang' ) {
136  if ( isset( $temp[$itemName] ) && !is_array( $temp[$itemName] ) ) {
137  $default = $temp[$itemName];
138  $temp[$itemName] = $item;
139  $temp[$itemName]['x-default'] = $default;
140  unset( $this->metadata[$type][$itemName] );
141  }
142  }
143  }
144 
145  $temp = $temp + $this->metadata[$type];
146  }
147  }
148  }
149 
150  return $temp;
151  }
152 
159  static function Jpeg( $filename ) {
160  $showXMP = XMPReader::isSupported();
161  $meta = new self();
162 
163  $seg = JpegMetadataExtractor::segmentSplitter( $filename );
164 
165  if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) {
166  $meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' );
167  }
168  if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) {
169  foreach ( $seg['PSIR'] as $curPSIRValue ) {
170  $meta->doApp13( $curPSIRValue );
171  }
172  }
173  if ( isset( $seg['XMP'] ) && $showXMP ) {
174  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
175  $xmp->parse( $seg['XMP'] );
176  foreach ( $seg['XMP_ext'] as $xmpExt ) {
177  /* Support for extended xmp in jpeg files
178  * is not well tested and a bit fragile.
179  */
180  $xmp->parseExtended( $xmpExt );
181  }
182  $res = $xmp->getResults();
183  foreach ( $res as $type => $array ) {
184  $meta->addMetadata( $array, $type );
185  }
186  }
187 
188  $meta->getExif( $filename, $seg['byteOrder'] ?? 'BE' );
189 
190  return $meta->getMetadataArray();
191  }
192 
201  public static function PNG( $filename ) {
202  $showXMP = XMPReader::isSupported();
203 
204  $meta = new self();
205  $array = PNGMetadataExtractor::getMetadata( $filename );
206  if ( isset( $array['text']['xmp']['x-default'] )
207  && $array['text']['xmp']['x-default'] !== '' && $showXMP
208  ) {
209  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
210  $xmp->parse( $array['text']['xmp']['x-default'] );
211  $xmpRes = $xmp->getResults();
212  foreach ( $xmpRes as $type => $xmpSection ) {
213  $meta->addMetadata( $xmpSection, $type );
214  }
215  }
216  unset( $array['text']['xmp'] );
217  $meta->addMetadata( $array['text'], 'native' );
218  unset( $array['text'] );
219  $array['metadata'] = $meta->getMetadataArray();
220  $array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION;
221 
222  return $array;
223  }
224 
233  public static function GIF( $filename ) {
234  $meta = new self();
235  $baseArray = GIFMetadataExtractor::getMetadata( $filename );
236 
237  if ( count( $baseArray['comment'] ) > 0 ) {
238  $meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' );
239  }
240 
241  if ( $baseArray['xmp'] !== '' && XMPReader::isSupported() ) {
242  $xmp = new XMPReader( LoggerFactory::getInstance( 'XMP' ), $filename );
243  $xmp->parse( $baseArray['xmp'] );
244  $xmpRes = $xmp->getResults();
245  foreach ( $xmpRes as $type => $xmpSection ) {
246  $meta->addMetadata( $xmpSection, $type );
247  }
248  }
249 
250  unset( $baseArray['comment'] );
251  unset( $baseArray['xmp'] );
252 
253  $baseArray['metadata'] = $meta->getMetadataArray();
254  $baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION;
255 
256  return $baseArray;
257  }
258 
272  public static function Tiff( $filename ) {
273  if ( file_exists( $filename ) ) {
274  $byteOrder = self::getTiffByteOrder( $filename );
275  if ( !$byteOrder ) {
276  throw new MWException( "Error determining byte order of $filename" );
277  }
278  $exif = new Exif( $filename, $byteOrder );
279  $data = $exif->getFilteredData();
280  if ( $data ) {
281  $data['MEDIAWIKI_EXIF_VERSION'] = Exif::version();
282 
283  return $data;
284  } else {
285  throw new MWException( "Could not extract data from tiff file $filename" );
286  }
287  } else {
288  throw new MWException( "File doesn't exist - $filename" );
289  }
290  }
291 
299  static function getTiffByteOrder( $filename ) {
300  $fh = fopen( $filename, 'rb' );
301  if ( !$fh ) {
302  return false;
303  }
304  $head = fread( $fh, 2 );
305  fclose( $fh );
306 
307  switch ( $head ) {
308  case 'II':
309  return 'LE'; // II for intel.
310  case 'MM':
311  return 'BE'; // MM for motorla.
312  default:
313  return false; // Something went wrong.
314 
315  }
316  }
317 }
BitmapMetadataHandler\Tiff
static Tiff( $filename)
This doesn't do much yet, but eventually I plan to add XMP support for Tiff.
Definition: BitmapMetadataHandler.php:272
PNGMetadataExtractor\VERSION
const VERSION
Definition: PNGMetadataExtractor.php:43
captcha-old.count
count
Definition: captcha-old.py:249
BitmapMetadataHandler\$iptcType
string $iptcType
Definition: BitmapMetadataHandler.php:54
BitmapMetadataHandler\GIF
static GIF( $filename)
function for gif images.
Definition: BitmapMetadataHandler.php:233
$res
$res
Definition: database.txt:21
Exif
Class to extract and validate Exif data from jpeg (and possibly tiff) files.
Definition: Exif.php:32
php
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:35
BitmapMetadataHandler
Class to deal with reconciling and extracting metadata from bitmap images.
Definition: BitmapMetadataHandler.php:37
MWException
MediaWiki exception.
Definition: MWException.php:26
BitmapMetadataHandler\$metaPriority
array $metaPriority
Metadata priority.
Definition: BitmapMetadataHandler.php:42
JpegMetadataExtractor\doPSIR
static doPSIR( $app13)
This reads the photoshop image resource.
Definition: JpegMetadataExtractor.php:212
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Exif\version
static version()
#-
Definition: Exif.php:582
IPTC\parse
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
BitmapMetadataHandler\getMetadataArray
getMetadataArray()
Merge together the various types of metadata the different types have different priorites,...
Definition: BitmapMetadataHandler.php:124
PNGMetadataExtractor\getMetadata
static getMetadata( $filename)
Definition: PNGMetadataExtractor.php:46
GIFMetadataExtractor\getMetadata
static getMetadata( $filename)
Definition: GIFMetadataExtractor.php:56
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:988
BitmapMetadataHandler\PNG
static PNG( $filename)
Entry point for png At some point in the future this might merge the png various tEXt chunks to that ...
Definition: BitmapMetadataHandler.php:201
XMPReader
Definition: XMPReader.php:33
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2221
BitmapMetadataHandler\getExif
getExif( $filename, $byteOrder)
Get exif info using exif class.
Definition: BitmapMetadataHandler.php:89
JpegMetadataExtractor\segmentSplitter
static segmentSplitter( $filename)
Function to extract metadata segments of interest from jpeg files based on GIFMetadataExtractor.
Definition: JpegMetadataExtractor.php:52
BitmapMetadataHandler\doApp13
doApp13( $app13)
This does the photoshop image resource app13 block of interest, IPTC-IIM metadata is stored here.
Definition: BitmapMetadataHandler.php:64
BitmapMetadataHandler\$metadata
array $metadata
Definition: BitmapMetadataHandler.php:39
as
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
Definition: distributors.txt:9
LoggerFactory
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
GIFMetadataExtractor\VERSION
const VERSION
Definition: GIFMetadataExtractor.php:44
BitmapMetadataHandler\Jpeg
static Jpeg( $filename)
Main entry point for jpeg's.
Definition: BitmapMetadataHandler.php:159
$wgShowEXIF
$wgShowEXIF
Show Exif data, on by default if available.
Definition: DefaultSettings.php:793
BitmapMetadataHandler\addMetadata
addMetadata( $metaArray, $type='other')
Add misc metadata.
Definition: BitmapMetadataHandler.php:106
$type
$type
Definition: testCompression.php:48
BitmapMetadataHandler\getTiffByteOrder
static getTiffByteOrder( $filename)
Read the first 2 bytes of a tiff file to figure out Little Endian or Big Endian.
Definition: BitmapMetadataHandler.php:299