MediaWiki REL1_39
ExifBitmapHandler.php
Go to the documentation of this file.
1<?php
2
27
37 public const BROKEN_FILE = '-1';
38
40 public const OLD_BROKEN_FILE = '0';
41
42 public function convertMetadataVersion( $metadata, $version = 1 ) {
43 // basically flattens arrays.
44 $version = is_int( $version ) ? $version : intval( explode( ';', $version, 2 )[0] );
45 if ( $version < 1 || $version >= 2 ) {
46 return $metadata;
47 }
48
49 if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
50 return $metadata;
51 }
52
53 // Treat Software as a special case because in can contain
54 // an array of (SoftwareName, Version).
55 if ( isset( $metadata['Software'] )
56 && is_array( $metadata['Software'] )
57 && is_array( $metadata['Software'][0] )
58 && isset( $metadata['Software'][0][0] )
59 && isset( $metadata['Software'][0][1] )
60 ) {
61 $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
62 . $metadata['Software'][0][1] . ')';
63 }
64
65 $formatter = new FormatMetadata;
66
67 // ContactInfo also has to be dealt with specially
68 if ( isset( $metadata['Contact'] ) ) {
69 $metadata['Contact'] = $formatter->collapseContactInfo(
70 is_array( $metadata['Contact'] ) ? $metadata['Contact'] : [ $metadata['Contact'] ]
71 );
72 }
73
74 // Ignore Location shown if it is not a simple string
75 if ( isset( $metadata['LocationShown'] ) && !is_string( $metadata['LocationShown'] ) ) {
76 unset( $metadata['LocationShown'] );
77 }
78
79 foreach ( $metadata as &$val ) {
80 if ( is_array( $val ) ) {
81 // @phan-suppress-next-line SecurityCheck-DoubleEscaped Ambiguous with the true for nohtml
82 $val = $formatter->flattenArrayReal( $val, 'ul', true );
83 }
84 }
85 $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
86
87 return $metadata;
88 }
89
94 public function isFileMetadataValid( $image ) {
95 $showEXIF = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::ShowEXIF );
96 if ( !$showEXIF ) {
97 # Metadata disabled and so an empty field is expected
99 }
100 $exif = $image->getMetadataArray();
101 if ( !$exif ) {
102 wfDebug( __METHOD__ . ': error unserializing?' );
103 return self::METADATA_BAD;
104 }
105 if ( $exif === [ '_error' => self::OLD_BROKEN_FILE ] ) {
106 # Old special value indicating that there is no Exif data in the file.
107 # or that there was an error well extracting the metadata.
108 wfDebug( __METHOD__ . ": back-compat version" );
110 }
111
112 if ( $exif === [ '_error' => self::BROKEN_FILE ] ) {
113 return self::METADATA_GOOD;
114 }
115
116 if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
117 || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
118 ) {
119 if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
120 && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
121 ) {
122 // back-compatible but old
123 wfDebug( __METHOD__ . ": back-compat version" );
124
126 }
127 # Wrong (non-compatible) version
128 wfDebug( __METHOD__ . ": wrong version" );
129
130 return self::METADATA_BAD;
131 }
132
133 return self::METADATA_GOOD;
134 }
135
141 public function formatMetadata( $image, $context = false ) {
142 $meta = $this->getCommonMetaArray( $image );
143 if ( !$meta ) {
144 return false;
145 }
146
147 return $this->formatMetadataHelper( $meta, $context );
148 }
149
150 public function getCommonMetaArray( File $file ) {
151 $exif = $file->getMetadataArray();
152 if ( !$exif ) {
153 return [];
154 }
155 unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
156
157 return $exif;
158 }
159
160 public function getMetadataType( $image ) {
161 return 'exif';
162 }
163
164 protected function applyExifRotation( $info, $metadata ) {
165 if ( $this->autoRotateEnabled() ) {
166 $rotation = $this->getRotationForExifFromOrientation( $metadata['Orientation'] ?? null );
167 } else {
168 $rotation = 0;
169 }
170
171 if ( $rotation == 90 || $rotation == 270 ) {
172 $width = $info['width'];
173 $info['width'] = $info['height'];
174 $info['height'] = $width;
175 }
176 return $info;
177 }
178
191 public function getRotation( $file ) {
192 if ( !$this->autoRotateEnabled() ) {
193 return 0;
194 }
195
196 $orientation = $file->getMetadataItem( 'Orientation' );
197 return $this->getRotationForExifFromOrientation( $orientation );
198 }
199
208 protected function getRotationForExifFromOrientation( $orientation ) {
209 if ( $orientation === null ) {
210 return 0;
211 }
212 # See http://sylvana.net/jpegcrop/exif_orientation.html
213 switch ( $orientation ) {
214 case 8:
215 return 90;
216 case 3:
217 return 180;
218 case 6:
219 return 270;
220 default:
221 return 0;
222 }
223 }
224}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Generic handler for bitmap images.
Stuff specific to JPEG and (built-in) TIFF handler.
getRotationForExifFromOrientation( $orientation)
Given a chunk of serialized Exif metadata, return the orientation as degrees of rotation.
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
applyExifRotation( $info, $metadata)
const OLD_BROKEN_FILE
Outdated error extracting metadata.
convertMetadataVersion( $metadata, $version=1)
Convert metadata version.
getRotation( $file)
On supporting image formats, try to read out the low-level orientation of the file and return the ang...
const BROKEN_FILE
Error extracting metadata.
formatMetadata( $image, $context=false)
static version()
#-
Definition Exif.php:715
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:67
Format Image metadata values into a human readable form.
collapseContactInfo(array $vals)
Format the contact info field into a single value.
const METADATA_COMPATIBLE
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
const METADATA_GOOD
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42