Aude has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/155517

Change subject: Handle MWContentSerializationException in JsonDumpGenerator
......................................................................

Handle MWContentSerializationException in JsonDumpGenerator

This can happen if there is a corrupt entity in the database,
but should not cause the entire dump to fail.  The entity
can be skipped with an error message, as done for other error
situations.

Bug: 69846
Change-Id: I541fb68cd8697c8e9e6617c4cc8ca556a3e03238
---
M repo/includes/Dumpers/JsonDumpGenerator.php
M repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php
2 files changed, 53 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/17/155517/1

diff --git a/repo/includes/Dumpers/JsonDumpGenerator.php 
b/repo/includes/Dumpers/JsonDumpGenerator.php
index 07b0f4e..6694be4 100644
--- a/repo/includes/Dumpers/JsonDumpGenerator.php
+++ b/repo/includes/Dumpers/JsonDumpGenerator.php
@@ -3,6 +3,7 @@
 namespace Wikibase\Dumpers;
 
 use InvalidArgumentException;
+use MWContentSerializationException;
 use MWException;
 use Wikibase\DataModel\Entity\EntityId;
 use Wikibase\EntityIdPager;
@@ -235,10 +236,15 @@
                        }
 
                        try {
-                               $entity = $this->entityLookup->getEntity( 
$entityId );
+                               try {
+                                       $entity = 
$this->entityLookup->getEntity( $entityId );
 
-                               if ( !$entity ) {
-                                       throw new StorageException( 'Entity not 
found: ' . $entityId->getSerialization() );
+                                       if ( !$entity ) {
+                                               throw new StorageException( 
'Entity not found: ' . $entityId->getSerialization() );
+                                       }
+                               } catch( MWContentSerializationException $ex ) {
+                                       throw new StorageException( 
'Deserialization error for '
+                                               . $entityId->getSerialization() 
);
                                }
 
                                $data = $this->entitySerializer->getSerialized( 
$entity );
diff --git a/repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php 
b/repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php
index 1e6efcc..3797569 100644
--- a/repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php
+++ b/repo/tests/phpunit/includes/Dumpers/JsonDumpGeneratorTest.php
@@ -2,6 +2,7 @@
 
 namespace Wikibase\Test\Dumpers;
 
+use MWContentSerializationException;
 use Wikibase\DataModel\Entity\BasicEntityIdParser;
 use Wikibase\DataModel\Entity\Entity;
 use Wikibase\DataModel\Entity\EntityId;
@@ -172,6 +173,49 @@
                $this->testTypeFilterDump( $ids, null, $ids );
        }
 
+       /**
+        * @dataProvider idProvider
+        */
+       public function 
testGenerateDump_HandlesMWContentSerializationException( array $ids ) {
+               $jsonDumper = $this->getJsonDumperWithExceptionHandler( $ids );
+               $pager = $this->makeIdPager( $ids );
+
+               ob_start();
+               $jsonDumper->generateDump( $pager );
+               $json = ob_get_clean();
+
+               $data = json_decode( $json, true );
+               $this->assertEquals( array(), $data );
+       }
+
+       private function getJsonDumperWithExceptionHandler( array $ids ) {
+               $entityLookup = 
$this->getEntityLookupThrowsMWContentSerializationException();
+               $out = fopen( 'php://output', 'w' );
+               $serializer = new DispatchingEntitySerializer( 
$this->serializerFactory );
+
+               $jsonDumper = new JsonDumpGenerator( $out, $entityLookup, 
$serializer );
+
+               $exceptionHandler = $this->getMock( 
'Wikibase\Lib\Reporting\ExceptionHandler' );
+               $exceptionHandler->expects( $this->exactly( count( $ids ) ) )
+                       ->method( 'handleException' );
+
+               $jsonDumper->setExceptionHandler( $exceptionHandler );
+
+               return $jsonDumper;
+       }
+
+       private function getEntityLookupThrowsMWContentSerializationException() 
{
+               $entityLookup = $this->getMock( 
'Wikibase\Lib\Store\EntityLookup' );
+               $entityLookup->expects( $this->any() )
+                       ->method( 'getEntity' )
+                       ->will( $this->returnCallback( function ( EntityId $id 
) {
+                                       throw new 
MWContentSerializationException( 'cannot deserialize!' );
+                               }
+                       ) );
+
+               return $entityLookup;
+       }
+
        public static function idProvider() {
                $p10 = new PropertyId( 'P10' );
                $q30 = new ItemId( 'Q30' );

-- 
To view, visit https://gerrit.wikimedia.org/r/155517
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I541fb68cd8697c8e9e6617c4cc8ca556a3e03238
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to