protected function setUp() { parent::setUp(); if (!defined('WB_VERSION')) { $this->markTestSkipped('Entity info tables are not available locally on the client'); } $this->tablesUsed[] = 'wb_property_info'; $this->tablesUsed[] = 'wb_terms'; $this->tablesUsed[] = 'wb_entity_per_page'; $termRows = array(); $infoRows = array(); $eppRows = array(); $pageId = 1000; foreach ($this->getKnownEntities() as $entity) { $eppRows[] = array($entity->getType(), $entity->getId()->getNumericId(), $pageId++, null); $labels = $entity->getFingerprint()->getLabels()->toTextArray(); $descriptions = $entity->getFingerprint()->getDescriptions()->toTextArray(); $aliases = $entity->getFingerprint()->getAliasGroups()->toTextArray(); $termRows = array_merge($termRows, $this->getTermRows($entity->getId(), 'label', $labels)); $termRows = array_merge($termRows, $this->getTermRows($entity->getId(), 'description', $descriptions)); $termRows = array_merge($termRows, $this->getTermRows($entity->getId(), 'alias', $aliases)); if ($entity instanceof Property) { $infoRows[] = array($entity->getId()->getNumericId(), $entity->getDataTypeId(), '{"type":"' . $entity->getDataTypeId() . '"}'); } } foreach ($this->getKnownRedirects() as $from => $toId) { $fromId = new ItemId($from); $eppRows[] = array($fromId->getEntityType(), $fromId->getNumericId(), $pageId++, $toId->getSerialization()); } $this->insertRows('wb_terms', array('term_entity_type', 'term_entity_id', 'term_type', 'term_language', 'term_text', 'term_search_key'), $termRows); $this->insertRows('wb_property_info', array('pi_property_id', 'pi_type', 'pi_info'), $infoRows); $eppColumns = array('epp_entity_type', 'epp_entity_id', 'epp_page_id', 'epp_redirect_target'); $this->insertRows('wb_entity_per_page', $eppColumns, $eppRows); }
private function getItemJson(string $id) : array { // TODO: handle id exception // https://groups.google.com/forum/#!topic/clean-code-discussion/GcQNqWG_fuo $id = new ItemId($id); $itemRow = $this->itemStore->getItemRowByNumericItemId($id->getNumericId()); if ($itemRow === null) { throw new NoNullableReturnTypesException(); } return json_decode($itemRow->getItemJson(), true); }
/** * Returns true if the link matches the given conditions. * * @param ItemId $itemId * @param SiteLink $siteLink * @param int[] $numericIds Numeric (unprefixed) item ids * @param string[] $siteIds * @param string[] $pageNames * * @return bool */ private function linkMatches(ItemId $itemId, SiteLink $siteLink, array $numericIds, array $siteIds, array $pageNames) { return (empty($numericIds) || in_array($itemId->getNumericId(), $numericIds)) && (empty($siteIds) || in_array($siteLink->getSiteId(), $siteIds)) && (empty($pageNames) || in_array($siteLink->getPageName(), $pageNames)); }
/** * Fill the subscription table with rows based on entries in wb_items_per_site. * * @param ItemId|null $startItem The item to start with. */ public function fillSubscriptionTable(ItemId $startItem = null) { $continuation = $startItem === null ? null : array($startItem->getNumericId(), 0); while (true) { $count = $this->processSubscriptionBatch($continuation); if ($count > 0) { $this->progressReporter->reportMessage('Populating subscription table: ' . "inserted {$count} subscriptions, continuing at item #{$continuation[0]}."); } else { break; } } }
/** * @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; }
/** * @see SiteLinkLookup::getSiteLinksForItem * * @param ItemId $itemId * * @return SiteLink[] * @note The SiteLink objects returned by this method do not contain badges! */ public function getSiteLinksForItem(ItemId $itemId) { $numericId = $itemId->getNumericId(); $dbr = $this->getConnection(DB_SLAVE); $rows = $dbr->select($this->table, array('ips_site_id', 'ips_site_page'), array('ips_item_id' => $numericId), __METHOD__); $siteLinks = array(); foreach ($rows as $row) { $siteLinks[] = new SiteLink($row->ips_site_id, $row->ips_site_page); } $this->releaseConnection($dbr); return $siteLinks; }
public function testGetNumericId() { $id = new ItemId('Q1'); $this->assertSame(1, $id->getNumericId()); }
/** * 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(); }
/** * @param ItemId $id * * @return bool */ public function has(ItemId $id) { return array_key_exists($id->getNumericId(), $this->ids); }
/** * Returns a map of fake local page IDs to the corresponding local page names. * The fake page IDs are the IDs of the items that have a sitelink to the * respective page on the local wiki: * * @example if Q100 has a link enwiki => 'Emmy', * then 100 => 'Emmy' will be in the map returned by this method. * * @param array[] $pageNamesPerItemId Assoc array mapping entity IDs to lists of sitelinks. * * @return string[] */ private function getFakePageIdMap(array $pageNamesPerItemId) { $titlesByPageId = array(); $siteId = 'enwiki'; foreach ($pageNamesPerItemId as $idString => $pageNames) { $itemId = new ItemId($idString); // If $links[0] is set, it's considered a link to the local wiki. // The index 0 is effectively an alias for $siteId; if (isset($pageNames[0])) { $pageNames[$siteId] = $pageNames[0]; } if (isset($pageNames[$siteId])) { $pageId = $itemId->getNumericId(); $titlesByPageId[$pageId] = $pageNames[$siteId]; } } return $titlesByPageId; }