/** * @param Entity $entity * @param string|null $expectedEntityType * @param EntityId|null $expectedEntityId * * @return EntityHolder */ private function newHolder(Entity $entity, $expectedEntityType = null, EntityId $expectedEntityId = null) { $wikibaseRepo = WikibaseRepo::getDefaultInstance(); $codec = new EntityContentDataCodec(new BasicEntityIdParser(), $wikibaseRepo->getInternalEntitySerializer(), $wikibaseRepo->getInternalEntityDeserializer()); $blob = $codec->encodeEntity($entity, CONTENT_FORMAT_JSON); return new DeferredDecodingEntityHolder($codec, $blob, CONTENT_FORMAT_JSON, $expectedEntityType ?: $entity->getType(), $expectedEntityId); }
/** * @see EntityHolder::getEntityId * * This implements lazy deserialization of the blob passed to the constructor. * * @param string $expectedClass The class with which the result is expected to be compatible. * Defaults to EntityDocument. * * @throws RuntimeException If the entity held by this EntityHolder is not compatible with $expectedClass. * @return EntityDocument */ public function getEntity($expectedClass = 'Wikibase\\DataModel\\Entity\\EntityDocument') { if (!$this->entity) { $this->entity = $this->codec->decodeEntity($this->blob, $this->contentFormat); if (!$this->entity instanceof EntityDocument) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected EntityDocument, got ' . gettype($this->entity)); } elseif ($this->entity->getType() !== $this->entityType) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $this->entityType . ', got ' . $this->entity->getType()); } elseif ($this->entity->getId() === null) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected an entity id to be set, got null'); } elseif ($this->entityId && !$this->entity->getId()->equals($this->entityId)) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $this->entityId . ', got ' . $this->entity->getId()); } } if (!$this->entity instanceof $expectedClass) { throw new RuntimeException('Deferred decoding resulted in an incompatible entity, ' . 'expected ' . $expectedClass . ', got ' . get_class($this->entity)); } return $this->entity; }
/** * @see ContentHandler::unserializeContent * * @param string $blob * @param string|null $format * * @throws MWContentSerializationException * @return EntityContent */ public function unserializeContent($blob, $format = null) { $redirect = $this->contentCodec->decodeRedirect($blob, $format); if ($redirect !== null) { return $this->makeEntityRedirectContent($redirect); } else { $holder = new DeferredDecodingEntityHolder($this->contentCodec, $blob, $format, $this->getEntityType()); $entityContent = $this->makeEntityContent($holder); return $entityContent; } }
/** * @see ContentHandler::unserializeContent * * @param string $blob * @param string|null $format * * @throws MWContentSerializationException * @return EntityContent */ public function unserializeContent($blob, $format = null) { $redirect = $this->contentCodec->decodeRedirect($blob, $format); if ($redirect) { if ($redirect === null) { throw new MWContentSerializationException('The serialized data contains neither an Entity nor an EntityRedirect!'); } return $this->makeEntityRedirectContent($redirect); } else { $holder = new DeferredDecodingEntityHolder($this->contentCodec, $blob, $format, $this->getEntityType()); $entityContent = $this->makeEntityContent($holder); return $entityContent; } }
/** * Construct an EntityRevision object from a database row from the revision and text tables. * * @see loadEntityBlob() * * @param object $row a row object as expected Revision::getRevisionText(). That is, it * should contain the relevant fields from the revision and/or text table. * * @throws MWContentSerializationException * @return object[] list( EntityRevision|null $entityRevision, EntityRedirect|null $entityRedirect ) * with either $entityRevision or $entityRedirect or both being null (but not both being non-null). */ private function loadEntity($row) { $blob = $this->loadEntityBlob($row); $entity = $this->contentCodec->decodeEntity($blob, $row->rev_content_format); if ($entity) { $entityRevision = new EntityRevision($entity, (int) $row->rev_id, $row->rev_timestamp); $result = array($entityRevision, null); } else { $redirect = $this->contentCodec->decodeRedirect($blob, $row->rev_content_format); if (!$redirect) { throw new MWContentSerializationException('The serialized data contains neither an Entity nor an EntityRedirect!'); } $result = array(null, $redirect); } return $result; }