Merge "mediawiki.user: Return a promise from getRights/getGroups"
[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
31 const BROKEN_FILE = '0'; // value to store in img_metadata if error extracting metadata.
32
33 function getMetadata( $image, $filename ) {
34 try {
35 $parsedGIFMetadata = BitmapMetadataHandler::GIF( $filename );
36 } catch ( Exception $e ) {
37 // Broken file?
38 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
39 return self::BROKEN_FILE;
40 }
41
42 return serialize( $parsedGIFMetadata );
43 }
44
45 /**
46 * @param $image File
47 * @return array|bool
48 */
49 function formatMetadata( $image ) {
50 $meta = $this->getCommonMetaArray( $image );
51 if ( count( $meta ) === 0 ) {
52 return false;
53 }
54
55 return $this->formatMetadataHelper( $meta );
56 }
57
58 /**
59 * Return the standard metadata elements for #filemetadata parser func.
60 * @param File $image
61 * @return array|bool
62 */
63 public function getCommonMetaArray( File $image ) {
64 $meta = $image->getMetadata();
65
66 if ( !$meta ) {
67 return array();
68 }
69 $meta = unserialize( $meta );
70 if ( !isset( $meta['metadata'] ) ) {
71 return array();
72 }
73 unset( $meta['metadata']['_MW_GIF_VERSION'] );
74 return $meta['metadata'];
75 }
76
77 /**
78 * @param $image File
79 * @todo unittests
80 * @return bool
81 */
82 function getImageArea( $image ) {
83 $ser = $image->getMetadata();
84 if ( $ser ) {
85 $metadata = unserialize( $ser );
86 return $image->getWidth() * $image->getHeight() * $metadata['frameCount'];
87 } else {
88 return $image->getWidth() * $image->getHeight();
89 }
90 }
91
92 /**
93 * @param $image File
94 * @return bool
95 */
96 function isAnimatedImage( $image ) {
97 $ser = $image->getMetadata();
98 if ( $ser ) {
99 $metadata = unserialize( $ser );
100 if ( $metadata['frameCount'] > 1 ) {
101 return true;
102 }
103 }
104 return false;
105 }
106
107 /**
108 * We cannot animate thumbnails that are bigger than a particular size
109 * @param File $file
110 * @return bool
111 */
112 function canAnimateThumbnail( $file ) {
113 global $wgMaxAnimatedGifArea;
114 $answer = $this->getImageArea( $file ) <= $wgMaxAnimatedGifArea;
115 return $answer;
116 }
117
118 function getMetadataType( $image ) {
119 return 'parsed-gif';
120 }
121
122 function isMetadataValid( $image, $metadata ) {
123 if ( $metadata === self::BROKEN_FILE ) {
124 // Do not repetitivly regenerate metadata on broken file.
125 return self::METADATA_GOOD;
126 }
127
128 wfSuppressWarnings();
129 $data = unserialize( $metadata );
130 wfRestoreWarnings();
131
132 if ( !$data || !is_array( $data ) ) {
133 wfDebug( __METHOD__ . " invalid GIF metadata\n" );
134 return self::METADATA_BAD;
135 }
136
137 if ( !isset( $data['metadata']['_MW_GIF_VERSION'] )
138 || $data['metadata']['_MW_GIF_VERSION'] != GIFMetadataExtractor::VERSION ) {
139 wfDebug( __METHOD__ . " old but compatible GIF metadata\n" );
140 return self::METADATA_COMPATIBLE;
141 }
142 return self::METADATA_GOOD;
143 }
144
145 /**
146 * @param $image File
147 * @return string
148 */
149 function getLongDesc( $image ) {
150 global $wgLang;
151
152 $original = parent::getLongDesc( $image );
153
154 wfSuppressWarnings();
155 $metadata = unserialize( $image->getMetadata() );
156 wfRestoreWarnings();
157
158 if ( !$metadata || $metadata['frameCount'] <= 1 ) {
159 return $original;
160 }
161
162 /* Preserve original image info string, but strip the last char ')' so we can add even more */
163 $info = array();
164 $info[] = $original;
165
166 if ( $metadata['looped'] ) {
167 $info[] = wfMessage( 'file-info-gif-looped' )->parse();
168 }
169
170 if ( $metadata['frameCount'] > 1 ) {
171 $info[] = wfMessage( 'file-info-gif-frames' )->numParams( $metadata['frameCount'] )->parse();
172 }
173
174 if ( $metadata['duration'] ) {
175 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
176 }
177
178 return $wgLang->commaList( $info );
179 }
180 }