/** * @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; }
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 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()); } } }
/** * @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; }
public function getApiItemUrl(ItemId $id) : string { return $this->apiUrl . '/items/' . $id->getSerialization(); }
/** * 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); }
private function indexByItemId(ItemId $itemId, SiteLink $siteLink) { $prefixedId = $itemId->getSerialization(); $this->linksByItemId[$prefixedId][] = $siteLink; }
/** * @param ItemId $id * @param string[] $labels * @param Statement[]|null $statements * @param SiteLink[]|null $siteLinks * * @return Item */ private function createTestItem(ItemId $id, array $labels, array $statements = null, array $siteLinks = null) { $item = new Item($id); $item->getFingerprint()->setDescription('de', 'Description of ' . $id->getSerialization()); foreach ($labels as $lang => $label) { $item->setLabel($lang, $label); } if ($statements !== null) { $item->setStatements(new StatementList($statements)); } if ($siteLinks !== null) { $item->setSiteLinkList(new SiteLinkList($siteLinks)); } $this->siteLinkLookup->putEntity($item); return $item; }
private function markIdAsProcessed(ItemId $itemId) { file_put_contents($this->getProcessedListPath(), $itemId->getSerialization() . PHP_EOL, FILE_APPEND); }
/** * 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; }
/** * 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; }
/** * @dataProvider serializationProvider */ public function testUnserialize($json, $expected) { $id = new ItemId('Q1'); $id->unserialize($json); $this->assertSame($expected, $id->getSerialization()); }
/** * Verify a created job * * @param JobSpecification $job */ public function verifyJob(JobSpecification $job) { $itemId = new ItemId('Q123'); $moveData = $this->getFakeMoveData(); $this->assertInstanceOf('IJobSpecification', $job); $this->assertEquals('UpdateRepoOnMove', $job->getType()); $params = $job->getParams(); $this->assertEquals($moveData['siteId'], $params['siteId']); $this->assertEquals($moveData['oldTitle'], $params['oldTitle']); $this->assertEquals($moveData['newTitle'], $params['newTitle']); $this->assertEquals($moveData['user'], $params['user']); $this->assertEquals($itemId->getSerialization(), $params['entityId']); }
/** * @see QueryPage::linkParameters * * @return array */ public function linkParameters() { return array('badge' => $this->badgeId->getSerialization()); }
/** * 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; }
public function testGetDescription_usage() { $usages = new HashUsageAccumulator(); $wikibaseLuaBindings = $this->getWikibaseLuaBindings(new MockRepository(), new HashSiteLinkStore(), $usages); $itemId = new ItemId('Q7'); $wikibaseLuaBindings->getDescription($itemId->getSerialization()); $this->assertTrue($this->hasUsage($usages->getUsages(), $itemId, EntityUsage::OTHER_USAGE), 'other usage'); $this->assertFalse($this->hasUsage($usages->getUsages(), $itemId, EntityUsage::LABEL_USAGE), 'label usage'); $this->assertFalse($this->hasUsage($usages->getUsages(), $itemId, EntityUsage::TITLE_USAGE), 'title usage'); $this->assertFalse($this->hasUsage($usages->getUsages(), $itemId, EntityUsage::ALL_USAGE), 'all usage'); }