MediaWiki  master
FormatMetadata Class Reference

Format Image metadata values into a human readable form. More...

Inheritance diagram for FormatMetadata:
Collaboration diagram for FormatMetadata:

Public Member Functions

 collapseContactInfo ( $vals)
 Format the contact info field into a single value. More...
 
 fetchExtendedMetadata (File $file)
 Get an array of extended metadata. More...
 
 flattenArrayReal ( $vals, $type='ul', $noHtml=false)
 A function to collapse multivalued tags into a single value. More...
 
 makeFormattedData ( $tags)
 Numbers given by Exif user agents are often magical, that is they should be replaced by a detailed explanation depending on their value which most of the time are plain integers. More...
 
 setSingleLanguage ( $val)
 Trigger only outputting single language for multilanguage fields. More...
 
- Public Member Functions inherited from ContextSource
 canUseWikiPage ()
 Check whether a WikiPage object can be get with getWikiPage(). More...
 
 exportSession ()
 Export the resolved user IP, HTTP headers, user ID, and session ID. More...
 
 getConfig ()
 
 getContext ()
 Get the base IContextSource object. More...
 
 getLanguage ()
 
 getOutput ()
 
 getRequest ()
 
 getSkin ()
 
 getStats ()
 
 getTiming ()
 
 getTitle ()
 
 getUser ()
 
 getWikiPage ()
 Get the WikiPage object. More...
 
 msg ( $key)
 Get a Message object with context set Parameters are the same as wfMessage() More...
 
 setContext (IContextSource $context)
 

Static Public Member Functions

static flattenArrayContentLang ( $vals, $type='ul', $noHtml=false, $context=false)
 Flatten an array, using the content language for any messages. More...
 
static getFormattedData ( $tags, $context=false)
 Numbers given by Exif user agents are often magical, that is they should be replaced by a detailed explanation depending on their value which most of the time are plain integers. More...
 
static getVisibleFields ()
 Get a list of fields that are visible by default. More...
 

Protected Member Functions

 discardMultipleValues (&$metadata)
 Takes an array returned by the getExtendedMetadata* functions, and turns all fields into single-valued ones by dropping extra values. More...
 
 getExtendedMetadataFromFile (File $file)
 Get file-based metadata in standardized format. More...
 
 getExtendedMetadataFromHook (File $file, array $extendedMetadata, &$maxCacheTime)
 Get additional metadata from hooks in standardized format. More...
 
 getPriorityLanguages ()
 Returns a list of languages (first is best) to use when formatting multilang fields, based on user and site preferences. More...
 
 resolveMultilangMetadata (&$metadata)
 Takes an array returned by the getExtendedMetadata* functions, and resolves multi-language values in it. More...
 
 resolveMultilangValue ( $value)
 Turns an XMP-style multilang array into a single value. More...
 
 resolveMultivalueValue ( $value)
 Turns an XMP-style multivalue array into a single value by dropping all but the first value. More...
 
 sanitizeArrayForAPI (&$arr)
 Makes sure the given array is a valid API response fragment. More...
 
 sanitizeKeyForAPI ( $key)
 Turns a string into a valid API identifier. More...
 

Protected Attributes

bool $singleLang = false
 Only output a single language for multi-language fields. More...
 

Private Member Functions

 convertNewsCode ( $val)
 Fetch the human readable version of a news code. More...
 
 exifMsg ( $tag, $val, $arg=null, $arg2=null)
 Convenience function for getFormattedData() More...
 
 formatCoords ( $coord, $type)
 Format a coordinate value, convert numbers from floating point into degree minute second representation. More...
 
 formatFraction ( $num)
 Format a rational number, reducing fractions. More...
 
 formatNum ( $num, $round=false)
 Format a number, convert numbers from fractions into floating point numbers, joins arrays of numbers with commas. More...
 
 gcd ( $a, $b)
 Calculate the greatest common divisor of two integers. More...
 
 langItem ( $value, $lang, $default=false, $noHtml=false)
 Helper function for creating lists of translations. More...
 

Detailed Description

Format Image metadata values into a human readable form.

Note lots of these messages use the prefix 'exif' even though they may not be exif properties. For example 'exif-ImageDescription' can be the Exif ImageDescription, or it could be the iptc-iim caption property, or it could be the xmp dc:description property. This is because these messages should be independent of how the data is stored, sine the user doesn't care if the description is stored in xmp, exif, etc only that its a description. (Additionally many of these properties are merged together following the MWG standard, such that for example, exif properties override XMP properties that mean the same thing if there is a conflict).

It should perhaps use a prefix like 'metadata' instead, but there is already a large number of messages using the 'exif' prefix.

Since
1.23 the class extends ContextSource and various formerly-public internal methods are private

Definition at line 51 of file FormatMetadata.php.

Member Function Documentation

◆ collapseContactInfo()

FormatMetadata::collapseContactInfo (   $vals)

Format the contact info field into a single value.

This function might be called from JpegHandler::convertMetadataVersion which is why it is public.

Parameters
array$valsArray with fields of the ContactInfo struct defined in the IPTC4XMP spec. Or potentially an array with one element that is a free form text value from the older iptc iim 1:118 prop.
Returns
string HTML-ish looking wikitext
Since
1.23 no longer static

Definition at line 1447 of file FormatMetadata.php.

References as, flattenArrayReal(), and ContextSource\msg().

Referenced by ExifBitmapHandler\convertMetadataVersion(), and makeFormattedData().

◆ convertNewsCode()

FormatMetadata::convertNewsCode (   $val)
private

Fetch the human readable version of a news code.

A news code is an 8 digit code. The first two digits are a general classification, so we just translate that.

Note, leading 0's are significant, so this is a string, not an int.

Parameters
string$valThe 8 digit news code.
Returns
string The human readable form

Definition at line 1329 of file FormatMetadata.php.

References exifMsg().

Referenced by makeFormattedData().

◆ discardMultipleValues()

FormatMetadata::discardMultipleValues ( $metadata)
protected

Takes an array returned by the getExtendedMetadata* functions, and turns all fields into single-valued ones by dropping extra values.

Parameters
array&$metadata
Since
1.25

Definition at line 1797 of file FormatMetadata.php.

References as, and resolveMultivalueValue().

Referenced by fetchExtendedMetadata().

◆ exifMsg()

FormatMetadata::exifMsg (   $tag,
  $val,
  $arg = null,
  $arg2 = null 
)
private

Convenience function for getFormattedData()

Parameters
string$tagThe tag name to pass on
string$valThe value of the tag
string$argAn argument to pass ($1)
string$arg2A 2nd argument to pass ($2)
Returns
string The text content of "exif-$tag-$val" message in lower case

Definition at line 1219 of file FormatMetadata.php.

References ContextSource\msg().

Referenced by convertNewsCode(), and makeFormattedData().

◆ fetchExtendedMetadata()

FormatMetadata::fetchExtendedMetadata ( File  $file)

Get an array of extended metadata.

(See the imageinfo API for format.)

Parameters
File$fileFile to use
Returns
array [<property name>=""> => ['value' =>

]], or [] on error

Since
1.23

Definition at line 1586 of file FormatMetadata.php.

References $cache, File\DELETED_FILE, discardMultipleValues(), getExtendedMetadataFromFile(), getExtendedMetadataFromHook(), ContextSource\getLanguage(), File\getSha1(), File\isDeleted(), resolveMultilangMetadata(), Hooks\run(), sanitizeArrayForAPI(), and wfTimestampNow().

◆ flattenArrayContentLang()

static FormatMetadata::flattenArrayContentLang (   $vals,
  $type = 'ul',
  $noHtml = false,
  $context = false 
)
static

Flatten an array, using the content language for any messages.

Parameters
array$valsArray of values
string$typeType of array (either lang, ul, ol). lang = language assoc array with keys being the lang code ul = unordered list, ol = ordered list type can also come from the '_type' member of $vals.
bool$noHtmlIf to avoid returning anything resembling HTML. (Ugly hack for backwards compatibility with old MediaWiki).
bool | IContextSource$context
Returns
string Single value (in wiki-syntax).
Since
1.23

Definition at line 1003 of file FormatMetadata.php.

References ContextSource\$context, $type, and ContextSource\setContext().

◆ flattenArrayReal()

FormatMetadata::flattenArrayReal (   $vals,
  $type = 'ul',
  $noHtml = false 
)

A function to collapse multivalued tags into a single value.

This turns an array of (for example) authors into a bulleted list.

This is public on the basis it might be useful outside of this class.

Parameters
array$valsArray of values
string$typeType of array (either lang, ul, ol). lang = language assoc array with keys being the lang code ul = unordered list, ol = ordered list type can also come from the '_type' member of $vals.
bool$noHtmlIf to avoid returning anything resembling HTML. (Ugly hack for backwards compatibility with old mediawiki).
Returns
string Single value (in wiki-syntax).
Since
1.23

