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

TablePartitioning.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7dee00bf9c5e2072b85f4f64c4f9c980c029837b (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
<?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
 */

/**
 * FIXMEA: simplify/delete this code
 *
 * @package Piwik
 * @subpackage Piwik_TablePartitioning
 */
abstract class Piwik_TablePartitioning
{
    protected $tableName = null;
    protected $generatedTableName = null;
    protected $timestamp = null;

    static public $tablesAlreadyInstalled = null;

    public function __construct($tableName)
    {
        $this->tableName = $tableName;
    }

    abstract protected function generateTableName();

    public function setTimestamp($timestamp)
    {
        $this->timestamp = $timestamp;
        $this->generatedTableName = null;
        $this->getTableName();
    }

    public function getTableName()
    {
        // table name already processed
        if (!is_null($this->generatedTableName)) {
            return $this->generatedTableName;
        }

        if (is_null($this->timestamp)) {
            throw new Exception("You have to specify a timestamp for a Table Partitioning by date.");
        }

        // generate table name
        $this->generatedTableName = $this->generateTableName();

        // we make sure the table already exists
        $this->checkTableExists();
    }

    protected function checkTableExists()
    {
        if (is_null(self::$tablesAlreadyInstalled)) {
            self::$tablesAlreadyInstalled = Piwik::getTablesInstalled($forceReload = false);
        }

        if (!in_array($this->generatedTableName, self::$tablesAlreadyInstalled)) {
            $db = Zend_Registry::get('db');
            $sql = Piwik::getTableCreateSql($this->tableName);

            $config = Piwik_Config::getInstance();
            $prefixTables = $config->database['tables_prefix'];
            $sql = str_replace($prefixTables . $this->tableName, $this->generatedTableName, $sql);
            try {
                $db->query($sql);
            } catch (Exception $e) {
                // mysql error 1050: table already exists
                if (!$db->isErrNo($e, '1050')) {
                    // failed for some other reason
                    throw $e;
                }
            }

            self::$tablesAlreadyInstalled[] = $this->generatedTableName;
        }
    }

    public function __toString()
    {
        return $this->getTableName();
    }
}

/**
 *
 * @package Piwik
 * @subpackage Piwik_TablePartitioning
 */
class Piwik_TablePartitioning_Monthly extends Piwik_TablePartitioning
{
    private static $blobArchiveTable = null;
    private static $numericArchiveTable = null;
    
    public function __construct($tableName)
    {
        parent::__construct($tableName);
    }

    protected function generateTableName()
    {
        $config = Piwik_Config::getInstance();
        return $config->database['tables_prefix'] . $this->tableName . "_" . date("Y_m", $this->timestamp);
    }
    
    /**
     * Creates archive_blob & archive_numeric tables for a period if they don't already exist.
     * 
     * @param Piwik_Date
     */
    public static function createArchiveTablesIfAbsent($dateInMonth)
    {
        $timestamp = $dateInMonth->getTimestamp();
        
        self::$blobArchiveTable->setTimestamp($timestamp);
        self::$blobArchiveTable->getTableName();
        
        self::$numericArchiveTable->setTimestamp($timestamp);
        self::$numericArchiveTable->getTableName();
    }
    
    public static function init()
    {
        self::$blobArchiveTable = new Piwik_TablePartitioning_Monthly('archive_blob');
        self::$numericArchiveTable = new Piwik_TablePartitioning_Monthly('archive_numeric');
    }
}

Piwik_TablePartitioning_Monthly::init();