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

2.14.2.php « Updates « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4cbcc452bdb2814c2b2cd0353768b40b5778412e (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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.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;
use Piwik\Updater\Migration\Factory as MigrationFactory;

/**
 * Update for version 2.14.2.
 */
class Updates_2_14_2 extends Updates
{
    /**
     * @var MigrationFactory
     */
    private $migration;

    public function __construct(MigrationFactory $factory)
    {
        $this->migration = $factory;
    }

    /**
     * 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 getMigrations(Updater $updater)
    {
        $visitMigrations = $this->getMigrationsThatRemoveOptionEntriesOfNotActuallyInstalledColumns(VisitDimension::INSTALLER_PREFIX, 'log_visit');
        $actionMigrations = $this->getMigrationsThatRemoveOptionEntriesOfNotActuallyInstalledColumns(ActionDimension::INSTALLER_PREFIX, 'log_link_visit_action');
        $conversionMigrations = $this->getMigrationsThatRemoveOptionEntriesOfNotActuallyInstalledColumns(ConversionDimension::INSTALLER_PREFIX, 'log_conversion');

        $migrations = array();

        foreach ($visitMigrations as $migration) {
            $migrations[] = $migration;
        }

        foreach ($actionMigrations as $migration) {
            $migrations[] = $migration;
        }

        foreach ($conversionMigrations as $migration) {
            $migrations[] = $migration;
        }

        return $migrations;
    }

    private function getMigrationsThatRemoveOptionEntriesOfNotActuallyInstalledColumns($dimensionPrefix, $tableName)
    {
        $componentPrefix = 'version_' . $dimensionPrefix;

        $notActuallyInstalledColumns = self::getNotActuallyInstalledColumnNames($componentPrefix, $tableName);

        $sqls = array();
        foreach ($notActuallyInstalledColumns as $column) {
            $sqls[] = $this->buildRemoveOptionEntrySql($componentPrefix . $column);
        }

        return $sqls;
    }

    private function buildRemoveOptionEntrySql($optionName)
    {
        $tableName = Common::prefixTable('option');

        $sql = sprintf("DELETE FROM `%s` WHERE `option_name` = ?", $tableName);

        return $this->migration->db->boundSql($sql, array($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->executeMigrations(__FILE__, $this->getMigrations($updater));
    }
}