Definition at line 1033 of file FormatMetadata.php.

References $content, $lang, $type, as, getPriorityLanguages(), langItem(), Html\rawElement(), and wfDebug().

Referenced by collapseContactInfo(), and makeFormattedData().

◆ formatCoords()

FormatMetadata::formatCoords (   $coord,
  $type 
)
private

Format a coordinate value, convert numbers from floating point into degree minute second representation.

Parameters
int$coordDegrees, minutes and seconds
string$typeLatitude or longitude (for if its a NWS or E)
Returns
mixed A floating point number or whatever we were fed

Definition at line 1404 of file FormatMetadata.php.

References $type, formatNum(), and ContextSource\msg().

Referenced by makeFormattedData().

◆ formatFraction()

FormatMetadata::formatFraction (   $num)
private

Format a rational number, reducing fractions.

Parameters
mixed$numThe value to format
Returns
mixed A floating point number or whatever we were fed

Definition at line 1275 of file FormatMetadata.php.

References formatNum(), and gcd().

Referenced by makeFormattedData().

◆ formatNum()

FormatMetadata::formatNum (   $num,
  $round = false 
)
private

Format a number, convert numbers from fractions into floating point numbers, joins arrays of numbers with commas.

Parameters
mixed$numThe value to format
float | int | bool$roundDigits to round to or false.
Returns
mixed A floating point number or whatever we were fed

Definition at line 1239 of file FormatMetadata.php.

References $out, as, and ContextSource\getLanguage().

Referenced by formatCoords(), formatFraction(), and makeFormattedData().

◆ gcd()

FormatMetadata::gcd (   $a,
  $b 
)
private

Calculate the greatest common divisor of two integers.

Parameters
int$aNumerator
int$bDenominator
Returns
int

Definition at line 1297 of file FormatMetadata.php.

Referenced by formatFraction().

◆ getExtendedMetadataFromFile()

FormatMetadata::getExtendedMetadataFromFile ( File  $file)
protected

Get file-based metadata in standardized format.

Note that for a remote file, this might return metadata supplied by extensions.

Parameters
File$fileFile to use
Returns
array [<property name>=""> => ['value' =>

]], or [] on error

Since
1.23

Definition at line 1636 of file FormatMetadata.php.

References $name, $title, File\getTimestamp(), File\getTitle(), and wfTimestamp().

Referenced by fetchExtendedMetadata().

◆ getExtendedMetadataFromHook()

FormatMetadata::getExtendedMetadataFromHook ( File  $file,
array  $extendedMetadata,
$maxCacheTime 
)
protected

Get additional metadata from hooks in standardized format.

Parameters
File$fileFile to use
array$extendedMetadata
int&$maxCacheTimeHook handlers might use this parameter to override cache time
Returns
array [<property name>=""> => ['value' =>

]], or [] on error

Since
1.23

Definition at line 1685 of file FormatMetadata.php.

References $value, as, ContextSource\getContext(), and Hooks\run().

Referenced by fetchExtendedMetadata().

◆ getFormattedData()

static FormatMetadata::getFormattedData (   $tags,
  $context = false 
)
static

Numbers given by Exif user agents are often magical, that is they should be replaced by a detailed explanation depending on their value which most of the time are plain integers.

This function formats Exif (and other metadata) values into human readable form.

This is the usual entry point for this class.

Parameters
array$tagsThe Exif data to format ( as returned by Exif::getFilteredData() or BitmapMetadataHandler )
bool | IContextSource$contextContext to use (optional)
Returns
array

Definition at line 82 of file FormatMetadata.php.

References ContextSource\$context, and ContextSource\setContext().

Referenced by MediaHandler\formatMetadataHelper(), and FormatMetadataTest\testGetFormattedData().

◆ getPriorityLanguages()

FormatMetadata::getPriorityLanguages ( )
protected

Returns a list of languages (first is best) to use when formatting multilang fields, based on user and site preferences.

Returns
array
Since
1.23

Definition at line 1881 of file FormatMetadata.php.

References Language\getFallbacksIncludingSiteLanguage(), and ContextSource\getLanguage().

Referenced by flattenArrayReal(), and resolveMultilangValue().

◆ getVisibleFields()

static FormatMetadata::getVisibleFields ( )
static

