/**
  * @see SpecialSetEntity::getValue()
  *
  * @since 0.4
  *
  * @param Fingerprint $fingerprint
  * @param string $languageCode
  *
  * @return string
  */
 protected function getValue(Fingerprint $fingerprint, $languageCode)
 {
     if ($fingerprint->hasAliasGroup($languageCode)) {
         return implode('|', $fingerprint->getAliasGroup($languageCode)->getAliases());
     }
     return '';
 }
 public function testGivenUntrimmedLabel_generateDoesNotTrim()
 {
     $fingerprint = new Fingerprint();
     $fingerprint->setLabel('en', ' untrimmed label ');
     $generator = new FingerprintSearchTextGenerator();
     $text = $generator->generate($fingerprint);
     $this->assertSame(" untrimmed label ", $text);
 }
 /**
  * Applies the change to the fingerprint
  *
  * @param Fingerprint $fingerprint
  */
 private function updateFingerprint(Fingerprint $fingerprint)
 {
     if ($this->description === null) {
         $fingerprint->removeDescription($this->languageCode);
     } else {
         $fingerprint->getDescriptions()->setTextForLanguage($this->languageCode, $this->description);
     }
 }
예제 #4
0
 /**
  * Applies the change to the fingerprint
  *
  * @param Fingerprint $fingerprint
  */
 private function updateFingerprint(Fingerprint $fingerprint)
 {
     if ($this->label === null) {
         $fingerprint->removeLabel($this->languageCode);
     } else {
         $fingerprint->getLabels()->setTextForLanguage($this->languageCode, $this->label);
     }
 }
 /**
  * @return Item[]
  */
 public function itemProvider()
 {
     $fingerprint = new Fingerprint();
     $fingerprint->setLabel('en', 'kittens');
     $nonEmptyItem = new Item();
     $nonEmptyItem->setFingerprint($fingerprint);
     return array($this->addStatementsAndSave(new Item()), $this->addStatementsAndSave($nonEmptyItem));
 }
 private function newCountryProperty()
 {
     $fingerprint = new Fingerprint();
     $fingerprint->setLabel('en', 'country');
     $fingerprint->setLabel('nl', 'land');
     $fingerprint->setDescription('en', 'sovereign state of this item');
     $countryProperty = new Property(new PropertyId('P17'), $fingerprint, 'wikibase-item');
     $countryProperty->getStatements()->addNewStatement(new PropertyValueSnak(42, new StringValue('foobar')));
     return $countryProperty;
 }
 /**
  * @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);
 }
예제 #9
0
 /**
  * @see Comparable::equals
  *
  * Two items are considered equal if they are of the same
  * type and have the same value. The value does not include
  * the id, so entities with the same value but different id
  * are considered equal.
  *
  * @since 0.1
  *
  * @param mixed $target
  *
  * @return bool
  */
 public function equals($target)
 {
     if ($this === $target) {
         return true;
     }
     return $target instanceof self && $this->fingerprint->equals($target->fingerprint) && $this->siteLinks->equals($target->siteLinks) && $this->statements->equals($target->statements);
 }
 /**
  * @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();
     $descriptions = $fingerprint->getDescriptions()->toTextArray();
     if ($languageCodes !== null) {
         $languageKeys = array_flip($languageCodes);
         $labels = array_intersect_key($labels, $languageKeys);
         $descriptions = array_intersect_key($descriptions, $languageKeys);
     }
     // Nothing to do if there are no labels OR no descriptions, since
     // a conflict requires a label AND a description.
     if (empty($labels) || empty($descriptions)) {
         return Result::newSuccess();
     }
     return $this->duplicateDetector->detectLabelDescriptionConflicts($entityId->getEntityType(), $labels, $descriptions, $entityId);
 }
예제 #11
0
 /**
  * @param Fingerprint $fingerprint
  *
  * @return string[]
  */
 public static function getMainTermsAsLowerCaseStrings(Fingerprint $fingerprint)
 {
     $strings = array();
     $langsToUse = array('en', 'en-gb');
     foreach ($langsToUse as $lang) {
         try {
             $strings[] = $fingerprint->getLabel($lang)->getText();
         } catch (Exception $e) {
             // Ignore!
         }
         try {
             $strings = array_merge($strings, $fingerprint->getAliasGroup($lang)->getAliases());
         } catch (Exception $e) {
             // Ignore!
         }
     }
     return array_map('strtolower', $strings);
 }
 /**
  * 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);
 }
 private function newFingerprint()
 {
     $fingerprint = new Fingerprint();
     $fingerprint->setLabel('en', 'foo');
     $fingerprint->setLabel('de', 'bar');
     $fingerprint->setLabel('nl', 'baz');
     $fingerprint->setDescription('de', 'de description');
     $fingerprint->setAliasGroup('en', ['first en alias', 'second en alias']);
     $fingerprint->setAliasGroup('de', ['first de alias', 'second de alias']);
     return $fingerprint;
 }
 /**
  * @param Fingerprint $fingerprint
  *
  * @return ChangeOp[]
  */
 private function getChangeOps(Fingerprint $fingerprint)
 {
     $changeOpFactory = $this->changeOpFactory;
     $changeOps = array();
     if ($this->label !== '') {
         if (!$fingerprint->hasLabel($this->languageCode) || $fingerprint->getLabel($this->languageCode)->getText() !== $this->label) {
             $changeOps['wbsetlabel'] = $changeOpFactory->newSetLabelOp($this->languageCode, $this->label);
         }
     } elseif ($fingerprint->hasLabel($this->languageCode)) {
         $changeOps['wbsetlabel'] = $changeOpFactory->newRemoveLabelOp($this->languageCode);
     }
     if ($this->description !== '') {
         if (!$fingerprint->hasDescription($this->languageCode) || $fingerprint->getDescription($this->languageCode)->getText() !== $this->description) {
             $changeOps['wbsetdescription'] = $changeOpFactory->newSetDescriptionOp($this->languageCode, $this->description);
         }
     } elseif ($fingerprint->hasDescription($this->languageCode)) {
         $changeOps['wbsetdescription'] = $changeOpFactory->newRemoveDescriptionOp($this->languageCode);
     }
     if (!empty($this->aliases)) {
         if (!$fingerprint->hasAliasGroup($this->languageCode) || $fingerprint->getAliasGroup($this->languageCode)->getAliases() !== $this->aliases) {
             $changeOps['wbsetaliases'] = $changeOpFactory->newSetAliasesOp($this->languageCode, $this->aliases);
         }
     } elseif ($fingerprint->hasAliasGroup($this->languageCode)) {
         $changeOps['wbsetaliases'] = $changeOpFactory->newRemoveAliasesOp($this->languageCode, $fingerprint->getAliasGroup($this->languageCode)->getAliases());
     }
     return $changeOps;
 }
