| 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 MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class GIFHandler extends BitmapHandler { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private const BROKEN_FILE = '0'; |
| 39 |
|
| 40 |
public function getSizeAndMetadata( $state, $filename ) { |
| 41 |
try { |
| 42 |
$parsedGIFMetadata = BitmapMetadataHandler::GIF( $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' => $parsedGIFMetadata['width'], |
| 54 |
'height' => $parsedGIFMetadata['height'], |
| 55 |
'bits' => $parsedGIFMetadata['bits'], |
| 56 |
'metadata' => array_diff_key( |
| 57 |
$parsedGIFMetadata, |
| 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 |
public function getCommonMetaArray( File $image ) { |
| 83 |
$meta = $image->getMetadataArray(); |
| 84 |
if ( !isset( $meta['metadata'] ) ) { |
| 85 |
return []; |
| 86 |
} |
| 87 |
unset( $meta['metadata']['_MW_GIF_VERSION'] ); |
| 88 |
|
| 89 |
return $meta['metadata']; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function getImageArea( $image ) { |
| 99 |
$metadata = $image->getMetadataArray(); |
| 100 |
if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) { |
| 101 |
return $image->getWidth() * $image->getHeight() * $metadata['frameCount']; |
| 102 |
} |
| 103 |
return $image->getWidth() * $image->getHeight(); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function isAnimatedImage( $image ) { |
| 111 |
$metadata = $image->getMetadataArray(); |
| 112 |
if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) { |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function canAnimateThumbnail( $file ) { |
| 125 |
$maxAnimatedGifArea = MediaWikiServices::getInstance()->getMainConfig() |
| 126 |
->get( MainConfigNames::MaxAnimatedGifArea ); |
| 127 |
|
| 128 |
return $this->getImageArea( $file ) <= $maxAnimatedGifArea; |
| 129 |
} |
| 130 |
|
| 131 |
public function getMetadataType( $image ) { |
| 132 |
return 'parsed-gif'; |
| 133 |
} |
| 134 |
|
| 135 |
public function isFileMetadataValid( $image ) { |
| 136 |
$data = $image->getMetadataArray(); |
| 137 |
if ( $data === [ '_error' => self::BROKEN_FILE ] ) { |
| 138 |
|
| 139 |
return self::METADATA_GOOD; |
| 140 |
} |
| 141 |
|
| 142 |
if ( !$data || isset( $data['_error'] ) ) { |
| 143 |
wfDebug( __METHOD__ . " invalid GIF metadata" ); |
| 144 |
|
| 145 |
return self::METADATA_BAD; |
| 146 |
} |
| 147 |
|
| 148 |
if ( !isset( $data['metadata']['_MW_GIF_VERSION'] ) |
| 149 |
|| $data['metadata']['_MW_GIF_VERSION'] !== GIFMetadataExtractor::VERSION |
| 150 |
) { |
| 151 |
wfDebug( __METHOD__ . " old but compatible GIF metadata" ); |
| 152 |
|
| 153 |
return self::METADATA_COMPATIBLE; |
| 154 |
} |
| 155 |
|
| 156 |
return self::METADATA_GOOD; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function getLongDesc( $image ) { |
| 164 |
global $wgLang; |
| 165 |
|
| 166 |
$original = parent::getLongDesc( $image ); |
| 167 |
|
| 168 |
$metadata = $image->getMetadataArray(); |
| 169 |
|
| 170 |
if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) { |
| 171 |
return $original; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
$info = []; |
| 176 |
$info[] = $original; |
| 177 |
|
| 178 |
if ( $metadata['looped'] ) { |
| 179 |
$info[] = wfMessage( 'file-info-gif-looped' )->parse(); |
| 180 |
} |
| 181 |
|
| 182 |
if ( $metadata['frameCount'] > 1 ) { |
| 183 |
$info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse(); |
| 184 |
} |
| 185 |
|
| 186 |
if ( $metadata['duration'] ) { |
| 187 |
$info[] = $wgLang->formatTimePeriod( $metadata['duration'] ); |
| 188 |
} |
| 189 |
|
| 190 |
return $wgLang->commaList( $info ); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public function getLength( $file ) { |
| 202 |
$metadata = $file->getMetadataArray(); |
| 203 |
|
| 204 |
if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) { |
| 205 |
return 0.0; |
| 206 |
} |
| 207 |
return (float)$metadata['duration']; |
| 208 |
} |
| 209 |
} |