From 440d9b84beef50eaefd209fc371ce315a242c923 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 4 Jul 2019 10:52:55 +0200 Subject: [PATCH] SlotRecord:compute sha1 if empty. The SHA1 should be computed automatically if empty, not just if it is missing entirely. This is needed since rev_sha1 may contain an empty string for old revisions. Bug: T200653 Bug: T219816 Change-Id: Ia6870a828bc9661fb05085e36315a86483ec48c4 --- includes/Revision/SlotRecord.php | 5 +++++ tests/phpunit/includes/Revision/SlotRecordTest.php | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/includes/Revision/SlotRecord.php b/includes/Revision/SlotRecord.php index 064f7a47b6..7465f79000 100644 --- a/includes/Revision/SlotRecord.php +++ b/includes/Revision/SlotRecord.php @@ -539,6 +539,11 @@ class SlotRecord { try { $sha1 = $this->getStringField( 'content_sha1' ); } catch ( IncompleteRevisionException $ex ) { + $sha1 = null; + } + + // Compute if missing. Missing could mean null or empty. + if ( $sha1 === null || $sha1 === '' ) { $format = $this->hasField( 'format_name' ) ? $this->getStringField( 'format_name' ) : null; diff --git a/tests/phpunit/includes/Revision/SlotRecordTest.php b/tests/phpunit/includes/Revision/SlotRecordTest.php index 1b6ff2aace..6495967ff4 100644 --- a/tests/phpunit/includes/Revision/SlotRecordTest.php +++ b/tests/phpunit/includes/Revision/SlotRecordTest.php @@ -77,7 +77,7 @@ class SlotRecordTest extends MediaWikiTestCase { $this->assertFalse( $record->isInherited() ); $this->assertSame( 'A', $record->getContent()->getText() ); $this->assertSame( 1, $record->getSize() ); - $this->assertNotNull( $record->getSha1() ); + $this->assertNotEmpty( $record->getSha1() ); $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() ); $this->assertSame( 2, $record->getRevision() ); $this->assertSame( 2, $record->getRevision() ); @@ -96,7 +96,7 @@ class SlotRecordTest extends MediaWikiTestCase { $this->assertFalse( $record->hasOrigin() ); $this->assertSame( 'A', $record->getContent()->getText() ); $this->assertSame( 1, $record->getSize() ); - $this->assertNotNull( $record->getSha1() ); + $this->assertNotEmpty( $record->getSha1() ); $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() ); $this->assertSame( 'myRole', $record->getRole() ); } @@ -177,6 +177,14 @@ class SlotRecordTest extends MediaWikiTestCase { $this->assertSame( $hash, $record->getSha1() ); } + public function testHashComputed() { + $row = $this->makeRow(); + $row->content_sha1 = ''; + + $rec = new SlotRecord( $row, new WikitextContent( 'A' ) ); + $this->assertNotEmpty( $rec->getSha1() ); + } + public function testNewWithSuppressedContent() { $input = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) ); $output = SlotRecord::newWithSuppressedContent( $input ); -- 2.20.1