| 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\Context\IContextSource; |
| 25 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class PNGHandler extends BitmapHandler { |
| 33 |
private const BROKEN_FILE = '0'; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function getSizeAndMetadata( $state, $filename ) { |
| 41 |
try { |
| 42 |
$metadata = BitmapMetadataHandler::PNG( $filename ); |
| 43 |
} catch ( TimeoutException $e ) { |
| 44 |
throw $e; |
| 45 |
} catch ( Exception $e ) { |
| 46 |
|
| 47 |
wfDebug( __METHOD__ . ': ' . $e->getMessage() ); |
| 48 |
|
| 49 |
return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ]; |
| 50 |
} |
| 51 |
|
| 52 |
return [ |
| 53 |
'width' => $metadata['width'], |
| 54 |
'height' => $metadata['height'], |
| 55 |
'bits' => $metadata['bitDepth'], |
| 56 |
'metadata' => array_diff_key( |
| 57 |
$metadata, |
| 58 |
[ 'width' => true, 'height' => true, 'bits' => true ] |
| 59 |
) |
| 60 |
]; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function formatMetadata( $image, $context = false ) { |
| 69 |
$meta = $this->getCommonMetaArray( $image ); |
| 70 |
if ( !$meta ) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
return $this->formatMetadataHelper( $meta, $context ); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function getCommonMetaArray( File $image ) { |
| 84 |
$meta = $image->getMetadataArray(); |
| 85 |
|
| 86 |
if ( !isset( $meta['metadata'] ) ) { |
| 87 |
return []; |
| 88 |
} |
| 89 |
unset( $meta['metadata']['_MW_PNG_VERSION'] ); |
| 90 |
|
| 91 |
return $meta['metadata']; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function isAnimatedImage( $image ) { |
| 99 |
$metadata = $image->getMetadataArray(); |
| 100 |
return isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function canAnimateThumbnail( $image ) { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
public function getMetadataType( $image ) { |
| 113 |
return 'parsed-png'; |
| 114 |
} |
| 115 |
|
| 116 |
public function isFileMetadataValid( $image ) { |
| 117 |
$data = $image->getMetadataArray(); |
| 118 |
if ( $data === [ '_error' => self::BROKEN_FILE ] ) { |
| 119 |
|
| 120 |
return self::METADATA_GOOD; |
| 121 |
} |
| 122 |
|
| 123 |
if ( !$data || isset( $data['_error'] ) ) { |
| 124 |
wfDebug( __METHOD__ . " invalid png metadata" ); |
| 125 |
|
| 126 |
return self::METADATA_BAD; |
| 127 |
} |
| 128 |
|
| 129 |
if ( !isset( $data['metadata']['_MW_PNG_VERSION'] ) |
| 130 |
|| $data['metadata']['_MW_PNG_VERSION'] !== PNGMetadataExtractor::VERSION |
| 131 |
) { |
| 132 |
wfDebug( __METHOD__ . " old but compatible png metadata" ); |
| 133 |
|
| 134 |
return self::METADATA_COMPATIBLE; |
| 135 |
} |
| 136 |
|
| 137 |
return self::METADATA_GOOD; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function getLongDesc( $image ) { |
| 145 |
global $wgLang; |
| 146 |
$original = parent::getLongDesc( $image ); |
| 147 |
|
| 148 |
$metadata = $image->getMetadataArray(); |
| 149 |
|
| 150 |
if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) { |
| 151 |
return $original; |
| 152 |
} |
| 153 |
|
| 154 |
$info = []; |
| 155 |
$info[] = $original; |
| 156 |
|
| 157 |
if ( $metadata['loopCount'] == 0 ) { |
| 158 |
$info[] = wfMessage( 'file-info-png-looped' )->parse(); |
| 159 |
} elseif ( $metadata['loopCount'] > 1 ) { |
| 160 |
$info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse(); |
| 161 |
} |
| 162 |
|
| 163 |
if ( $metadata['frameCount'] > 0 ) { |
| 164 |
$info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse(); |
| 165 |
} |
| 166 |
|
| 167 |
if ( $metadata['duration'] ) { |
| 168 |
$info[] = $wgLang->formatTimePeriod( $metadata['duration'] ); |
| 169 |
} |
| 170 |
|
| 171 |
return $wgLang->commaList( $info ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function getLength( $file ) { |
| 183 |
$metadata = $file->getMetadataArray(); |
| 184 |
|
| 185 |
if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) { |
| 186 |
return 0.0; |
| 187 |
} |
| 188 |
|
| 189 |
return (float)$metadata['duration']; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function supportsBucketing() { |
| 195 |
return false; |
| 196 |
} |
| 197 |
} |