| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\Logger\LoggerFactory; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use Wikimedia\XMPReader\Reader as XMPReader; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class BitmapMetadataHandler { |
| 44 |
|
| 45 |
private $metadata = []; |
| 46 |
|
| 47 |
|
| 48 |
private $metaPriority = [ |
| 49 |
20 => [ 'other' ], |
| 50 |
40 => [ 'native' ], |
| 51 |
60 => [ 'iptc-good-hash', 'iptc-no-hash' ], |
| 52 |
70 => [ 'xmp-deprecated' ], |
| 53 |
80 => [ 'xmp-general' ], |
| 54 |
90 => [ 'xmp-exif' ], |
| 55 |
100 => [ 'iptc-bad-hash' ], |
| 56 |
120 => [ 'exif' ], |
| 57 |
]; |
| 58 |
|
| 59 |
|
| 60 |
private $iptcType = 'iptc-no-hash'; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private function doApp13( $app13 ) { |
| 71 |
try { |
| 72 |
$this->iptcType = JpegMetadataExtractor::doPSIR( $app13 ); |
| 73 |
} catch ( InvalidPSIRException $e ) { |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
wfDebug( "Error parsing iptc data of file: " . $e->getMessage() ); |
| 78 |
$this->iptcType = 'iptc-no-hash'; |
| 79 |
} |
| 80 |
|
| 81 |
$iptc = IPTC::parse( $app13 ); |
| 82 |
$this->addMetadata( $iptc, $this->iptcType ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 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(); |
| 100 |
if ( $data ) { |
| 101 |
$this->addMetadata( $data, 'exif' ); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function addMetadata( $metaArray, $type = 'other' ) { |
| 113 |
if ( isset( $this->metadata[$type] ) ) { |
| 114 |
|
| 115 |
$metaArray += $this->metadata[$type]; |
| 116 |
} |
| 117 |
|
| 118 |
$this->metadata[$type] = $metaArray; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function getMetadataArray() { |
| 131 |
|
| 132 |
|
| 133 |
$temp = []; |
| 134 |
krsort( $this->metaPriority ); |
| 135 |
foreach ( $this->metaPriority as $pri ) { |
| 136 |
foreach ( $pri as $type ) { |
| 137 |
if ( isset( $this->metadata[$type] ) ) { |
| 138 |
|
| 139 |
|
| 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] ) |
| 143 |
) { |
| 144 |
$default = $temp[$itemName]; |
| 145 |
$temp[$itemName] = $item; |
| 146 |
$temp[$itemName]['x-default'] = $default; |
| 147 |
unset( $this->metadata[$type][$itemName] ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
$temp += $this->metadata[$type]; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return $temp; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static function Jpeg( $filename ) { |
| 166 |
$showXMP = XMPReader::isSupported(); |
| 167 |
$meta = new self(); |
| 168 |
|
| 169 |
$seg = JpegMetadataExtractor::segmentSplitter( $filename ); |
| 170 |
|
| 171 |
if ( isset( $seg['SOF'] ) ) { |
| 172 |
$meta->addMetadata( [ 'SOF' => $seg['SOF'] ] ); |
| 173 |
} |
| 174 |
if ( isset( $seg['COM'] ) && isset( $seg['COM'][0] ) ) { |
| 175 |
$meta->addMetadata( [ 'JPEGFileComment' => $seg['COM'] ], 'native' ); |
| 176 |
} |
| 177 |
if ( isset( $seg['PSIR'] ) && count( $seg['PSIR'] ) > 0 ) { |
| 178 |
foreach ( $seg['PSIR'] as $curPSIRValue ) { |
| 179 |
$meta->doApp13( $curPSIRValue ); |
| 180 |
} |
| 181 |
} |
| 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 ) { |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
$xmp->parseExtended( $xmpExt ); |
| 190 |
} |
| 191 |
$res = $xmp->getResults(); |
| 192 |
foreach ( $res as $type => $array ) { |
| 193 |
$meta->addMetadata( $array, $type ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
$meta->getExif( $filename, $seg['byteOrder'] ?? 'BE' ); |
| 198 |
|
| 199 |
return $meta->getMetadataArray(); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public static function PNG( $filename ) { |
| 211 |
$showXMP = XMPReader::isSupported(); |
| 212 |
|
| 213 |
$meta = new self(); |
| 214 |
$array = PNGMetadataExtractor::getMetadata( $filename ); |
| 215 |
if ( isset( $array['text']['xmp']['x-default'] ) |
| 216 |
&& $array['text']['xmp']['x-default'] !== '' && $showXMP |
| 217 |
) { |
| 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 ); |
| 223 |
} |
| 224 |
} |
| 225 |
unset( $array['text']['xmp'] ); |
| 226 |
|
| 227 |
$meta->addMetadata( $array['text'], 'native' ); |
| 228 |
unset( $array['text'] ); |
| 229 |
$array['metadata'] = $meta->getMetadataArray(); |
| 230 |
$array['metadata']['_MW_PNG_VERSION'] = PNGMetadataExtractor::VERSION; |
| 231 |
|
| 232 |
return $array; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public static function GIF( $filename ) { |
| 244 |
$meta = new self(); |
| 245 |
$baseArray = GIFMetadataExtractor::getMetadata( $filename ); |
| 246 |
|
| 247 |
if ( count( $baseArray['comment'] ) > 0 ) { |
| 248 |
$meta->addMetadata( [ 'GIFFileComment' => $baseArray['comment'] ], 'native' ); |
| 249 |
} |
| 250 |
|
| 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 ); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
unset( $baseArray['comment'] ); |
| 261 |
unset( $baseArray['xmp'] ); |
| 262 |
|
| 263 |
$baseArray['metadata'] = $meta->getMetadataArray(); |
| 264 |
$baseArray['metadata']['_MW_GIF_VERSION'] = GIFMetadataExtractor::VERSION; |
| 265 |
|
| 266 |
return $baseArray; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public static function Tiff( $filename ) { |
| 283 |
if ( file_exists( $filename ) ) { |
| 284 |
$byteOrder = self::getTiffByteOrder( $filename ); |
| 285 |
if ( !$byteOrder ) { |
| 286 |
throw new InvalidTiffException( "Error determining byte order of $filename" ); |
| 287 |
} |
| 288 |
$exif = new Exif( $filename, $byteOrder ); |
| 289 |
$data = $exif->getFilteredData(); |
| 290 |
if ( $data ) { |
| 291 |
$data['MEDIAWIKI_EXIF_VERSION'] = Exif::version(); |
| 292 |
|
| 293 |
return $data; |
| 294 |
} else { |
| 295 |
throw new InvalidTiffException( "Could not extract data from tiff file $filename" ); |
| 296 |
} |
| 297 |
} else { |
| 298 |
throw new InvalidTiffException( "File doesn't exist - $filename" ); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
public static function getTiffByteOrder( $filename ) { |
| 310 |
$fh = fopen( $filename, 'rb' ); |
| 311 |
if ( !$fh ) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
$head = fread( $fh, 2 ); |
| 315 |
fclose( $fh ); |
| 316 |
|
| 317 |
switch ( $head ) { |
| 318 |
case 'II': |
| 319 |
return 'LE'; |
| 320 |
case 'MM': |
| 321 |
return 'BE'; |
| 322 |
default: |
| 323 |
return false; |
| 324 |
|
| 325 |
} |
| 326 |
} |
| 327 |
} |