Make sure DjVu files do not attempt metadata extraction repeatedly
authorBrian Wolff <bawolff+wn@gmail.com>
Fri, 23 May 2014 00:00:21 +0000 (21:00 -0300)
committerBrian Wolff <bawolff+wn@gmail.com>
Fri, 23 May 2014 00:07:59 +0000 (21:07 -0300)
If a file is broken, we don't want to spend time trying to find
its metadata over and over again.

Bug: 41090
Change-Id: Iad63b8942af99e1ec44530599a43ec1d6b2b8a62

includes/media/DjVu.php
tests/phpunit/includes/media/DjVuTest.php

index 566efb2..200d526 100644 (file)
@@ -278,7 +278,14 @@ class DjVuHandler extends ImageHandler {
                $unser = unserialize( $metadata );
                wfRestoreWarnings();
                if ( is_array( $unser ) ) {
-                       return $unser['xml'];
+                       if ( isset( $unser['error'] ) ) {
+                               return false;
+                       } elseif ( isset( $unser['xml'] ) ) {
+                               return $unser['xml'];
+                       } else {
+                               // Should never ever reach here.
+                               throw new MWException( "Error unserializing DjVu metadata." );
+                       }
                }
 
                // unserialize failed. Guess it wasn't really serialized after all,
@@ -364,7 +371,8 @@ class DjVuHandler extends ImageHandler {
 
                $xml = $this->getDjVuImage( $image, $path )->retrieveMetaData();
                if ( $xml === false ) {
-                       return false;
+                       // Special value so that we don't repetitively try and decode a broken file.
+                       return serialize( array( 'error' => 'Error extracting metadata' ) );
                } else {
                        return serialize( array( 'xml' => $xml ) );
                }
index 537c124..5657a5a 100644 (file)
@@ -64,7 +64,8 @@ class DjVuTest extends MediaWikiTestCase {
        }
 
        public function testInvalidFile() {
-               $this->assertFalse(
+               $this->assertEquals(
+                       'a:1:{s:5:"error";s:25:"Error extracting metadata";}',
                        $this->handler->getMetadata( null, $this->filePath . '/README' ),
                        'Getting Metadata for an inexistent file should returns false'
                );