Skip to content

Commit

Permalink
Don't use numeric IDs for internal PHP serialization.
Browse files Browse the repository at this point in the history
NOTE: serialize/unserialize is used mainly when cloning Statements.
NOTE: it's unclear if this completely fixes T157442

TODO: proper unit tests, including tests for b/c mode

Bug: T157442
  • Loading branch information
daniel committed Feb 8, 2017
1 parent 05bfe15 commit db964fb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
12 changes: 8 additions & 4 deletions src/Entity/EntityIdValue.php
Expand Up @@ -31,8 +31,8 @@ public function __construct( EntityId $entityId ) {
*/
public function serialize() {
return json_encode( [
$this->entityId->getEntityType(),
$this->getNumericId()
get_class( $this->entityId ),
$this->entityId->getSerialization()
] );
}

Expand All @@ -58,10 +58,14 @@ private function getNumericId() {
* @throws IllegalValueException
*/
public function unserialize( $serialized ) {
list( $entityType, $numericId ) = json_decode( $serialized );
list( $type, $id ) = json_decode( $serialized );

try {
$entityId = LegacyIdInterpreter::newIdFromTypeAndNumber( $entityType, $numericId );
if ( is_string( $id ) ) {
$entityId = new $type( $id );
} else {
$entityId = self::newIdFromTypeAndNumber( $type, $id );
}
} catch ( InvalidArgumentException $ex ) {
throw new IllegalValueException( 'Invalid EntityIdValue serialization.' );
}
Expand Down
6 changes: 3 additions & 3 deletions src/Snak/PropertyValueSnak.php
Expand Up @@ -53,7 +53,7 @@ public function getDataValue() {
* @return string
*/
public function serialize() {
return serialize( [ $this->propertyId->getNumericId(), $this->dataValue ] );
return serialize( [ $this->propertyId->getSerialization(), $this->dataValue ] );
}

/**
Expand All @@ -64,8 +64,8 @@ public function serialize() {
* @param string $serialized
*/
public function unserialize( $serialized ) {
list( $numericId, $dataValue ) = unserialize( $serialized );
$this->__construct( $numericId, $dataValue );
list( $propertyId, $dataValue ) = unserialize( $serialized );
$this->__construct( self::newPropertyId( $propertyId ), $dataValue );
}

/**
Expand Down
21 changes: 19 additions & 2 deletions src/Snak/SnakObject.php
Expand Up @@ -102,7 +102,7 @@ public function equals( $target ) {
* @return string
*/
public function serialize() {
return serialize( $this->propertyId->getNumericId() );
return serialize( $this->propertyId->getSerialization() );
}

/**
Expand All @@ -113,7 +113,24 @@ public function serialize() {
* @param string $serialized
*/
public function unserialize( $serialized ) {
$this->propertyId = PropertyId::newFromNumber( unserialize( $serialized ) );
$this->propertyId = self::newPropertyId( unserialize( $serialized ) );
}

/**
* @param mixed $unserialized
*
* @return PropertyId
*/
protected static function newPropertyId( $unserialized ) {
if ( is_int( $unserialized ) ) {
return PropertyId::newFromNumber( $unserialized );
} elseif ( is_string( $unserialized ) ) {
return new PropertyId( $unserialized );
} elseif ( $unserialized instanceof PropertyId ) {
return $unserialized;
} else {
throw new InvalidArgumentException( 'unexpected property ID serialization' );
}
}

}

0 comments on commit db964fb

Please sign in to comment.