jenkins-bot has submitted this change and it was merged.
Change subject: Move code from abstract DiffChange to EntityChange
......................................................................
Move code from abstract DiffChange to EntityChange
I think *all* code in DiffChange can be moved, but I did not wanted to
make this patch to big. I tried hard to minimize the diff.
Change-Id: I4776e2a8f45f59c4ee40d010b29478ce627dc0e0
---
M lib/includes/Changes/DiffChange.php
M lib/includes/Changes/EntityChange.php
M lib/tests/phpunit/Changes/EntityChangeTest.php
3 files changed, 74 insertions(+), 124 deletions(-)
Approvals:
Daniel Kinzler: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/includes/Changes/DiffChange.php
b/lib/includes/Changes/DiffChange.php
index 2837c22..4e62a5b 100644
--- a/lib/includes/Changes/DiffChange.php
+++ b/lib/includes/Changes/DiffChange.php
@@ -3,8 +3,6 @@
namespace Wikibase;
use Diff\DiffOp\Diff\Diff;
-use Diff\DiffOp\DiffOp;
-use Wikibase\DataModel\Services\Diff\EntityTypeAwareDiffOpFactory;
/**
* Class for changes that can be represented as a Diff.
@@ -46,80 +44,6 @@
$info = $this->hasField( 'info' ) ? $this->getField( 'info' ) :
array();
$info['diff'] = $diff;
$this->setField( 'info', $info );
- }
-
- /**
- * @see ChangeRow::serializeInfo()
- *
- * Overwritten to use the array representation of the diff.
- *
- * @since 0.4
- * @param array $info
- * @return string
- */
- public function serializeInfo( array $info ) {
- if ( isset( $info['diff'] ) && $info['diff'] instanceof DiffOp
) {
- /** @var DiffOp $op */
- $op = $info['diff'];
- $info['diff'] = $op->toArray( array( $this,
'arrayalizeObjects' ) );
- }
-
- return parent::serializeInfo( $info );
- }
-
- /**
- * @see ChangeRow::unserializeInfo()
- *
- * Overwritten to use the array representation of the diff.
- *
- * @since 0.4
- * @param string $str
- * @return array the info array
- */
- public function unserializeInfo( $str ) {
- static $factory = null;
-
- if ( $factory == null ) {
- $factory = new EntityTypeAwareDiffOpFactory( array(
$this, 'objectifyArrays' ) );
- }
-
- $info = parent::unserializeInfo( $str );
-
- if ( isset( $info['diff'] ) && is_array( $info['diff'] ) ) {
- $info['diff'] = $factory->newFromArray( $info['diff'] );
- }
-
- return $info;
- }
-
- /**
- * Converts an object to an array structure.
- * Callback function for use by \Diff\DiffOp::toArray().
- *
- * Subclasses should override this to provide array representations of
specific value objects.
- *
- * @since 0.4
- *
- * @param mixed $data
- * @return mixed
- */
- public function arrayalizeObjects( $data ) {
- return $data; // noop
- }
-
- /**
- * May be overwritten by subclasses to provide special handling.
- * Callback function for use by \Diff\DiffOpFactory
- *
- * Subclasses should override this to reconstruct value objects from
arrays.
- *
- * @since 0.4
- *
- * @param array $data
- * @return mixed
- */
- public function objectifyArrays( array $data ) {
- return $data; // noop
}
}
diff --git a/lib/includes/Changes/EntityChange.php
b/lib/includes/Changes/EntityChange.php
index f62bcdb..85fa678 100644
--- a/lib/includes/Changes/EntityChange.php
+++ b/lib/includes/Changes/EntityChange.php
@@ -2,14 +2,19 @@
namespace Wikibase;
+use Deserializers\Deserializer;
+use Diff\DiffOp\DiffOp;
+use Diff\DiffOpFactory;
use MWException;
use RecentChange;
use Revision;
use RuntimeException;
+use Serializers\Serializer;
use User;
use Wikibase\Client\WikibaseClient;
use Wikibase\DataModel\Entity\EntityId;
use Wikibase\DataModel\Entity\BasicEntityIdParser;
+use Wikibase\DataModel\Services\Diff\EntityTypeAwareDiffOpFactory;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\Repo\WikibaseRepo;
@@ -265,28 +270,38 @@
}
/**
- * @see DiffChange::arrayalizeObjects
+ * @see ChangeRow::serializeInfo
*
- * Overwritten to handle Statement objects.
+ * Overwritten to use the array representation of the diff.
*
- * @since 0.4
- *
- * @param mixed $data
- * @return mixed
+ * @param array $info
+ * @return string
*/
- public function arrayalizeObjects( $data ) {
- $data = parent::arrayalizeObjects( $data );
+ public function serializeInfo( array $info ) {
+ if ( isset( $info['diff'] ) ) {
+ $diff = $info['diff'];
- if ( $data instanceof Statement ) {
- $array = $this->getStatementSerializer()->serialize(
$data );
- $array['_claimclass_'] = get_class( $data );
+ if ( $diff instanceof DiffOp ) {
+ $info['diff'] = $diff->toArray( function (
$data ) {
+ if ( !( $data instanceof Statement ) ) {
+ return $data;
+ }
- return $array;
+ $array =
$this->getStatementSerializer()->serialize( $data );
+ $array['_claimclass_'] = get_class(
$data );
+
+ return $array;
+ } );
+ }
}
- return $data;
+ return parent::serializeInfo( $info );
}
+ /**
+ * @throws RuntimeException
+ * @return Serializer
+ */
private function getStatementSerializer() {
// FIXME: the change row system needs to be reworked to either
allow for sane injection
// or to avoid this kind of configuration dependent tasks.
@@ -299,6 +314,10 @@
}
}
+ /**
+ * @throws RuntimeException
+ * @return Deserializer
+ */
private function getStatementDeserializer() {
// FIXME: the change row system needs to be reworked to either
allow for sane injection
// or to avoid this kind of configuration dependent tasks.
@@ -312,31 +331,47 @@
}
/**
- * @see DiffChange::objectifyArrays
+ * @see ChangeRow::unserializeInfo
*
- * Overwritten to handle Statement objects.
+ * Overwritten to use the array representation of the diff.
*
- * @since 0.4
- *
- * @param array $data
- * @return mixed
+ * @param string $serialization
+ * @return array the info array
*/
- public function objectifyArrays( array $data ) {
- $data = parent::objectifyArrays( $data );
+ public function unserializeInfo( $serialization ) {
+ static $factory = null;
- if ( is_array( $data ) && isset( $data['_claimclass_'] ) ) {
- $class = $data['_claimclass_'];
+ $info = parent::unserializeInfo( $serialization );
- if ( $class === Statement::class
- || is_subclass_of( $class, Statement::class )
- ) {
- unset( $data['_claimclass_'] );
-
- return
$this->getStatementDeserializer()->deserialize( $data );
+ if ( isset( $info['diff'] ) && is_array( $info['diff'] ) ) {
+ if ( $factory === null ) {
+ $factory = $this->newDiffOpFactory();
}
+
+ $info['diff'] = $factory->newFromArray( $info['diff'] );
}
- return $data;
+ return $info;
+ }
+
+ /**
+ * @return DiffOpFactory
+ */
+ private function newDiffOpFactory() {
+ return new EntityTypeAwareDiffOpFactory( function ( array $data
) {
+ if ( is_array( $data ) && isset( $data['_claimclass_']
) ) {
+ $class = $data['_claimclass_'];
+
+ if ( $class === Statement::class
+ || is_subclass_of( $class,
Statement::class )
+ ) {
+ unset( $data['_claimclass_'] );
+ return
$this->getStatementDeserializer()->deserialize( $data );
+ }
+ }
+
+ return $data;
+ } );
}
}
diff --git a/lib/tests/phpunit/Changes/EntityChangeTest.php
b/lib/tests/phpunit/Changes/EntityChangeTest.php
index 3c6a5d0..4a1e300 100644
--- a/lib/tests/phpunit/Changes/EntityChangeTest.php
+++ b/lib/tests/phpunit/Changes/EntityChangeTest.php
@@ -2,7 +2,6 @@
namespace Wikibase\Lib\Tests\Changes;
-use Diff\DiffOp\Diff\Diff;
use Diff\DiffOp\DiffOpAdd;
use RecentChange;
use Revision;
@@ -302,8 +301,9 @@
$this->assertEquals( $info, $change->unserializeInfo(
$change->serializeInfo( $info ) ) );
}
- public function
testGivenStatement_arrayalizeObjectsReturnsSerialization() {
+ public function testGivenStatement_serializeInfoSerializesStatement() {
$statement = new Statement( new PropertyNoValueSnak( 1 ) );
+ $info = array( 'diff' => new DiffOpAdd( $statement ) );
$expected = array(
'mainsnak' => array(
'snaktype' => 'novalue',
@@ -321,17 +321,12 @@
$this->setExpectedException( RuntimeException::class );
}
- $array = $change->arrayalizeObjects( $statement );
- $this->assertSame( $expected, $array );
+ $json = $change->serializeInfo( $info );
+ $array = json_decode( $json, true );
+ $this->assertSame( $expected, $array['diff']['newvalue'] );
}
- public function
testGivenNonStatement_arrayalizeObjectsReturnsOriginal() {
- $data = 'foo';
- $change = new EntityChange();
- $this->assertSame( $data, $change->arrayalizeObjects( $data ) );
- }
-
- public function
testGivenStatementSerialization_objectifyArraysReturnsStatement() {
+ public function
testGivenStatementSerialization_unserializeInfoDeserializesStatement() {
$data = array(
'mainsnak' => array(
'snaktype' => 'novalue',
@@ -340,16 +335,12 @@
'type' => 'statement',
'_claimclass_' => Statement::class,
);
+ $json = json_encode( array( 'diff' => array( 'type' => 'add',
'newvalue' => $data ) ) );
$change = new EntityChange();
- $statement = $change->objectifyArrays( $data );
+ $info = $change->unserializeInfo( $json );
+ $statement = $info['diff']->getNewValue();
$this->assertInstanceOf( Statement::class, $statement );
- }
-
- public function
testGivenNonStatementSerialization_objectifyArraysReturnsOriginal() {
- $data = array( 'foo' );
- $change = new EntityChange();
- $this->assertSame( $data, $change->objectifyArrays( $data ) );
}
}
--
To view, visit https://gerrit.wikimedia.org/r/286611
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I4776e2a8f45f59c4ee40d010b29478ce627dc0e0
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
Gerrit-Reviewer: Adrian Heine <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits