public function test_getNumUsableCustomVariables_ShouldReadFromCacheIfPossible()
 {
     $cache = Cache::getCacheGeneral();
     $cache['CustomVariables.NumUsableCustomVariables'] = 10;
     Cache::setCacheGeneral($cache);
     $this->assertSame(10, CustomVariables::getNumUsableCustomVariables());
 }
Exemple #2
0
 /**
  * Tracker requests will automatically trigger the Scheduled tasks.
  * This is useful for users who don't setup the cron,
  * but still want daily/weekly/monthly PDF reports emailed automatically.
  *
  * This is similar to calling the API CoreAdminHome.runScheduledTasks
  */
 public function runScheduledTasks()
 {
     $now = time();
     // Currently, there are no hourly tasks. When there are some,
     // this could be too aggressive minimum interval (some hours would be skipped in case of low traffic)
     $minimumInterval = TrackerConfig::getConfigValue('scheduled_tasks_min_interval');
     // If the user disabled browser archiving, he has already setup a cron
     // To avoid parallel requests triggering the Scheduled Tasks,
     // Get last time tasks started executing
     $cache = Cache::getCacheGeneral();
     if ($minimumInterval <= 0 || empty($cache['isBrowserTriggerEnabled'])) {
         Common::printDebug("-> Scheduled tasks not running in Tracker: Browser archiving is disabled.");
         return;
     }
     $nextRunTime = $cache['lastTrackerCronRun'] + $minimumInterval;
     if (defined('DEBUG_FORCE_SCHEDULED_TASKS') && DEBUG_FORCE_SCHEDULED_TASKS || $cache['lastTrackerCronRun'] === false || $nextRunTime < $now) {
         $cache['lastTrackerCronRun'] = $now;
         Cache::setCacheGeneral($cache);
         Option::set('lastTrackerCronRun', $cache['lastTrackerCronRun']);
         Common::printDebug('-> Scheduled Tasks: Starting...');
         $invokeScheduledTasksUrl = "?module=API&format=csv&convertToUnicode=0&method=CoreAdminHome.runScheduledTasks&trigger=archivephp";
         $cliMulti = new CliMulti();
         $cliMulti->runAsSuperUser();
         $responses = $cliMulti->request(array($invokeScheduledTasksUrl));
         $resultTasks = reset($responses);
         Common::printDebug($resultTasks);
         Common::printDebug('Finished Scheduled Tasks.');
     } else {
         Common::printDebug("-> Scheduled tasks not triggered.");
     }
     Common::printDebug("Next run will be from: " . date('Y-m-d H:i:s', $nextRunTime) . ' UTC');
 }
 public function test_storageCreateACacheEntryIfNoCacheExistsYet()
 {
     $cache = Cache::getCacheGeneral();
     $this->assertArrayNotHasKey('settingsStorage', $cache);
     // make sure there is no cache entry yet
     $this->setSettingValueAndMakeSureCacheGetsCreated('myVal');
     $cache = $this->getCache()->fetch($this->storage->getOptionKey());
     $this->assertEquals(array($this->setting->getKey() => 'myVal'), $cache);
 }
