MediaWiki master
ExifBitmapHandler.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Media;
4
17
27 public const BROKEN_FILE = '-1';
28
30 public const OLD_BROKEN_FILE = '0';
31
33 public function convertMetadataVersion( $metadata, $version = 1 ) {
34 // basically flattens arrays.
35 $version = is_int( $version ) ? $version : (int)explode( ';', $version, 2 )[0];
36 if ( $version < 1 || $version >= 2 ) {
37 return $metadata;
38 }
39
40 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] !== 2 ) {
41 return $metadata;
42 }
43
44 // Treat Software as a special case because in can contain
45 // an array of (SoftwareName, Version).
46 if ( isset( $metadata['Software'] )
47 && is_array( $metadata['Software'] )
48 && is_array( $metadata['Software'][0] )
49 && isset( $metadata['Software'][0][0] )
50 && isset( $metadata['Software'][0][1] )
51 ) {
52 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
53 . $metadata['Software'][0][1] . ')';
54 }
55
56 $formatter = new FormatMetadata;
57
58 // ContactInfo also has to be dealt with specially
59 if ( isset( $metadata['Contact'] ) ) {
60 $metadata['Contact'] = $formatter->collapseContactInfo(
61 is_array( $metadata['Contact'] ) ? $metadata['Contact'] : [ $metadata['Contact'] ]
62 );
63 }
64
65 // Ignore Location shown/created if they are not simple strings
66 foreach ( [ 'LocationShown', 'LocationCreated' ] as $metadataKey ) {
67 if ( isset( $metadata[ $metadataKey ] ) && !is_string( $metadata[ $metadataKey ] ) ) {
68 unset( $metadata[ $metadataKey ] );
69 }
70 }
71
72 foreach ( $metadata as &$val ) {
73 if ( is_array( $val ) ) {
74 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Ambiguous with the true for nohtml
75 $val = $formatter->flattenArrayReal( $val, 'ul', true );
76 }
77 }
78 unset( $val );
79 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
80
81 return $metadata;
82 }
83
88 public function isFileMetadataValid( $image ) {
89 $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
90 if ( !$showEXIF ) {
91 # Metadata disabled and so an empty field is expected
93 }
94 $exif = $image->getMetadataArray();
95 if ( !$exif ) {
96 wfDebug( __METHOD__ . ': error unserializing?' );
97 return self::METADATA_BAD;
98 }
99 if ( $exif === [ '_error' => self::OLD_BROKEN_FILE ] ) {
100 # Old special value indicating that there is no Exif data in the file.
101 # or that there was an error well extracting the metadata.
102 wfDebug( __METHOD__ . ": back-compat version" );
104 }
105
106 if ( $exif === [ '_error' => self::BROKEN_FILE ] ) {
107 return self::METADATA_GOOD;
108 }
109
110 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
111 || $exif['MEDIAWIKI_EXIF_VERSION'] !== Exif::version()
112 ) {
113 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
114 && $exif['MEDIAWIKI_EXIF_VERSION'] === 1
115 ) {
116 // back-compatible but old
117 wfDebug( __METHOD__ . ": back-compat version" );
118
120 }
121 # Wrong (non-compatible) version
122 wfDebug( __METHOD__ . ": wrong version" );
123
124 return self::METADATA_BAD;
125 }
126
127 return self::METADATA_GOOD;
128 }
129
135 public function formatMetadata( $image, $context = false ) {
136 $meta = $this->getCommonMetaArray( $image );
137 if ( !$meta ) {
138 return false;
139 }
140
141 return $this->formatMetadataHelper( $meta, $context );
142 }
143
145 public function getCommonMetaArray( File $file ) {
146 $exif = $file->getMetadataArray();
147 if ( !$exif ) {
148 return [];
149 }
150 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
151
152 return $exif;
153 }
154
156 public function getMetadataType( $image ) {
157 return 'exif';
158 }
159
165 protected function applyExifRotation( $info, $metadata ) {
166 if ( $this->autoRotateEnabled() ) {
167 $rotation = $this->getRotationForExifFromOrientation( $metadata['Orientation'] ?? null );
168 } else {
169 $rotation = 0;
170 }
171
172 if ( $rotation === 90 || $rotation === 270 ) {
173 $width = $info['width'];
174 $info['width'] = $info['height'];
175 $info['height'] = $width;
176 }
177 return $info;
178 }
179
192 public function getRotation( $file ) {
193 if ( !$this->autoRotateEnabled() ) {
194 return 0;
195 }
196
197 $orientation = $file->getMetadataItem( 'Orientation' );
198 return $this->getRotationForExifFromOrientation( $orientation );
199 }
200
209 protected function getRotationForExifFromOrientation( $orientation ) {
210 if ( $orientation === null ) {
211 return 0;
212 }
213 # See http://sylvana.net/jpegcrop/exif_orientation.html
214 switch ( $orientation ) {
215 case 8:
216 return 90;
217 case 3:
218 return 180;
219 case 6:
220 return 270;
221 default:
222 return 0;
223 }
224 }
225}
226
228class_alias( ExifBitmapHandler::class, 'ExifBitmapHandler' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:79
getMetadataArray()
Get the unserialized handler-specific metadata STUB.
Definition File.php:828
A class containing constants representing the names of configuration variables.
const ShowEXIF
Name constant for the ShowEXIF setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Generic handler for bitmap images.
Stuff specific to JPEG and (built-in) TIFF handler.
getRotation( $file)
On supporting image formats, try to read out the low-level orientation of the file and return the ang...
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.The returned data is largely the same ...
convertMetadataVersion( $metadata, $version=1)
Convert metadata version.By default just returns $metadata, but can be used to allow media handlers t...
const OLD_BROKEN_FILE
Outdated error extracting metadata.
const BROKEN_FILE
Error extracting metadata.
formatMetadata( $image, $context=false)
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.to overrideThis method is currentl...
getRotationForExifFromOrientation( $orientation)
Given a chunk of serialized Exif metadata, return the orientation as degrees of rotation.
static version()
The version of the output format.
Definition Exif.php:691
Format Image metadata values into a human readable form.
collapseContactInfo(array $vals)
Format the contact info field into a single value.
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Interface for objects which can provide a MediaWiki context on request.