public function setUp() { parent::setUp(); PiwikCache::flushAll(); Translate::loadAllTranslations(); $this->api = API::getInstance(); }
/** * @return \Piwik\Cache\Lazy */ private static function getCache() { if (is_null(self::$cache)) { self::$cache = PiwikCache::getLazyCache(); } return self::$cache; }
/** * @return array names of plugins that have been loaded */ public function loadTrackerPlugins() { $cacheId = 'PluginsTracker'; $cache = Cache::getEagerCache(); if ($cache->contains($cacheId)) { $pluginsTracker = $cache->fetch($cacheId); } else { $this->unloadPlugins(); $this->loadActivatedPlugins(); $pluginsTracker = array(); foreach ($this->loadedPlugins as $pluginName => $plugin) { if ($this->isTrackerPlugin($plugin)) { $pluginsTracker[] = $pluginName; } } if (!empty($pluginsTracker)) { $cache->save($cacheId, $pluginsTracker); } } if (empty($pluginsTracker)) { $this->unloadPlugins(); return array(); } $pluginsTracker = array_diff($pluginsTracker, $this->getTrackerPluginsNotToLoad()); $this->doNotLoadAlwaysActivatedPlugins(); $this->loadPlugins($pluginsTracker); // we could simply unload all plugins first before loading plugins but this way it is much faster // since we won't have to create each plugin again and we won't have to parse each plugin metadata file // again etc $this->makeSureOnlyActivatedPluginsAreLoaded(); return $pluginsTracker; }
public function test_onSiteDeleted_shouldClearSiteCache() { $cache = Cache::getLazyCache(); $cache->save($this->siteId, 'testcontent'); $this->manager->onSiteDeleted($this->siteId); $this->assertFalse($cache->contains($this->siteId)); }
/** * Called on Core install, update, plugin enable/disable * Will clear all cache that could be affected by the change in configuration being made */ public static function deleteAllCacheOnUpdate($pluginName = false) { AssetManager::getInstance()->removeMergedAssets($pluginName); View::clearCompiledTemplates(); TrackerCache::deleteTrackerCache(); PiwikCache::flushAll(); self::clearPhpCaches(); }
public function test_isUsedInAtLeastOneSite_shouldCache() { $key = '1.month.' . $this->date; $cache = Cache::getTransientCache(); $this->assertFalse($cache->contains($key)); $this->userId->isUsedInAtLeastOneSite($idSites = array(1), 'day', $this->date); $this->assertTrue($cache->contains($key)); $this->assertFalse($cache->fetch($key)); }
private function isUsedInSiteCached($idSite, $period, $date) { $cache = Cache::getTransientCache(); $key = sprintf('%d.%s.%s', $idSite, $period, $date); if (!$cache->contains($key)) { $result = $this->isUsedInSite($idSite, $period, $date); $cache->save($key, $result); } return $cache->fetch($key); }
/** * Returns list of search engines by URL * * @return array Array of ( URL => array( searchEngineName, keywordParameter, path, charset ) ) */ public function getDefinitions() { $cache = Cache::getEagerCache(); $cacheId = 'Social-' . self::OPTION_STORAGE_NAME; if ($cache->contains($cacheId)) { $list = $cache->fetch($cacheId); } else { $list = $this->loadDefinitions(); $cache->save($cacheId, $list); } return $list; }
private function getSpammerListFromCache() { $cache = Cache::getEagerCache(); $cacheId = 'ReferrerSpamFilter-' . self::OPTION_STORAGE_NAME; if ($cache->contains($cacheId)) { $list = $cache->fetch($cacheId); } else { $list = $this->loadSpammerList(); $cache->save($cacheId, $list); } return $list; }
public function test_getEagerCache_shouldPersistOnceEventWasTriggered() { $storageId = 'eagercache-test-ui'; $cache = Cache::getEagerCache(); $cache->save('test', 'mycontent'); // make sure something was changed, otherwise it won't save anything /** @var Cache\Backend $backend */ $backend = StaticContainer::get('Piwik\\Cache\\Backend'); $this->assertFalse($backend->doContains($storageId)); Piwik::postEvent('Request.dispatch.end'); // should trigger save $this->assertTrue($backend->doContains($storageId)); }
/** * Returns all language codes the transifex project is available for * * @return array * @throws AuthenticationFailedException * @throws Exception */ public function getAvailableLanguageCodes() { $cache = Cache::getTransientCache(); $cacheId = 'transifex_languagescodes_' . $this->projectSlug; $languageCodes = $cache->fetch($cacheId); if (empty($languageCodes)) { $apiData = $this->getApiResults('project/' . $this->projectSlug . '/languages'); foreach ($apiData as $languageData) { $languageCodes[] = $languageData->language_code; } $cache->save($cacheId, $languageCodes); } return $languageCodes; }
/** * Returns a list of all available reports. Even not enabled reports will be returned. They will be already sorted * depending on the order and category of the report. * @return \Piwik\Plugin\Report[] * @api */ public function getAllReports() { $reports = $this->getAllReportClasses(); $cacheId = CacheId::languageAware('Reports' . md5(implode('', $reports))); $cache = PiwikCache::getTransientCache(); if (!$cache->contains($cacheId)) { $instances = array(); /** * Triggered to add new reports that cannot be picked up automatically by the platform. * This is useful if the plugin allows a user to create reports / dimensions dynamically. For example * CustomDimensions or CustomVariables. There are a variable number of dimensions in this case and it * wouldn't be really possible to create a report file for one of these dimensions as it is not known * how many Custom Dimensions will exist. * * **Example** * * public function addReport(&$reports) * { * $reports[] = new MyCustomReport(); * } * * @param Report[] $reports An array of reports */ Piwik::postEvent('Report.addReports', array(&$instances)); foreach ($reports as $report) { $instances[] = new $report(); } /** * Triggered to filter / restrict reports. * * **Example** * * public function filterReports(&$reports) * { * foreach ($reports as $index => $report) { * if ($report->getCategory() === 'Actions') {} * unset($reports[$index]); // remove all reports having this action * } * } * } * * @param Report[] $reports An array of reports */ Piwik::postEvent('Report.filterReports', array(&$instances)); usort($instances, array($this, 'sort')); $cache->save($cacheId, $instances); } return $cache->fetch($cacheId); }
/** * Setup the database and create the base tables for all tests */ public function setUp() { parent::setUp(); static::$fixture->extraDefinitions = array_merge(static::provideContainerConfigBeforeClass(), $this->provideContainerConfig()); static::$fixture->createEnvironmentInstance(); Db::createDatabaseObject(); Fixture::loadAllPlugins(new TestingEnvironmentVariables(), get_class($this), self::$fixture->extraPluginsToLoad); Access::getInstance()->setSuperUserAccess(true); if (!empty(self::$tableData)) { self::restoreDbTables(self::$tableData); } PiwikCache::getEagerCache()->flushAll(); PiwikCache::getTransientCache()->flushAll(); MenuAbstract::clearMenus(); }
public function setUp() { parent::setUp(); // make sure templates will be found Plugin\Manager::getInstance()->loadPlugin('CustomAlerts'); Plugin\Manager::getInstance()->loadPlugin('Morpheus'); if (class_exists('\\Piwik\\Cache\\PluginAwareStaticCache')) { \Piwik\Cache\PluginAwareStaticCache::clearAll(); // TODO remove this one } else { PiwikCache::flushAll(); } Translate::loadAllTranslations(); $this->controller = new CustomController(); }
public function test_flushAll_shouldActuallyFlushAllCaches() { $cache1 = Cache::getTransientCache(); $cache2 = Cache::getLazyCache(); $cache3 = Cache::getEagerCache(); $cache1->save('test1', 'content'); $cache2->save('test2', 'content'); $cache3->save('test3', 'content'); $this->assertTrue($cache1->contains('test1')); $this->assertTrue($cache2->contains('test2')); $this->assertTrue($cache3->contains('test3')); Cache::flushAll(); $this->assertFalse($cache1->contains('test1')); $this->assertFalse($cache2->contains('test2')); $this->assertFalse($cache3->contains('test3')); }
private function getSpammerListFromCache() { $cache = Cache::getEagerCache(); $cacheId = 'ReferrerSpamFilter-' . self::OPTION_STORAGE_NAME; if ($cache->contains($cacheId)) { $list = $cache->fetch($cacheId); } else { $list = $this->loadSpammerList(); $cache->save($cacheId, $list); } if (!is_array($list)) { Common::printDebug('Warning: could not read list of spammers from cache.'); return array(); } return $list; }
/** * Returns all registered visualization classes. Uses the 'Visualization.getAvailable' * event to retrieve visualizations. * * @return array Array mapping visualization IDs with their associated visualization classes. * @throws \Exception If a visualization class does not exist or if a duplicate visualization ID * is found. * @return array */ public static function getAvailableViewDataTables() { $cache = Cache::getTransientCache(); $cacheId = 'ViewDataTable.getAvailableViewDataTables'; $dataTables = $cache->fetch($cacheId); if (!empty($dataTables)) { return $dataTables; } $klassToExtend = '\\Piwik\\Plugin\\ViewDataTable'; /** @var string[] $visualizations */ $visualizations = PluginManager::getInstance()->findMultipleComponents('Visualizations', $klassToExtend); /** * Triggered when gathering all available DataTable visualizations. * * Plugins that want to expose new DataTable visualizations should subscribe to * this event and add visualization class names to the incoming array. * * **Example** * * public function addViewDataTable(&$visualizations) * { * $visualizations[] = 'Piwik\\Plugins\\MyPlugin\\MyVisualization'; * } * * @param array &$visualizations The array of all available visualizations. * @ignore * @deprecated since 2.5.0 Place visualization in a "Visualizations" directory instead. */ Piwik::postEvent('ViewDataTable.addViewDataTable', array(&$visualizations)); $result = array(); foreach ($visualizations as $viz) { if (!class_exists($viz)) { throw new \Exception("Invalid visualization class '{$viz}' found in Visualization.getAvailableVisualizations."); } if (!is_subclass_of($viz, $klassToExtend)) { throw new \Exception("ViewDataTable class '{$viz}' does not extend Plugin/ViewDataTable"); } $vizId = $viz::getViewDataTableId(); if (isset($result[$vizId])) { throw new \Exception("ViewDataTable ID '{$vizId}' is already in use!"); } $result[$vizId] = $viz; } $cache->save($cacheId, $result); return $result; }
/** * Returns all registered visualization classes. Uses the 'Visualization.getAvailable' * event to retrieve visualizations. * * @return array Array mapping visualization IDs with their associated visualization classes. * @throws \Exception If a visualization class does not exist or if a duplicate visualization ID * is found. * @return array */ public static function getAvailableViewDataTables() { $cache = Cache::getTransientCache(); $cacheId = 'ViewDataTable.getAvailableViewDataTables'; $dataTables = $cache->fetch($cacheId); if (!empty($dataTables)) { return $dataTables; } $klassToExtend = '\\Piwik\\Plugin\\ViewDataTable'; /** @var string[] $visualizations */ $visualizations = PluginManager::getInstance()->findMultipleComponents('Visualizations', $klassToExtend); $result = array(); foreach ($visualizations as $viz) { if (!class_exists($viz)) { throw new \Exception("Invalid visualization class '{$viz}' found in Visualization.getAvailableVisualizations."); } if (!is_subclass_of($viz, $klassToExtend)) { throw new \Exception("ViewDataTable class '{$viz}' does not extend Plugin/ViewDataTable"); } $vizId = $viz::getViewDataTableId(); if (isset($result[$vizId])) { throw new \Exception("ViewDataTable ID '{$vizId}' is already in use!"); } $result[$vizId] = $viz; } /** * Triggered to filter available DataTable visualizations. * * Plugins that want to disable certain visualizations should subscribe to * this event and remove visualizations from the incoming array. * * **Example** * * public function filterViewDataTable(&$visualizations) * { * unset($visualizations[HtmlTable::ID]); * } * * @param array &$visualizations An array of all available visualizations indexed by visualization ID. * @since Piwik 3.0.0 */ Piwik::postEvent('ViewDataTable.filterViewDataTable', array(&$result)); $cache->save($cacheId, $result); return $result; }
public function test_getIdSitesToArchiveWhenNoVisits_CanBeUsedToTriggerArchiving_EvenIfSiteHasNoVisits() { // add our mock archiver instance // TODO: should use a dummy plugin that is activated for this test explicitly, but that can be tricky, especially in the future PluginsArchiver::$archivers['VisitsSummary'] = 'Piwik\\Tests\\Integration\\ArchiveWithNoVisitsTest_MockArchiver'; // initiate archiving w/o adding the event and make sure no methods are called VisitsSummaryAPI::getInstance()->get($idSite = 1, 'week', '2012-01-01'); $this->assertEmpty(ArchiveWithNoVisitsTest_MockArchiver::$methodsCalled); // mark our only site as should archive when no visits $eventDispatcher = $this->getEventDispatcher(); $eventDispatcher->addObserver('Archiving.getIdSitesToArchiveWhenNoVisits', function (&$idSites) { $idSites[] = 1; }); Cache::getTransientCache()->flushAll(); // initiate archiving and make sure both aggregate methods are called correctly VisitsSummaryAPI::getInstance()->get($idSite = 1, 'week', '2012-01-10'); $expectedMethodCalls = array('aggregateDayReport', 'aggregateDayReport', 'aggregateDayReport', 'aggregateDayReport', 'aggregateDayReport', 'aggregateDayReport', 'aggregateDayReport', 'aggregateMultipleReports'); $this->assertEquals($expectedMethodCalls, ArchiveWithNoVisitsTest_MockArchiver::$methodsCalled); }
private function buildCache() { return Cache::getLazyCache(); }
/** * Returns the list of stored segments to pre-process for an individual site when executing * cron archiving. * * @param int $idSite The ID of the site to get stored segments for. * @return string[] The list of stored segments that apply to the requested site. */ public static function getKnownSegmentsToArchiveForSite($idSite) { $cacheId = 'KnownSegmentsToArchiveForSite' . $idSite; $cache = PiwikCache::getTransientCache(); if ($cache->contains($cacheId)) { return $cache->fetch($cacheId); } $segments = array(); /** * Triggered during the cron archiving process to collect segments that * should be pre-processed for one specific site. The archiving process will be launched * for each of these segments when archiving data for that one site. * * This event can be used to add segments to be pre-processed for one site. * * _Note: If you just want to add a segment that is managed by the user, you should use the * SegmentEditor API._ * * **Example** * * Piwik::addAction('Segments.getKnownSegmentsToArchiveForSite', function (&$segments, $idSite) { * $segments[] = 'country=jp;city=Tokyo'; * }); * * @param array &$segmentsToProcess List of segment definitions, eg, * * array( * 'browserCode=ff;resolution=800x600', * 'country=JP;city=Tokyo' * ) * * Add segments to this array in your event handler. * @param int $idSite The ID of the site to get segments for. */ Piwik::postEvent('Segments.getKnownSegmentsToArchiveForSite', array(&$segments, $idSite)); $segments = array_unique($segments); $cache->save($cacheId, $segments); return $segments; }
/** * Returns list of social networks by URL * * @see core/DataFiles/Socials.php * * @return array Array of ( URL => Social Network Name ) */ public static function getSocialUrls() { $cacheId = 'Common.getSocialUrls'; $cache = Cache::getTransientCache(); $socialUrls = $cache->fetch($cacheId); if (empty($socialUrls)) { require_once PIWIK_INCLUDE_PATH . '/core/DataFiles/Socials.php'; $socialUrls = $GLOBALS['Piwik_socialUrl']; Piwik::postEvent('Referrer.addSocialUrls', array(&$socialUrls)); $cache->save($cacheId, $socialUrls); } return $socialUrls; }
/** * Returns a URL query string as an array. * * @param string $urlQuery The query string, eg, `'?param1=value1¶m2=value2'`. * @return array eg, `array('param1' => 'value1', 'param2' => 'value2')` * @api */ public static function getArrayFromQueryString($urlQuery) { if (strlen($urlQuery) == 0) { return array(); } // TODO: this method should not use a cache. callers should instead have their own cache, configured through DI. // one undesirable side effect of using a cache here, is that this method can now init the StaticContainer, which makes setting // test environment for RequestCommand more complicated. $cache = Cache::getTransientCache(); $cacheKey = 'arrayFromQuery' . $urlQuery; if ($cache->contains($cacheKey)) { return $cache->fetch($cacheKey); } if ($urlQuery[0] == '?') { $urlQuery = substr($urlQuery, 1); } $separator = '&'; $urlQuery = $separator . $urlQuery; // $urlQuery = str_replace(array('%20'), ' ', $urlQuery); $referrerQuery = trim($urlQuery); $values = explode($separator, $referrerQuery); $nameToValue = array(); foreach ($values as $value) { $pos = strpos($value, '='); if ($pos !== false) { $name = substr($value, 0, $pos); $value = substr($value, $pos + 1); if ($value === false) { $value = ''; } } else { $name = $value; $value = false; } if (!empty($name)) { $name = Common::sanitizeInputValue($name); } if (!empty($value)) { $value = Common::sanitizeInputValue($value); } // if array without indexes $count = 0; $tmp = preg_replace('/(\\[|%5b)(]|%5d)$/i', '', $name, -1, $count); if (!empty($tmp) && $count) { $name = $tmp; if (isset($nameToValue[$name]) == false || is_array($nameToValue[$name]) == false) { $nameToValue[$name] = array(); } array_push($nameToValue[$name], $value); } elseif (!empty($name)) { $nameToValue[$name] = $value; } } $cache->save($cacheKey, $nameToValue); return $nameToValue; }
private function isIpInRange() { $cache = PiwikCache::getTransientCache(); $ip = IP::fromBinaryIP($this->ip); $key = 'VisitExcludedIsIpInRange' . $ip->toString(); if ($cache->contains($key)) { $isInRanges = $cache->fetch($key); } else { if ($this->isChromeDataSaverUsed($ip)) { $isInRanges = false; } else { $isInRanges = $ip->isInRanges($this->getBotIpRanges()); } $cache->save($key, $isInRanges); } return $isInRanges; }
private static function getCache() { return Cache::getLazyCache(); }
public function __construct($ttl = 300) { $this->ttl = (int) $ttl; $this->cache = PiwikCache::getEagerCache(); }
public function clearInMemoryCaches() { Archive::clearStaticCache(); DataTableManager::getInstance()->deleteAll(); Option::clearCache(); Site::clearCache(); Cache::deleteTrackerCache(); PiwikCache::getTransientCache()->flushAll(); PiwikCache::getEagerCache()->flushAll(); PiwikCache::getLazyCache()->flushAll(); ArchiveTableCreator::clear(); \Piwik\Plugins\ScheduledReports\API::$cache = array(); Singleton::clearAll(); PluginsArchiver::$archivers = array(); $_GET = $_REQUEST = array(); Translate::reset(); self::getConfig()->Plugins; // make sure Plugins exists in a config object for next tests that use Plugin\Manager // since Plugin\Manager uses getFromGlobalConfig which doesn't init the config object }
/** * Returns list of search engines by name * * @return array Array of ( searchEngineName => URL ) */ public function getNames() { $cacheId = 'SearchEngine.getSearchEngineNames'; $cache = Cache::getTransientCache(); $nameToUrl = $cache->fetch($cacheId); if (empty($nameToUrl)) { $searchEngines = $this->getDefinitions(); $nameToUrl = array(); foreach ($searchEngines as $url => $info) { if (!isset($nameToUrl[$info['name']])) { $nameToUrl[$info['name']] = $url; } } $cache->save($cacheId, $nameToUrl); } return $nameToUrl; }
/** * This method allows to set custom IP + server time + visitor ID, when using Tracking API. * These two attributes can be only set by the Super User (passing token_auth). */ protected function authenticateTrackingApi($tokenAuth) { $shouldAuthenticate = TrackerConfig::getConfigValue('tracking_requests_require_authentication'); if ($shouldAuthenticate) { try { $idSite = $this->getIdSite(); } catch (Exception $e) { $this->isAuthenticated = false; return; } if (empty($tokenAuth)) { $tokenAuth = Common::getRequestVar('token_auth', false, 'string', $this->params); } $cache = PiwikCache::getTransientCache(); $cacheKey = 'tracker_request_authentication_' . $idSite . '_' . $tokenAuth; if ($cache->contains($cacheKey)) { Common::printDebug("token_auth is authenticated in cache!"); $this->isAuthenticated = $cache->fetch($cacheKey); return; } try { $this->isAuthenticated = self::authenticateSuperUserOrAdmin($tokenAuth, $idSite); $cache->save($cacheKey, $this->isAuthenticated); } catch (Exception $e) { Common::printDebug("could not authenticate, caught exception: " . $e->getMessage()); $this->isAuthenticated = false; } if ($this->isAuthenticated) { Common::printDebug("token_auth is authenticated!"); } } else { $this->isAuthenticated = true; Common::printDebug("token_auth authentication not required"); } }
public function setUp() { Cache::getTransientCache()->flushAll(); parent::setUp(); }