/**
  * @see EntityLookup::hasEntity
  *
  * If the given entity ID points to a redirect, that redirect is resolved and the
  * existence of the target entity is checked.
  *
  * @param EntityId $entityId
  *
  * @return bool
  * @throws EntityLookupException
  */
 public function hasEntity(EntityId $entityId)
 {
     try {
         return $this->lookup->hasEntity($entityId);
     } catch (UnresolvedEntityRedirectException $ex) {
         return $this->lookup->hasEntity($ex->getRedirectTargetId());
     }
 }
 /**
  * @param PropertyId $propertyId
  *
  * @return Property
  * @throws PropertyDataTypeLookupException
  */
 private function getProperty(PropertyId $propertyId)
 {
     $property = $this->entityLookup->getEntity($propertyId);
     if (!$property instanceof Property) {
         throw new PropertyDataTypeLookupException($propertyId);
     }
     return $property;
 }
 /**
  * @param string $itemIdString - An item id
  * @param int $limit
  * @param float $minProbability
  * @param string $context
  * @throws \InvalidArgumentException
  * @return array
  */
 public function generateSuggestionsByItem($itemIdString, $limit, $minProbability, $context)
 {
     $id = new ItemId($itemIdString);
     $item = $this->entityLookup->getEntity($id);
     if ($item == null) {
         throw new InvalidArgumentException('Item ' . $id . ' could not be found');
     }
     $suggestions = $this->suggester->suggestByItem($item, $limit, $minProbability, $context);
     return $suggestions;
 }
 public function getDescriptions(EntityId $entityId, array $languageCodes)
 {
     $entity = $this->entityLookup->getEntity($entityId);
     if (is_null($entity)) {
         throw new OutOfBoundsException('Could not lookup entity');
     }
     if ($entity instanceof DescriptionsProvider) {
         return $entity->getDescriptions()->getWithLanguages($languageCodes)->toTextArray();
     } else {
         throw new OutOfBoundsException('Got entity but was not a DescriptionsProvider');
     }
 }
 /**
  * @param string $propertyLabelOrId
  * @param string $languageCode
  *
  * @throws PropertyLabelNotResolvedException
  * @return PropertyId
  */
 public function resolvePropertyId($propertyLabelOrId, $languageCode)
 {
     try {
         $propertyId = new PropertyId($propertyLabelOrId);
         if (!$this->entityLookup->hasEntity($propertyId)) {
             throw new PropertyLabelNotResolvedException($propertyLabelOrId, $languageCode);
         }
     } catch (InvalidArgumentException $ex) {
         $propertyId = $this->findPropertyByLabel($propertyLabelOrId, $languageCode);
     }
     return $propertyId;
 }
 /**
  * @param EntityId $entityId
  * @param string $propertyLabelOrId property label or ID (pXXX)
  * @param int[]|null $acceptableRanks
  *
  * @throws PropertyLabelNotResolvedException
  * @return string
  */
 public function render(EntityId $entityId, $propertyLabelOrId, array $acceptableRanks = null)
 {
     try {
         $entity = $this->entityLookup->getEntity($entityId);
     } catch (RevisionedUnresolvedRedirectException $ex) {
         return '';
     }
     if (!$entity instanceof StatementListProvider) {
         return '';
     }
     $propertyId = $this->propertyIdResolver->resolvePropertyId($propertyLabelOrId, $this->language->getCode());
     $snaks = $this->snaksFinder->findSnaks($entity, $propertyId, $acceptableRanks);
     return $this->formatSnaks($snaks);
 }
 /**
  * @see ValueValidator::validate()
  *
  * @param EntityIdValue|EntityId $value The ID to validate
  *
  * @return Result
  * @throws InvalidArgumentException
  */
 public function validate($value)
 {
     if ($value instanceof EntityIdValue) {
         $value = $value->getEntityId();
     }
     if (!$value instanceof EntityId) {
         throw new InvalidArgumentException("Expected an EntityId object");
     }
     $actualType = $value->getEntityType();
     $errors = array();
     if ($this->entityType !== null && $actualType !== $this->entityType) {
         $errors[] = Error::newError("Wrong entity type: " . $actualType, null, 'bad-entity-type', array($actualType));
     }
     if (!$this->entityLookup->hasEntity($value)) {
         $errors[] = Error::newError("Entity not found: " . $value, null, 'no-such-entity', array($value));
     }
     return empty($errors) ? Result::newSuccess() : Result::newError($errors);
 }
 /**
  * Updates the property info entry for the given property.
  * The property is loaded in full using the EntityLookup
  * provide to the constructor.
  *
  * @throws RuntimeException
  *
  * @param PropertyId $id the Property to process
  */
 private function updatePropertyInfo(PropertyId $id)
 {
     $property = $this->entityLookup->getEntity($id);
     if (!$property instanceof Property) {
         throw new RuntimeException('EntityLookup did not return a Property for id ' . $id->getSerialization());
     }
     $info = $this->propertyInfoBuilder->buildPropertyInfo($property);
     $this->propertyInfoTable->setPropertyInfo($property->getId(), $info);
 }
 /**
  * @param EntityId $entityId
  * @param array $languages used in thrown exceptions
  *
  * @throws TermLookupException
  * @return Fingerprint
  */
 private function fetchFingerprint(EntityId $entityId, array $languages)
 {
     try {
         $entity = $this->entityLookup->getEntity($entityId);
     } catch (EntityLookupException $ex) {
         throw new TermLookupException($entityId, $languages, 'The entity could not be loaded', $ex);
     }
     if ($entity === null) {
         throw new TermLookupException($entityId, $languages, 'The entity could not be loaded');
     }
     return $entity instanceof FingerprintProvider ? $entity->getFingerprint() : new Fingerprint();
 }
 private function setBadgesProperty(ItemId $itemId, ParserOutput $out)
 {
     /** @var Item $item */
     $item = $this->entityLookup->getEntity($itemId);
     if (!$item || !$item->getSiteLinkList()->hasLinkWithSiteId($this->siteId)) {
         // Probably some sort of race condition or data inconsistency, better log a warning
         wfLogWarning('According to a SiteLinkLookup ' . $itemId->getSerialization() . ' is linked to ' . $this->siteId . ' while it is not or it does not exist.');
         return;
     }
     $siteLink = $item->getSiteLinkList()->getBySiteId($this->siteId);
     foreach ($siteLink->getBadges() as $badge) {
         $out->setProperty('wikibase-badge-' . $badge->getSerialization(), true);
     }
 }
 /**
  * Parses a string to itemId
  *
  * @param string $itemString
  * @return ItemId|null
  */
 private function getItemId($itemString)
 {
     try {
         $itemId = $this->idParser->parse($itemString);
         if ($itemId instanceof ItemId) {
             if (!$this->entityLookup->hasEntity($itemId)) {
                 $this->errorMessageKey = "item-not-found";
                 return null;
             }
             return $itemId;
         }
     } catch (EntityIdParsingException $e) {
         $this->errorMessageKey = 'item-id-invalid';
     } catch (InvalidArgumentException $e) {
         $this->errorMessageKey = 'item-id-invalid';
     }
     return null;
 }
 /**
  * Finds the corresponding item on the repository and returns the item's site links.
  *
  * @since 0.1
  *
  * @param Title $title
  *
  * @return SiteLink[] A map of SiteLinks, indexed by global site id.
  */
 public function getEntityLinks(Title $title)
 {
     $links = array();
     $itemId = $this->siteLinkLookup->getItemIdForLink($this->siteId, $title->getPrefixedText());
     if ($itemId !== null) {
         //NOTE: SiteLinks we could get from $this->siteLinkLookup do not contain badges,
         //      so we have to fetch the links from the Item.
         /* @var Item $item */
         $item = $this->entityLookup->getEntity($itemId);
         if ($item) {
             $links = iterator_to_array($item->getSiteLinkList());
             $links = $this->indexLinksBySiteId($links);
         } else {
             wfLogWarning(__METHOD__ . ": Could not load item " . $itemId->getSerialization() . " for " . $title->getPrefixedText());
         }
     }
     return $links;
 }
 /**
  * Get entity from prefixed ID (e.g. "Q23") and return it as serialized array.
  *
  * @since 0.5
  *
  * @param string $prefixedEntityId
  *
  * @return array|null
  */
 public function getEntity($prefixedEntityId)
 {
     $prefixedEntityId = trim($prefixedEntityId);
     $entityId = $this->entityIdParser->parse($prefixedEntityId);
     $this->usageAccumulator->addAllUsage($entityId);
     try {
         $entityObject = $this->entityLookup->getEntity($entityId);
     } catch (RevisionedUnresolvedRedirectException $ex) {
         // We probably hit a double redirect
         wfLogWarning('Encountered a UnresolvedRedirectException when trying to load ' . $prefixedEntityId);
         return null;
     }
     if ($entityObject === null) {
         return null;
     }
     $entityArr = $this->newClientEntitySerializer()->serialize($entityObject);
     // Renumber the entity as Lua uses 1-based array indexing
     $this->renumber($entityArr);
     $entityArr['schemaVersion'] = 2;
     return $entityArr;
 }
 /**
  * Rebuilds EntityPerPageTable for specified pages
  *
  * @param ItemId[] $itemIds
  *
  * @return int
  */
 private function rebuildSiteLinks(array $itemIds)
 {
     $this->entityPrefetcher->prefetch($itemIds);
     $c = 0;
     foreach ($itemIds as $itemId) {
         if (!$itemId instanceof ItemId) {
             // Just in case someone is using a EntityIdPager which doesn't filter non-Items
             continue;
         }
         $item = $this->entityLookup->getEntity($itemId);
         if (!$item) {
             continue;
         }
         $ok = $this->siteLinkTable->saveLinksOfItem($item);
         if (!$ok) {
             $this->report('Saving sitelinks for Item ' . $item->getId()->getSerialization() . ' failed');
         }
         $c++;
     }
     // Wait for the slaves, just in case we e.g. hit a range of ids which need a lot of writes.
     wfWaitForSlaves();
     return $c;
 }
 /**
  * @param EntityId $entityId
  *
  * @throws EntityLookupException
  * @throws StorageException
  *
  * @return string|null
  */
 protected function generateDumpForEntityId(EntityId $entityId)
 {
     try {
         $entity = $this->entityLookup->getEntity($entityId);
         if (!$entity) {
             throw new EntityLookupException($entityId, 'Entity not found: ' . $entityId->getSerialization());
         }
     } catch (MWContentSerializationException $ex) {
         throw new StorageException('Deserialization error for ' . $entityId->getSerialization());
     } catch (RevisionedUnresolvedRedirectException $e) {
         // Redirects aren't supposed to be in the JSON dumps
         return null;
     }
     $data = $this->entitySerializer->serialize($entity);
     $data = $this->injectEntitySerializationWithDataTypes($data);
     // HACK: replace empty arrays with objects at the first level of the array
     foreach ($data as &$element) {
         if (empty($element)) {
             $element = new stdClass();
         }
     }
     $json = $this->encode($data);
     return $json;
 }
 /**
  * @expectedException InvalidArgumentException
  */
 public function testGenerateSuggestionsWithNonExistentItem()
 {
     $itemId = new ItemId('Q41');
     $this->lookup->expects($this->once())->method('getEntity')->with($this->equalTo($itemId))->will($this->returnValue(null));
     $this->suggestionGenerator->generateSuggestionsByItem('Q41', 100, 0.0, 'item');
 }
 /**
  * @see ItemLookup::getPropertyForId
  */
 public function getPropertyForId(PropertyId $propertyId)
 {
     $entity = $this->entityLookup->getEntity($propertyId);
     return $entity;
 }
 /**
  * @see EntityLookup::hasEntity
  *
  * @param EntityId $entityId
  *
  * @return bool
  * @throws EntityLookupException
  */
 public function hasEntity(EntityId $entityId)
 {
     return $this->entityLookup->hasEntity($entityId);
 }
 /**
  * @see ItemLookup::getItemForId
  */
 public function getItemForId(ItemId $itemId)
 {
     $entity = $this->entityLookup->getEntity($itemId);
     return $entity;
 }
 /**
  * Add stubs for any entities that were previously mentioned (e.g. as properties
  * or data values).
  *
  * @param EntityLookup $entityLookup
  */
 public function resolveMentionedEntities(EntityLookup $entityLookup)
 {
     $hasRedirect = false;
     foreach ($this->entitiesResolved as $id) {
         // $value is true if the entity has already been resolved,
         // or an EntityId to resolve.
         if (!$id instanceof EntityId) {
             continue;
         }
         try {
             $entity = $entityLookup->getEntity($id);
             if (!$entity) {
                 continue;
             }
             $this->addEntityStub($entity);
         } catch (RevisionedUnresolvedRedirectException $ex) {
             // NOTE: this may add more entries to the end of entitiesResolved
             $target = $ex->getRedirectTargetId();
             $this->addEntityRedirect($id, $target);
             $hasRedirect = true;
         }
     }
     // If we encountered redirects, the redirect targets may now need resolving.
     // They actually got added to $this->entitiesResolved, but may not have been
     // processed by the loop above, because they got added while the loop was in progress.
     if ($hasRedirect) {
         // Call resolveMentionedEntities() recursively to resolve any yet unresolved
         // redirect targets. The regress will eventually terminate even for circular
         // redirect chains, because the second time an entity ID is encountered, it
         // will be marked as already resolved.
         $this->resolveMentionedEntities($entityLookup);
     }
 }