/** * @param string $guid * * @return Statement */ public function getFromGuid($guid) { $params = array('claim' => $guid); $result = $this->api->getRequest(new SimpleRequest('wbgetclaims', $params)); $statementSerialization = array_shift(array_shift($result['claims'])); return $this->statementDeserializer->deserialize($statementSerialization); }
/** * @see Iterator::next */ public function next() { $this->currentEntity = null; $isEntity = false; $text = ''; while (true) { $line = fgets($this->fileStream); if ($line === false) { return; } if (preg_match('/<model>wikibase-[a-z]+<\\/model>/', $line)) { $isEntity = true; } elseif (preg_match('/<text.*>(.*)<\\/text>/', $line, $m)) { $text = $m[1]; } elseif (preg_match('/<\\/page>/', $line, $m) && $isEntity) { $json = json_decode(html_entity_decode($text), true); if ($json === null) { $this->logger->error('The serialization is not a valid JSON document.'); continue; } if (array_key_exists('redirect', $json)) { $this->logger->info('Entity redirection not supported.'); continue; } try { $this->currentEntity = $this->entityDeserializer->deserialize($json); return; } catch (DeserializationException $e) { $id = array_key_exists('id', $json) ? $json['id'] : ''; $this->logger->error('Deserialization of entity ' . $id . ' failed: ' . $e->getMessage()); } } } }
/** * @param array $serialization * * @return ReferenceList */ private function getDeserialized(array $serialization) { $referenceList = new ReferenceList(); foreach ($serialization as $referenceSerialization) { $referenceList->addReference($this->referenceDeserializer->deserialize($referenceSerialization)); } return $referenceList; }
/** * @param array $serialization * * @return TermList */ private function getDeserialized($serialization) { $termList = new TermList(); foreach ($serialization as $termSerialization) { $termList->setTerm($this->termDeserializer->deserialize($termSerialization)); } return $termList; }
private function deserializeItemId($serialization) { $itemId = $this->entityIdDeserializer->deserialize($serialization); if (!$itemId instanceof ItemId) { throw new InvalidAttributeException('badges', $serialization, "'{$serialization}' is not a valid item ID"); } return $itemId; }
/** * @param array $entityResult * @returns Revision * @todo this could be factored into a different class? */ private function newRevisionFromResult(array $entityResult) { if (array_key_exists('missing', $entityResult)) { return false; //Throw an exception? } return new Revision($this->getContentFromEntity($this->entityDeserializer->deserialize($entityResult)), new PageIdentifier(null, intval($entityResult['pageid'])), $entityResult['lastrevid'], null, null, $entityResult['modified']); }
/** * @param array $serialization * * @return SnakList */ private function deserializeSnaks(array $serialization) { $snaks = $this->snaksDeserializer->deserialize($serialization['snaks']); if (array_key_exists('snaks-order', $serialization)) { $this->assertSnaksOrderIsArray($serialization); $snaks->orderByProperty($serialization['snaks-order']); } return $snaks; }
/** * @param array[] $serialization * * @return StatementList */ private function getDeserialized(array $serialization) { $statementList = new StatementList(); foreach ($serialization as $key => $statementArray) { if (is_string($key)) { foreach ($statementArray as $statementSerialization) { $statementList->addStatement($this->statementDeserializer->deserialize($statementSerialization)); } } else { $statementList->addStatement($this->statementDeserializer->deserialize($statementArray)); } } return $statementList; }
private function setReferencesFromSerialization(array &$serialization, Statement $statement) { if (!array_key_exists('references', $serialization)) { return; } $statement->setReferences($this->referencesDeserializer->deserialize($serialization['references'])); }
/** * @param array $serialization * * @return StatementList|null */ private function deserializeStatements(array $serialization) { if (array_key_exists('statements', $serialization)) { return $this->statementListDeserializer->deserialize($serialization['statements']); } return null; }
/** * @param array $document * @return EntityDocument|null */ public function buildEntityForDocument(array $document) { try { return $this->entityDeserializer->deserialize($document); } catch (DeserializationException $exception) { return null; } }
private function setStatementListFromSerialization(array $serialization, Property $property) { if (!array_key_exists('claims', $serialization)) { return; } $statements = $this->statementListDeserializer->deserialize($serialization['claims']); $property->setStatements($statements); }
private function parseResponse(array $response) { $entities = array(); foreach ($response['entities'] as $serializedEntity) { $entities[] = $this->deserializer->deserialize($serializedEntity); } return $entities; }
private function deserializePropertyId($serialization) { $propertyId = $this->entityIdDeserializer->deserialize($serialization); if (!$propertyId instanceof PropertyId) { throw new InvalidAttributeException('property', $serialization, "'{$serialization}' is not a valid property ID"); } return $propertyId; }
private function setSiteLinksFromSerialization(SiteLinkList $siteLinkList, array $serialization) { if (!array_key_exists('sitelinks', $serialization)) { return; } $this->assertAttributeIsArray($serialization, 'sitelinks'); foreach ($serialization['sitelinks'] as $siteLinksSerialization) { $siteLinkList->addSiteLink($this->siteLinkDeserializer->deserialize($siteLinksSerialization)); } }
/** * @since 0.7 * * @param string|string[] $inputValues one or more * @param string $parser Id of the ValueParser to use * * @returns Promise of a DataValue object or array of DataValue objects with same keys as values */ public function parseAsync($inputValues, $parser) { $promise = $this->api->getRequestAsync(new SimpleRequest('wbparsevalue', array('parser' => $parser, 'values' => implode('|', $inputValues)))); return $promise->then(function ($result) use($inputValues) { if (is_array($inputValues)) { $indexedResults = array(); foreach ($result['results'] as $resultElement) { if (in_array($resultElement['raw'], $inputValues)) { $indexedResults[array_search($resultElement['raw'], $inputValues)] = $this->dataValueDeserializer->deserialize($resultElement); } else { throw new RuntimeException("Failed to match parse results with input data"); } } return $indexedResults; } else { return $this->dataValueDeserializer->deserialize($result['results'][0]); } }); }
/** * @since 0.2 * * @param Snak $mainSnak * @param EntityId|Item|Property|string $target * @param EditInfo|null $editInfo * * @return string the GUID of the claim * @throws UnexpectedValueException */ public function create(Snak $mainSnak, $target, EditInfo $editInfo = null) { if (is_string($target)) { $entityId = $target; } elseif ($target instanceof EntityId) { $entityId = $target->getSerialization(); } elseif ($target instanceof Item || $target instanceof Property) { $entityId = $target->getId()->getSerialization(); } else { throw new UnexpectedValueException('$target needs to be an EntityId, Entity or string'); } $params = array('entity' => $entityId, 'snaktype' => $mainSnak->getType(), 'property' => $mainSnak->getPropertyId()->getSerialization()); if ($mainSnak instanceof PropertyValueSnak) { $serializedDataValue = $this->dataValueSerializer->serialize($mainSnak->getDataValue()); $params['value'] = json_encode($serializedDataValue['value']); } $result = $this->api->postRequest('wbcreateclaim', $params, $editInfo); return $result['claims']['id']; }
/** * @param array $entitiesResult * @returns Revisions * @todo this could be factored into a different class? */ private function newRevisionsFromResult(array $entitiesResult) { $revisions = new Revisions(); foreach ($entitiesResult as $entityResult) { if (array_key_exists('missing', $entityResult)) { continue; } $revisions->addRevision(new Revision($this->getContentFromEntity($this->entityDeserializer->deserialize($entityResult)), new PageIdentifier(null, $entityResult['pageid']), $entityResult['lastrevid'], null, null, $entityResult['modified'])); } return $revisions; }
/** * @param array $data * @param EntityId|string|null $id * * @return object */ public function unserializeEntity(array $data, $id = null) { if ($id !== null) { if (is_string($id)) { $id = $this->idParser->parse($id); } $data['id'] = $id->getSerialization(); $data['type'] = $id->getEntityType(); } $entity = $this->deserializer->deserialize($data); return $entity; }
/** * @see Iterator::next */ public function next() { $this->currentEntity = null; while (true) { $line = fgets($this->fileStream); if ($line === false) { return; } $line = trim($line, ", \n\t\r"); if ($line !== '' && $line[0] === '{') { $json = json_decode($line, true); try { $this->currentEntity = $this->entityDeserializer->deserialize($json); return; } catch (DeserializationException $e) { $id = array_key_exists('id', $json) ? $json['id'] : ''; $this->logger->error('Deserialization of entity ' . $id . ' failed: ' . $e->getMessage()); } } } }
/** * @param array[] $statements array of serialized statements * * @return ChangeOp[] */ private function getModifyStatementChangeOps(array $statements) { $opsToReturn = array(); foreach ($statements as $statementArray) { if (!array_key_exists('remove', $statementArray)) { try { $statement = $this->statementDeserializer->deserialize($statementArray); if (!$statement instanceof Statement) { throw new IllegalValueException('Statement serialization did not contained a Statement.'); } $opsToReturn[] = $this->statementChangeOpFactory->newSetStatementOp($statement); } catch (IllegalValueException $ex) { $this->errorReporter->dieException($ex, 'invalid-claim'); } catch (MWException $ex) { $this->errorReporter->dieException($ex, 'invalid-claim'); } } } return $opsToReturn; }
/** * @param array $params * * @throws IllegalValueException * @throws UsageException * @throws LogicException * @return Statement */ private function getClaimFromParams(array $params) { try { $serializedStatement = json_decode($params['claim'], true); if (!is_array($serializedStatement)) { throw new IllegalValueException('Failed to get statement from Serialization'); } $claim = $this->statementDeserializer->deserialize($serializedStatement); if (!$claim instanceof Statement) { throw new IllegalValueException('Failed to get statement from Serialization'); } return $claim; } catch (InvalidArgumentException $invalidArgumentException) { $this->errorReporter->dieError('Failed to get claim from claim Serialization ' . $invalidArgumentException->getMessage(), 'invalid-claim'); } catch (OutOfBoundsException $outOfBoundsException) { $this->errorReporter->dieError('Failed to get claim from claim Serialization ' . $outOfBoundsException->getMessage(), 'invalid-claim'); } // Note: since dieUsage() never returns, this should be unreachable! throw new LogicException('ApiErrorReporter::dieError did not throw an exception'); }
/** * Decodes a blob loaded from storage into an Entity. * * @see EntityHandler::unserializeContent() * * @param string $blob * @param string|null $format The serialization format of $blob. * * @throws InvalidArgumentException If the format is not supported. * @throws MWContentSerializationException * @return EntityDocument|null The entity represented by $blob, or null if $blob represents a * redirect. */ public function decodeEntity($blob, $format) { if ($this->maxBlobSize > 0 && strlen($blob) > $this->maxBlobSize) { throw new MWContentSerializationException('Blob too big for deserialization!'); } $data = $this->decodeEntityContentData($blob, $format); if ($this->extractEntityId($data, 'redirect')) { // If it's a redirect, return null. return null; } try { $entity = $this->entityDeserializer->deserialize($data); } catch (DeserializationException $ex) { throw new MWContentSerializationException($ex->getMessage(), 0, $ex); } if (!$entity instanceof EntityDocument) { throw new InvalidArgumentException('Invalid $entityDeserializer provided'); } return $entity; }
/** * @since 0.1 * @param Revision $revision * @param EditInfo|null $editInfo * * @throws RuntimeException * @throws InvalidArgumentException * @returns Item|Property new version of the entity */ public function save(Revision $revision, EditInfo $editInfo = null) { if (!$revision->getContent()->getData() instanceof EntityDocument) { throw new RuntimeException('Can only save Content of EntityDocuments'); } /** @var Item|Property $entity */ $entity = $revision->getContent()->getData(); $serialized = $this->entitySerializer->serialize($entity); $params = array('data' => json_encode($serialized)); $revId = $revision->getId(); if (!is_null($revId)) { $params['baserevid'] = $revId; } $entityId = $entity->getId(); if (!is_null($entityId)) { $params['id'] = $entityId->getSerialization(); // Always clear so that removing elements is possible $params['clear'] = 'true'; // Add more detail to the default "Cleared an entity" summary // Note: this is later overridden if a summary is provided in the EditInfo $params['summary'] = 'Edited a ' . $entity->getType(); } else { $params['new'] = $entity->getType(); } // If no editInfo is explicitly passed call back to the one in the revision? if ($editInfo === null) { $editInfo = $revision->getEditInfo(); } if ($editInfo->getBot()) { $params['bot'] = true; } if ($editInfo->getMinor()) { $params['minor'] = true; } $summary = $editInfo->getSummary(); if (!empty($summary)) { $params['summary'] = $summary; } $result = $this->api->postRequest('wbeditentity', $params, $editInfo); return $this->entityDeserializer->deserialize($result['entity']); }
private function assertDeserializesWithoutException(Deserializer $deserializer, $serialization) { $deserializer->deserialize($serialization); $this->assertTrue(true, 'No exception occurred during deserialization'); }
protected function assertDeserializerThatCanDeserializeObject(Deserializer $deserializer, $object) { $this->assertTrue($deserializer->isDeserializerFor($object)); }
/** * @dataProvider entityProvider */ public function testGivenEntitySerialization_entityIsReturned($fileName, $serialization) { $entity = $this->deserializer->deserialize($serialization); $this->assertInstanceOf('Wikibase\\DataModel\\Entity\\Entity', $entity, 'Deserialization of ' . $fileName . ' should lead to an Entity instance'); }
/** * @see Deserializer::isDeserializerFor * * @since 1.0 * * @param mixed $serialization * * @return boolean */ public function isDeserializerFor($serialization) { return $this->deserializer->isDeserializerFor($serialization); }