Get a list of fields that are visible by default.

Returns
array
Since
1.23

Definition at line 1565 of file FormatMetadata.php.

References $line, $lines, $matches, as, text, and wfMessage().

Referenced by MediaHandler\visibleMetadataFields().

◆ langItem()

FormatMetadata::langItem (   $value,
  $lang,
  $default = false,
  $noHtml = false 
)
private

Helper function for creating lists of translations.

Parameters
string$valueValue (this is not escaped)
string$langLang code of item or false
bool$defaultIf it is default value.
bool$noHtmlIf to avoid html (for back-compat)
Exceptions
MWException
Returns
string Language item (Note: despite how this looks, this is treated as wikitext, not as HTML).

Definition at line 1156 of file FormatMetadata.php.

References $lang, $value, Language\fetchLanguageName(), and ContextSource\msg().

Referenced by flattenArrayReal().

◆ makeFormattedData()

FormatMetadata::makeFormattedData (   $tags)

Numbers given by Exif user agents are often magical, that is they should be replaced by a detailed explanation depending on their value which most of the time are plain integers.

This function formats Exif (and other metadata) values into human readable form.

Parameters
array$tagsThe Exif data to format ( as returned by Exif::getFilteredData() or BitmapMetadataHandler )
Returns
array
Since
1.23

Definition at line 102 of file FormatMetadata.php.

References $e, $lang, $s, $time, $type, as, collapseContactInfo(), convertNewsCode(), exifMsg(), Language\fetchLanguageName(), flattenArrayReal(), formatCoords(), formatFraction(), formatNum(), ContextSource\getLanguage(), list, ContextSource\msg(), and wfTimestamp().

◆ resolveMultilangMetadata()

FormatMetadata::resolveMultilangMetadata ( $metadata)
protected

Takes an array returned by the getExtendedMetadata* functions, and resolves multi-language values in it.

Parameters
array&$metadata
Since
1.23

Definition at line 1780 of file FormatMetadata.php.

References as, and resolveMultilangValue().

Referenced by fetchExtendedMetadata().

◆ resolveMultilangValue()

FormatMetadata::resolveMultilangValue (   $value)
protected

Turns an XMP-style multilang array into a single value.

If the value is not a multilang array, it is returned unchanged. See mediawiki.org/wiki/Manual:File_metadata_handling::Multi-language_array_format

Parameters
mixed$value
Returns
mixed Value in best language, null if there were no languages at all
Since
1.23

Definition at line 1714 of file FormatMetadata.php.

References $lang, $value, as, getPriorityLanguages(), and null.

Referenced by resolveMultilangMetadata().

◆ resolveMultivalueValue()

FormatMetadata::resolveMultivalueValue (   $value)
protected

Turns an XMP-style multivalue array into a single value by dropping all but the first value.

If the value is not a multivalue array (or a multivalue array inside a multilang array), it is returned unchanged. See mediawiki.org/wiki/Manual:File_metadata_handling::Multi-language_array_format

Parameters
mixed$value
Returns
mixed The value, or the first value if there were multiple ones
Since
1.25

Definition at line 1755 of file FormatMetadata.php.

References $value, as, and key.

Referenced by discardMultipleValues().

◆ sanitizeArrayForAPI()

FormatMetadata::sanitizeArrayForAPI ( $arr)
protected

Makes sure the given array is a valid API response fragment.

Parameters
array&$arr

Definition at line 1817 of file FormatMetadata.php.

References $keys, $value, as, sanitizeKeyForAPI(), and ApiResult\setPreserveKeysList().

Referenced by fetchExtendedMetadata().

◆ sanitizeKeyForAPI()

FormatMetadata::sanitizeKeyForAPI (   $key)
protected

Turns a string into a valid API identifier.

Parameters
string$key
Returns
string
Since
1.23

Definition at line 1855 of file FormatMetadata.php.

Referenced by sanitizeArrayForAPI().

◆ setSingleLanguage()

FormatMetadata::setSingleLanguage (   $val)

Trigger only outputting single language for multilanguage fields.

Parameters
bool$val
Since
1.23

Definition at line 65 of file FormatMetadata.php.

Referenced by ApiQueryImageInfo\getInfo().

Member Data Documentation

◆ $singleLang

bool FormatMetadata::$singleLang = false
protected

Only output a single language for multi-language fields.

Since
1.23

Definition at line 57 of file FormatMetadata.php.


The documentation for this class was generated from the following file: