27use Wikimedia\RequestTimeout\TimeoutException;
28use Wikimedia\XMPReader\Reader as XMPReader;
46 private $metadata = [];
49 private $metaPriority = [
52 60 => [
'iptc-good-hash',
'iptc-no-hash' ],
53 70 => [
'xmp-deprecated' ],
54 80 => [
'xmp-general' ],
56 100 => [
'iptc-bad-hash' ],
61 private $iptcType =
'iptc-no-hash';
71 private function doApp13( $app13 ) {
74 }
catch ( TimeoutException $e ) {
76 }
catch ( Exception $e ) {
80 wfDebug(
"Error parsing iptc data of file: " . $e->getMessage() );
81 $this->iptcType =
'iptc-no-hash';
98 public function getExif( $filename, $byteOrder ) {
99 $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
100 if ( file_exists( $filename ) && $showEXIF ) {
101 $exif =
new Exif( $filename, $byteOrder );
102 $data = $exif->getFilteredData();
116 if ( isset( $this->metadata[
$type] ) ) {
118 $metaArray += $this->metadata[
$type];
121 $this->metadata[
$type] = $metaArray;
137 krsort( $this->metaPriority );
138 foreach ( $this->metaPriority as $pri ) {
139 foreach ( $pri as
$type ) {
140 if ( isset( $this->metadata[
$type] ) ) {
143 foreach ( $this->metadata[
$type] as $itemName => $item ) {
144 if ( is_array( $item ) && isset( $item[
'_type'] ) && $item[
'_type'] ===
'lang' &&
145 isset( $temp[$itemName] ) && !is_array( $temp[$itemName] )
147 $default = $temp[$itemName];
148 $temp[$itemName] = $item;
149 $temp[$itemName][
'x-default'] = $default;
150 unset( $this->metadata[
$type][$itemName] );
154 $temp += $this->metadata[
$type];
168 public static function Jpeg( $filename ) {
169 $showXMP = XMPReader::isSupported();
174 if ( isset( $seg[
'SOF'] ) ) {
175 $meta->addMetadata( [
'SOF' => $seg[
'SOF'] ] );
177 if ( isset( $seg[
'COM'] ) && isset( $seg[
'COM'][0] ) ) {
178 $meta->addMetadata( [
'JPEGFileComment' => $seg[
'COM'] ],
'native' );
180 if ( isset( $seg[
'PSIR'] ) && count( $seg[
'PSIR'] ) > 0 ) {
181 foreach ( $seg[
'PSIR'] as $curPSIRValue ) {
182 $meta->doApp13( $curPSIRValue );
185 if ( isset( $seg[
'XMP'] ) && $showXMP ) {
186 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
187 $xmp->parse( $seg[
'XMP'] );
188 foreach ( $seg[
'XMP_ext'] as $xmpExt ) {
192 $xmp->parseExtended( $xmpExt );
194 $res = $xmp->getResults();
196 $meta->addMetadata( $array,
$type );
200 $meta->getExif( $filename, $seg[
'byteOrder'] ??
'BE' );
202 return $meta->getMetadataArray();
213 public static function PNG( $filename ) {
214 $showXMP = XMPReader::isSupported();
218 if ( isset( $array[
'text'][
'xmp'][
'x-default'] )
219 && $array[
'text'][
'xmp'][
'x-default'] !==
'' && $showXMP
221 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
222 $xmp->parse( $array[
'text'][
'xmp'][
'x-default'] );
223 $xmpRes = $xmp->getResults();
224 foreach ( $xmpRes as
$type => $xmpSection ) {
225 $meta->addMetadata( $xmpSection,
$type );
228 unset( $array[
'text'][
'xmp'] );
230 $meta->addMetadata( $array[
'text'],
'native' );
231 unset( $array[
'text'] );
232 $array[
'metadata'] = $meta->getMetadataArray();
246 public static function GIF( $filename ) {
250 if ( count( $baseArray[
'comment'] ) > 0 ) {
251 $meta->addMetadata( [
'GIFFileComment' => $baseArray[
'comment'] ],
'native' );
254 if ( $baseArray[
'xmp'] !==
'' && XMPReader::isSupported() ) {
255 $xmp =
new XMPReader( LoggerFactory::getInstance(
'XMP' ), $filename );
256 $xmp->parse( $baseArray[
'xmp'] );
257 $xmpRes = $xmp->getResults();
258 foreach ( $xmpRes as
$type => $xmpSection ) {
259 $meta->addMetadata( $xmpSection,
$type );
263 unset( $baseArray[
'comment'] );
264 unset( $baseArray[
'xmp'] );
266 $baseArray[
'metadata'] = $meta->getMetadataArray();
285 public static function Tiff( $filename ) {
286 if ( file_exists( $filename ) ) {
289 throw new MWException(
"Error determining byte order of $filename" );
291 $exif =
new Exif( $filename, $byteOrder );
292 $data = $exif->getFilteredData();
298 throw new MWException(
"Could not extract data from tiff file $filename" );
301 throw new MWException(
"File doesn't exist - $filename" );
313 $fh = fopen( $filename,
'rb' );
317 $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 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.