/** * Create a repo link directly to the item. * We can't use Special:ItemByTitle here as the item might have already been updated. * * @param Title $title * * @return string|null */ private function getItemUrl(Title $title) { $entityId = $this->siteLinkLookup->getItemIdForSiteLink(new SiteLink($this->siteId, $title->getPrefixedText())); if (!$entityId) { return null; } return $this->repoLinker->getEntityUrl($entityId); }
/** * Get the EntityId that we want to update * * @return EntityId|null */ public function getEntityId() { if ($this->entityId === false) { $this->entityId = $this->siteLinkLookup->getItemIdForSiteLink(new SiteLink($this->siteId, $this->title->getPrefixedText())); if ($this->entityId === null) { wfDebugLog('UpdateRepo', "Couldn't find an item for {$this->title->getPrefixedText()}"); } } return $this->entityId; }
/** * @param string $site * @param string $title * * @throws UsageException If no such entity is found. * @return EntityId The ID of the entity connected to $title on $site. */ protected function getEntityIdFromSiteTitleCombination($site, $title) { // FIXME: Normalization missing, see T47282. $itemId = $this->siteLinkLookup->getItemIdForLink($site, $title); if ($itemId === null) { $this->errorReporter->dieError('No entity found matching site link ' . $site . ':' . $title, 'no-such-entity-link'); } return $itemId; }
/** * 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; }
/** * Returns a list of entities a given site is subscribed to. * * @param string $siteId Site ID of the client site. * @param EntityId[]|null $entityIds The entities we are interested in, or null for "any". * * @return EntityId[] a list of entity IDs the client wiki is subscribed to. * The result is limited to entity ids also present in $entityIds, if given. */ public function getSubscriptions($siteId, array $entityIds) { // NOTE: non-Item ids are ignored, since only items can be subscribed to // via sitelinks. $entityIds = $this->getItemIds($entityIds); $numericIds = array_keys($entityIds); if (empty($numericIds)) { return array(); } $links = $this->siteLinkLookup->getLinks($numericIds, array($siteId)); // collect the item IDs present in these links $linkedItems = array(); foreach ($links as $link) { list(, , $id) = $link; $linkedItems[$id] = $entityIds[$id]; } return $linkedItems; }
/** * @param Parser $parser * @param PPFrame $frame * @param array $args * * @return EntityId|null */ private function getEntityIdForStatementListProvider(Parser $parser, PPFrame $frame, array $args) { $from = $frame->getArgument('from'); if ($from && $this->allowArbitraryDataAccess) { $entityId = $this->getEntityIdFromString($parser, $from); } else { $title = $parser->getTitle(); $siteLink = new SiteLink($this->siteId, $title->getPrefixedText()); $entityId = $this->siteLinkLookup->getItemIdForSiteLink($siteLink); } return $entityId; }
/** * @see SpecialWikibasePage::execute * * @since 0.1 * * @param string|null $subPage */ public function execute($subPage) { parent::execute($subPage); // Setup $request = $this->getRequest(); $parts = $subPage === '' ? array() : explode('/', $subPage, 2); $site = trim($request->getVal('site', isset($parts[0]) ? $parts[0] : '')); $page = trim($request->getVal('page', isset($parts[1]) ? $parts[1] : '')); $itemContent = null; // If there are enough data, then try to lookup the item content if ($site !== '' && $page !== '') { // FIXME: This code is duplicated in ItemByTitleHelper::getItemId! // Try to get a item content $siteId = $this->stringNormalizer->trimToNFC($site); // no stripping of underscores here! $pageName = $this->stringNormalizer->trimToNFC($page); if (!$this->sites->getSite($siteId)) { // HACK: If the site ID isn't known, add "wiki" to it; this allows the wikipedia // subdomains to be used to refer to wikipedias, instead of requiring their // full global id to be used. // @todo: Ideally, if the site can't be looked up by global ID, we // should try to look it up by local navigation ID. // Support for this depends on bug T50934. $siteId .= 'wiki'; } /* @var ItemHandler $itemHandler */ $itemId = $this->siteLinkLookup->getItemIdForLink($siteId, $pageName); // Do we have an item content, and if not can we try harder? if ($itemId === null) { // Try harder by requesting normalization on the external site $siteObj = $this->sites->getSite($siteId); if ($siteObj instanceof Site) { $pageName = $siteObj->normalizePageName($page); $itemId = $this->siteLinkLookup->getItemIdForLink($siteId, $pageName); } } // Redirect to the item page if we found its content if ($itemId !== null) { $title = $this->titleLookup->getTitleForId($itemId); $query = $request->getValues(); unset($query['title']); unset($query['site']); unset($query['page']); $itemUrl = $title->getFullUrl($query); $this->getOutput()->redirect($itemUrl); return; } } // If there is no item content post the switch form $this->switchForm($site, $page); }
/** * Tries to find item id for given siteId and title combination * * @param string $siteId * @param string $title * @param bool $normalize * * @return ItemId|null */ private function getItemId($siteId, $title, $normalize) { // FIXME: This code is duplicated in SpecialItemByTitle::execute! $title = $this->stringNormalizer->trimToNFC($title); $id = $this->siteLinkLookup->getItemIdForLink($siteId, $title); // Try harder by requesting normalization on the external site. if ($id === null && $normalize === true) { $siteObj = $this->siteStore->getSite($siteId); //XXX: this passes the normalized title back into $title by reference... $this->normalizeTitle($title, $siteObj); $id = $this->siteLinkLookup->getItemIdForLink($siteObj->getGlobalId(), $title); } return $id; }
/** * Load the sitelink using a SiteLinkLookup. Resolves item redirects, if needed. * * @param string $site * @param ItemId $itemId * * @return array[] */ private function loadLinks($site, ItemId $itemId) { $links = $this->siteLinkLookup->getLinks(array($itemId->getNumericId()), array($site)); if (isset($links[0])) { return $links; } // Maybe the item is a redirect: Try to resolve the redirect and load // the links from there. $redirectTarget = $this->redirectLookup->getRedirectForEntityId($itemId); if ($redirectTarget instanceof ItemId) { return $this->siteLinkLookup->getLinks(array($redirectTarget->getNumericId()), array($site)); } return array(); }
/** * @see UsageLookup::getUnusedEntities * * @param EntityId[] $entityIds * * @return EntityId[] A list of elements of $entities that are unused. */ public function getUnusedEntities(array $entityIds) { if (empty($entityIds)) { return array(); } // Non-item entities are always considered unused by this implementation. $nonItemIds = array_filter($entityIds, function (EntityId $id) { return !$id instanceof ItemId; }); $numericItemIds = $this->getNumericItemIds($entityIds); $rows = $this->siteLinkLookup->getLinks($numericItemIds, array($this->clientSiteId)); $used = $this->getItemIdsFromSiteLinkRows($rows); $unusedIds = array_diff($numericItemIds, $used); return array_merge($nonItemIds, $this->makeItemIds($unusedIds)); }
/** * @param string $prefixedEntityId * * @since 0.5 * @return string|null Null if no site link found. */ public function getSiteLinkPageName($prefixedEntityId) { try { $itemId = new ItemId($prefixedEntityId); } catch (InvalidArgumentException $e) { return null; } // @fixme the SiteLinks do not contain badges! but all we want here is page name. $siteLinkRows = $this->siteLinkLookup->getLinks(array($itemId->getNumericId()), array($this->siteId)); foreach ($siteLinkRows as $siteLinkRow) { $siteLink = new SiteLink($siteLinkRow[0], $siteLinkRow[1]); $this->usageAccumulator->addTitleUsage($itemId); return $siteLink->getPageName(); } return null; }
/** * @param string $globalSiteId * @param string $pageTitle * * @return ItemId|null */ private function getAndCacheItemIdForLink($globalSiteId, $pageTitle) { $itemId = $this->lookup->getItemIdForLink($globalSiteId, $pageTitle); $this->cache->set($this->getByPageCacheKey($globalSiteId, $pageTitle), $itemId instanceof ItemId ? $itemId->getSerialization() : null, $this->cacheDuration); return $itemId; }
/** * @param IContextSource $context * @param Title $title * * @return array */ private function getPageInfoRow(IContextSource $context, Title $title) { $entityId = $this->siteLinkLookup->getItemIdForSiteLink(new SiteLink($this->siteId, $title->getPrefixedText())); $row = $entityId ? $this->getItemPageInfo($context, $entityId) : $this->getUnconnectedItemPageInfo($context); return $row; }
/** * @param Title $title * * @return Item|null */ private function getItemId(Title $title) { $siteLink = new SiteLink($this->localSiteId, $title->getPrefixedText()); return $this->siteLinkLookup->getItemIdForSiteLink($siteLink); }
/** * @param Title $title * * @return ItemId|null */ private function getItemIdForTitle(Title $title) { return $this->siteLinkLookup->getItemIdForLink($this->siteId, $title->getPrefixedText()); }
/** * Returns a usage lookup based on $siteLinklookup. * Local page IDs are spoofed using the numeric item ID as the local page ID. * * @param SiteLinkLookup $siteLinkLookup * * @return UsageLookup */ private function getUsageLookup(SiteLinkLookup $siteLinkLookup) { $usageLookup = $this->getMock('Wikibase\\Client\\Usage\\UsageLookup'); $usageLookup->expects($this->any())->method('getPagesUsing')->will($this->returnCallback(function ($ids) use($siteLinkLookup) { $pages = array(); foreach ($ids as $id) { if (!$id instanceof ItemId) { continue; } $links = $siteLinkLookup->getSiteLinksForItem($id); foreach ($links as $link) { if ($link->getSiteId() === 'enwiki') { // we use the numeric item id as the fake page id of the local page! $usages = array(new EntityUsage($id, EntityUsage::SITELINK_USAGE), new EntityUsage($id, EntityUsage::LABEL_USAGE, 'en')); $pages[] = new PageEntityUsages($id->getNumericId(), $usages); } } } return new ArrayIterator($pages); })); return $usageLookup; }