27use Wikimedia\XMPReader\Reader as XMPReader;
45 private $metadata = [];
48 private $metaPriority = [
51 60 => [
'iptc-good-hash',
'iptc-no-hash' ],
52 70 => [
'xmp-deprecated' ],
53 80 => [
'xmp-general' ],
55 100 => [
'iptc-bad-hash' ],
60 private $iptcType =
'iptc-no-hash';
70 private function doApp13( $app13 ) {
77 wfDebug(
"Error parsing iptc data of file: " . $e->getMessage() );
78 $this->iptcType =
'iptc-no-hash';
95 public function getExif( $filename, $byteOrder ) {
96 $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
97 if ( file_exists( $filename ) && $showEXIF ) {
98 $exif =
new Exif( $filename, $byteOrder );
99 $data = $exif->getFilteredData();
113 if ( isset( $this->metadata[$type] ) ) {
115 $metaArray += $this->metadata[$type];
118 $this->metadata[$type] = $metaArray;
134 krsort( $this->metaPriority );
135 foreach ( $this->metaPriority as $pri ) {
136 foreach ( $pri as $type ) {
137 if ( isset( $this->metadata[$type] ) ) {
140 foreach ( $this->metadata[$type] as $itemName => $item ) {
141 if ( is_array( $item ) && isset( $item[
'_type'] ) && $item[
'_type'] ===
'lang' &&
142 isset( $temp[$itemName] ) && !is_array( $temp[$itemName] )
144 $default = $temp[$itemName];
145 $temp[$itemName] = $item;
146 $temp[$itemName][
'x-default'] = $default;
147 unset( $this->metadata[$type][$itemName] );
151 $temp += $this->metadata[$type];
165 public static function Jpeg( $filename ) {
166 $showXMP = XMPReader::isSupported();
171 if ( isset( $seg[
'SOF'] ) ) {
172 $meta->addMetadata( [
'SOF' => $seg[
'SOF'] ] );
174 if ( isset( $seg[
'COM'] ) && isset( $seg[
'COM'][0] ) ) {
175 $meta->addMetadata( [
'JPEGFileComment' => $seg[
'COM'] ],
'native' );
177 if ( isset( $seg[
'PSIR'] ) && count( $seg[
'PSIR'] ) > 0 ) {
178 foreach ( $seg[
'PSIR'] as $curPSIRValue ) {
179 $meta->doApp13( $curPSIRValue );
182 if ( isset( $seg[
'XMP'] ) && $showXMP ) {
183 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
184 $xmp->parse( $seg[
'XMP'] );
185 foreach ( $seg[
'XMP_ext'] as $xmpExt ) {
189 $xmp->parseExtended( $xmpExt );
191 $res = $xmp->getResults();
192 foreach ( $res as $type => $array ) {
193 $meta->addMetadata( $array, $type );
197 $meta->getExif( $filename, $seg[
'byteOrder'] ??
'BE' );
199 return $meta->getMetadataArray();
210 public static function PNG( $filename ) {
211 $showXMP = XMPReader::isSupported();
215 if ( isset( $array[
'text'][
'xmp'][
'x-default'] )
216 && $array[
'text'][
'xmp'][
'x-default'] !==
'' && $showXMP
218 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
219 $xmp->parse( $array[
'text'][
'xmp'][
'x-default'] );
220 $xmpRes = $xmp->getResults();
221 foreach ( $xmpRes as $type => $xmpSection ) {
222 $meta->addMetadata( $xmpSection, $type );
225 unset( $array[
'text'][
'xmp'] );
227 $meta->addMetadata( $array[
'text'],
'native' );
228 unset( $array[
'text'] );
229 $array[
'metadata'] = $meta->getMetadataArray();
243 public static function GIF( $filename ) {
247 if ( count( $baseArray[
'comment'] ) > 0 ) {
248 $meta->addMetadata( [
'GIFFileComment' => $baseArray[
'comment'] ],
'native' );
251 if ( $baseArray[
'xmp'] !==
'' && XMPReader::isSupported() ) {
252 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
253 $xmp->parse( $baseArray[
'xmp'] );
254 $xmpRes = $xmp->getResults();
255 foreach ( $xmpRes as $type => $xmpSection ) {
256 $meta->addMetadata( $xmpSection, $type );
260 unset( $baseArray[
'comment'] );
261 unset( $baseArray[
'xmp'] );
263 $baseArray[
'metadata'] = $meta->getMetadataArray();
282 public static function Tiff( $filename ) {
283 if ( file_exists( $filename ) ) {
288 $exif =
new Exif( $filename, $byteOrder );
289 $data = $exif->getFilteredData();
310 $fh = fopen( $filename,
'rb' );
314 $head = fread( $fh, 2 );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Class to extract and validate Exif data from jpeg (and possibly tiff) files.
static version()
The version of the output format.
static parse( $rawData)
This takes the results of iptcparse() and puts it into a form that can be handled by mediawiki.
A class containing constants representing the names of configuration variables.