Merge "Added a separate error message for mkdir failures"
[lhc/web/wiklou.git] / includes / media / GIF.php
1 <?php
2 /**
3 * Handler for GIF images.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Media
22 */
23
24 /**
25 * Handler for GIF images.
26 *
27 * @ingroup Media
28 */
29 class GIFHandler extends BitmapHandler {
30 const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
31
32 function getMetadata( $image, $filename ) {
33 try {
34 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
35 } catch ( Exception $e ) {
36 // Broken file?
37 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
38
39 return self::BROKEN_FILE;
40 }
41
42 return serialize( $parsedGIFMetadata );
43 }
44
45 /**
46 * @param File $image
47 * @param bool|IContextSource $context Context to use (optional)
48 * @return array|bool
49 */
50 function formatMetadata( $image, $context = false ) {
51 $meta = $this->getCommonMetaArray( $image );
52 if ( count( $meta ) === 0 ) {
53 return false;
54 }
55
56 return $this->formatMetadataHelper( $meta, $context );
57 }
58
59 /**
60 * Return the standard metadata elements for #filemetadata parser func.
61 * @param File $image
62 * @return array|bool
63 */
64 public function getCommonMetaArray( File $image ) {
65 $meta = $image->getMetadata();
66
67 if ( !$meta ) {
68 return [];
69 }
70 $meta = unserialize( $meta );
71 if ( !isset( $meta['metadata'] ) ) {
72 return [];
73 }
74 unset( $meta['metadata']['_MW_GIF_VERSION'] );
75
76 return $meta['metadata'];
77 }
78
79 /**
80 * @todo Add unit tests
81 *
82 * @param File $image
83 * @return bool
84 */
85 function getImageArea( $image ) {
86 $ser = $image->getMetadata();
87 if ( $ser ) {
88 $metadata = unserialize( $ser );
89
90 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
91 } else {
92 return $image->getWidth() * $image->getHeight();
93 }
94 }
95
96 /**
97 * @param File $image
98 * @return bool
99 */
100 function isAnimatedImage( $image ) {
101 $ser = $image->getMetadata();
102 if ( $ser ) {
103 $metadata = unserialize( $ser );
104 if ( $metadata['frameCount'] > 1 ) {
105 return true;
106 }
107 }
108
109 return false;
110 }
111
112 /**
113 * We cannot animate thumbnails that are bigger than a particular size
114 * @param File $file
115 * @return bool
116 */
117 function canAnimateThumbnail( $file ) {
118 global $wgMaxAnimatedGifArea;
119 $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
120
121 return $answer;
122 }
123
124 function getMetadataType( $image ) {
125 return 'parsed-gif';
126 }
127
128 function isMetadataValid( $image, $metadata ) {
129 if ( $metadata === self::BROKEN_FILE ) {
130 // Do not repetitivly regenerate metadata on broken file.
131 return self::METADATA_GOOD;
132 }
133
134 MediaWiki\suppressWarnings();
135 $data = unserialize( $metadata );
136 MediaWiki\restoreWarnings();
137
138 if ( !$data || !is_array( $data ) ) {
139 wfDebug( __METHOD__ . " invalid GIF metadata\n" );
140
141 return self::METADATA_BAD;
142 }
143
144 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
145 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION
146 ) {
147 wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
148
149 return self::METADATA_COMPATIBLE;
150 }
151
152 return self::METADATA_GOOD;
153 }
154
155 /**
156 * @param File $image
157 * @return string
158 */
159 function getLongDesc( $image ) {
160 global $wgLang;
161
162 $original = parent::getLongDesc( $image );
163
164 MediaWiki\suppressWarnings();
165 $metadata = unserialize( $image->getMetadata() );
166 MediaWiki\restoreWarnings();
167
168 if ( !$metadata || $metadata['frameCount'] <= 1 ) {
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
191 /**
192 * Return the duration of the GIF file.
193 *
194 * Shown in the &query=imageinfo&iiprop=size api query.
195 *
196 * @param File $file
197 * @return float The duration of the file.
198 */
199 public function getLength( $file ) {
200 $serMeta = $file->getMetadata();
201 MediaWiki\suppressWarnings();
202 $metadata = unserialize( $serMeta );
203 MediaWiki\restoreWarnings();
204
205 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
206 return 0.0;
207 } else {
208 return (float)$metadata['duration'];
209 }
210 }
211 }