Make DjVu metadata be stored as serialized PHP array.
authorBrian Wolff <bawolff+wn@gmail.com>
Thu, 5 Dec 2013 23:50:17 +0000 (19:50 -0400)
committerBryanDavis <bdavis@wikimedia.org>
Fri, 11 Apr 2014 21:50:08 +0000 (21:50 +0000)
Previously metadata was stored as a string of XML. Some of the
code in File expects the metadata to be an array serialized using
PHP's serialization format. In particular, this fixes the api
query=imageinfo module, and by extension instantCommons.

This supersedes Icaa16eeb

Bug: 37764
Change-Id: I5c1d2d2434f70b57137837bade797d4133c47b70

includes/media/DjVu.php

index 1202c9a..3bc5636 100644 (file)
@@ -230,6 +230,30 @@ class DjVuHandler extends ImageHandler {
                return $deja;
        }
 
+       /**
+        * Get metadata, unserializing it if neccessary.
+        *
+        * @param File $file The DjVu file in question
+        * @return String XML metadata as a string.
+        */
+       private function getUnserializedMetadata( File $file ) {
+               $metadata = $file->getMetadata();
+               if ( substr( $metadata, 0, 3 ) === '<?xml' ) {
+                       // Old style. Not serialized but instead just a raw string of XML.
+                       return $metadata;
+               }
+
+               wfSuppressWarnings();
+               $unser = unserialize( $metadata );
+               wfRestoreWarnings();
+               if ( is_array( $unser ) ) {
+                       return $unser['xml'];
+               }
+
+               // unserialize failed. Guess it wasn't really serialized after all,
+               return $metadata;
+       }
+
        /**
         * Cache a document tree for the DjVu XML metadata
         * @param File $image
@@ -244,7 +268,7 @@ class DjVuHandler extends ImageHandler {
                        return $image->dejaMetaTree;
                }
 
-               $metadata = $image->getMetadata();
+               $metadata = $this->getUnserializedMetadata( $image );
                if ( !$this->isMetadataValid( $image, $metadata ) ) {
                        wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
 
@@ -307,7 +331,12 @@ class DjVuHandler extends ImageHandler {
        function getMetadata( $image, $path ) {
                wfDebug( "Getting DjVu metadata for $path\n" );
 
-               return $this->getDjVuImage( $image, $path )->retrieveMetaData();
+               $xml = $this->getDjVuImage( $image, $path )->retrieveMetaData();
+               if ( $xml === false ) {
+                       return false;
+               } else {
+                       return serialize( array( 'xml' => $xml ) );
+               }
        }
 
        function getMetadataType( $image ) {