(bug 29471) Exception thrown for files with invalid date in metadata
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Sun, 19 Jun 2011 21:38:58 +0000 (21:38 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Sun, 19 Jun 2011 21:38:58 +0000 (21:38 +0000)
MediaWiki doesn't handle BC dates nicely, so check for that.

RELEASE-NOTES-1.19
includes/media/FormatMetadata.php
tests/phpunit/includes/media/FormatMetadataTest.php [new file with mode: 0644]
tests/phpunit/includes/media/broken_exif_date.jpg [new file with mode: 0644]

index 935f5fe..a66aed0 100644 (file)
@@ -107,6 +107,7 @@ production.
 * Do not try to group together a page creation and edit in the RSS feed of RC.
 * (bug 29342) Patrol preferences shouldn't be visible to users who don't have
   patrol permissions
+* (bug 29471) Exception thrown for files with invalid date in metadata
 
 === API changes in 1.19 ===
 * BREAKING CHANGE: action=watch now requires POST and token.
index e830901..47fc1ad 100644 (file)
@@ -104,7 +104,7 @@ class FormatMetadata {
 
                                $time = wfTimestamp( TS_MW, '1971:01:01 ' . $tags[$tag] );
                                // the 1971:01:01 is just a placeholder, and not shown to user.
-                               if ( $time ) {
+                               if ( $time && intval( $time ) > 0 ) {
                                        $tags[$tag] = $wgLang->time( $time );
                                }
                                continue;
@@ -234,7 +234,7 @@ class FormatMetadata {
                                                $val = wfMsg( 'exif-unknowndate' );
                                        } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
                                                $time = wfTimestamp( TS_MW, $val );
-                                               if ( $time ) {
+                                               if ( $time && intval( $time ) > 0 ) {
                                                        $val = $wgLang->timeanddate( $time );
                                                }
                                        } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
@@ -243,7 +243,7 @@ class FormatMetadata {
                                                        . substr( $val, 5, 2 )
                                                        . substr( $val, 8, 2 )
                                                        . '000000' );
-                                               if ( $time ) {
+                                               if ( $time && intval( $time ) > 0 ) {
                                                        $val = $wgLang->date( $time );
                                                }
                                        }
diff --git a/tests/phpunit/includes/media/FormatMetadataTest.php b/tests/phpunit/includes/media/FormatMetadataTest.php
new file mode 100644 (file)
index 0000000..98c8013
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+class FormatMetadataTest extends MediaWikiTestCase {
+       public function testInvalidDate() {
+               $file = UnregisteredLocalFile::newFromPath( dirname( __FILE__ ) . 
+                       '/broken_exif_date.jpg', 'image/jpeg' );
+               
+               // Throws an error if bug hit
+               $meta = $file->formatMetadata();
+               
+               // Find date exif entry
+               $this->assertArrayHasKey( 'visible', $meta );
+               $dateIndex = null;
+               foreach ( $meta['visible'] as $i => $data ) {
+                       if ( $data['id'] == 'exif-datetimeoriginal' ) {
+                               $dateIndex = $i;
+                       }
+               }
+               $this->assertNotNull( $dateIndex, 'Date entry exists in metadata' );
+               $this->assertEquals( '0000:01:00 00:02:27', 
+                       $meta['visible'][$dateIndex]['value'],
+                       'File with invalid date metadata (bug 29471)' );
+       }
+}
\ No newline at end of file
diff --git a/tests/phpunit/includes/media/broken_exif_date.jpg b/tests/phpunit/includes/media/broken_exif_date.jpg
new file mode 100644 (file)
index 0000000..82f62f5
Binary files /dev/null and b/tests/phpunit/includes/media/broken_exif_date.jpg differ