File size: 5,724 Bytes
1a49063 | 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 | <?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CustomDimensions\Dao;
use Piwik\Common;
use Piwik\DataAccess\TableMetadata;
use Piwik\Db;
use Piwik\DbHelper;
use Piwik\Plugins\CustomDimensions\CustomDimensions;
use Exception;
class LogTable
{
public const DEFAULT_CUSTOM_DIMENSION_COUNT = 5;
private $scope = null;
private $table = null;
public function __construct($scope)
{
$this->scope = $scope;
$this->table = Common::prefixTable($this->getTableNameFromScope($scope));
}
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 CustomDimensions::SCOPE_ACTION:
return 'log_link_visit_action';
case CustomDimensions::SCOPE_VISIT:
return 'log_visit';
case CustomDimensions::SCOPE_CONVERSION:
return 'log_conversion';
default:
throw new Exception('Unsupported scope ' . $scope);
}
}
/**
* @see getInstalledIndexes()
* @return int
*/
public function getNumInstalledIndexes()
{
$indexes = $this->getInstalledIndexes();
return count($indexes);
}
public function getInstalledIndexes()
{
$columns = $this->getCustomDimensionColumnNames();
if (empty($columns)) {
return array();
}
$indexes = array_map(function ($column) {
$onlyNumber = str_replace('custom_dimension_', '', $column);
if (is_numeric($onlyNumber)) {
return (int) $onlyNumber;
}
}, $columns);
return array_values(array_unique($indexes));
}
private function getCustomDimensionColumnNames()
{
$tableMetadataAccess = new TableMetadata();
$columns = $tableMetadataAccess->getColumns($this->table);
$dimensionColumns = array_filter($columns, function ($column) {
return LogTable::isCustomDimensionColumn($column);
});
return $dimensionColumns;
}
public static function isCustomDimensionColumn($column)
{
return (bool) preg_match('/^custom_dimension_(\d+)$/', '' . $column);
}
public static function buildCustomDimensionColumnName($indexOrDimension)
{
if (is_array($indexOrDimension) && isset($indexOrDimension['index'])) {
$indexOrDimension = $indexOrDimension['index'];
}
$indexOrDimension = (int) $indexOrDimension;
if ($indexOrDimension >= 1) {
return 'custom_dimension_' . (int) $indexOrDimension;
}
}
public function removeCustomDimension($index)
{
if ($index < 1) {
return;
}
$field = self::buildCustomDimensionColumnName($index);
$this->dropColumn($field);
}
public function addManyCustomDimensions($count, $extraAlter = null)
{
if ($count < 0) {
return;
}
$indexes = $this->getInstalledIndexes();
if (empty($indexes)) {
$highestIndex = 0;
} else {
$highestIndex = max($indexes);
}
$total = $highestIndex + $count;
$queries = array();
if (isset($extraAlter)) {
// we make sure to install needed tracker request processor columns first, before installing custom dimensions
// if something fails custom dimensions can be added later any time
$queries[] = $extraAlter;
}
for ($index = $highestIndex; $index < $total; $index++) {
$queries[] = $this->getAddColumnQueryToAddCustomDimension($index + 1);
}
if (!empty($queries)) {
$sql = 'ALTER TABLE ' . $this->table . ' ' . implode(', ', $queries) . ';';
Db::exec($sql);
}
}
private function getAddColumnQueryToAddCustomDimension($index)
{
$field = self::buildCustomDimensionColumnName($index);
return sprintf('ADD COLUMN %s VARCHAR(255) DEFAULT NULL', $field);
}
public function install()
{
$numDimensionsInstalled = $this->getNumInstalledIndexes();
$numDimensionsToAdd = self::DEFAULT_CUSTOM_DIMENSION_COUNT - $numDimensionsInstalled;
$query = null;
if ($this->scope === CustomDimensions::SCOPE_VISIT && !$this->hasColumn('last_idlink_va')) {
$query = 'ADD COLUMN last_idlink_va BIGINT UNSIGNED DEFAULT NULL';
} elseif ($this->scope === CustomDimensions::SCOPE_ACTION && !$this->hasColumn('time_spent')) {
$query = 'ADD COLUMN time_spent INT UNSIGNED DEFAULT NULL';
}
$this->addManyCustomDimensions($numDimensionsToAdd, $query);
}
public function uninstall()
{
foreach ($this->getInstalledIndexes() as $index) {
$this->removeCustomDimension($index);
}
if ($this->scope === CustomDimensions::SCOPE_VISIT) {
$this->dropColumn('last_idlink_va');
} elseif ($this->scope === CustomDimensions::SCOPE_ACTION) {
$this->dropColumn('time_spent');
}
}
private function hasColumn($field)
{
$columns = DbHelper::getTableColumns($this->table);
return array_key_exists($field, $columns);
}
private function dropColumn($field)
{
if ($this->hasColumn($field)) {
$sql = sprintf('ALTER TABLE %s DROP COLUMN %s;', $this->table, $field);
Db::exec($sql);
}
}
}
|