Expose $wgMaxArticleSize in siteinfo query api
[mediawiki.git] / includes / media / PNG.php
blob8a3e001128713dc97cd062ebddd7b94992b2da19
1 <?php
2 /**
3 * Handler for PNG images.
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.
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.
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
20 * @file
21 * @ingroup Media
24 /**
25 * Handler for PNG images.
27 * @ingroup Media
29 class PNGHandler extends BitmapHandler {
30 const BROKEN_FILE = '0';
32 /**
33 * @param File $image
34 * @param string $filename
35 * @return string
37 function getMetadata( $image, $filename ) {
38 try {
39 $metadata = BitmapMetadataHandler::PNG( $filename );
40 } catch ( Exception $e ) {
41 // Broken file?
42 wfDebug( __METHOD__ . ': ' . $e->getMessage() . "\n" );
44 return self::BROKEN_FILE;
47 return serialize( $metadata );
50 /**
51 * @param File $image
52 * @param bool|IContextSource $context Context to use (optional)
53 * @return array|bool
55 function formatMetadata( $image, $context = false ) {
56 $meta = $this->getCommonMetaArray( $image );
57 if ( count( $meta ) === 0 ) {
58 return false;
61 return $this->formatMetadataHelper( $meta, $context );
64 /**
65 * Get a file type independent array of metadata.
67 * @param File $image
68 * @return array The metadata array
70 public function getCommonMetaArray( File $image ) {
71 $meta = $image->getMetadata();
73 if ( !$meta ) {
74 return [];
76 $meta = unserialize( $meta );
77 if ( !isset( $meta['metadata'] ) ) {
78 return [];
80 unset( $meta['metadata']['_MW_PNG_VERSION'] );
82 return $meta['metadata'];
85 /**
86 * @param File $image
87 * @return bool
89 function isAnimatedImage( $image ) {
90 $ser = $image->getMetadata();
91 if ( $ser ) {
92 $metadata = unserialize( $ser );
93 if ( $metadata['frameCount'] > 1 ) {
94 return true;
98 return false;
102 * We do not support making APNG thumbnails, so always false
103 * @param File $image
104 * @return bool False
106 function canAnimateThumbnail( $image ) {
107 return false;
110 function getMetadataType( $image ) {
111 return 'parsed-png';
114 function isMetadataValid( $image, $metadata ) {
116 if ( $metadata === self::BROKEN_FILE ) {
117 // Do not repetitivly regenerate metadata on broken file.
118 return self::METADATA_GOOD;
121 MediaWiki\suppressWarnings();
122 $data = unserialize( $metadata );
123 MediaWiki\restoreWarnings();
125 if ( !$data || !is_array( $data ) ) {
126 wfDebug( __METHOD__ . " invalid png metadata\n" );
128 return self::METADATA_BAD;
131 if ( !isset( $data['metadata']['_MW_PNG_VERSION'] )
132 || $data['metadata']['_MW_PNG_VERSION'] != PNGMetadataExtractor::VERSION
134 wfDebug( __METHOD__ . " old but compatible png metadata\n" );
136 return self::METADATA_COMPATIBLE;
139 return self::METADATA_GOOD;
143 * @param File $image
144 * @return string
146 function getLongDesc( $image ) {
147 global $wgLang;
148 $original = parent::getLongDesc( $image );
150 MediaWiki\suppressWarnings();
151 $metadata = unserialize( $image->getMetadata() );
152 MediaWiki\restoreWarnings();
154 if ( !$metadata || $metadata['frameCount'] <= 0 ) {
155 return $original;
158 $info = [];
159 $info[] = $original;
161 if ( $metadata['loopCount'] == 0 ) {
162 $info[] = wfMessage( 'file-info-png-looped' )->parse();
163 } elseif ( $metadata['loopCount'] > 1 ) {
164 $info[] = wfMessage( 'file-info-png-repeat' )->numParams( $metadata['loopCount'] )->parse();
167 if ( $metadata['frameCount'] > 0 ) {
168 $info[] = wfMessage( 'file-info-png-frames' )->numParams( $metadata['frameCount'] )->parse();
171 if ( $metadata['duration'] ) {
172 $info[] = $wgLang->formatTimePeriod( $metadata['duration'] );
175 return $wgLang->commaList( $info );
179 * Return the duration of an APNG file.
181 * Shown in the &query=imageinfo&iiprop=size api query.
183 * @param File $file
184 * @return float The duration of the file.
186 public function getLength( $file ) {
187 $serMeta = $file->getMetadata();
188 MediaWiki\suppressWarnings();
189 $metadata = unserialize( $serMeta );
190 MediaWiki\restoreWarnings();
192 if ( !$metadata || !isset( $metadata['duration'] ) || !$metadata['duration'] ) {
193 return 0.0;
194 } else {
195 return (float)$metadata['duration'];
199 // PNGs should be easy to support, but it will need some sharpening applied
200 // and another user test to check if the perceived quality change is noticeable
201 public function supportsBucketing() {
202 return false;