MediaWiki  1.30.0
ExifBitmap.php
Go to the documentation of this file.
1 <?php
31  const BROKEN_FILE = '-1'; // error extracting metadata
32  const OLD_BROKEN_FILE = '0'; // outdated error extracting metadata.
33 
34  function convertMetadataVersion( $metadata, $version = 1 ) {
35  // basically flattens arrays.
36  $version = intval( explode( ';', $version, 2 )[0] );
37  if ( $version < 1 || $version >= 2 ) {
38  return $metadata;
39  }
40 
41  $avoidHtml = true;
42 
43  if ( !is_array( $metadata ) ) {
44  $metadata = unserialize( $metadata );
45  }
46  if ( !isset( $metadata['MEDIAWIKI_EXIF_VERSION'] ) || $metadata['MEDIAWIKI_EXIF_VERSION'] != 2 ) {
47  return $metadata;
48  }
49 
50  // Treat Software as a special case because in can contain
51  // an array of (SoftwareName, Version).
52  if ( isset( $metadata['Software'] )
53  && is_array( $metadata['Software'] )
54  && is_array( $metadata['Software'][0] )
55  && isset( $metadata['Software'][0][0] )
56  && isset( $metadata['Software'][0][1] )
57  ) {
58  $metadata['Software'] = $metadata['Software'][0][0] . ' (Version '
59  . $metadata['Software'][0][1] . ')';
60  }
61 
62  $formatter = new FormatMetadata;
63 
64  // ContactInfo also has to be dealt with specially
65  if ( isset( $metadata['Contact'] ) ) {
66  $metadata['Contact'] =
67  $formatter->collapseContactInfo(
68  $metadata['Contact'] );
69  }
70 
71  foreach ( $metadata as &$val ) {
72  if ( is_array( $val ) ) {
73  $val = $formatter->flattenArrayReal( $val, 'ul', $avoidHtml );
74  }
75  }
76  $metadata['MEDIAWIKI_EXIF_VERSION'] = 1;
77 
78  return $metadata;
79  }
80 
86  function isMetadataValid( $image, $metadata ) {
88  if ( !$wgShowEXIF ) {
89  # Metadata disabled and so an empty field is expected
90  return self::METADATA_GOOD;
91  }
92  if ( $metadata === self::OLD_BROKEN_FILE ) {
93  # Old special value indicating that there is no Exif data in the file.
94  # or that there was an error well extracting the metadata.
95  wfDebug( __METHOD__ . ": back-compat version\n" );
96 
98  }
99  if ( $metadata === self::BROKEN_FILE ) {
100  return self::METADATA_GOOD;
101  }
102  MediaWiki\suppressWarnings();
103  $exif = unserialize( $metadata );
104  MediaWiki\restoreWarnings();
105  if ( !isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
106  || $exif['MEDIAWIKI_EXIF_VERSION'] != Exif::version()
107  ) {
108  if ( isset( $exif['MEDIAWIKI_EXIF_VERSION'] )
109  && $exif['MEDIAWIKI_EXIF_VERSION'] == 1
110  ) {
111  // back-compatible but old
112  wfDebug( __METHOD__ . ": back-compat version\n" );
113 
115  }
116  # Wrong (non-compatible) version
117  wfDebug( __METHOD__ . ": wrong version\n" );
118 
119  return self::METADATA_BAD;
120  }
121 
122  return self::METADATA_GOOD;
123  }
124 
130  function formatMetadata( $image, $context = false ) {
131  $meta = $this->getCommonMetaArray( $image );
132  if ( count( $meta ) === 0 ) {
133  return false;
134  }
135 
136  return $this->formatMetadataHelper( $meta, $context );
137  }
138 
139  public function getCommonMetaArray( File $file ) {
140  $metadata = $file->getMetadata();
141  if ( $metadata === self::OLD_BROKEN_FILE
142  || $metadata === self::BROKEN_FILE
143  || $this->isMetadataValid( $file, $metadata ) === self::METADATA_BAD
144  ) {
145  // So we don't try and display metadata from PagedTiffHandler
146  // for example when using InstantCommons.
147  return [];
148  }
149 
150  $exif = unserialize( $metadata );
151  if ( !$exif ) {
152  return [];
153  }
154  unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
155 
156  return $exif;
157  }
158 
159  function getMetadataType( $image ) {
160  return 'exif';
161  }
162 
171  function getImageSize( $image, $path ) {
172  $gis = parent::getImageSize( $image, $path );
173 
174  // Don't just call $image->getMetadata(); FSFile::getPropsFromPath() calls us with a bogus object.
175  // This may mean we read EXIF data twice on initial upload.
176  if ( $this->autoRotateEnabled() ) {
177  $meta = $this->getMetadata( $image, $path );
178  $rotation = $this->getRotationForExif( $meta );
179  } else {
180  $rotation = 0;
181  }
182 
183  if ( $rotation == 90 || $rotation == 270 ) {
184  $width = $gis[0];
185  $gis[0] = $gis[1];
186  $gis[1] = $width;
187  }
188 
189  return $gis;
190  }
191 
204  public function getRotation( $file ) {
205  if ( !$this->autoRotateEnabled() ) {
206  return 0;
207  }
208 
209  $data = $file->getMetadata();
210 
211  return $this->getRotationForExif( $data );
212  }
213 
222  protected function getRotationForExif( $data ) {
223  if ( !$data ) {
224  return 0;
225  }
226  MediaWiki\suppressWarnings();
227  $data = unserialize( $data );
228  MediaWiki\restoreWarnings();
229  if ( isset( $data['Orientation'] ) ) {
230  # See http://sylvana.net/jpegcrop/exif_orientation.html
231  switch ( $data['Orientation'] ) {
232  case 8:
233  return 90;
234  case 3:
235  return 180;
236  case 6:
237  return 270;
238  default:
239  return 0;
240  }
241  }
242 
243  return 0;
244  }
245 }
ExifBitmapHandler
Stuff specific to JPEG and (built-in) TIFF handler.
Definition: ExifBitmap.php:30
$context
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2581
ExifBitmapHandler\getImageSize
getImageSize( $image, $path)
Wrapper for base classes ImageHandler::getImageSize() that checks for rotation reported from metadata...
Definition: ExifBitmap.php:171
File\getMetadata
getMetadata()
Get handler-specific metadata Overridden by LocalFile, UnregisteredLocalFile STUB.
Definition: File.php:639
captcha-old.count
count
Definition: captcha-old.py:249
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
ExifBitmapHandler\formatMetadata
formatMetadata( $image, $context=false)
Definition: ExifBitmap.php:130
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ExifBitmapHandler\getCommonMetaArray
getCommonMetaArray(File $file)
Get an array of standard (FormatMetadata type) metadata values.
Definition: ExifBitmap.php:139
MediaHandler\METADATA_COMPATIBLE
const METADATA_COMPATIBLE
Definition: MediaHandler.php:34
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:51
BitmapHandler
Generic handler for bitmap images.
Definition: Bitmap.php:29
Exif\version
static version()
#-
Definition: Exif.php:582
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:1047
FormatMetadata\collapseContactInfo
collapseContactInfo( $vals)
Format the contact info field into a single value.
Definition: FormatMetadata.php:1445
$image
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check $image
Definition: hooks.txt:781
ExifBitmapHandler\getMetadataType
getMetadataType( $image)
Get a string describing the type of metadata, for display purposes.
Definition: ExifBitmap.php:159
MediaHandler\getMetadata
getMetadata( $image, $path)
Get handler-specific metadata which will be saved in the img_metadata field.
Definition: MediaHandler.php:122
BitmapHandler\autoRotateEnabled
autoRotateEnabled()
Definition: Bitmap.php:528
ExifBitmapHandler\convertMetadataVersion
convertMetadataVersion( $metadata, $version=1)
Convert metadata version.
Definition: ExifBitmap.php:34
FormatMetadata
Format Image metadata values into a human readable form.
Definition: FormatMetadata.php:51
ExifBitmapHandler\getRotation
getRotation( $file)
On supporting image formats, try to read out the low-level orientation of the file and return the ang...
Definition: ExifBitmap.php:204
ExifBitmapHandler\OLD_BROKEN_FILE
const OLD_BROKEN_FILE
Definition: ExifBitmap.php:32
MediaHandler\formatMetadataHelper
formatMetadataHelper( $metadataArray, $context=false)
sorts the visible/invisible field.
Definition: MediaHandler.php:504
$path
$path
Definition: NoLocalSettings.php:26
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MediaHandler\METADATA_BAD
const METADATA_BAD
Definition: MediaHandler.php:33
ExifBitmapHandler\isMetadataValid
isMetadataValid( $image, $metadata)
Definition: ExifBitmap.php:86
ExifBitmapHandler\BROKEN_FILE
const BROKEN_FILE
Definition: ExifBitmap.php:31
ExifBitmapHandler\getRotationForExif
getRotationForExif( $data)
Given a chunk of serialized Exif metadata, return the orientation as degrees of rotation.
Definition: ExifBitmap.php:222
$wgShowEXIF
$wgShowEXIF
Show Exif data, on by default if available.
Definition: DefaultSettings.php:669
MediaHandler\METADATA_GOOD
const METADATA_GOOD
Definition: MediaHandler.php:32