Soeren.oldag has uploaded a new change for review. https://gerrit.wikimedia.org/r/205829
Change subject: Fixed serializers for XML output. ...................................................................... Fixed serializers for XML output. Change-Id: Ief8dfc2ad8010697f809e19a590ff76389af6881 --- M api/CrossCheck.php M includes/Serializer/CompareResultSerializer.php M includes/Serializer/CrossCheckResultListSerializer.php A includes/Serializer/IndexedTagsSerializer.php M includes/Serializer/SerializerFactory.php M tests/phpunit/Api/CrossCheckTest.php M tests/phpunit/Serializer/CompareResultSerializerTest.php M tests/phpunit/Serializer/CrossCheckResultListSerializerTest.php A tests/phpunit/Serializer/IndexedTagsSerializerTest.php M tests/phpunit/Serializer/SerializerBaseTest.php M tests/phpunit/Serializer/SerializerFactoryTest.php 11 files changed, 244 insertions(+), 23 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikidataQualityExternalValidation refs/changes/29/205829/1 diff --git a/api/CrossCheck.php b/api/CrossCheck.php index 86cb7e0..aa936fc 100644 --- a/api/CrossCheck.php +++ b/api/CrossCheck.php @@ -177,12 +177,21 @@ * @return array */ private function writeResultOutput( array $resultLists ) { - $serializer = $this->serializerFactory->newCrossCheckResultListSerializer(); + $serializer = $this->serializerFactory->newCrossCheckResultListSerializer( $this->getResult()->getIsRawMode() ); $output = array (); foreach ( $resultLists as $entityId => $resultList ) { if ( $resultList ) { - $output[ $entityId ] = $serializer->serialize( $resultList ); + $serializedResultList = $serializer->serialize( $resultList ); + + if ( $this->getResult()->getIsRawMode() ) { + $output[ ] = array_merge( + array( 'id' => (string)$entityId ), + $serializedResultList + ); + } else { + $output[ (string)$entityId ] = $serializedResultList; + } } else { $output[ $entityId ] = array ( 'missing' => '' @@ -190,6 +199,7 @@ } } + $this->getResult()->setIndexedTagName( $output, 'entity' ); $this->getResult()->addValue( null, 'results', $output ); $this->getResultBuilder()->markSuccess( 1 ); } diff --git a/includes/Serializer/CompareResultSerializer.php b/includes/Serializer/CompareResultSerializer.php index e9ca3f9..c6e8864 100644 --- a/includes/Serializer/CompareResultSerializer.php +++ b/includes/Serializer/CompareResultSerializer.php @@ -17,7 +17,7 @@ * @author BP2014N1 * @license GNU GPL v2+ */ -class CompareResultSerializer implements DispatchableSerializer { +class CompareResultSerializer extends IndexedTagsSerializer implements DispatchableSerializer { /** * @var Serializer @@ -28,7 +28,9 @@ * @param Serializer $dataValueSerializer * @param bool $shouldIndexTags */ - public function __construct( Serializer $dataValueSerializer ) { + public function __construct( Serializer $dataValueSerializer, $shouldIndexTags = false ) { + parent::__construct( $shouldIndexTags ); + $this->dataValueSerializer = $dataValueSerializer; } @@ -70,6 +72,7 @@ }, $compareResult->getExternalValues() ); + $this->setIndexedTagName( $externalValues, 'dataValue' ); return array ( 'localValue' => $this->dataValueSerializer->serialize( $compareResult->getLocalValue() ), diff --git a/includes/Serializer/CrossCheckResultListSerializer.php b/includes/Serializer/CrossCheckResultListSerializer.php index f1f1b6d..fd7bae0 100644 --- a/includes/Serializer/CrossCheckResultListSerializer.php +++ b/includes/Serializer/CrossCheckResultListSerializer.php @@ -15,14 +15,20 @@ * @author BP2014N1 * @license GNU GPL v2+ */ -class CrossCheckResultListSerializer implements DispatchableSerializer { +class CrossCheckResultListSerializer extends IndexedTagsSerializer implements DispatchableSerializer { /** * @var Serializer */ private $crossCheckResultSerializer; - public function __construct( Serializer $crossCheckResultSerializer ) { + /** + * @param Serializer $crossCheckResultSerializer + * @param bool $shouldIndexTags + */ + public function __construct( Serializer $crossCheckResultSerializer, $shouldIndexTags = false ) { + parent::__construct( $shouldIndexTags ); + $this->crossCheckResultSerializer = $crossCheckResultSerializer; } @@ -57,11 +63,23 @@ } private function getSerialized( CrossCheckResultList $resultList ) { - $serialization = array (); - foreach ( $resultList as $result ) { - $serialization[ $result->getPropertyId()->getSerialization() ][ ] = $this->crossCheckResultSerializer->serialize( $result ); - } + $serialization = array(); + foreach ( $resultList->getPropertyIds() as $propertyId ) { + if ( $this->shouldIndexTags() ) { + $index = count( $serialization ); + $serialization[ $index ][ 'id' ] = $propertyId->getSerialization(); + $this->setIndexedTagName( $serialization[ $index ], 'result' ); + } else { + $index = (string)$propertyId; + } - return $serialization; + foreach ( $resultList->getWithPropertyId( $propertyId ) as $result ) { + $serialization[ $index ][ ] = $this->crossCheckResultSerializer->serialize( $result ); + } + } + + $this->setIndexedTagName( $serialization, 'property' ); + + return $serialization; } } \ No newline at end of file diff --git a/includes/Serializer/IndexedTagsSerializer.php b/includes/Serializer/IndexedTagsSerializer.php new file mode 100644 index 0000000..f519270 --- /dev/null +++ b/includes/Serializer/IndexedTagsSerializer.php @@ -0,0 +1,64 @@ +<?php + +namespace WikidataQuality\ExternalValidation\Serializer; + +use ApiResult; +use Doctrine\Instantiator\Exception\InvalidArgumentException; +use Serializers\Serializer; + + +/** + * Class IndexedTagsSerializer + * + * @package WikidataQuality\ExternalValidation\Serializer + * @author BP2014N1 + * @license GNU GPL v2+ + */ +abstract class IndexedTagsSerializer implements Serializer { + + /** + * Determines, if tags should be indexed. + * @var bool + */ + private $shouldIndexTags; + + /** + * @param bool $shouldIndexTags + */ + public function __construct( $shouldIndexTags = false ) { + if ( !is_bool( $shouldIndexTags ) ) { + throw new InvalidArgumentException( '$shouldIndexTags must be boolean.' ); + } + + $this->shouldIndexTags = $shouldIndexTags; + } + + /** + * @see Serializer::serialize + * + * @param mixed $object + * @return array|int|string|bool|float A possibly nested structure consisting of only arrays and scalar values + */ + public abstract function serialize( $object ); + + /** + * @return bool + */ + public function shouldIndexTags() { + return $this->shouldIndexTags; + } + + /** + * In case the array contains indexed values (in addition to named), + * give all indexed values the given tag name. This function MUST be + * called on every array that has numerical indexes. + * + * @param array $arr + * @param string $tag + */ + protected function setIndexedTagName( array &$arr, $tag ) { + if ( $this->shouldIndexTags() ) { + ApiResult::setIndexedTagName( $arr, $tag ); + } + } +} \ No newline at end of file diff --git a/includes/Serializer/SerializerFactory.php b/includes/Serializer/SerializerFactory.php index ca15c4e..6da4237 100644 --- a/includes/Serializer/SerializerFactory.php +++ b/includes/Serializer/SerializerFactory.php @@ -38,10 +38,11 @@ /** * Returns a serializer that can serialize CompareResult objects * + * @param bool $shouldIndexTags * @return Serializer */ - public function newCompareResultSerializer() { - return new CompareResultSerializer( $this->dataValueSerializer ); + public function newCompareResultSerializer( $shouldIndexTags = false ) { + return new CompareResultSerializer( $this->dataValueSerializer, $shouldIndexTags ); } /** @@ -56,12 +57,13 @@ /** * Returns a serializer that can serialize CrossCheckResult objects * + * @param bool $shouldIndexTags * @return Serializer */ - public function newCrossCheckResultSerializer() { + public function newCrossCheckResultSerializer( $shouldIndexTags = false ) { return new CrossCheckResultSerializer( $this->newDumpMetaInformationSerializer(), - $this->newCompareResultSerializer(), + $this->newCompareResultSerializer( $shouldIndexTags ), $this->newReferenceResultSerializer() ); } @@ -69,11 +71,13 @@ /** * Returns a serializer that can serialize CrossCheckResultList objects * + * @param bool $shouldIndexTags * @return Serializer */ - public function newCrossCheckResultListSerializer() { + public function newCrossCheckResultListSerializer( $shouldIndexTags = false ) { return new CrossCheckResultListSerializer( - $this->newCrossCheckResultSerializer() + $this->newCrossCheckResultSerializer( $shouldIndexTags ), + $shouldIndexTags ); } } \ No newline at end of file diff --git a/tests/phpunit/Api/CrossCheckTest.php b/tests/phpunit/Api/CrossCheckTest.php index b7ba9aa..6356b98 100644 --- a/tests/phpunit/Api/CrossCheckTest.php +++ b/tests/phpunit/Api/CrossCheckTest.php @@ -27,6 +27,7 @@ * @uses WikidataQuality\ExternalValidation\CrossCheck\Result\ReferenceResult * @uses WikidataQuality\ExternalValidation\CrossCheck\Result\CrossCheckResult * @uses WikidataQuality\ExternalValidation\CrossCheck\Result\CrossCheckResultList + * @uses WikidataQuality\ExternalValidation\Serializer\IndexedTagsSerializer * @uses WikidataQuality\ExternalValidation\Serializer\CompareResultSerializer * @uses WikidataQuality\ExternalValidation\Serializer\ReferenceResultSerializer * @uses WikidataQuality\ExternalValidation\Serializer\CrossCheckResultSerializer diff --git a/tests/phpunit/Serializer/CompareResultSerializerTest.php b/tests/phpunit/Serializer/CompareResultSerializerTest.php index 853b691..7d97a4c 100644 --- a/tests/phpunit/Serializer/CompareResultSerializerTest.php +++ b/tests/phpunit/Serializer/CompareResultSerializerTest.php @@ -7,6 +7,7 @@ use WikidataQuality\ExternalValidation\Serializer\CompareResultSerializer; /** + * @covers WikidataQuality\ExternalValidation\Serializer\IndexedTagsSerializer * @covers WikidataQuality\ExternalValidation\Serializer\CompareResultSerializer * * @uses WikidataQuality\ExternalValidation\CrossCheck\Result\CompareResult @@ -16,13 +17,13 @@ */ class CompareResultSerializerTest extends SerializerBaseTest { - protected function buildSerializer() { + protected function buildSerializer( $shouldIndexTags = false ) { $serializerMock = $this->getMock( 'Serializers\Serializer' ); $serializerMock->expects( $this->any() ) ->method( 'serialize' ) ->willReturn( 'foobar' ); - return new CompareResultSerializer( $serializerMock ); + return new CompareResultSerializer( $serializerMock, $shouldIndexTags ); } public function serializableProvider() { @@ -85,6 +86,48 @@ ), false ) + ), + array( + array( + 'localValue' => 'foobar', + 'externalValues' => array( + 'foobar', + '_element' => 'dataValue' + ), + 'result' => 'mismatch' + ), + new CompareResult( + $this->getDataValueMock(), + array( + $this->getDataValueMock() + ), + true + ), + array( + 'shouldIndexTags' => true + ) + ), + array( + array( + 'localValue' => 'foobar', + 'externalValues' => array( + 0 => 'foobar', + 1 => 'foobar', + '_element' => 'dataValue' + ), + 'result' => 'match' + ), + new CompareResult( + $this->getDataValueMock(), + array( + $this->getDataValueMock(), + $this->getDataValueMock() + ), + false + ), + array( + 'shouldIndexTags' => true + ) ) ); } diff --git a/tests/phpunit/Serializer/CrossCheckResultListSerializerTest.php b/tests/phpunit/Serializer/CrossCheckResultListSerializerTest.php index 6dfa08a..d89596d 100644 --- a/tests/phpunit/Serializer/CrossCheckResultListSerializerTest.php +++ b/tests/phpunit/Serializer/CrossCheckResultListSerializerTest.php @@ -8,6 +8,7 @@ /** + * @covers WikidataQuality\ExternalValidation\Serializer\IndexedTagsSerializer * @covers WikidataQuality\ExternalValidation\Serializer\CrossCheckResultListSerializer * * @uses WikidataQuality\ExternalValidation\CrossCheck\Result\CrossCheckResultList @@ -17,13 +18,13 @@ */ class CrossCheckResultListSerializerTest extends SerializerBaseTest { - protected function buildSerializer() { + protected function buildSerializer( $shouldIndexTags = false ) { $serializerMock = $this->getMock( 'Serializers\Serializer' ); $serializerMock->expects( $this->any() ) ->method( 'serialize' ) ->willReturn( 'foobar' ); - return new CrossCheckResultListSerializer( $serializerMock ); + return new CrossCheckResultListSerializer( $serializerMock, $shouldIndexTags ); } public function serializableProvider() { @@ -100,6 +101,32 @@ $this->getCrossCheckResultMock( new PropertyId( 'P31' ) ) ) ) + ), + array( + array( + 0 => array( + 0 => 'foobar', + 1 => 'foobar', + 'id' => 'P42', + '_element' => 'result' + ), + 1 => array( + 'foobar', + 'id' => 'P31', + '_element' => 'result' + ), + '_element' => 'property' + ), + new CrossCheckResultList( + array( + $this->getCrossCheckResultMock( new PropertyId( 'P42' ) ), + $this->getCrossCheckResultMock( new PropertyId( 'P42' ) ), + $this->getCrossCheckResultMock( new PropertyId( 'P31' ) ) + ) + ), + array( + 'shouldIndexTags' => true + ) ) ); } diff --git a/tests/phpunit/Serializer/IndexedTagsSerializerTest.php b/tests/phpunit/Serializer/IndexedTagsSerializerTest.php new file mode 100644 index 0000000..1d91c0c --- /dev/null +++ b/tests/phpunit/Serializer/IndexedTagsSerializerTest.php @@ -0,0 +1,48 @@ +<?php + +namespace WikidataQuality\ExternalValidation\Tests\Serializer; + + +/** + * @covers WikidataQuality\ExternalValidation\Serializer\IndexedTagsSerializer + * + * @author BP2014N1 + * @license GNU GPL v2+ + */ +class IndexedTagsSerializerTest extends \MediaWikiTestCase { + + /** + * @dataProvider constructDataProvider + */ + public function testConstruct( $shouldIndexTags, $expectedException = null ) { + if ( $expectedException ) { + $this->setExpectedException( $expectedException ); + } + + $serializer = $this->getMockBuilder( 'WikidataQuality\ExternalValidation\Serializer\IndexedTagsSerializer' ) + ->setConstructorArgs( array( $shouldIndexTags ) ) + ->setMethods( array() ) + ->getMockForAbstractClass(); + + $this->assertEquals( $shouldIndexTags, $serializer->shouldIndexTags() ); + } + + /** + * Test cases for testConstruct + * @return array + */ + public function constructDataProvider() { + return array( + array( + true + ), + array( + false + ), + array( + 42, + 'InvalidArgumentException' + ) + ); + } +} diff --git a/tests/phpunit/Serializer/SerializerBaseTest.php b/tests/phpunit/Serializer/SerializerBaseTest.php index e0e135c..c6a04d9 100644 --- a/tests/phpunit/Serializer/SerializerBaseTest.php +++ b/tests/phpunit/Serializer/SerializerBaseTest.php @@ -69,10 +69,12 @@ /** * @dataProvider serializationProvider */ - public function testSerialization( $serialization, $object ) { + public function testSerialization( $serialization, $object, $serializerParameter = array() ) { + $serializer = call_user_func_array( array( $this, 'buildSerializer' ), $serializerParameter ); + $this->assertEquals( $serialization, - $this->buildSerializer()->serialize( $object ) + $serializer->serialize( $object ) ); } diff --git a/tests/phpunit/Serializer/SerializerFactoryTest.php b/tests/phpunit/Serializer/SerializerFactoryTest.php index 43e72a9..370308f 100644 --- a/tests/phpunit/Serializer/SerializerFactoryTest.php +++ b/tests/phpunit/Serializer/SerializerFactoryTest.php @@ -29,6 +29,7 @@ * @uses WikidataQuality\ExternalValidation\CrossCheck\Result\CrossCheckResult * @uses WikidataQuality\ExternalValidation\CrossCheck\Result\CrossCheckResultList * @uses WikidataQuality\ExternalValidation\DumpMetaInformation + * @uses WikidataQuality\ExternalValidation\Serializer\IndexedTagsSerializer * @uses WikidataQuality\ExternalValidation\Serializer\CompareResultSerializer * @uses WikidataQuality\ExternalValidation\Serializer\ReferenceResultSerializer * @uses WikidataQuality\ExternalValidation\Serializer\CrossCheckResultSerializer -- To view, visit https://gerrit.wikimedia.org/r/205829 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ief8dfc2ad8010697f809e19a590ff76389af6881 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/WikidataQualityExternalValidation Gerrit-Branch: master Gerrit-Owner: Soeren.oldag <soeren_ol...@freenet.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits