Debian Codegraph @

includes/media/ExifBitmapHandler.php

raw symbols SCIP
1 <?php
2
3 /**
4 * Handler for bitmap images with exif metadata.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Media
23 */
24
25 use MediaWiki\Context\IContextSource;
26 use MediaWiki\MainConfigNames;
27 use MediaWiki\MediaWikiServices;
28
29 /**
30 * Stuff specific to JPEG and (built-in) TIFF handler.
31 * All metadata related, since both JPEG and TIFF support Exif.
32 *
33 * @stable to extend
34 * @ingroup Media
35 */
36 class ExifBitmapHandler extends BitmapHandler {
37 /** Error extracting metadata */
38 public const BROKEN_FILE = '-1';
39
40 /** Outdated error extracting metadata */
41 public const OLD_BROKEN_FILE = '0';
42
43 public function convertMetadataVersion( $metadata, $version = 1 ) {
44 // basically flattens arrays.
45 $version = is_int( $version ) ? $version : (int)explode( ';', $version, 2 )[0];
46 if ( $version < 1 || $version >= 2 ) {
47 return $metadata;
48 }
49
50 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] !== 2 ) {
51 return $metadata;
52 }
53
54 // Treat Software as a special case because in can contain
55 // an array of (SoftwareName, Version).
56 if ( isset( $metadata['Software'] )
57 && is_array( $metadata['Software'] )
58 && is_array( $metadata['Software'][0] )
59 && isset( $metadata['Software'][0][0] )
60 && isset( $metadata['Software'][0][1] )
61 ) {
62 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
63 . $metadata['Software'][0][1] . ')';
64 }
65
66 $formatter = new FormatMetadata;
67
68 // ContactInfo also has to be dealt with specially
69 if ( isset( $metadata['Contact'] ) ) {
70 $metadata['Contact'] = $formatter->collapseContactInfo(
71 is_array( $metadata['Contact'] ) ? $metadata['Contact'] : [ $metadata['Contact'] ]
72 );
73 }
74
75 // Ignore Location shown/created if they are not simple strings
76 foreach ( [ 'LocationShown', 'LocationCreated' ] as $metadataKey ) {
77 if ( isset( $metadata[ $metadataKey ] ) && !is_string( $metadata[ $metadataKey ] ) ) {
78 unset( $metadata[ $metadataKey ] );
79 }
80 }
81
82 foreach ( $metadata as &$val ) {
83 if ( is_array( $val ) ) {
84 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Ambiguous with the true for nohtml
85 $val = $formatter->flattenArrayReal( $val, 'ul', true );
86 }
87 }
88 unset( $val );
89 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
90
91 return $metadata;
92 }
93
94 /**
95 * @param File $image
96 * @return bool|int
97 */
98 public function isFileMetadataValid( $image ) {
99 $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
100 if ( !$showEXIF ) {
101 # Metadata disabled and so an empty field is expected
102 return self::METADATA_GOOD;
103 }
104 $exif = $image->getMetadataArray();
105 if ( !$exif ) {
106 wfDebug( __METHOD__ . ': error unserializing?' );
107 return self::METADATA_BAD;
108 }
109 if ( $exif === [ '_error' => self::OLD_BROKEN_FILE ] ) {
110 # Old special value indicating that there is no Exif data in the file.
111 # or that there was an error well extracting the metadata.
112 wfDebug( __METHOD__ . ": back-compat version" );
113 return self::METADATA_COMPATIBLE;
114 }
115
116 if ( $exif === [ '_error' => self::BROKEN_FILE ] ) {
117 return self::METADATA_GOOD;
118 }
119
120 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
121 || $exif['MEDIAWIKI_EXIF_VERSION'] !== Exif::version()
122 ) {
123 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
124 && $exif['MEDIAWIKI_EXIF_VERSION'] === 1
125 ) {
126 // back-compatible but old
127 wfDebug( __METHOD__ . ": back-compat version" );
128
129 return self::METADATA_COMPATIBLE;
130 }
131 # Wrong (non-compatible) version
132 wfDebug( __METHOD__ . ": wrong version" );
133
134 return self::METADATA_BAD;
135 }
136
137 return self::METADATA_GOOD;
138 }
139
140 /**
141 * @param File $image
142 * @param IContextSource|false $context
143 * @return array[]|false
144 */
145 public function formatMetadata( $image, $context = false ) {
146 $meta = $this->getCommonMetaArray( $image );
147 if ( !$meta ) {
148 return false;
149 }
150
151 return $this->formatMetadataHelper( $meta, $context );
152 }
153
154 public function getCommonMetaArray( File $file ) {
155 $exif = $file->getMetadataArray();
156 if ( !$exif ) {
157 return [];
158 }
159 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
160
161 return $exif;
162 }
163
164 public function getMetadataType( $image ) {
165 return 'exif';
166 }
167
168 protected function applyExifRotation( $info, $metadata ) {
169 if ( $this->autoRotateEnabled() ) {
170 $rotation = $this->getRotationForExifFromOrientation( $metadata['Orientation'] ?? null );
171 } else {
172 $rotation = 0;
173 }
174
175 if ( $rotation === 90 || $rotation === 270 ) {
176 $width = $info['width'];
177 $info['width'] = $info['height'];
178 $info['height'] = $width;
179 }
180 return $info;
181 }
182
183 /**
184 * On supporting image formats, try to read out the low-level orientation
185 * of the file and return the angle that the file needs to be rotated to
186 * be viewed.
187 *
188 * This information is only useful when manipulating the original file;
189 * the width and height we normally work with is logical, and will match
190 * any produced output views.
191 *
192 * @param File $file
193 * @return int 0, 90, 180 or 270
194 */
195 public function getRotation( $file ) {
196 if ( !$this->autoRotateEnabled() ) {
197 return 0;
198 }
199
200 $orientation = $file->getMetadataItem( 'Orientation' );
201 return $this->getRotationForExifFromOrientation( $orientation );
202 }
203
204 /**
205 * Given a chunk of serialized Exif metadata, return the orientation as
206 * degrees of rotation.
207 *
208 * @param int|null $orientation
209 * @return int 0, 90, 180 or 270
210 * @todo FIXME: Orientation can include flipping as well; see if this is an issue!
211 */
212 protected function getRotationForExifFromOrientation( $orientation ) {
213 if ( $orientation === null ) {
214 return 0;
215 }
216 # See http://sylvana.net/jpegcrop/exif_orientation.html
217 switch ( $orientation ) {
218 case 8:
219 return 90;
220 case 3:
221 return 180;
222 case 6:
223 return 270;
224 default:
225 return 0;
226 }
227 }
228 }