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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CustomVariables;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\DataAccess\TableMetadata;
use Piwik\DataTable;
use Piwik\Db;
use Piwik\Log;
use Piwik\Piwik;
use Psr\Log\LoggerInterface;
class Model
{
const DEFAULT_CUSTOM_VAR_COUNT = 5;
const SCOPE_PAGE = 'page';
const SCOPE_VISIT = 'visit';
const SCOPE_CONVERSION = 'conversion';
private $scope = null;
private $table = null;
private $tableUnprefixed = null;
public function __construct($scope)
{
if (empty($scope) || !in_array($scope, $this->getScopes())) {
throw new \Exception('Invalid custom variable scope');
}
$this->scope = $scope;
$this->tableUnprefixed = $this->getTableNameFromScope($scope);
$this->table = Common::prefixTable($this->tableUnprefixed);
}
public function getUnprefixedTableName()
{
return $this->tableUnprefixed;
}
private function getTableNameFromScope($scope)
{
// actually we should have a class for each scope but don't want to overengineer it for now
switch ($scope) {
case self::SCOPE_PAGE:
return 'log_link_visit_action';
case self::SCOPE_VISIT:
return 'log_visit';
case self::SCOPE_CONVERSION:
return 'log_conversion';
}
}
public function getScope()
{
return $this->scope;
}
public function getScopeName()
{
return ucfirst($this->scope);
}
public function getScopeDescription()
{
switch ($this->scope) {
case Model::SCOPE_PAGE:
return Piwik::translate('CustomVariables_ScopePage');
case Model::SCOPE_VISIT:
return Piwik::translate('CustomVariables_ScopeVisit');
case Model::SCOPE_CONVERSION:
return Piwik::translate('CustomVariables_ScopeConversion');
}
return ucfirst($this->scope);
}
/**
* @see getHighestCustomVarIndex()
* @return int
*/
public function getCurrentNumCustomVars()
{
$indexes = $this->getCustomVarIndexes();
return count($indexes);
}
/**
* result of getHighestCustomVarIndex() can be different to getCurrentNumCustomVars() in case there are some missing
* custom variable indexes. For instance in case of manual changes on the DB
*
* custom_var_v1
* custom_var_v2
* custom_var_v4
*
* getHighestCustomVarIndex() -> returns 4
* getCurrentNumCustomVars() -> returns 3
*
* @return int
*/
public function getHighestCustomVarIndex()
{
$indexes = $this->getCustomVarIndexes();
if (empty($indexes)) {
return 0;
}
return max($indexes);
}
public function getCustomVarIndexes()
{
$columns = $this->getCustomVarColumnNames();
if (empty($columns)) {
return array();
}
$indexes = array_map(function ($column) {
return Model::getCustomVariableIndexFromFieldName($column);
}, $columns);
return array_values(array_unique($indexes));
}
private function getCustomVarColumnNames()
{
$tableMetadataAccess = new TableMetadata();
$columns = $tableMetadataAccess->getColumns($this->table);
$customVarColumns = array_filter($columns, function ($column) {
return false !== strpos($column, 'custom_var_');
});
return $customVarColumns;
}
public function removeCustomVariable()
{
$index = $this->getHighestCustomVarIndex();
if ($index < 1) {
return null;
}
Db::exec(sprintf('ALTER TABLE %s ', $this->table)
. sprintf('DROP COLUMN custom_var_k%d,', $index)
. sprintf('DROP COLUMN custom_var_v%d;', $index));
return $index;
}
public function addCustomVariable()
{
$index = $this->getHighestCustomVarIndex() + 1;
$maxLen = CustomVariables::getMaxLengthCustomVariables();
Db::exec(sprintf('ALTER TABLE %s ', $this->table)
. sprintf('ADD COLUMN custom_var_k%d VARCHAR(%d) DEFAULT NULL,', $index, $maxLen)
. sprintf('ADD COLUMN custom_var_v%d VARCHAR(%d) DEFAULT NULL;', $index, $maxLen));
return $index;
}
public static function getCustomVariableIndexFromFieldName($fieldName)
{
$onlyNumber = str_replace(array('custom_var_k', 'custom_var_v'), '', $fieldName);
if (is_numeric($onlyNumber)) {
return (int) $onlyNumber;
}
}
public static function getScopes()
{
return array(self::SCOPE_PAGE, self::SCOPE_VISIT, self::SCOPE_CONVERSION);
}
public static function install()
{
foreach (self::getScopes() as $scope) {
$model = new Model($scope);
try {
$maxCustomVars = self::DEFAULT_CUSTOM_VAR_COUNT;
$customVarsToAdd = $maxCustomVars - $model->getCurrentNumCustomVars();
for ($index = 0; $index < $customVarsToAdd; $index++) {
$model->addCustomVariable();
}
} catch (\Exception $e) {
StaticContainer::get(LoggerInterface::class)->error('Failed to add custom variable: {exception}', [
'exception' => $e,
'ignoreInScreenWriter' => true,
]);
}
}
}
public static function uninstall()
{
foreach (self::getScopes() as $scope) {
$model = new Model($scope);
while ($model->getHighestCustomVarIndex()) {
$model->removeCustomVariable();
}
}
}
}
|