13use Wikimedia\XMPReader\Reader as XMPReader;
31 private $metadata = [];
34 private $metaPriority = [
37 60 => [
'iptc-good-hash',
'iptc-no-hash' ],
38 70 => [
'xmp-deprecated' ],
39 80 => [
'xmp-general' ],
41 100 => [
'iptc-bad-hash' ],
46 private $iptcType =
'iptc-no-hash';
56 private function doApp13( $app13 ) {
63 wfDebug(
"Error parsing iptc data of file: " . $e->getMessage() );
64 $this->iptcType =
'iptc-no-hash';
81 public function getExif( $filename, $byteOrder ) {
82 $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
83 if ( file_exists( $filename ) && $showEXIF ) {
84 $exif =
new Exif( $filename, $byteOrder );
85 $data = $exif->getFilteredData();
99 if ( isset( $this->metadata[$type] ) ) {
101 $metaArray += $this->metadata[$type];
104 $this->metadata[$type] = $metaArray;
120 krsort( $this->metaPriority );
121 foreach ( $this->metaPriority as $pri ) {
122 foreach ( $pri as $type ) {
123 if ( isset( $this->metadata[$type] ) ) {
126 foreach ( $this->metadata[$type] as $itemName => $item ) {
127 if ( is_array( $item ) && isset( $item[
'_type'] ) && $item[
'_type'] ===
'lang' &&
128 isset( $temp[$itemName] ) && !is_array( $temp[$itemName] )
130 $default = $temp[$itemName];
131 $temp[$itemName] = $item;
132 $temp[$itemName][
'x-default'] = $default;
133 unset( $this->metadata[$type][$itemName] );
137 $temp += $this->metadata[$type];
151 public static function Jpeg( $filename ) {
152 $showXMP = XMPReader::isSupported();
157 if ( isset( $seg[
'SOF'] ) ) {
158 $meta->addMetadata( [
'SOF' => $seg[
'SOF'] ] );
160 if ( isset( $seg[
'COM'] ) && isset( $seg[
'COM'][0] ) ) {
161 $meta->addMetadata( [
'JPEGFileComment' => $seg[
'COM'] ],
'native' );
163 if ( isset( $seg[
'PSIR'] ) && count( $seg[
'PSIR'] ) > 0 ) {
164 foreach ( $seg[
'PSIR'] as $curPSIRValue ) {
165 $meta->doApp13( $curPSIRValue );
168 if ( isset( $seg[
'XMP'] ) && $showXMP ) {
169 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
170 $xmp->parse( $seg[
'XMP'] );
171 foreach ( $seg[
'XMP_ext'] as $xmpExt ) {
175 $xmp->parseExtended( $xmpExt );
177 $res = $xmp->getResults();
178 foreach ( $res as $type => $array ) {
179 $meta->addMetadata( $array, $type );
183 $meta->getExif( $filename, $seg[
'byteOrder'] ??
'BE' );
185 return $meta->getMetadataArray();
196 public static function PNG( $filename ) {
197 $showXMP = XMPReader::isSupported();
201 if ( isset( $array[
'text'][
'xmp'][
'x-default'] )
202 && $array[
'text'][
'xmp'][
'x-default'] !==
'' && $showXMP
204 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
205 $xmp->parse( $array[
'text'][
'xmp'][
'x-default'] );
206 $xmpRes = $xmp->getResults();
207 foreach ( $xmpRes as $type => $xmpSection ) {
208 $meta->addMetadata( $xmpSection, $type );
212 if ( $array[
'exif'] ) {
215 $tmpFile = MediaWikiServices::getInstance()->
216 getTempFSFileFactory()->
217 newTempFSFile(
'png-exif_',
'tiff' );
218 $exifDataFile = $tmpFile->getPath();
219 file_put_contents( $exifDataFile, $array[
'exif'] );
221 $meta->getExif( $exifDataFile, $byteOrder );
223 unset( $array[
'exif'] );
224 unset( $array[
'text'][
'xmp'] );
225 $meta->addMetadata( $array[
'text'],
'native' );
226 unset( $array[
'text'] );
227 $array[
'metadata'] = $meta->getMetadataArray();
241 public static function GIF( $filename ) {
245 if ( count( $baseArray[
'comment'] ) > 0 ) {
246 $meta->addMetadata( [
'GIFFileComment' => $baseArray[
'comment'] ],
'native' );
249 if ( $baseArray[
'xmp'] !==
'' && XMPReader::isSupported() ) {
250 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
251 $xmp->parse( $baseArray[
'xmp'] );
252 $xmpRes = $xmp->getResults();
253 foreach ( $xmpRes as $type => $xmpSection ) {
254 $meta->addMetadata( $xmpSection, $type );
258 unset( $baseArray[
'comment'] );
259 unset( $baseArray[
'xmp'] );
261 $baseArray[
'metadata'] = $meta->getMetadataArray();
280 public static function Tiff( $filename ) {
281 if ( file_exists( $filename ) ) {
285 'Error determining byte order of {filename}',
286 [
'filename' => $filename ]
289 $exif =
new Exif( $filename, $byteOrder );
290 $data = $exif->getFilteredData();
297 'Could not extract data from tiff file {filename}',
298 [
'filename' => $filename ]
303 "File {filename} doesn't exist",
304 [
'filename' => $filename ]
317 $fh = fopen( $filename,
'rb' );
321 $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.