MediaWiki REL1_38
GIFHandler.php
Go to the documentation of this file.
1<?php
25use Wikimedia\RequestTimeout\TimeoutException;
26
36 private const BROKEN_FILE = '0';
37
38 public function getSizeAndMetadata( $state, $filename ) {
39 try {
40 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
41 } catch ( TimeoutException $e ) {
42 throw $e;
43 } catch ( Exception $e ) {
44 // Broken file?
45 wfDebug( __METHOD__ . ': ' . $e->getMessage() );
46
47 return [ 'metadata' => [ '_error' => self::BROKEN_FILE ] ];
48 }
49
50 return [
51 'width' => $parsedGIFMetadata['width'],
52 'height' => $parsedGIFMetadata['height'],
53 'bits' => $parsedGIFMetadata['bits'],
54 'metadata' => array_diff_key(
55 $parsedGIFMetadata,
56 [ 'width' => true, 'height' => true, 'bits' => true ]
57 )
58 ];
59 }
60
66 public function formatMetadata( $image, $context = false ) {
67 $meta = $this->getCommonMetaArray( $image );
68 if ( !$meta ) {
69 return false;
70 }
71
72 return $this->formatMetadataHelper( $meta, $context );
73 }
74
80 public function getCommonMetaArray( File $image ) {
81 $meta = $image->getMetadataArray();
82 if ( !isset( $meta['metadata'] ) ) {
83 return [];
84 }
85 unset( $meta['metadata']['_MW_GIF_VERSION'] );
86
87 return $meta['metadata'];
88 }
89
96 public function getImageArea( $image ) {
97 $metadata = $image->getMetadataArray();
98 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 0 ) {
99 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
100 } else {
101 return $image->getWidth() * $image->getHeight();
102 }
103 }
104
109 public function isAnimatedImage( $image ) {
110 $metadata = $image->getMetadataArray();
111 if ( isset( $metadata['frameCount'] ) && $metadata['frameCount'] > 1 ) {
112 return true;
113 }
114
115 return false;
116 }
117
123 public function canAnimateThumbnail( $file ) {
124 $maxAnimatedGifArea = MediaWikiServices::getInstance()->getMainConfig()->get( 'MaxAnimatedGifArea' );
125
126 return $this->getImageArea( $file ) <= $maxAnimatedGifArea;
127 }
128
129 public function getMetadataType( $image ) {
130 return 'parsed-gif';
131 }
132
133 public function isFileMetadataValid( $image ) {
134 $data = $image->getMetadataArray();
135 if ( $data === [ '_error' => self::BROKEN_FILE ] ) {
136 // Do not repetitively regenerate metadata on broken file.
137 return self::METADATA_GOOD;
138 }
139
140 if ( !$data || isset( $data['_error'] ) ) {
141 wfDebug( __METHOD__ . " invalid GIF metadata" );
142
143 return self::METADATA_BAD;
144 }
145
146 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
147 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
148 ) {
149 wfDebug( __METHOD__ . " old but compatible GIF metadata" );
150
152 }
153
154 return self::METADATA_GOOD;
155 }
156
161 public function getLongDesc( $image ) {
162 global $wgLang;
163
164 $original = parent::getLongDesc( $image );
165
166 $metadata = $image->getMetadataArray();
167
168 if ( !$metadata || isset( $metadata['_error'] ) || $metadata['frameCount'] <= 0 ) {
169 return $original;
170 }
171
172 /* Preserve original image info string, but strip the last char ')' so we can add even more */
173 $info = [];
174 $info[] = $original;
175
176 if ( $metadata['looped'] ) {
177 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
178 }
179
180 if ( $metadata['frameCount'] > 1 ) {
181 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
182 }
183
184 if ( $metadata['duration'] ) {
185 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
186 }
187
188 return $wgLang->commaList( $info );
189 }
190
199 public function getLength( $file ) {
200 $metadata = $file->getMetadataArray();
201
202 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
203 return 0.0;
204 } else {
205 return (float)$metadata['duration'];
206 }
207 }
208}
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') &&! $wgCommandLineMode) $wgLang
Definition Setup.php:927
Generic handler for bitmap images.
static GIF( $filename)
function for gif images.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:67
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:746
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)
const BROKEN_FILE
Value to store in img_metadata if there was error extracting metadata.
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
MediaWikiServices is the service locator for the application scope of MediaWiki.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42