/**
  * @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();
 }
 /**
  * @see SearchIndexField::getFieldData
  *
  * @param EntityDocument $entity
  *
  * @return int
  */
 public function getFieldData(EntityDocument $entity)
 {
     if ($entity instanceof FingerprintProvider) {
         return $entity->getFingerprint()->getLabels()->count();
     }
     return 0;
 }
 /**
  * @see SearchIndexField::getFieldData
  *
  * @param EntityDocument $entity
  *
  * @return int
  */
 public function getFieldData(EntityDocument $entity)
 {
     if ($entity instanceof Item) {
         return $entity->getSiteLinkList()->count();
     }
     return 0;
 }
 /**
  * @see SearchIndexField::getFieldData
  *
  * @param EntityDocument $entity
  *
  * @return int
  */
 public function getFieldData(EntityDocument $entity)
 {
     if ($entity instanceof StatementListHolder) {
         return $entity->getStatements()->count();
     }
     return 0;
 }
 /**
  * @param EntityDocument $entity
  *
  * @throws InvalidArgumentException
  */
 public function addEntity(EntityDocument $entity)
 {
     if ($entity->getId() === null) {
         throw new InvalidArgumentException('The entity needs to have an ID');
     }
     $this->entities[$entity->getId()->getSerialization()] = $entity;
 }
 /**
  * @param EntityDocument $entity
  */
 private function save(EntityDocument $entity)
 {
     $flags = $entity->getId() ? EDIT_UPDATE : EDIT_NEW;
     $store = WikibaseRepo::getDefaultInstance()->getEntityStore();
     $rev = $store->saveEntity($entity, '', $GLOBALS['wgUser'], $flags);
     $entity->setId($rev->getEntity()->getId());
 }
 /**
  * @param EntityDocument $entity
  */
 public function processEntity(EntityDocument $entity)
 {
     if ($entity instanceof StatementListProvider && $this->statementDataUpdaters) {
         $this->processStatements($entity->getStatements());
     }
     if ($entity instanceof Item && $this->siteLinkDataUpdaters) {
         $this->processSiteLinks($entity->getSiteLinkList());
     }
 }
 /**
  * @see ChangeOp::apply()
  * - a new claim with $snak as mainsnak gets added when $claimGuid is empty and $snak is set
  * - the claim's mainsnak gets set to $snak when $claimGuid and $snak are set
  */
 public function apply(EntityDocument $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $entity->getStatements();
     if (empty($this->statementGuid)) {
         $this->addStatement($statements, $entity->getId(), $summary);
     } else {
         $this->setStatement($statements, $summary);
     }
     $entity->setStatements($statements);
 }
 /**
  * @see ChangeOp::apply()
  */
 public function apply(EntityDocument $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $entity->getStatements();
     $statement = $statements->getFirstStatementWithGuid($this->statementGuid);
     if ($statement === null) {
         throw new ChangeOpException("Entity does not have a statement with GUID {$this->statementGuid}");
     }
     $qualifiers = $statement->getQualifiers();
     $this->removeQualifier($qualifiers, $summary);
     $statement->setQualifiers($qualifiers);
     $entity->setStatements($statements);
 }
 /**
  * @see SpecialSetEntity::setValue()
  *
  * @since 0.4
  *
  * @param EntityDocument $entity
  * @param string $languageCode
  * @param string $value
  *
  * @return Summary
  */
 protected function setValue(EntityDocument $entity, $languageCode, $value)
 {
     if (!$entity instanceof FingerprintProvider) {
         throw new InvalidArgumentException('$entity must be a FingerprintProvider');
     }
     $summary = new Summary('wbsetaliases');
     if ($value === '') {
         $aliases = $entity->getFingerprint()->getAliasGroup($languageCode)->getAliases();
         $changeOp = $this->termChangeOpFactory->newRemoveAliasesOp($languageCode, $aliases);
     } else {
         $changeOp = $this->termChangeOpFactory->newSetAliasesOp($languageCode, explode('|', $value));
     }
     $this->applyChangeOp($changeOp, $entity, $summary);
     return $summary;
 }
 /**
  * @see ChangeOp::apply
  *
  * @param EntityDocument $entity
  * @param Summary|null $summary
  *
  * @throws InvalidArgumentException
  * @throws ChangeOpException
  */
 public function apply(EntityDocument $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $entity->getStatements();
     $statement = $statements->getFirstStatementWithGuid($this->guid);
     if ($statement === null) {
         throw new ChangeOpException("Entity does not have statement with GUID {$this->guid}");
     }
     $statements->removeStatementsWithGuid($this->guid);
     $entity->setStatements($statements);
     $removedSnak = $statement->getMainSnak();
     $this->updateSummary($summary, 'remove', '', $this->getSummaryArgs($removedSnak));
 }
