Merge "FormatMetadata::fetchExtendedMetadata: Ignore multiple EXIF/XMP values"
[lhc/web/wiklou.git] / includes / media / FormatMetadata.php
index 4200541..07d7618 100644 (file)
@@ -72,7 +72,7 @@ class FormatMetadata extends ContextSource {
         *
         * This is the usual entry point for this class.
         *
-        * @param array $tags the Exif data to format ( as returned by
+        * @param array $tags The Exif data to format ( as returned by
         *   Exif::getFilteredData() or BitmapMetadataHandler )
         * @param bool|IContextSource $context Context to use (optional)
         * @return array
@@ -92,7 +92,7 @@ class FormatMetadata extends ContextSource {
         * value which most of the time are plain integers. This function
         * formats Exif (and other metadata) values into human readable form.
         *
-        * @param array $tags the Exif data to format ( as returned by
+        * @param array $tags The Exif data to format ( as returned by
         *   Exif::getFilteredData() or BitmapMetadataHandler )
         * @return array
         * @since 1.23
@@ -1006,7 +1006,7 @@ class FormatMetadata extends ContextSource {
        /**
         * Flatten an array, using the user language for any messages.
         *
-        * @param array $vals array of values
+        * @param array $vals Array of values
         * @param string $type Type of array (either lang, ul, ol).
         *   lang = language assoc array with keys being the lang code
         *   ul = unordered list, ol = ordered list
@@ -1031,7 +1031,7 @@ class FormatMetadata extends ContextSource {
         *
         * This is public on the basis it might be useful outside of this class.
         *
-        * @param array $vals array of values
+        * @param array $vals Array of values
         * @param string $type Type of array (either lang, ul, ol).
         *     lang = language assoc array with keys being the lang code
         *     ul = unordered list, ol = ordered list
@@ -1624,6 +1624,7 @@ class FormatMetadata extends ContextSource {
                        if ( $this->singleLang ) {
                                $this->resolveMultilangMetadata( $extendedMetadata );
                        }
+                       $this->discardMultipleValues( $extendedMetadata );
                        // Make sure the metadata won't break the API when an XML format is used.
                        // This is an API-specific function so it would be cleaner to call it from
                        // outside fetchExtendedMetadata, but this way we don't need to redo the
@@ -1706,7 +1707,7 @@ class FormatMetadata extends ContextSource {
         *
         * @param File $file File to use
         * @param array $extendedMetadata
-        * @param int $maxCacheTime hook handlers might use this parameter to override cache time
+        * @param int $maxCacheTime Hook handlers might use this parameter to override cache time
         *
         * @return array [<property name> => ['value' => <value>]], or [] on error
         * @since 1.23
@@ -1741,7 +1742,7 @@ class FormatMetadata extends ContextSource {
         * If the value is not a multilang array, it is returned unchanged.
         * See mediawiki.org/wiki/Manual:File_metadata_handling#Multi-language_array_format
         * @param mixed $value
-        * @return mixed value in best language, null if there were no languages at all
+        * @return mixed Value in best language, null if there were no languages at all
         * @since 1.23
         */
        protected function resolveMultilangValue( $value ) {
@@ -1776,6 +1777,32 @@ class FormatMetadata extends ContextSource {
                return null;
        }
 
+       /**
+        * 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
+        * @param mixed $value
+        * @return mixed The value, or the first value if there were multiple ones
+        * @since 1.25
+        */
+       protected function resolveMultivalueValue( $value ) {
+               if ( !is_array( $value ) ) {
+                       return $value;
+               } elseif ( isset( $value['_type'] ) && $value['_type'] === 'lang' ) { // if this is a multilang array, process fields separately
+                       $newValue = array();
+                       foreach ( $value as $k => $v ) {
+                               $newValue[$k] = $this->resolveMultivalueValue( $v );
+                       }
+                       return $newValue;
+               } else { // _type is 'ul' or 'ol' or missing in which case it defaults to 'ul'
+                       list( $k, $v ) = each( $value );
+                       if ( $k === '_type' ) {
+                               $v = current( $value );
+                       }
+                       return $v;
+               }
+       }
+
        /**
         * Takes an array returned by the getExtendedMetadata* functions,
         * and resolves multi-language values in it.
@@ -1793,6 +1820,29 @@ class FormatMetadata extends ContextSource {
                }
        }
 
+       /**
+        * Takes an array returned by the getExtendedMetadata* functions,
+        * and turns all fields into single-valued ones by dropping extra values.
+        * @param array $metadata
+        * @since 1.25
+        */
+       protected function discardMultipleValues( &$metadata ) {
+               if ( !is_array( $metadata ) ) {
+                       return;
+               }
+               foreach ( $metadata as $key => &$field ) {
+                       if ( $key === 'Software' || $key === 'Contact' ) {
+                               // we skip some fields which have composite values. They are not particularly interesting
+                               // and you can get them via the metadata / commonmetadata APIs anyway.
+                               continue;
+                       }
+                       if ( isset( $field['value'] ) ) {
+                               $field['value'] = $this->resolveMultivalueValue( $field['value'] );
+                       }
+               }
+
+       }
+
        /**
         * Makes sure the given array is a valid API response fragment
         * (can be transformed into XML)