MediaWiki master
GIFHandler.php
Go to the documentation of this file.
1<?php
28use Wikimedia\RequestTimeout\TimeoutException;
29
39 private const BROKEN_FILE = '0';
40
41 public function getSizeAndMetadata( $state, $filename ) {
42 try {
43 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
44 } catch ( TimeoutException $e ) {
45 throw $e;
46 } catch ( Exception $e ) {
47 // Broken file?
48 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
49
50 return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
51 }
52
53 return [
54 'width' => $parsedGIFMetadata['width'],
55 'height' => $parsedGIFMetadata['height'],
56 'bits' => $parsedGIFMetadata['bits'],
57 'metadata' => array_diff_key(
58 $parsedGIFMetadata,
59 [ 'width' => true, 'height' => true, 'bits' => true ]
60 )
61 ];
62 }
63
69 public function formatMetadata( $image, $context = false ) {
70 $meta = $this->getCommonMetaArray( $image );
71 if ( !$meta ) {
72 return false;
73 }
74
75 return $this->formatMetadataHelper( $meta, $context );
76 }
77
83 public function getCommonMetaArray( File $image ) {
84 $meta = $image->getMetadataArray();
85 if ( !isset( $meta['metadata'] ) ) {
86 return [];
87 }
88 unset( $meta['metadata']['_MW_GIF_VERSION'] );
89
90 return $meta['metadata'];
91 }
92
99 public function getImageArea( $image ) {
100 $metadata = $image->getMetadataArray();
101 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
102 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
103 }
104 return $image->getWidth() * $image->getHeight();
105 }
106
111 public function isAnimatedImage( $image ) {
112 $metadata = $image->getMetadataArray();
113 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
114 return true;
115 }
116
117 return false;
118 }
119
125 public function canAnimateThumbnail( $file ) {
126 $maxAnimatedGifArea = MediaWikiServices::getInstance()->getMainConfig()
127 ->get( MainConfigNames::MaxAnimatedGifArea );
128
129 return $this->getImageArea( $file ) <= $maxAnimatedGifArea;
130 }
131
132 public function getMetadataType( $image ) {
133 return 'parsed-gif';
134 }
135
136 public function isFileMetadataValid( $image ) {
137 $data = $image->getMetadataArray();
138 if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
139 // Do not repetitively regenerate metadata on broken file.
140 return self::METADATA_GOOD;
141 }
142
143 if ( !$data || isset( $data['_error'] ) ) {
144 wfDebug( __METHOD__ . " invalid GIF metadata" );
145
146 return self::METADATA_BAD;
147 }
148
149 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
150 || $data['metadata']['_MW_GIF_VERSION'] !== GIFMetadataExtractor::VERSION
151 ) {
152 wfDebug( __METHOD__ . " old but compatible GIF metadata" );
153
155 }
156
157 return self::METADATA_GOOD;
158 }
159
164 public function getLongDesc( $image ) {
165 global $wgLang;
166
167 $original = parent::getLongDesc( $image );
168
169 $metadata = $image->getMetadataArray();
170
171 if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
172 return $original;
173 }
174
175 /* Preserve original image info string, but strip the last char ')' so we can add even more */
176 $info = [];
177 $info[] = $original;
178
179 if ( $metadata['looped'] ) {
180 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
181 }
182
183 if ( $metadata['frameCount'] > 1 ) {
184 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
185 }
186
187 if ( $metadata['duration'] ) {
188 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
189 }
190
191 return $wgLang->commaList( $info );
192 }
193
202 public function getLength( $file ) {
203 $metadata = $file->getMetadataArray();
204
205 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
206 return 0.0;
207 }
208 return (float)$metadata['duration'];
209 }
210}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined( 'MW_NO_SESSION') &&MW_ENTRY_POINT !=='cli' $wgLang
Definition Setup.php:563
Generic handler for bitmap images.
static GIF( $filename)
function for gif images.
Handler for GIF images.
isFileMetadataValid( $image)
Check if the metadata is valid for this handler.
getImageArea( $image)
getLength( $file)
Return the duration of the GIF file.
getLongDesc( $image)
isAnimatedImage( $image)
canAnimateThumbnail( $file)
We cannot animate thumbnails that are bigger than a particular size.
formatMetadata( $image, $context=false)
getSizeAndMetadata( $state, $filename)
Get image size information and metadata array.
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
getCommonMetaArray(File $image)
Return the standard metadata elements for #filemetadata parser func.
const METADATA_COMPATIBLE
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
const METADATA_GOOD
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:791
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Interface for objects which can provide a MediaWiki context on request.