Exemple #4
0
 private function getFromTrackerCache($name, $config)
 {
     $name = $this->prefix($name);
     $cache = Cache::getCacheGeneral();
     if (array_key_exists($name, $cache)) {
         $value = $cache[$name];
         settype($value, $config['type']);
         return $value;
     }
     return $config['default'];
 }
 public static function getMaxCustomVariables()
 {
     $cache = Cache::getCacheGeneral();
     $cacheKey = 'CustomVariables.MaxNumCustomVariables';
     if (!array_key_exists($cacheKey, $cache)) {
         $maxCustomVar = 0;
         foreach (Model::getScopes() as $scope) {
             $model = new Model($scope);
             $highestIndex = $model->getHighestCustomVarIndex();
             if ($highestIndex > $maxCustomVar) {
                 $maxCustomVar = $highestIndex;
             }
         }
         $cache[$cacheKey] = $maxCustomVar;
         Cache::setCacheGeneral($cache);
     }
     return $cache[$cacheKey];
 }
 /**
  * Returns the number of available custom variables that can be used.
  *
  * "Can be used" is identifed by the minimum number of available custom variables across all relevant tables. Eg
  * if there are 6 custom variables installed in log_visit but only 5 in log_conversion, we consider only 5 custom
  * variables as usable.
  * @return int
  */
 public static function getNumUsableCustomVariables()
 {
     $cache = Cache::getCacheGeneral();
     $cacheKey = 'CustomVariables.NumUsableCustomVariables';
     if (!array_key_exists($cacheKey, $cache)) {
         $minCustomVar = null;
         foreach (Model::getScopes() as $scope) {
             $model = new Model($scope);
             $highestIndex = $model->getHighestCustomVarIndex();
             if (!isset($minCustomVar)) {
                 $minCustomVar = $highestIndex;
             }
             if ($highestIndex < $minCustomVar) {
                 $minCustomVar = $highestIndex;
             }
         }
         if (!isset($minCustomVar)) {
             $minCustomVar = 0;
         }
         $cache[$cacheKey] = $minCustomVar;
         Cache::setCacheGeneral($cache);
     }
     return $cache[$cacheKey];
 }
