/**
  * @see EntityValidator::validate()
  *
  * @param EntityDocument $entity
  *
  * @return Result
  */
 public function validateEntity(EntityDocument $entity)
 {
     if ($entity instanceof FingerprintProvider) {
         return $this->duplicateDetector->detectLabelConflicts($entity->getType(), $entity->getFingerprint()->getLabels()->toTextArray(), null, $entity->getId());
     }
     return Result::newSuccess();
 }
Ejemplo n.º 2
0
 /**
  * @see EntityStore::assignFreshId()
  *
  * @param EntityDocument $entity
  *
  * @throws StorageException
  */
 public function assignFreshId(EntityDocument $entity)
 {
     if ($entity->getId() !== null) {
         throw new StorageException('This entity already has an ID!');
     }
     $contentModelId = $this->contentFactory->getContentModelForType($entity->getType());
     $numericId = $this->idGenerator->getNewId($contentModelId);
     //FIXME: this relies on setId() accepting numeric IDs!
     $entity->setId($numericId);
 }
 /**
  * @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;
 }
 /**
  * @param EntityDocument $entity
  *
  * @return TermIndexEntry[]
  */
 public function getEntityTerms(EntityDocument $entity)
 {
     // FIXME: OCP violation. No support for new types of entities can be registered
     if ($entity instanceof FingerprintProvider) {
         $fingerprint = $entity->getFingerprint();
         /** @var EntityDocument $entity */
         $extraFields = array('entityType' => $entity->getType());
         $entityId = $entity->getId();
         if ($entityId !== null) {
             $extraFields['entityId'] = $entityId->getNumericId();
         }
         return $this->getFingerprintTerms($fingerprint, $extraFields);
     }
     return array();
 }
Ejemplo n.º 5
0
 /**
  * @see EntityDocumentSaver::saveEntityDocument
  */
 public function saveEntityDocument(EntityDocument $entityDocument)
 {
     $this->database->selectCollection($entityDocument->getType())->upsert($this->buildGetEntityForIdQuery($entityDocument->getId()), $this->documentBuilder->buildDocumentForEntity($entityDocument));
 }
 /**
  * Validates this ChangeOp
  *
  * @see ChangeOp::validate()
  *
  * @since 0.5
  *
  * @param EntityDocument $entity
  *
  * @throws ChangeOpException
  * @return Result
  */
 public function validate(EntityDocument $entity)
 {
     $languageValidator = $this->termValidatorFactory->getLanguageValidator();
     $termValidator = $this->termValidatorFactory->getLabelValidator($entity->getType());
     // check that the language is valid
     $result = $languageValidator->validate($this->languageCode);
     if (!$result->isValid()) {
         return $result;
     }
     // It should be possible to remove invalid aliases, but not to add/set new invalid ones
     if ($this->action === 'set' || $this->action === '' || $this->action === 'add') {
         // Check that the new aliases are valid
         foreach ($this->aliases as $alias) {
             $result = $termValidator->validate($alias);
             if (!$result->isValid()) {
                 return $result;
             }
         }
     } elseif ($this->action !== 'remove') {
         throw new ChangeOpException('Bad action: ' . $this->action);
     }
     //XXX: Do we want to check the updated fingerprint, as we do for labels and descriptions?
     return $result;
 }
 /**
  * @param array $data
  * @param EntityDocument $entity
  */
 private function checkEntityType(array $data, EntityDocument $entity)
 {
     if (isset($data['type']) && $entity->getType() !== $data['type']) {
         $this->errorReporter->dieError('Invalid field used in call: "type", must match type associated with id', 'param-invalid');
     }
 }
 private function getSerialization(EntityDocument $entity, PropertyId $propertyId)
 {
     return array('id' => $entity->getId()->getSerialization(), 'type' => $entity->getType(), 'labels' => array('de' => array('language' => 'de', 'value' => 'Kuchen'), 'en' => array('language' => 'en', 'value' => 'Cake')), 'claims' => array($propertyId->getSerialization() => array(array('id' => $this->makeGuid($entity->getId()), 'mainsnak' => array('snaktype' => 'value', 'property' => $propertyId->getSerialization(), 'datavalue' => array('value' => 'kittens!', 'type' => 'string')), 'type' => 'statement', 'rank' => 'normal'))));
 }
 /**
  * Validates this ChangeOp
  *
  * @see ChangeOp::validate()
  *
  * @since 0.5
  *
  * @param EntityDocument $entity
  *
  * @return Result
  */
 public function validate(EntityDocument $entity)
 {
     if (!$entity instanceof FingerprintHolder) {
         throw new InvalidArgumentException('$entity must be a FingerprintHolder');
     }
     $languageValidator = $this->termValidatorFactory->getLanguageValidator();
     $termValidator = $this->termValidatorFactory->getDescriptionValidator();
     $fingerprintValidator = $this->termValidatorFactory->getFingerprintValidator($entity->getType());
     // check that the language is valid
     $result = $languageValidator->validate($this->languageCode);
     if ($result->isValid() && $this->description !== null) {
         // Check that the new description is valid
         $result = $termValidator->validate($this->description);
     }
     if (!$result->isValid()) {
         return $result;
     }
     // Check if the new fingerprint of the entity is valid (e.g. if the combination
     // of label and description  is still unique)
     $fingerprint = unserialize(serialize($entity->getFingerprint()));
     $this->updateFingerprint($fingerprint);
     $result = $fingerprintValidator->validateFingerprint($fingerprint, $entity->getId(), array($this->languageCode));
     return $result;
 }
