public function testPruneStaleUsages()
 {
     $t1 = '20150111000000';
     $t2 = '20150222000000';
     $q3 = new ItemId('Q3');
     $q4 = new ItemId('Q4');
     $q5 = new ItemId('Q5');
     $usagesT1 = array(new EntityUsage($q3, EntityUsage::SITELINK_USAGE), new EntityUsage($q3, EntityUsage::LABEL_USAGE), new EntityUsage($q4, EntityUsage::LABEL_USAGE));
     $usagesT2 = array(new EntityUsage($q3, EntityUsage::SITELINK_USAGE), new EntityUsage($q4, EntityUsage::LABEL_USAGE), new EntityUsage($q5, EntityUsage::ALL_USAGE));
     // init database: $usagesT2 get timestamp $t2,
     // array_diff( $usagesT1, $usagesT2 ) get timestamp $t1.
     $this->tracker->trackUsedEntities(23, $usagesT1, $t1);
     $this->tracker->trackUsedEntities(23, $usagesT2, $t2);
     // pruning should remove entries with a timestamp < $t2
     $this->tracker->pruneStaleUsages(23, $t2);
     $actualUsages = $this->getUsages(23, $t2);
     $this->assertSameUsages($usagesT2, $actualUsages);
 }
 /**
  * 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);
     }
 }