| <?php |
|
|
| |
| |
| |
| |
| |
| |
|
|
| namespace Piwik\Tracker; |
|
|
| use Piwik\Access; |
| use Piwik\ArchiveProcessor\Rules; |
| use Piwik\Cache as PiwikCache; |
| use Piwik\Config; |
| use Piwik\Container\StaticContainer; |
| use Piwik\Option; |
| use Piwik\Piwik; |
| use Piwik\Tracker; |
| use Piwik\Log\LoggerInterface; |
|
|
| |
| |
| |
| |
| class Cache |
| { |
| |
| |
| |
| |
| private static $delegatingCacheClears; |
|
|
| |
| |
| |
| |
| private static $delegatedClears = []; |
|
|
| private static $cacheIdGeneral = 'general'; |
|
|
| |
| |
| |
| |
| public static $cache; |
|
|
| |
| |
| |
| private static function getCache() |
| { |
| if (is_null(self::$cache)) { |
| self::$cache = PiwikCache::getLazyCache(); |
| } |
|
|
| return self::$cache; |
| } |
|
|
| private static function getTtl() |
| { |
| return Config::getInstance()->Tracker['tracker_cache_file_ttl']; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function getCacheWebsiteAttributes($idSite) |
| { |
| if ('all' === $idSite) { |
| return []; |
| } |
|
|
| $idSite = (int)$idSite; |
| if ($idSite <= 0) { |
| return []; |
| } |
|
|
| $cache = self::getCache(); |
| $cacheId = self::getCacheKeyWebsiteAttributes($idSite); |
| $cacheContent = $cache->fetch($cacheId); |
|
|
| if (false !== $cacheContent) { |
| return $cacheContent; |
| } |
|
|
| return self::updateCacheWebsiteAttributes($idSite); |
| } |
|
|
| private static function getCacheKeyWebsiteAttributes($idSite) |
| { |
| return $idSite; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public static function updateCacheWebsiteAttributes($idSite) |
| { |
| $cache = self::getCache(); |
| $cacheId = self::getCacheKeyWebsiteAttributes($idSite); |
|
|
| Tracker::initCorePiwikInTrackerMode(); |
|
|
| $content = []; |
| |
| |
| |
| |
| Access::doAsSuperUser(function () use (&$content, $idSite) { |
| /** |
| * Triggered to get the attributes of a site entity that might be used by the |
| * Tracker. |
| * |
| * Plugins add new site attributes for use in other tracking events must |
| * use this event to put those attributes in the Tracker Cache. |
| * |
| * **Example** |
| * |
| * public function getSiteAttributes($content, $idSite) |
| * { |
| * $sql = "SELECT info FROM " . Common::prefixTable('myplugin_extra_site_info') . " WHERE idsite = ?"; |
| * $content['myplugin_site_data'] = Db::fetchOne($sql, [$idSite]); |
| * } |
| * |
| * @param array &$content Array mapping of site attribute names with values. |
| * @param int $idSite The site ID to get attributes for. |
| */ |
| Piwik::postEvent('Tracker.Cache.getSiteAttributes', [&$content, $idSite]); |
|
|
| $logger = StaticContainer::get(LoggerInterface::class); |
| $logger->debug("Website $idSite tracker cache was re-created."); |
| }); |
|
|
| |
| |
| if (!empty($content)) { |
| $cache->save($cacheId, $content, self::getTtl()); |
| } |
|
|
| Tracker::restoreTrackerPlugins(); |
|
|
| return $content; |
| } |
|
|
| |
| |
| |
| public static function clearCacheGeneral() |
| { |
| if (self::$delegatingCacheClears) { |
| self::$delegatedClears[__FUNCTION__] = [__FUNCTION__, []]; |
| return; |
| } |
|
|
| self::getCache()->delete(self::$cacheIdGeneral); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function getCacheGeneral() |
| { |
| $cache = self::getCache(); |
| $cacheContent = $cache->fetch(self::$cacheIdGeneral); |
|
|
| if (false !== $cacheContent) { |
| return $cacheContent; |
| } |
|
|
| return self::updateGeneralCache(); |
| } |
|
|
| |
| |
| |
| |
| |
| public static function updateGeneralCache() |
| { |
| Tracker::initCorePiwikInTrackerMode(); |
| $cacheContent = [ |
| 'isBrowserTriggerEnabled' => Rules::isBrowserTriggerEnabled(), |
| 'lastTrackerCronRun' => Option::get('lastTrackerCronRun'), |
| ]; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| Piwik::postEvent('Tracker.setTrackerCacheGeneral', [&$cacheContent]); |
| self::setCacheGeneral($cacheContent); |
|
|
| $logger = StaticContainer::get(LoggerInterface::class); |
| $logger->debug("General tracker cache was re-created."); |
|
|
| Tracker::restoreTrackerPlugins(); |
|
|
| return $cacheContent; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| public static function setCacheGeneral($value) |
| { |
| $cache = self::getCache(); |
|
|
| return $cache->save(self::$cacheIdGeneral, $value, self::getTtl()); |
| } |
|
|
| |
| |
| |
| |
| |
| public static function regenerateCacheWebsiteAttributes($idSites = []) |
| { |
| if (!is_array($idSites)) { |
| $idSites = [$idSites]; |
| } |
|
|
| foreach ($idSites as $idSite) { |
| self::deleteCacheWebsiteAttributes($idSite); |
| self::getCacheWebsiteAttributes($idSite); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| public static function deleteCacheWebsiteAttributes($idSite) |
| { |
| if (self::$delegatingCacheClears) { |
| self::$delegatedClears[__FUNCTION__ . $idSite] = [__FUNCTION__, func_get_args()]; |
| return; |
| } |
|
|
| self::getCache()->delete((int)$idSite); |
| } |
|
|
| |
| |
| |
| public static function deleteTrackerCache() |
| { |
| if (self::$delegatingCacheClears) { |
| self::$delegatedClears[__FUNCTION__] = [__FUNCTION__, []]; |
| return; |
| } |
|
|
| self::getCache()->flushAll(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static function withDelegatedCacheClears($callback) |
| { |
| try { |
| self::$delegatingCacheClears = true; |
| self::$delegatedClears = []; |
|
|
| return $callback(); |
| } finally { |
| self::$delegatingCacheClears = false; |
|
|
| self::callAllDelegatedClears(); |
|
|
| self::$delegatedClears = []; |
| } |
| } |
|
|
| private static function callAllDelegatedClears() |
| { |
| foreach (self::$delegatedClears as list($methodName, $params)) { |
| if (!method_exists(self::class, $methodName)) { |
| continue; |
| } |
|
|
| call_user_func_array([self::class, $methodName], $params); |
| } |
| } |
| } |
|
|