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

Option.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e4a33480ddd1ef2f5513a0144fd221c72d050ab4 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?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
 */
namespace Piwik;


/**
 * Option provides a very simple mechanism to save/retrieve key-values pair
 * from the database (persistent key-value datastore).
 *
 * This is useful to save Piwik-wide preferences, configuration values.
 *
 * @package Piwik
 */
class Option
{
    /**
     * @var array
     */
    private $all = array();

    /**
     * @var bool
     */
    private $loaded = false;

    /**
     * Singleton instance
     * @var \Piwik\Option
     */
    static private $instance = null;

    /**
     * Returns Singleton instance
     *
     * @return \Piwik\Option
     */
    static public function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self;
        }
        return self::$instance;
    }

    /**
     * Private Constructor
     */
    private function __construct()
    {
    }

    /**
     * Returns the option value for the requested option $name, fetching from database, if not in cache.
     *
     * @param string $name  Key
     * @return string|bool  Value or false, if not found
     */
    public function get($name)
    {
        $this->autoload();
        if (isset($this->all[$name])) {
            return $this->all[$name];
        }
        $value = Db::fetchOne('SELECT option_value ' .
            'FROM `' . Common::prefixTable('option') . '`' .
            'WHERE option_name = ?', $name);
        if ($value === false) {
            return false;
        }
        $this->all[$name] = $value;
        return $value;
    }

    /**
     * Sets the option value in the database and cache
     *
     * @param string $name
     * @param string $value
     * @param int $autoLoad  if set to 1, this option value will be automatically loaded; should be set to 1 for options that will always be used in the Piwik request.
     */
    public function set($name, $value, $autoLoad = 0)
    {
        $autoLoad = (int)$autoLoad;
        Db::query('INSERT INTO `' . Common::prefixTable('option') . '` (option_name, option_value, autoload) ' .
                ' VALUES (?, ?, ?) ' .
                ' ON DUPLICATE KEY UPDATE option_value = ?',
            array($name, $value, $autoLoad, $value));
        $this->all[$name] = $value;
    }

    /**
     * Delete key-value pair from database and reload cache.
     *
     * @param string $name   Key to match exactly
     * @param string $value  Optional value
     */
    public function delete($name, $value = null)
    {
        $sql = 'DELETE FROM `' . Common::prefixTable('option') . '` WHERE option_name = ?';
        $bind[] = $name;

        if (isset($value)) {
            $sql .= ' AND option_value = ?';
            $bind[] = $value;
        }

        Db::query($sql, $bind);

        $this->clearCache();
    }

    /**
     * Delete key-value pair(s) from database and reload cache.
     * The supplied pattern should use '%' as wildcards, and literal '_' should be escaped.
     *
     * @param string $name   Pattern of key to match.
     * @param string $value  Optional value
     */
    public function deleteLike($name, $value = null)
    {
        $sql = 'DELETE FROM `' . Common::prefixTable('option') . '` WHERE option_name LIKE ?';
        $bind[] = $name;

        if (isset($value)) {
            $sql .= ' AND option_value = ?';
            $bind[] = $value;
        }

        Db::query($sql, $bind);

        $this->clearCache();
    }

    /**
     * Initialize cache with autoload settings.
     *
     * @return void
     */
    private function autoload()
    {
        if ($this->loaded) {
            return;
        }

        $all = Db::fetchAll('SELECT option_value, option_name
								FROM `' . Common::prefixTable('option') . '`
								WHERE autoload = 1');
        foreach ($all as $option) {
            $this->all[$option['option_name']] = $option['option_value'];
        }

        $this->loaded = true;
    }

    /**
     * Clears the cache
     * Used in unit tests to reset the state of the object between tests
     *
     * @return void
     */
    public function clearCache()
    {
        $this->loaded = false;
        $this->all = array();
    }
}