public function testGetUnusedEntities()
 {
     $q3 = new ItemId('Q3');
     $q4 = new ItemId('Q4');
     $q6 = new ItemId('Q6');
     $u3i = new EntityUsage($q3, EntityUsage::SITELINK_USAGE);
     $u3l = new EntityUsage($q3, EntityUsage::LABEL_USAGE, 'de');
     $u4l = new EntityUsage($q4, EntityUsage::LABEL_USAGE, 'de');
     $usages = array($u3i, $u3l, $u4l);
     $this->putUsages(23, $usages, '20150102030405');
     Assert::assertEmpty($this->lookup->getUnusedEntities(array($q4)), 'Q4 should not be unused');
     $unused = $this->lookup->getUnusedEntities(array($q4, $q6));
     Assert::assertCount(1, $unused);
     Assert::assertEquals($q6, reset($unused), 'Q6 shouold be unused');
 }
 /**
  * Removes stale usage information for the given page, and removes
  * any subscriptions that have become unnecessary.
  *
  * @param int $pageId The ID of the page the entities are used on.
  * @param string $lastUpdatedBefore timestamp. Entries older than this timestamp will be removed.
  *
  * @see UsageTracker::trackUsedEntities
  *
  * @throws InvalidArgumentException
  */
 public function pruneUsagesForPage($pageId, $lastUpdatedBefore = '00000000000000')
 {
     if (!is_int($pageId)) {
         throw new InvalidArgumentException('$pageId must be an int!');
     }
     if (!is_string($lastUpdatedBefore) || $lastUpdatedBefore === '') {
         throw new InvalidArgumentException('$lastUpdatedBefore must be a timestamp string!');
     }
     $prunedUsages = $this->usageTracker->pruneStaleUsages($pageId, $lastUpdatedBefore);
     $prunedEntityIds = $this->getEntityIds($prunedUsages);
     $unusedIds = $this->usageLookup->getUnusedEntities($prunedEntityIds);
     if (!empty($unusedIds)) {
         // Unsubscribe from anything that was pruned and is otherwise unused.
         $this->subscriptionManager->unsubscribe($this->clientId, $unusedIds);
     }
 }
 /**
  * Returns the page updates implied by the given the change.
  *
  * @param EntityChange $change
  *
  * @return Traversable of PageEntityUsages
  */
 private function getAffectedPages(EntityChange $change)
 {
     $entityId = $change->getEntityId();
     $changedAspects = $this->getChangedAspects($change);
     $usages = $this->usageLookup->getPagesUsing(array($entityId), array_merge($changedAspects, array(EntityUsage::ALL_USAGE)));
     // @todo: use iterators throughout!
     $usages = iterator_to_array($usages, true);
     $usages = $this->transformAllPageEntityUsages($usages, $entityId, $changedAspects);
     if ($change instanceof ItemChange && in_array(EntityUsage::TITLE_USAGE, $changedAspects)) {
         $siteLinkDiff = $change->getSiteLinkDiff();
         $namesFromDiff = $this->getPagesReferencedInDiff($siteLinkDiff);
         $titlesFromDiff = $this->getTitlesFromTexts($namesFromDiff);
         $usagesFromDiff = $this->makeVirtualUsages($titlesFromDiff, $entityId, array(EntityUsage::SITELINK_USAGE));
         //FIXME: we can't really merge if $usages is an iterator, not an array.
         //TODO: Inject $usagesFromDiff "on the fly" while streaming other usages.
         //NOTE: $usages must pass through mergeUsagesInto for re-indexing
         $mergedUsages = array();
         $this->mergeUsagesInto($usages, $mergedUsages);
         $this->mergeUsagesInto($usagesFromDiff, $mergedUsages);
         $usages = $mergedUsages;
     }
     return new ArrayIterator($usages);
 }