Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Cache.php « Tracker « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4f7e5809f3e88c47607f0b4c4aa74f785d340b3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
use Piwik\Config;
use Piwik\Piwik;

/**
 * Simple cache mechanism used in Tracker to avoid requesting settings from mysql on every request
 *
 * @package Piwik
 * @subpackage Piwik_Tracker
 */
class Piwik_Tracker_Cache
{
    /**
     * Public for tests only
     * @var Piwik_CacheFile
     */
    static public $trackerCache = null;

    static protected function getInstance()
    {
        if (is_null(self::$trackerCache)) {
            $ttl = Config::getInstance()->Tracker['tracker_cache_file_ttl'];
            self::$trackerCache = new Piwik_CacheFile('tracker', $ttl);
        }
        return self::$trackerCache;
    }

    /**
     * Returns array containing data about the website: goals, URLs, etc.
     *
     * @param int $idSite
     * @return array
     */
    static function getCacheWebsiteAttributes($idSite)
    {
        $idSite = (int)$idSite;

        $cache = self::getInstance();
        if (($cacheContent = $cache->get($idSite)) !== false) {
            return $cacheContent;
        }
        
        Piwik_Tracker::initCorePiwikInTrackerMode();

        // save current user privilege and temporarily assume super user privilege
        $isSuperUser = Piwik::isUserIsSuperUser();
        Piwik::setUserIsSuperUser();

        $content = array();
        Piwik_PostEvent('Common.fetchWebsiteAttributes', array(&$content, $idSite));

        // restore original user privilege
        Piwik::setUserIsSuperUser($isSuperUser);

        // if nothing is returned from the plugins, we don't save the content
        // this is not expected: all websites are expected to have at least one URL
        if (!empty($content)) {
            $cache->set($idSite, $content);
        }
        return $content;
    }

    /**
     * Clear general (global) cache
     */
    static public function clearCacheGeneral()
    {
        self::getInstance()->delete('general');
    }

    /**
     * Returns contents of general (global) cache.
     * If the cache file tmp/cache/tracker/general.php does not exist yet, create it
     *
     * @return array
     */
    static public function getCacheGeneral()
    {
        $cache = self::getInstance();
        $cacheId = 'general';
        $expectedRows = 3;
        if (($cacheContent = $cache->get($cacheId)) !== false
            && count($cacheContent) == $expectedRows
        ) {
            return $cacheContent;
        }

        Piwik_Tracker::initCorePiwikInTrackerMode();
        $cacheContent = array(
            'isBrowserTriggerEnabled' => Piwik_ArchiveProcessor_Rules::isBrowserTriggerEnabled(),
            'lastTrackerCronRun'               => Piwik_GetOption('lastTrackerCronRun'),
            'currentLocationProviderId'        => Piwik_UserCountry_LocationProvider::getCurrentProviderId(),
        );
        self::setCacheGeneral($cacheContent);
        return $cacheContent;
    }

    /**
     * Store data in general (global cache)
     *
     * @param mixed $value
     * @return bool
     */
    static public function setCacheGeneral($value)
    {
        $cache = self::getInstance();
        $cacheId = 'general';
        $cache->set($cacheId, $value);
        return true;
    }

    /**
     * Regenerate Tracker cache files
     *
     * @param array|int $idSites  Array of idSites to clear cache for
     */
    static public function regenerateCacheWebsiteAttributes($idSites = array())
    {
        if (!is_array($idSites)) {
            $idSites = array($idSites);
        }
        foreach ($idSites as $idSite) {
            self::deleteCacheWebsiteAttributes($idSite);
            self::getCacheWebsiteAttributes($idSite);
        }
    }

    /**
     * Delete existing Tracker cache
     *
     * @param string $idSite  (website ID of the site to clear cache for
     */
    static public function deleteCacheWebsiteAttributes($idSite)
    {
        $idSite = (int)$idSite;
        self::getInstance()->delete($idSite);
    }

    /**
     * Deletes all Tracker cache files
     */
    static public function deleteTrackerCache()
    {
        self::getInstance()->deleteAll();
    }

}