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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Updates;
use Piwik\Common;
use Piwik\DbHelper;
use Piwik\Option;
use Piwik\Plugin\Dimension\ActionDimension;
use Piwik\Plugin\Dimension\ConversionDimension;
use Piwik\Plugin\Dimension\VisitDimension;
use Piwik\Tracker\Visit;
use Piwik\Updater;
use Piwik\Updates;
/**
* Update for version 2.14.2.
*/
class Updates_2_14_2 extends Updates
{
/**
* Removes option entries for columns that are marked as installed but are actually no longer installed due to
* a bug in previous versions where the option entries were not correctly removed.
*
* @param Updater $updater
* @return array
*/
public function getMigrationQueries(Updater $updater)
{
$visitSqls = self::getSqlsThatRemoveOptionEntriesOfNotActuallyInstalledColumns(VisitDimension::INSTALLER_PREFIX, 'log_visit');
$actionSqls = self::getSqlsThatRemoveOptionEntriesOfNotActuallyInstalledColumns(ActionDimension::INSTALLER_PREFIX, 'log_link_visit_action');
$conversionSqls = self::getSqlsThatRemoveOptionEntriesOfNotActuallyInstalledColumns(ConversionDimension::INSTALLER_PREFIX, 'log_conversion');
$sqls = array();
foreach ($visitSqls as $sql) {
$sqls[$sql] = false;
}
foreach ($actionSqls as $sql) {
$sqls[$sql] = false;
}
foreach ($conversionSqls as $sql) {
$sqls[$sql] = false;
}
return $sqls;
}
private static function getSqlsThatRemoveOptionEntriesOfNotActuallyInstalledColumns($dimensionPrefix, $tableName)
{
$componentPrefix = 'version_' . $dimensionPrefix;
$notActuallyInstalledColumns = self::getNotActuallyInstalledColumnNames($componentPrefix, $tableName);
$sqls = array();
foreach ($notActuallyInstalledColumns as $column) {
$sqls[] = self::buildRemoveOptionEntrySql($componentPrefix . $column);
}
return $sqls;
}
private static function buildRemoveOptionEntrySql($optionName)
{
$tableName = Common::prefixTable('option');
return sprintf("DELETE FROM `%s` WHERE `option_name` = '%s'", $tableName, $optionName);
}
/**
* @param string $componentPrefix
* @param string $tableName
* @return array An array of columns that are marked as installed but are actually removed. There was a bug
* where option entries were not correctly removed. eg array('idvist', 'server_time', ...)
*/
private static function getNotActuallyInstalledColumnNames($componentPrefix, $tableName)
{
$installedVisitColumns = self::getMarkedAsInstalledColumns($componentPrefix);
$existingVisitColumns = self::getActuallyExistingColumns($tableName);
return array_diff($installedVisitColumns, $existingVisitColumns);
}
/**
* @param string $componentPrefix eg 'version_log_visit.'
* @return array An array of column names that are marked as installed. eg array('idvist', 'server_time', ...)
*/
private static function getMarkedAsInstalledColumns($componentPrefix)
{
$installedVisitColumns = Option::getLike($componentPrefix . '%');
$installedVisitColumns = array_keys($installedVisitColumns);
$installedVisitColumns = array_map(function ($entry) use ($componentPrefix) {
return str_replace($componentPrefix, '', $entry);
}, $installedVisitColumns);
return $installedVisitColumns;
}
/**
* @param string $tableName
* @return array An array of actually existing column names in the given table. eg array('idvist', 'server_time', ...)
*/
private static function getActuallyExistingColumns($tableName)
{
$tableName = Common::prefixTable($tableName);
return array_keys(DbHelper::getTableColumns($tableName));
}
/**
* @param Updater $updater
*/
public function doUpdate(Updater $updater)
{
$updater->executeMigrationQueries(__FILE__, $this->getMigrationQueries($updater));
}
}
|