File size: 1,895 Bytes
e852054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?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\AnonymousPiwikUsageMeasurement\Tracker;

use Piwik\Common;
use Piwik\Db;
use Piwik\DbHelper;

class Profiles
{
    private $tableName = 'usage_measurement_profiles';
    private $tableNamePrefixed;

    public function __construct()
    {
        $this->tableNamePrefixed = Common::prefixTable($this->tableName);
    }

    public function popAll()
    {
        $profiles = Db::get()->fetchAll('SELECT * FROM ' . $this->tableNamePrefixed);
        Db::get()->query('DELETE FROM ' . $this->tableNamePrefixed);

        return $profiles;
    }

    public function pushProfile($creationDate, $category, $name, $action, $count, $wallTimeInMs)
    {
        $sql  = 'INSERT INTO `' . $this->tableNamePrefixed .
                '` (creation_date, `category`, `name`, `action`, `count`, `wall_time`) ' .
                ' VALUES (?, ?, ?, ?, ?, ?) ' .
                ' ON DUPLICATE KEY UPDATE `count` = `count` + ?, `wall_time` = `wall_time` + ?';
        $bind = array($creationDate, $category, $name, $action, $count, $wallTimeInMs, $count, $wallTimeInMs);

        Db::get()->query($sql, $bind);
    }

    public function install()
    {
        $table = "`creation_date` datetime NOT NULL ,
                  `category` VARCHAR(200) NOT NULL ,
                  `name` VARCHAR(200) NOT NULL ,
                  `action` VARCHAR(200) NOT NULL,
                  `count` INT UNSIGNED NOT NULL DEFAULT 0 ,
                  `wall_time` BIGINT UNSIGNED NOT NULL DEFAULT 0 ,
                  PRIMARY KEY(`category`(64), `name`(64), `action`(63))";

        DbHelper::createTable($this->tableName, $table);
    }

    public function uninstall()
    {
        Db::dropTables(array($this->tableNamePrefixed));
    }
}