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); }
/** * @param ItemId $id * @param ItemId $target * * @throws RuntimeException * @return EntityContent */ protected function makeItemRedirectContent(ItemId $id, ItemId $target) { $title = Title::newFromText($target->getSerialization()); $redirect = new EntityRedirect($id, $target); $content = ItemContent::newFromRedirect($redirect, $title); return $content; }
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); }
public function testAsArray() { $id = new ItemId('Q7'); $aspect = EntityUsage::LABEL_USAGE; $modifier = 'ru'; $expected = array('entityId' => $id->getSerialization(), 'aspect' => $aspect, 'modifier' => null); $usage = new EntityUsage($id, $aspect); $this->assertEquals($expected, $usage->asArray()); $expected['modifier'] = $modifier; $usage = new EntityUsage($id, $aspect, $modifier); $this->assertEquals($expected, $usage->asArray()); }
public function execute(CommandHelper $commandHelper) { $db = $commandHelper->getDatabase(); $wikibaseFactory = $commandHelper->getWikibaseFactory(); $revisionsGetter = $wikibaseFactory->newRevisionGetter(); $siteLinkSetter = $wikibaseFactory->newSiteLinkSetter(); $wiki = $commandHelper->getOption('wiki'); $badgeId = new ItemId($commandHelper->getOption('badge')); $editInfo = new EditInfo($this->getSummary($commandHelper->getOption('summary'), $commandHelper->getOption('category'), $wiki, $badgeId->getSerialization()), false, $commandHelper->getOption('bot')); $results = $db->query('SELECT page_title FROM categorylinks JOIN page ON page_id = cl_from WHERE cl_to = "' . $db->escape_string($commandHelper->getOption('category')) . '" AND page_namespace = 0'); $skipped = 0; $added = 0; $failed = 0; if ($results) { while ($row = $results->fetch_assoc()) { try { /** @var Item $item */ $revision = $revisionsGetter->getFromSiteAndTitle($wiki, $row['page_title']); if ($revision === false) { $commandHelper->writeln("\nNo item found for {$wiki}:{$row['page_title']}"); $failed++; continue; } $item = $revision->getContent()->getData(); $badges = $item->getSiteLinkList()->getBySiteId($wiki)->getBadges(); if (in_array($badgeId, $badges)) { $commandHelper->write('.'); $skipped++; continue; } $badges[] = $badgeId; $siteLinkSetter->set(new SiteLink($wiki, $row['page_title'], $badges), new SiteLink($wiki, $row['page_title']), $editInfo); $commandHelper->writeln("\nAdded badge for {$wiki}:{$row['page_title']}"); $added++; } catch (Exception $ex) { $commandHelper->writeln("\nFailed to add badge for {$wiki}:{$row['page_title']} (" . $ex->getMessage() . ")"); $failed++; } } } $results->free(); $commandHelper->writeln("Finished iterating through {$results->num_rows} site links."); $commandHelper->writeln("Added: {$added}"); $commandHelper->writeln("Skipped: {$skipped}"); $commandHelper->writeln("Failed: {$failed}"); }
public function getEntityTitleProvider() { $itemId = ItemId::newFromNumber(388); $propertyId = PropertyId::newFromNumber(472); $settings = $this->getRepoSettings(); return array(array('Q388', $settings[0], $itemId), array('Item:Q388', $settings[2], $itemId), array('Property:P472', $settings[0], $propertyId)); }
private function parseItemList(array $itemNumericIds) { $itemIds = []; foreach ($itemNumericIds as $itemNumericId) { $itemIds[] = ItemId::newFromNumber($itemNumericId); } return $itemIds; }
/** * @param string $entityType * @param int|float|string $numericId * * @return EntityId * @throws InvalidArgumentException */ public static function newIdFromTypeAndNumber($entityType, $numericId) { if ($entityType === 'item') { return ItemId::newFromNumber($numericId); } elseif ($entityType === 'property') { return PropertyId::newFromNumber($numericId); } throw new InvalidArgumentException('Invalid entityType ' . $entityType); }
/** * Can be integer since 0.1. * Can be ItemId since 0.5. * Can be null since 1.0. * * @param ItemId|int|null $id * * @throws InvalidArgumentException */ public function setId($id) { if ($id === null || $id instanceof ItemId) { $this->id = $id; } elseif (is_int($id)) { $this->id = ItemId::newFromNumber($id); } else { throw new InvalidArgumentException('$id must be an instance of ItemId, an integer,' . ' or null'); } }
/** * Verify a created job * * @param JobSpecification $job */ public function verifyJob(JobSpecification $job) { $itemId = new ItemId('Q123'); $data = $this->getFakeData(); $this->assertInstanceOf('IJobSpecification', $job); $this->assertEquals('UpdateRepoOnDelete', $job->getType()); $params = $job->getParams(); $this->assertEquals($data['siteId'], $params['siteId']); $this->assertEquals($data['title'], $params['title']); $this->assertEquals($data['user'], $params['user']); $this->assertEquals($itemId->getSerialization(), $params['entityId']); }
/** * @param ResultWrapper $res A result set with the ips_item_id and ips_site_id fields * set for each row. * @param array &$continuation Single item ID => site ID pair or empty. * * @return array[] An associative array mapping item IDs to lists of site IDs. */ private function getSubscriptionsPerItemFromRows(ResultWrapper $res, &$continuation = array()) { $subscriptionsPerItem = array(); $currentItemId = 0; $itemId = null; foreach ($res as $row) { if ($row->ips_item_id != $currentItemId) { $currentItemId = $row->ips_item_id; $itemId = ItemId::newFromNumber($currentItemId)->getSerialization(); } $subscriptionsPerItem[$itemId][] = $row->ips_site_id; $continuation = array($currentItemId, $row->ips_row_id); } return $subscriptionsPerItem; }
public function provideIds() { return array(array('Q1'), array(ItemId::newFromNumber(1))); }
/** * @param int[] $numericIds * * @return ItemId[] */ private function makeItemIds(array $numericIds) { return array_map(function ($numericId) { return ItemId::newFromNumber($numericId); }, $numericIds); }
public function addStatementToEntity(Statement $statement, ItemId $itemId, $baseRevisionId = null) { if ($this->currentItemId !== null && !$this->currentItemId->equals($itemId)) { $this->saveStatements(); } $this->currentItemId = $itemId; $this->currentBaseRevisionId = $baseRevisionId; $this->statements[] = $statement; }
protected function getItemIdsFromRows($rows) { $itemIds = array(); foreach ($rows as $row) { $itemIds[] = ItemId::newFromNumber((int) $row->entity_id); } return $itemIds; }
/** * @dataProvider invalidNumericIdProvider */ public function testNewFromNumberWithInvalidNumericId($number) { $this->setExpectedException('InvalidArgumentException'); ItemId::newFromNumber($number); }
/** * @see QueryPage::linkParameters * * @return array */ public function linkParameters() { return array('badge' => $this->badgeId->getSerialization()); }
public function testDeleteTermsForEntity() { $lookup = $this->getTermIndex(); $id = new ItemId('Q10'); $item = new Item($id); $item->setLabel('en', 'abc'); $item->setLabel('de', 'def'); $item->setLabel('nl', 'ghi'); $item->setDescription('en', 'testDeleteTermsForEntity'); $item->setAliases('fr', array('o', '_', 'O')); $lookup->saveTermsOfEntity($item); $this->assertTermExists($lookup, 'testDeleteTermsForEntity'); $this->assertTrue($lookup->deleteTermsOfEntity($item->getId()) !== false); $this->assertNotTermExists($lookup, 'testDeleteTermsForEntity'); $abc = new TermIndexEntry(array('termType' => TermIndexEntry::TYPE_LABEL, 'termText' => 'abc')); $matchedTerms = $lookup->getMatchingTerms(array($abc), array(TermIndexEntry::TYPE_LABEL), Item::ENTITY_TYPE); foreach ($matchedTerms as $matchedTerm) { if ($matchedTerm->getEntityId() === $id) { $this->fail('Failed to delete term or entity: ' . $id->getSerialization()); } } }
/** * Get Message for a conflict * * @param array $conflict A record as returned by SiteLinkConflictLookup::getConflictsForItem() * * @return Error */ private function getConflictError(array $conflict) { $entityId = ItemId::newFromNumber($conflict['itemId']); return new UniquenessViolation($entityId, 'SiteLink conflict', 'sitelink-conflict', array(new SiteLink($conflict['siteId'], $conflict['sitePage']), $entityId)); }
/** * @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; }
private function indexByItemId(ItemId $itemId, SiteLink $siteLink) { $prefixedId = $itemId->getSerialization(); $this->linksByItemId[$prefixedId][] = $siteLink; }
public function testGetNamespaceForType() { $factory = $this->newFactory(); $id = new ItemId('Q42'); $ns = $factory->getNamespaceForType($id->getEntityType()); $this->assertGreaterThanOrEqual(0, $ns, 'namespace'); }
/** * Makes sure that calling $method with $params will purge the cache * for Q1. * * @param string $method * @param array $params */ private function purgeMethodTest($method, array $params) { $fromSlave = EntityRevisionLookup::LATEST_FROM_SLAVE; $q1 = new ItemId('Q1'); $lookup = $this->getMock('Wikibase\\Lib\\Store\\Sql\\WikiPageEntityMetaDataAccessor'); $lookup->expects($this->exactly(2))->method('loadRevisionInformation')->with(array($q1->getSerialization() => $q1))->will($this->returnCallback(function (array $entityIds) { static $firstCall = true; if ($firstCall) { $firstCall = false; return array('Q1' => 'Foo'); } else { return array('Q1' => 'Bar'); } })); $accessor = new PrefetchingWikiPageEntityMetaDataAccessor($lookup); $rows = $accessor->loadRevisionInformation(array($q1), $fromSlave); $result = $rows[$q1->getSerialization()]; $this->assertSame('Foo', $result); call_user_func_array(array($accessor, $method), $params); // Load it again after purge $rows = $accessor->loadRevisionInformation(array($q1), $fromSlave); $result = $rows[$q1->getSerialization()]; $this->assertSame('Bar', $result); }
/** * Validates badges from params and turns them into an array of ItemIds. * * @param string[] $badgesParams * * @return ItemId[] */ protected function parseSiteLinkBadges(array $badgesParams) { $badges = array(); foreach ($badgesParams as $badgeSerialization) { try { $badgeId = new ItemId($badgeSerialization); } catch (InvalidArgumentException $ex) { $this->errorReporter->dieError('Badges: could not parse "' . $badgeSerialization . '", the id is invalid', 'invalid-entity-id'); continue; } if (!array_key_exists($badgeId->getSerialization(), $this->badgeItems)) { $this->errorReporter->dieError('Badges: item "' . $badgeSerialization . '" is not a badge', 'not-badge'); } $itemTitle = $this->getTitleLookup()->getTitleForId($badgeId); if (is_null($itemTitle) || !$itemTitle->exists()) { $this->errorReporter->dieError('Badges: no item found matching id "' . $badgeSerialization . '"', 'no-such-entity'); } $badges[] = $badgeId; } return $badges; }
public function getApiItemUrl(ItemId $id) : string { return $this->apiUrl . '/items/' . $id->getSerialization(); }
/** * @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 $direction either 'from' or 'to' * @param ItemId $getId * @param string|null $customSummary * * @return Summary */ private function getSummary($direction, $getId, $customSummary = null) { $summary = new Summary('wbmergeitems', $direction, null, array($getId->getSerialization())); if ($customSummary !== null) { $summary->setUserSummary($customSummary); } return $summary; }
/** * Validates badges from params and turns them into an array of ItemIds. * * @param string[] $badges * @param Status $status * * @return ItemId[]|boolean */ private function parseBadges(array $badges, Status $status) { $badgesObjects = array(); foreach ($badges as $badge) { try { $badgeId = new ItemId($badge); } catch (InvalidArgumentException $ex) { $status->fatal('wikibase-wikibaserepopage-not-itemid', $badge); return false; } if (!array_key_exists($badgeId->getSerialization(), $this->badgeItems)) { $status->fatal('wikibase-setsitelink-not-badge', $badgeId->getSerialization()); return false; } $itemTitle = $this->getEntityTitle($badgeId); if (is_null($itemTitle) || !$itemTitle->exists()) { $status->fatal('wikibase-wikibaserepopage-invalid-id', $badgeId); return false; } $badgesObjects[] = $badgeId; } return $badgesObjects; }
/** * Returns an array of SiteLink objects for an item. If the item isn't known or not an Item, * an empty array is returned. * * @since 0.4 * * @param ItemId $itemId * * @return SiteLink[] */ public function getSiteLinksForItem(ItemId $itemId) { $cacheKey = $this->cacheKeyPrefix . ':sitelinks:' . $itemId->getSerialization(); $siteLinks = $this->cache->get($cacheKey); if (!is_array($siteLinks)) { $siteLinks = $this->lookup->getSiteLinksForItem($itemId); $this->cache->set($cacheKey, $siteLinks, $this->cacheDuration); } return $siteLinks; }
private function markIdAsProcessed(ItemId $itemId) { file_put_contents($this->getProcessedListPath(), $itemId->getSerialization() . PHP_EOL, FILE_APPEND); }