예제 #15
0
 private function assertFingerprintResultsFromPatch(Fingerprint $expected, Fingerprint $original, EntityDiff $patch)
 {
     $this->assertTrue($expected->equals($this->getPatchedFingerprint($original, $patch)));
 }
 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);
     }
 }
 private function assetFingerprintEquals(Fingerprint $expected, Fingerprint $actual)
 {
     // TODO: Compare serializations.
     $this->assertTrue($expected->equals($actual), 'Fingerprint mismatches');
 }
예제 #18
0
 private function fingerprintToPartialArray(Fingerprint $fingerprint)
 {
     return array('label' => $fingerprint->getLabels()->toTextArray(), 'description' => $fingerprint->getDescriptions()->toTextArray());
 }
 /**
  * @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;
 }
 /**
  * @see SpecialSetEntity::getValue()
  *
  * @since 0.4
  *
  * @param Fingerprint $fingerprint
  * @param string $languageCode
  *
  * @return string
  */
 protected function getValue(Fingerprint $fingerprint, $languageCode)
 {
     return $fingerprint->hasDescription($languageCode) ? $fingerprint->getDescription($languageCode)->getText() : '';
 }
예제 #21
0
 public function testHasAliasGroupReturnsTrueOnlyWhenAliasGroupExists()
 {
     $fingerprint = new Fingerprint();
     $fingerprint->setAliasGroup('en', array('foo'));
     $this->assertTrue($fingerprint->hasAliasGroup('en'));
     $this->assertFalse($fingerprint->hasAliasGroup('de'));
 }