Exemple #7
0
 /**
  * Tracker requests will automatically trigger the Scheduled tasks.
  * This is useful for users who don't setup the cron,
  * but still want daily/weekly/monthly PDF reports emailed automatically.
  *
  * This is similar to calling the API CoreAdminHome.runScheduledTasks
  */
 protected static function runScheduledTasks()
 {
     $now = time();
     // Currently, there are no hourly tasks. When there are some,
     // this could be too aggressive minimum interval (some hours would be skipped in case of low traffic)
     $minimumInterval = Config::getInstance()->Tracker['scheduled_tasks_min_interval'];
     // If the user disabled browser archiving, he has already setup a cron
     // To avoid parallel requests triggering the Scheduled Tasks,
     // Get last time tasks started executing
     $cache = Cache::getCacheGeneral();
     if ($minimumInterval <= 0 || empty($cache['isBrowserTriggerEnabled'])) {
         Common::printDebug("-> Scheduled tasks not running in Tracker: Browser archiving is disabled.");
         return;
     }
     $nextRunTime = $cache['lastTrackerCronRun'] + $minimumInterval;
     if (isset($GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS']) && $GLOBALS['PIWIK_TRACKER_DEBUG_FORCE_SCHEDULED_TASKS'] || $cache['lastTrackerCronRun'] === false || $nextRunTime < $now) {
         $cache['lastTrackerCronRun'] = $now;
         Cache::setCacheGeneral($cache);
         self::initCorePiwikInTrackerMode();
         Option::set('lastTrackerCronRun', $cache['lastTrackerCronRun']);
         Common::printDebug('-> Scheduled Tasks: Starting...');
         // save current user privilege and temporarily assume Super User privilege
         $isSuperUser = Piwik::hasUserSuperUserAccess();
         // Scheduled tasks assume Super User is running
         Piwik::setUserHasSuperUserAccess();
         // While each plugins should ensure that necessary languages are loaded,
         // we ensure English translations at least are loaded
         Translate::loadEnglishTranslation();
         ob_start();
         CronArchive::$url = SettingsPiwik::getPiwikUrl();
         $cronArchive = new CronArchive();
         $cronArchive->runScheduledTasksInTrackerMode();
         $resultTasks = ob_get_contents();
         ob_clean();
         // restore original user privilege
         Piwik::setUserHasSuperUserAccess($isSuperUser);
         foreach (explode('</pre>', $resultTasks) as $resultTask) {
             Common::printDebug(str_replace('<pre>', '', $resultTask));
         }
         Common::printDebug('Finished Scheduled Tasks.');
     } else {
         Common::printDebug("-> Scheduled tasks not triggered.");
     }
     Common::printDebug("Next run will be from: " . date('Y-m-d H:i:s', $nextRunTime) . ' UTC');
 }
Exemple #8
0
 /**
  * Returns the ID of the current LocationProvider (see UserCountry plugin code) from
  * the Tracker cache.
  */
 public static function getCurrentLocationProviderId()
 {
     $cache = TrackerCache::getCacheGeneral();
     return empty($cache['currentLocationProviderId']) ? DefaultProvider::ID : $cache['currentLocationProviderId'];
 }
 public function getCachedInstalledIndexesForScope($scope)
 {
     $cache = Cache::getCacheGeneral();
     $key = 'custom_dimension_indexes_installed_' . $scope;
     if (empty($cache[$key])) {
         return array();
     }
     return $cache[$key];
 }
Exemple #10
0
 private function getCachedUrlsByHostAndIdSite()
 {
     $cache = Cache::getCacheGeneral();
     if (!empty($cache['allUrlsByHostAndIdSite'])) {
         return $cache['allUrlsByHostAndIdSite'];
     }
     return array();
 }
Exemple #11
0
 /**
  * We have previously tried to detect the campaign variables in the URL
  * so at this stage, if the referrer host is the current host,
  * or if the referrer host is any of the registered URL for this website,
  * it is considered a direct entry
  * @return bool
  */
 protected function detectReferrerDirectEntry()
 {
     if (empty($this->referrerHost)) {
         return false;
     }
     $cache = Cache::getCacheGeneral();
     if (!empty($cache['allUrlsByHostAndIdSite'])) {
         $directEntry = new SiteUrls();
         $matchingSites = $directEntry->getIdSitesMatchingUrl($this->referrerUrlParse, $cache['allUrlsByHostAndIdSite']);
         if (isset($matchingSites) && is_array($matchingSites) && in_array($this->idsite, $matchingSites)) {
             $this->typeReferrerAnalyzed = Common::REFERRER_TYPE_DIRECT_ENTRY;
             return true;
         } elseif (isset($matchingSites)) {
             return false;
         }
     }
     // fallback logic if the referrer domain is not known to any site to not break BC
     if (isset($this->currentUrlParse['host'])) {
         // this might be actually buggy if first thing tracked is eg an outlink and referrer is from that site
         $currentHost = Common::mb_strtolower($this->currentUrlParse['host']);
         if ($currentHost == Common::mb_strtolower($this->referrerHost)) {
             $this->typeReferrerAnalyzed = Common::REFERRER_TYPE_DIRECT_ENTRY;
             return true;
         }
     }
     return false;
 }
 public function test_save_shouldClearTrackerCacheEntries()
 {
     $this->setSuperUser();
     Cache::setCacheGeneral(array('testSetting' => 1));
     $this->assertArrayHasKey('testSetting', Cache::getCacheGeneral());
     $this->addSystemSetting('mysystemsetting2', 'mytitle2');
     $this->settings->save();
     $this->assertArrayNotHasKey('testSetting', Cache::getCacheGeneral());
 }
 public function test_shouldCacheInstalledIndexes()
 {
     Cache::clearCacheGeneral();
     $cache = Cache::getCacheGeneral();
     $test = array(CustomDimensions::SCOPE_VISIT => range(1, 5), CustomDimensions::SCOPE_ACTION => range(1, 5), CustomDimensions::SCOPE_CONVERSION => range(2, 5));
     foreach (CustomDimensions::getScopes() as $scope) {
         $key = 'custom_dimension_indexes_installed_' . $scope;
         $this->assertArrayHasKey($key, $cache);
         $this->assertSame(range(1, 5), $cache[$key]);
     }
 }
 /**
  * Returns true if DoNotTrack header checking is enabled. This function is called by the
  * Tracker.
  */
 private function isActiveInTracker()
 {
     $cache = Cache::getCacheGeneral();
     return !empty($cache[self::OPTION_NAME]);
 }