Repository navigation
Expand file tree
/
Copy pathRangeUpdater.php
More file actions
145 lines (122 loc) · 4.35 KB
/
Copy pathRangeUpdater.php
File metadata and controls
145 lines (122 loc) · 4.35 KB
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
138
139
140
141
142
143
144
145
<?php
namespace Piwik\Plugins\VipDetector;
use Exception;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Http;
use Piwik\Log\LoggerInterface;
use Piwik\Plugins\VipDetector\Dao\DatabaseMethods;
use Piwik\Plugins\VipDetector\libs\Helpers;
use Piwik\SettingsPiwik;
class RangeUpdater
{
private LoggerInterface $logger;
/**
* @var string $source_type How we are acquiring the data
*/
private string $source_type;
/**
* @var string $source Where the data is
*/
private string $source;
/**
* @param string $source Where the data is
* @param string $source_type How we are acquiring the data
* @throws Exception
*/
public function __construct(string $source, string $source_type)
{
// @phan-suppress-next-line PhanAccessMethodInternal
$this->logger = StaticContainer::get(LoggerInterface::class);
$this->source = $source; // Path or Url to the source
$this->source_type = $source_type; // url or file
}
/**
* Try to fetch the data file and seed the database
* @return void The status if the import
* @throws Exception
*/
public function import(): void
{
// Load the json source
$sourceData = $this->loadJson($this->source_type, $this->source);
$this->insertData($sourceData);
$this->logger->info("Done loading.");
}
/**
* Load the JSON Data. This can be done from a local file via the command line, or via HTTP from the UI.
* @param string $source_type
* @param string $source
* @return \stdClass[] $rangeInfo
* @throws Exception
*/
private function loadJson(string $source_type, string $source): array
{
// At the moment this can be "file" or "url".
switch ($source_type) {
case 'file':
$this->logger->info("Source is file. Start loading");
$source_string = @file_get_contents($source);
break;
case 'url':
if (!SettingsPiwik::isInternetEnabled()) {
throw new Exception("To load from a remote URL internet access needs to be enabled.");
}
$this->logger->info("Source is remote URL. Start loading.");
// download the file.
$source_string = Http::sendHttpRequest($source, 30);
break;
default:
throw new Exception("Invalid source type.");
}
// request was ok, but response was empty, file not found, etc
if (!$source_string) {
throw new Exception("File could not be loaded.");
}
// input is not valid json.
if (!$json = json_decode($source_string)) {
throw new Exception("File is not JSON.");
}
return $json;
}
/**
* Insert the data to the respective tables
* @throws Exception
* @param \stdClass[] $data
*/
private function insertData(array $data): void
{
// loop through all elements in the source
foreach ($data as $entry) {
$name = Common::sanitizeInputValues($entry->name);
// only insert the name if it is not already there
try {
$nameId = DatabaseMethods::getNameId($name);
} catch (Exception $ex1) {
if (!DatabaseMethods::insertName($name)) {
$this->logger->critical("Name {$name} could not be inserted.");
}
$nameId = DatabaseMethods::getNameId($name);
}
foreach ($entry->ranges as $range) {
$this->logger->debug("Currently at {$name}: {$range}");
try {
$rangeInfo = Helpers::getRangeInfo($range);
} catch (Exception $ex) {
$this->logger->critical("Invalid range {$range}");
continue;
}
// same as for name, don't insert duplicates
if (!DatabaseMethods::checkRangeInDb($rangeInfo)) {
$rangeInfoToInsert = array_merge(
$rangeInfo,
['name_id' => $nameId]
);
DatabaseMethods::insertRange(
$rangeInfoToInsert
);
}
}
}
}
}