/**
  * Applies the change to the fingerprint
  *
  * @param Fingerprint $fingerprint
  *
  * @throws ChangeOpException
  */
 private function updateFingerprint(Fingerprint $fingerprint)
 {
     if ($fingerprint->getAliasGroups()->hasGroupForLanguage($this->languageCode)) {
         $oldAliases = $fingerprint->getAliasGroup($this->languageCode)->getAliases();
     } else {
         $oldAliases = array();
     }
     if ($this->action === 'set' || $this->action === '') {
         $newAliases = $this->aliases;
     } elseif ($this->action === 'add') {
         $newAliases = array_merge($oldAliases, $this->aliases);
     } elseif ($this->action === 'remove') {
         $newAliases = array_diff($oldAliases, $this->aliases);
     } else {
         throw new ChangeOpException('Bad action: ' . $this->action);
     }
     $fingerprint->getAliasGroups()->setAliasesForLanguage($this->languageCode, $newAliases);
 }
 /**
  * @param Fingerprint $fingerprint
  *
  * @return string
  */
 public function generate(Fingerprint $fingerprint)
 {
     $text = implode("\n", $fingerprint->getLabels()->toTextArray());
     $text .= "\n" . implode("\n", $fingerprint->getDescriptions()->toTextArray());
     /** @var AliasGroup $aliasGroup */
     foreach ($fingerprint->getAliasGroups() as $aliasGroup) {
         $text .= "\n" . implode("\n", $aliasGroup->getAliases());
     }
     return trim($text, "\n");
 }
 /**
  * @see FingerprintValidator::validateFingerprint()
  *
  * @param Fingerprint $fingerprint
  * @param EntityId $entityId
  * @param string[]|null $languageCodes
  *
  * @return Result
  */
 public function validateFingerprint(Fingerprint $fingerprint, EntityId $entityId, array $languageCodes = null)
 {
     $labels = $fingerprint->getLabels()->toTextArray();
     $aliases = $fingerprint->getAliasGroups()->toTextArray();
     if ($languageCodes !== null) {
         $languageKeys = array_flip($languageCodes);
         $labels = array_intersect_key($labels, $languageKeys);
         $aliases = array_intersect_key($aliases, $languageKeys);
     }
     // Nothing to do if there are no labels AND no aliases.
     if (empty($labels) && empty($aliases)) {
         return Result::newSuccess();
     }
     return $this->duplicateDetector->detectLabelConflicts($entityId->getEntityType(), $labels, null, $entityId);
 }
 /**
  * @param Fingerprint $fingerprint
  * @param array $extraFields
  *
  * @return TermIndexEntry[]
  */
 private function getFingerprintTerms(Fingerprint $fingerprint, array $extraFields = array())
 {
     $terms = array();
     foreach ($fingerprint->getDescriptions()->toTextArray() as $languageCode => $description) {
         $term = new TermIndexEntry($extraFields);
         $term->setLanguage($languageCode);
         $term->setType(TermIndexEntry::TYPE_DESCRIPTION);
         $term->setText($description);
         $terms[] = $term;
     }
     foreach ($fingerprint->getLabels()->toTextArray() as $languageCode => $label) {
         $term = new TermIndexEntry($extraFields);
         $term->setLanguage($languageCode);
         $term->setType(TermIndexEntry::TYPE_LABEL);
         $term->setText($label);
         $terms[] = $term;
     }
     /** @var AliasGroup $aliasGroup */
     foreach ($fingerprint->getAliasGroups() as $aliasGroup) {
         foreach ($aliasGroup->getAliases() as $alias) {
             $term = new TermIndexEntry($extraFields);
             $term->setLanguage($aliasGroup->getLanguageCode());
             $term->setType(TermIndexEntry::TYPE_ALIAS);
             $term->setText($alias);
             $terms[] = $term;
         }
     }
     return $terms;
 }
Exemplo n.º 5
0
 public function testSetAliasGroups()
 {
     $fingerprint = new Fingerprint();
     $fingerprint->setAliasGroup('en', array('foo'));
     $groups = new AliasGroupList(array(new AliasGroup('de', array('bar'))));
     $fingerprint->setAliasGroups($groups);
     $this->assertEquals($groups, $fingerprint->getAliasGroups());
 }
 /**
  * @param Fingerprint $fingerprint
  * @param string $languageCode
  * @param Title|null $title
  *
  * @return string HTML
  */
 private function getEntityTermsForLanguageView(Fingerprint $fingerprint, $languageCode, Title $title = null)
 {
     $languageName = $this->languageNameLookup->getName($languageCode);
     $labels = $fingerprint->getLabels();
     $descriptions = $fingerprint->getDescriptions();
     $hasLabel = $labels->hasTermForLanguage($languageCode);
     $hasDescription = $descriptions->hasTermForLanguage($languageCode);
     return $this->templateFactory->render('wikibase-entitytermsforlanguageview', 'tr', 'td', $languageCode, $this->templateFactory->render('wikibase-entitytermsforlanguageview-language', htmlspecialchars($title === null ? '#' : $title->getLocalURL(array('setlang' => $languageCode))), htmlspecialchars($languageName)), $this->templateFactory->render('wikibase-labelview', $hasLabel ? '' : 'wb-empty', htmlspecialchars($hasLabel ? $labels->getByLanguage($languageCode)->getText() : $this->msg('wikibase-label-empty')->text()), ''), $this->templateFactory->render('wikibase-descriptionview', $hasDescription ? '' : 'wb-empty', htmlspecialchars($hasDescription ? $descriptions->getByLanguage($languageCode)->getText() : $this->msg('wikibase-description-empty')->text()), '', ''), $this->getAliasesView($fingerprint->getAliasGroups(), $languageCode), '');
 }
Exemplo n.º 7
0
 private function patchAliases(Fingerprint $fingerprint, Diff $aliasesDiff)
 {
     $patchedAliases = $this->patcher->patch($this->getAliasesArrayForPatching($fingerprint->getAliasGroups()), $aliasesDiff);
     $fingerprint->setAliasGroups($this->getAliasesFromArrayForPatching($patchedAliases));
 }
 private function injectFingerprint(array $types = null, array &$entityRecord, Fingerprint $fingerprint, array $languages = null)
 {
     if ($types === null || in_array('label', $types)) {
         $labels = $fingerprint->getLabels();
         if ($languages !== null) {
             $labels = $labels->getWithLanguages($languages);
         }
         $this->injectLabels($entityRecord, $labels);
     }
     if ($types === null || in_array('description', $types)) {
         $descriptions = $fingerprint->getDescriptions();
         if ($languages !== null) {
             $descriptions = $descriptions->getWithLanguages($languages);
         }
         $this->injectDescriptions($entityRecord, $descriptions);
     }
     if ($types === null || in_array('alias', $types)) {
         $aliases = $fingerprint->getAliasGroups();
         if ($languages !== null) {
             $aliases = $aliases->getWithLanguages($languages);
         }
         $this->injectAliases($entityRecord, $aliases);
     }
 }