Ejemplo n.º 10
0
 /**
  * @param EntityDocument $entity
  *
  * @throws InvalidArgumentException
  * @return string[] A list of permissions
  */
 protected function getRequiredPermissions(EntityDocument $entity)
 {
     $permissions = $this->isWriteMode() ? array('read', 'edit') : array('read');
     $permissions[] = $entity->getType() . '-term';
     return $permissions;
 }
 /**
  * @see EntityHolder::getEntityType
  *
  * @return string
  */
 public function getEntityType()
 {
     return $this->entity->getType();
 }
 /**
  * Constructs an EntityChange from the given old and new Entity.
  *
  * @since 0.5
  *
  * @param string      $action The action name
  * @param EntityDocument|null $oldEntity
  * @param EntityDocument|null $newEntity
  * @param array $fields additional fields to set
  *
  * @return EntityChange
  * @throws MWException
  */
 public function newFromUpdate($action, EntityDocument $oldEntity = null, EntityDocument $newEntity = null, array $fields = array())
 {
     if ($oldEntity === null && $newEntity === null) {
         throw new MWException('Either $oldEntity or $newEntity must be given');
     }
     if ($oldEntity === null) {
         $oldEntity = $this->entityFactory->newEmpty($newEntity->getType());
         $id = $newEntity->getId();
     } elseif ($newEntity === null) {
         $newEntity = $this->entityFactory->newEmpty($oldEntity->getType());
         $id = $oldEntity->getId();
     } elseif ($oldEntity->getType() !== $newEntity->getType()) {
         throw new MWException('Entity type mismatch');
     } else {
         $id = $newEntity->getId();
     }
     // HACK: don't include statements diff, since those are unused and not helpful
     // performance-wise to the dispatcher and change handling.
     // For a better solution, see T113468.
     if ($oldEntity instanceof StatementListHolder) {
         $oldEntity->setStatements(new StatementList());
         $newEntity->setStatements(new StatementList());
     }
     $diff = $this->entityDiffer->diffEntities($oldEntity, $newEntity);
     /** @var EntityChange $instance */
     $instance = self::newForEntity($action, $id, $fields);
     $instance->setDiff($diff);
     return $instance;
 }
 /**
  * @param EntityDocument $entity
  *
  * @return bool
  */
 private function isAllowedToChangeTerms(EntityDocument $entity)
 {
     $action = $entity->getType() . '-term';
     if (!$this->getUser()->isAllowed($action)) {
         $this->showErrorHTML($this->msg('permissionserrors') . ': ' . $action);
         return false;
     }
     return true;
 }
 /**
  * @see EntityPermissionChecker::getPermissionStatusForEntity
  *
  * @note When checking for the 'edit' permission, this will check the 'createpage'
  * permission first in case the entity does not yet exist (i.e. if $entity->getId()
  * returns null).
  *
  * @param User $user
  * @param string $permission
  * @param EntityDocument $entity
  * @param string $quick
  *
  * @return Status a status object representing the check's result.
  *
  * @todo Move to a separate service (merge into WikiPageEntityStore?)
  */
 public function getPermissionStatusForEntity(User $user, $permission, EntityDocument $entity, $quick = '')
 {
     $id = $entity->getId();
     $status = null;
     if (!$id) {
         $entityType = $entity->getType();
         if ($permission === 'edit') {
             // for editing a non-existing page, check the createpage permission
             $status = $this->getPermissionStatusForEntityType($user, 'createpage', $entityType, $quick);
         }
         if (!$status || $status->isOK()) {
             $status = $this->getPermissionStatusForEntityType($user, $permission, $entityType, $quick);
         }
     } else {
         $status = $this->getPermissionStatusForEntityId($user, $permission, $id, $quick);
     }
     return $status;
 }
Ejemplo n.º 15
0
 private function assertTypesMatch(EntityDocument $from, EntityDocument $to)
 {
     if ($from->getType() !== $to->getType()) {
         throw new InvalidArgumentException('Can only diff two entities of the same type');
     }
 }
 /**
  * Adds meta-information about an entity (such as the ID and type) to the RDF graph.
  *
  * @todo: extract into MetaDataRdfBuilder
  *
  * @param EntityDocument $entity
  */
 private function addEntityMetaData(EntityDocument $entity)
 {
     $entityLName = $this->vocabulary->getEntityLName($entity->getId());
     $this->writer->about(RdfVocabulary::NS_ENTITY, $entityLName)->a(RdfVocabulary::NS_ONTOLOGY, $this->vocabulary->getEntityTypeName($entity->getType()));
     if ($entity instanceof Property) {
         $this->writer->say(RdfVocabulary::NS_ONTOLOGY, 'propertyType')->is(RdfVocabulary::NS_ONTOLOGY, $this->vocabulary->getDataTypeName($entity));
         $id = $entity->getId()->getSerialization();
         $this->writePropertyPredicates($id, $this->propertyIsLink($entity));
         $this->writeNovalueClass($id);
     }
 }