Example #12
0
 /**
  * @see Iterator::key
  */
 public function key()
 {
     if ($this->currentEntity === null) {
         return null;
     }
     return $this->currentEntity->getId()->getSerialization();
 }
 /**
  * Watches or unwatches the entity.
  *
  * @note Keep in sync with logic in EditPage!
  * @todo: move to separate service
  *
  * @param bool $watch whether to watch or unwatch the page.
  *
  * @throws MWException
  */
 private function updateWatchlist($watch)
 {
     if ($this->getTitle() === null) {
         throw new MWException('Title not yet known!');
     }
     $this->entityStore->updateWatchlist($this->user, $this->newEntity->getId(), $watch);
 }
 /**
  * @see ChangeOp::apply()
  */
 public function apply(EntityDocument $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     $statements = $entity->getStatements();
     $statement = $statements->getFirstStatementWithGuid($this->statementGuid);
     if ($statement === null) {
         throw new ChangeOpException("Entity does not have claim with GUID {$this->statementGuid}");
     }
     $references = $statement->getReferences();
     $this->removeReference($references, $summary);
     if ($summary !== null) {
         $summary->addAutoSummaryArgs($this->getSnakSummaryArgs($statement->getMainSnak()));
     }
     $statement->setReferences($references);
     $entity->setStatements($statements);
 }
 /**
  * @param string $guid
  * @param EntityDocument $entity
  *
  * @throws UsageException
  * @return Statement
  */
 public function getStatementFromEntity($guid, EntityDocument $entity)
 {
     if (!$entity instanceof StatementListProvider) {
         $this->errorReporter->dieError('Entity type does not support statements', 'no-such-claim');
     }
     $statement = $entity->getStatements()->getFirstStatementWithGuid($guid);
     if ($statement === null) {
         $this->errorReporter->dieError('Could not find the statement', 'no-such-claim');
     }
     return $statement;
 }
 /**
  * @see ChangeOp::apply
  *
  * @param EntityDocument $entity
  * @param Summary|null $summary
  *
  * @throws ChangeOpException
  */
 public function apply(EntityDocument $entity, Summary $summary = null)
 {
     if (!$entity instanceof StatementListHolder) {
         throw new InvalidArgumentException('$entity must be a StatementListHolder');
     }
     if ($this->statement->getGuid() === null) {
         $this->statement->setGuid($this->guidGenerator->newGuid($entity->getId()));
     }
     $guid = $this->guidParser->parse($this->statement->getGuid());
     if ($this->guidValidator->validate($guid->getSerialization()) === false) {
         throw new ChangeOpException("Claim does not have a valid GUID");
     } elseif (!$entity->getId()->equals($guid->getEntityId())) {
         throw new ChangeOpException("Claim GUID invalid for given entity");
     }
     $this->applyStatementToEntity($entity, $summary);
 }
 /**
  * @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 $fromEntity
  * @param EntityDocument $toEntity
  *
  * @throws ItemMergeException
  */
 private function validateEntities(EntityDocument $fromEntity, EntityDocument $toEntity)
 {
     if (!($fromEntity instanceof Item && $toEntity instanceof Item)) {
         throw new ItemMergeException('One or more of the entities are not items', 'not-item');
     }
     if ($toEntity->getId()->equals($fromEntity->getId())) {
         throw new ItemMergeException('You must provide unique ids', 'cant-merge-self');
     }
 }
 /**
  * Calculate a weight the given entity to be used for ranking. Should be normalized
  * between 0 and 1, but that's not a strong constraint.
  * This implementation uses the max of the number of labels and the number of sitelinks.
  *
  * TODO Should be moved to its own object and be added via dependency injection
  *
  * @param EntityDocument $entity
  *
  * @return float weight
  */
 private function getWeight(EntityDocument $entity)
 {
     // FIXME: OCP violation. No support for new types of entities can be registered
     $weight = 0.0;
     if ($entity instanceof FingerprintProvider) {
         $weight = max($weight, $entity->getFingerprint()->getLabels()->count() / 1000.0);
     }
     if ($entity instanceof Item) {
         $weight = max($weight, $entity->getSiteLinkList()->count() / 1000.0);
     }
     return $weight;
 }
 /**
  * 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;
 }
 /**
  * Returns the form elements.
  *
  * @since 0.5
  *
  * @param EntityDocument|null $entity
  *
  * @return string HTML
  */
 protected function getFormElements(EntityDocument $entity = null)
 {
     $id = 'wb-modifyentity-id';
     return Html::label($this->msg('wikibase-modifyentity-id')->text(), $id, array('class' => 'wb-label')) . Html::input('id', $entity === null ? '' : $entity->getId(), 'text', array('class' => 'wb-input', 'id' => $id));
 }
 /**
  * @param ParserOutput $parserOutput
  * @param EntityDocument $entity
  */
 private function addTitleTextToParserOutput(ParserOutput $parserOutput, EntityDocument $entity)
 {
     $titleText = null;
     if ($entity instanceof FingerprintProvider) {
         $labels = $entity->getFingerprint()->getLabels()->toTextArray();
         $preferred = $this->languageFallbackChain->extractPreferredValue($labels);
         if (is_array($preferred)) {
             $titleText = $preferred['value'];
         }
     }
     if (!is_string($titleText)) {
         $entityId = $entity->getId();
         if ($entityId instanceof EntityId) {
             $titleText = $entityId->getSerialization();
         }
     }
     $parserOutput->setExtensionData('wikibase-titletext', $titleText);
 }
 /**
  * Add the entity's terms to the RDF graph.
  *
  * @param EntityDocument $entity the entity to output.
  */
 public function addEntity(EntityDocument $entity)
 {
     if ($entity instanceof FingerprintProvider) {
         $fingerprint = $entity->getFingerprint();
         /** @var EntityDocument $entity */
         $entityLName = $this->vocabulary->getEntityLName($entity->getId());
         $this->addLabels($entityLName, $fingerprint->getLabels());
         $this->addDescriptions($entityLName, $fingerprint->getDescriptions());
         $this->addAliases($entityLName, $fingerprint->getAliasGroups());
     }
 }
 /**
  * @see EntityStore::saveEntity
  * @see WikiPage::doEditContent
  *
  * @param EntityDocument $entity
  * @param string $summary
  * @param User $user
  * @param int $flags
  * @param int|bool $baseRevId
  *
  * @throws StorageException
  * @return EntityRevision
  */
 public function saveEntity(EntityDocument $entity, $summary, User $user, $flags = 0, $baseRevId = false)
 {
     if ($entity->getId() === null) {
         if (($flags & EDIT_NEW) !== EDIT_NEW) {
             throw new StorageException(Status::newFatal('edit-gone-missing'));
         }
         $this->assignFreshId($entity);
     }
     $content = $this->contentFactory->newFromEntity($entity);
     $revision = $this->saveEntityContent($content, $summary, $user, $flags, $baseRevId);
     $entityRevision = new EntityRevision($entity, $revision->getId(), $revision->getTimestamp());
     $this->dispatcher->dispatch('entityUpdated', $entityRevision);
     return $entityRevision;
 }
 /**
  * @param EntityDocument $entity
  * @param string|null $guid
  *
  * @return StatementList
  */
 private function getStatements(EntityDocument $entity, $guid = null)
 {
     if (!$entity instanceof StatementListProvider) {
         return new StatementList();
     }
     $statements = $entity->getStatements();
     if ($guid === null) {
         return $statements->filter($this->newRequestParamsBasedFilter());
     }
     $statement = $statements->getFirstStatementWithGuid($guid);
     return new StatementList($statement === null ? array() : $statement);
 }
 /**
  * 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;
 }
 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'))));
 }
 /**
  * @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');
     }
 }
 /**
  * @see EntityStore::assignFreshId
  *
  * @param EntityDocument $entity
  *
  * @throws StorageException
  */
 public function assignFreshId(EntityDocument $entity)
 {
     //TODO: Find a canonical way to generate an EntityId from the maxId number.
     //XXX: Using setId() with an integer argument is deprecated!
     $numericId = ++$this->maxEntityId;
     $entity->setId($numericId);
 }
 /**
  * @see EntityDocumentSaver::saveEntityDocument
  */
 public function saveEntityDocument(EntityDocument $entityDocument)
 {
     $this->database->selectCollection($entityDocument->getType())->upsert($this->buildGetEntityForIdQuery($entityDocument->getId()), $this->documentBuilder->buildDocumentForEntity($entityDocument));
 }