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
|
<?php
/**
* Copyright (C) Piwik PRO - All rights reserved.
*
* Using this code requires that you first get a license from Piwik PRO.
* Unauthorized copying of this file, via any medium is strictly prohibited.
*
* @link http://piwik.pro
*/
namespace Piwik\Plugins\FileSynchronizer;
use Piwik\Common;
use Piwik\Date;
use Piwik\Db;
use Piwik\DbHelper;
class Dao
{
/**
* @var Db
*/
private $db;
private $tableName = 'file_synchronizer';
private $tableNamePrefixed;
public function __construct()
{
$this->db = Db::get();
$this->tableNamePrefixed = Common::prefixTable($this->tableName);
}
public function getAllSyncedFiles()
{
return Db::fetchAll('SELECT * FROM ' . $this->tableNamePrefixed . ' WHERE end_date is not null');
}
public function getAllSyncingFiles()
{
return Db::fetchAll('SELECT * FROM ' . $this->tableNamePrefixed . ' WHERE end_date is null');
}
public function isSynced($fileHash)
{
// "end_date is null" => probably in process by another server
// "exit_code = 0" => file was already imported successfully
return (bool) $this->db->fetchOne("SELECT 1 FROM " . $this->tableNamePrefixed . "
WHERE `file_hash` = ? and (end_date is null or exit_code = 0) LIMIT 1", array($fileHash));
}
public function isHashFileCreated($fileHash)
{
return (bool) $this->db->fetchOne("SELECT 1 FROM " . $this->tableNamePrefixed . "
WHERE `file_hash` = ? and hash_file_created = 1 LIMIT 1", array($fileHash));
}
public function markHashFileCreated($fileHash)
{
$this->db->update($this->tableNamePrefixed, array(
'hash_file_created' => 1,
), '`file_hash` = "' . $fileHash . '"');
}
public function logFileSyncStart($source, $target, $fileHash, $fileSize, $startDate)
{
$this->db->insert($this->tableNamePrefixed, array(
'source' => $source,
'target' => $target,
'file_hash' => $fileHash,
'file_size' => $fileSize,
'start_date' => $startDate,
));
return (int) $this->db->lastInsertId();
}
public function logFileSyncFinished($id, $command, $output, $exitCode, $duration, $endDate)
{
$this->db->update($this->tableNamePrefixed, array(
'output' => $output,
'command' => $command,
'exit_code' => $exitCode,
'end_date' => $endDate,
'duration_in_ms' => $duration,
), 'idfilesync = ' . (int) $id);
}
public function install()
{
$table = "`idfilesync` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`start_date` datetime NOT NULL ,
`end_date` datetime NULL ,
`duration_in_ms` INT UNSIGNED NULL ,
`source` TEXT NOT NULL ,
`target` TEXT NOT NULL ,
`file_size` INT UNSIGNED NOT NULL DEFAULT 0,
`file_hash` VARCHAR(32) NOT NULL ,
`hash_file_created` TINYINT UNSIGNED NOT NULL DEFAULT 0,
`command` TEXT NULL,
`output` TEXT NULL ,
`exit_code` TINYINT UNSIGNED NULL";
DbHelper::createTable($this->tableName, $table);
}
public function uninstall()
{
Db::dropTables(array($this->tableNamePrefixed));
}
}
|