/**
  * @see EntityValidator::validate()
  *
  * @param EntityDocument $entity
  *
  * @return Result
  */
 public function validateEntity(EntityDocument $entity)
 {
     if ($entity instanceof FingerprintProvider) {
         return $this->duplicateDetector->detectLabelDescriptionConflicts($entity->getType(), $entity->getFingerprint()->getLabels()->toTextArray(), $entity->getFingerprint()->getDescriptions()->toTextArray(), $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 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;
 }
 /**
  * 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;
 }
 /**
  * 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 ChangeOp::apply()
  */
 public function apply(EntityDocument $entity, Summary $summary = null)
 {
     if (!$entity instanceof FingerprintHolder) {
         throw new InvalidArgumentException('$entity must be a FingerprintHolder');
     }
     $fingerprint = $entity->getFingerprint();
     $this->updateSummary($summary, $this->action, $this->languageCode, $this->aliases);
     $this->updateFingerprint($fingerprint);
     $entity->setFingerprint($fingerprint);
 }
 /**
  * @param EntityDocument $entity
  */
 private function buildResult(EntityDocument $entity)
 {
     $builder = $this->getResultBuilder();
     if ($entity instanceof FingerprintProvider) {
         $fingerprint = $entity->getFingerprint();
         $builder->addLabels($fingerprint->getLabels(), 'entity');
         $builder->addDescriptions($fingerprint->getDescriptions(), 'entity');
         $builder->addAliasGroupList($fingerprint->getAliasGroups(), 'entity');
     }
     if ($entity instanceof Item) {
         $builder->addSiteLinkList($entity->getSiteLinkList(), 'entity');
     }
     if ($entity instanceof StatementListProvider) {
         $builder->addStatements($entity->getStatements(), 'entity');
     }
 }
 /**
  * @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);
 }
 /**
  * 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;
 }
 /**
  * @see SpecialModifyEntity::modifyEntity
  *
  * @param EntityDocument $entity
  *
  * @return Summary|bool
  */
 protected function modifyEntity(EntityDocument $entity)
 {
     if (!$entity instanceof FingerprintHolder) {
         throw new InvalidArgumentException('$entity must be a FingerprintHolder');
     }
     $changeOps = $this->getChangeOps($entity->getFingerprint());
     $summary = false;
     $success = true;
     foreach ($changeOps as $module => $changeOp) {
         $summary = new Summary($module);
         try {
             $this->applyChangeOp($changeOp, $entity, $summary);
         } catch (ChangeOpException $ex) {
             $this->showErrorHTML($ex->getMessage());
             $success = false;
         }
     }
     if (!$success) {
         return false;
     } elseif (count($changeOps) === 1) {
         return $summary;
     }
     return $this->getSummaryForLabelDescriptionAliases();
 }