예제 #22
0
 /**
  * @see SpecialSetEntity::getValue()
  *
  * @since 0.4
  *
  * @param Fingerprint $fingerprint
  * @param string $languageCode
  *
  * @return string
  */
 protected function getValue(Fingerprint $fingerprint, $languageCode)
 {
     return $fingerprint->hasLabel($languageCode) ? $fingerprint->getLabel($languageCode)->getText() : '';
 }
 /**
  * @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), '');
 }
예제 #24
0
 /**
  * Construct mock repository matching the test data.
  *
  * @return MockRepository
  */
 public function getMockRepository()
 {
     static $repo;
     if (!empty($repo)) {
         return $repo;
     }
     $repo = new MockRepository();
     foreach (self::getTestProperties() as $prop) {
         list($id, $type) = $prop;
         $fingerprint = new Fingerprint();
         $fingerprint->setLabel('en', "Property{$id}");
         $entity = new Property(PropertyId::newFromNumber($id), $fingerprint, $type);
         $repo->putEntity($entity);
     }
     $q42 = new ItemId('Q42');
     $fingerprint = new Fingerprint();
     $fingerprint->setLabel('en', 'Item42');
     $entity = new Item($q42, $fingerprint);
     $repo->putEntity($entity);
     $repo->putRedirect(new EntityRedirect(new ItemId('Q4242'), $q42));
     return $repo;
 }
예제 #25
0
 public function getEntityTermsProvider()
 {
     $fingerprint = new Fingerprint();
     $fingerprint->setLabel('en', 'kittens!!!:)');
     $fingerprint->setDescription('es', 'es un gato!');
     $fingerprint->setAliasGroup('en', array('kitten-alias'));
     $item = new Item(new ItemId('Q999'));
     $item->setFingerprint($fingerprint);
     $expectedTerms = array(new TermIndexEntry(array('entityId' => 999, 'entityType' => 'item', 'termText' => 'es un gato!', 'termLanguage' => 'es', 'termType' => 'description')), new TermIndexEntry(array('entityId' => 999, 'entityType' => 'item', 'termText' => 'kittens!!!:)', 'termLanguage' => 'en', 'termType' => 'label')), new TermIndexEntry(array('entityId' => 999, 'entityType' => 'item', 'termText' => 'kitten-alias', 'termLanguage' => 'en', 'termType' => 'alias')));
     return array(array($expectedTerms, $item), array(array(), new Item()), array(array(), $this->getMock('Wikibase\\DataModel\\Entity\\EntityDocument')));
 }
 public function testGetTitleHtml_labelIsEscaped()
 {
     $entityTermsView = $this->getEntityTermsView();
     $fingerprint = new Fingerprint();
     $fingerprint->setLabel('en', '<a href="#">evil html</a>');
     $html = $entityTermsView->getTitleHtml($fingerprint, null);
     $this->assertContains('evil html', $html, 'make sure it works');
     $this->assertNotContains('href="#"', $html);
     $this->assertNotContains('&amp;', $html, 'no double escaping');
 }
예제 #27
0
 /**
  * Returns if the Property has no content.
  * Having an id and type set does not count as having content.
  *
  * @since 0.1
  *
  * @return bool
  */
 public function isEmpty()
 {
     return $this->fingerprint->isEmpty() && $this->statements->isEmpty();
 }
예제 #28
0
 private function patchAliases(Fingerprint $fingerprint, Diff $aliasesDiff)
 {
     $patchedAliases = $this->patcher->patch($this->getAliasesArrayForPatching($fingerprint->getAliasGroups()), $aliasesDiff);
     $fingerprint->setAliasGroups($this->getAliasesFromArrayForPatching($patchedAliases));
 }