/** * @see EntityLookup::getEntity * * If the given entity ID points to a redirect, that redirect is resolved and the * target entity returned. * * Callers can detect the presence of a redirect by comparing the ID of the returned * Entity with the request ID. * * @param EntityId $entityId * * @return EntityDocument|null * @throws EntityLookupException */ public function getEntity(EntityId $entityId) { try { return $this->lookup->getEntity($entityId); } catch (UnresolvedEntityRedirectException $ex) { return $this->lookup->getEntity($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'); } }
/** * @see EntityLookup::getEntity * * @param EntityId $entityId * * @throws EntityAccessLimitException * @return EntityDocument */ public function getEntity(EntityId $entityId) { $entityIdSerialization = $entityId->getSerialization(); if (!array_key_exists($entityIdSerialization, $this->entitiesAccessed)) { $this->entityAccessCount++; $this->entitiesAccessed[$entityIdSerialization] = true; } if ($this->entityAccessCount > $this->entityAccessLimit) { throw new EntityAccessLimitException($entityId, 'To many entities loaded, must not load more than ' . $this->entityAccessLimit . ' entities.'); } return $this->entityLookup->getEntity($entityId); }
/** * @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); }
/** * 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); } }
/** * 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; }
/** * @see ItemLookup::getPropertyForId */ public function getPropertyForId(PropertyId $propertyId) { $entity = $this->entityLookup->getEntity($propertyId); return $entity; }
/** * @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); } }