/**
  * @see UsageAccumulator::addUsage
  *
  * @param EntityUsage $usage
  */
 public function addUsage(EntityUsage $usage)
 {
     $usages = $this->getUsages();
     $key = $usage->getIdentityString();
     $usages[$key] = $usage;
     $this->parserOutput->setExtensionData('wikibase-entity-usage', $usages);
 }
 public function testNewSpec()
 {
     $usageQ5X = new EntityUsage(new ItemId('Q5'), 'X');
     $title = Title::makeTitle(NS_MAIN, 'Foo');
     $title->resetArticleID(17);
     $touched = '20150101000000';
     $usages = array($usageQ5X);
     $spec = AddUsagesForPageJob::newSpec($title, $usages, $touched);
     $expected = array('pageId' => $title->getArticleID(), 'usages' => array($usageQ5X->asArray()), 'touched' => '20150101000000');
     $this->assertEquals('wikibase-addUsagesForPage', $spec->getType());
     $this->assertEquals($title->getFullText(), $spec->getTitle()->getFullText());
     $this->assertEquals($expected, $spec->getParams());
 }
 public function testGetPagesUsing()
 {
     $q3 = new ItemId('Q3');
     $q4 = new ItemId('Q4');
     $q6 = new ItemId('Q6');
     $u3s = new EntityUsage($q3, EntityUsage::SITELINK_USAGE);
     $u3l = new EntityUsage($q3, EntityUsage::LABEL_USAGE, 'de');
     $u4l = new EntityUsage($q4, EntityUsage::LABEL_USAGE, 'de');
     $u4t = new EntityUsage($q4, EntityUsage::TITLE_USAGE);
     $this->putUsages(23, array($u3s, $u3l, $u4l), '20150102030405');
     $this->putUsages(42, array($u4l, $u4t), '20150102030405');
     $pages = $this->lookup->getPagesUsing(array($q6));
     Assert::assertEmpty(iterator_to_array($pages));
     $pages = $this->lookup->getPagesUsing(array($q3));
     $this->assertSamePageEntityUsages(array(23 => new PageEntityUsages(23, array($u3s, $u3l))), iterator_to_array($pages), 'Pages using Q3');
     $pages = $this->lookup->getPagesUsing(array($q4, $q3), array(EntityUsage::makeAspectKey(EntityUsage::LABEL_USAGE, 'de')));
     $this->assertSamePageEntityUsages(array(23 => new PageEntityUsages(23, array($u3l, $u4l)), 42 => new PageEntityUsages(42, array($u4l))), iterator_to_array($pages), 'Pages using "label" on Q4 or Q3');
     $pages = $this->lookup->getPagesUsing(array($q3), array(EntityUsage::ALL_USAGE));
     Assert::assertEmpty(iterator_to_array($pages), 'Pages using "all" on Q3');
     $pages = $this->lookup->getPagesUsing(array($q4), array(EntityUsage::SITELINK_USAGE));
     Assert::assertEmpty(iterator_to_array($pages), 'Pages using "sitelinks" on Q4');
     $pages = $this->lookup->getPagesUsing(array($q3, $q4), array(EntityUsage::TITLE_USAGE, EntityUsage::SITELINK_USAGE));
     Assert::assertCount(2, iterator_to_array($pages), 'Pages using "title" or "sitelinks" on Q3 or Q4');
     $this->putUsages(23, array(), '20150102030405');
 }
 private function assertUsageTracking(ItemId $id, $aspect, ParserOutput $parserOutput)
 {
     $usageAcc = new ParserOutputUsageAccumulator($parserOutput);
     $usages = $usageAcc->getUsages();
     $expected = new EntityUsage($id, $aspect);
     $usageIdentities = array_map(function (EntityUsage $usage) {
         return $usage->getIdentityString();
     }, $usages);
     $expectedIdentities = array($expected->getIdentityString());
     $this->assertEquals($expectedIdentities, array_values($usageIdentities));
 }
 public function getChangedAspectsProvider()
 {
     $changeFactory = TestChanges::getEntityChangeFactory();
     $cases = array();
     $q1 = new ItemId('Q1');
     $q2 = new ItemId('Q2');
     $cases['create linked item Q1'] = array(array(EntityUsage::SITELINK_USAGE, EntityUsage::TITLE_USAGE), $changeFactory->newFromUpdate(ItemChange::ADD, null, $this->getItemWithSiteLinks($q1, array('enwiki' => '1'))));
     $cases['unlink item Q1'] = array(array(EntityUsage::SITELINK_USAGE, EntityUsage::TITLE_USAGE), $changeFactory->newFromUpdate(ItemChange::UPDATE, $this->getItemWithSiteLinks($q1, array('enwiki' => '1')), new Item($q1)));
     $cases['link item Q2'] = array(array(EntityUsage::SITELINK_USAGE, EntityUsage::TITLE_USAGE), $changeFactory->newFromUpdate(ItemChange::UPDATE, new Item($q2), $this->getItemWithSiteLinks($q2, array('enwiki' => '2'))));
     $cases['change link of Q1'] = array(array(EntityUsage::SITELINK_USAGE, EntityUsage::TITLE_USAGE), $changeFactory->newFromUpdate(ItemChange::UPDATE, $this->getItemWithSiteLinks($q1, array('enwiki' => '1')), $this->getItemWithSiteLinks($q1, array('enwiki' => '2'))));
     $cases['delete linked item Q2'] = array(array(EntityUsage::SITELINK_USAGE, EntityUsage::TITLE_USAGE), $changeFactory->newFromUpdate(ItemChange::REMOVE, $this->getItemWithSiteLinks($q2, array('enwiki' => '2')), null), 'item connected to client was deleted');
     $cases['add another sitelink to Q2'] = array(array(EntityUsage::SITELINK_USAGE), $changeFactory->newFromUpdate(ItemChange::UPDATE, $this->getItemWithSiteLinks($q2, array('enwiki' => '2')), $this->getItemWithSiteLinks($q2, array('enwiki' => '2', 'itwiki' => 'DUE'))));
     $cases['alias change on Q1'] = array(array(EntityUsage::OTHER_USAGE), $changeFactory->newFromUpdate(ItemChange::UPDATE, new Item($q1), $this->getItemWithAliases($q1, 'de', array('EINS'))));
     $cases['local label change on Q1 (used by Q2)'] = array(array(EntityUsage::makeAspectKey(EntityUsage::LABEL_USAGE, 'en')), $changeFactory->newFromUpdate(ItemChange::UPDATE, new Item($q1), $this->getItemWithLabel($q1, 'en', 'ONE')));
     $badges = array(new ItemId('Q34'));
     $cases['badge only change on Q1'] = array(array(EntityUsage::SITELINK_USAGE), $changeFactory->newFromUpdate(ItemChange::UPDATE, $this->getItemWithSiteLinks($q1, array('enwiki' => '1')), $this->getItemWithSiteLinks($q1, array('enwiki' => '1'), $badges)));
     $cases['statement change on Q1'] = array(array(EntityUsage::OTHER_USAGE), $changeFactory->newFromUpdate(ItemChange::UPDATE, new Item($q1), $this->getItemWithStatement($q1, new PropertyId('P5'), new StringValue('Hello'))));
     return $cases;
 }
 private function hasUsage($actualUsages, EntityId $entityId, $aspect)
 {
     $usage = new EntityUsage($entityId, $aspect);
     $key = $usage->getIdentityString();
     return isset($actualUsages[$key]);
 }
 /**
  * @dataProvider provideMakeAspectKey
  */
 public function testMakeAspectKey($aspect, $modifier, $expectedKey)
 {
     $key = EntityUsage::makeAspectKey($aspect, $modifier);
     $this->assertEquals($expectedKey, $key);
 }
 /**
  * @param Traversable $rows
  *
  * @return PageEntityUsages[]
  */
 private function foldRowsIntoPageEntityUsages(Traversable $rows)
 {
     $usagesPerPage = array();
     foreach ($rows as $row) {
         $pageId = (int) $row->eu_page_id;
         if (isset($usagesPerPage[$pageId])) {
             $pageEntityUsages = $usagesPerPage[$pageId];
         } else {
             $pageEntityUsages = new PageEntityUsages($pageId);
         }
         $entityId = $this->idParser->parse($row->eu_entity_id);
         list($aspect, $modifier) = EntityUsage::splitAspectKey($row->eu_aspect);
         $usage = new EntityUsage($entityId, $aspect, $modifier);
         $pageEntityUsages->addUsages(array($usage));
         $usagesPerPage[$pageId] = $pageEntityUsages;
     }
     return $usagesPerPage;
 }
 /**
  * @param Title[] $titles
  * @param EntityId $entityId
  * @param string[] $aspects
  *
  * @return PageEntityUsages[]
  */
 private function makeVirtualUsages(array $titles, EntityId $entityId, array $aspects)
 {
     $usagesForItem = array();
     foreach ($aspects as $aspect) {
         list($aspect, $modifier) = EntityUsage::splitAspectKey($aspect);
         $usagesForItem[] = new EntityUsage($entityId, $aspect, $modifier);
     }
     $usagesPerPage = array();
     foreach ($titles as $title) {
         $pageId = $title->getArticleID();
         if ($pageId === 0) {
             wfDebugLog('WikibaseChangeNotification', __METHOD__ . ': Article ID for ' . $title->getFullText() . ' is 0.');
             continue;
         }
         $usagesPerPage[$pageId] = new PageEntityUsages($pageId, $usagesForItem);
     }
     return $usagesPerPage;
 }
 /**
  * @param string[] $aspectKeys Array of aspect keys, with modifiers applied.
  * @param string[] $relevant Array of aspects (without modifiers).
  * @param array &$aspects Associative array of aspect keys (with modifiers) as keys, the values
  * being meaningless (a.k.a. HashSet).
  */
 private function intersectAspectsIntoKeys(array $aspectKeys, array $relevant, array &$aspects)
 {
     $relevant = array_flip($relevant);
     foreach ($aspectKeys as $aspectKey) {
         $aspect = EntityUsage::stripModifier($aspectKey);
         if (array_key_exists($aspect, $relevant)) {
             $aspects[$aspectKey] = null;
         }
     }
 }