1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeoTimeZone; |
4
|
|
|
|
5
|
|
|
use ZipArchive; |
6
|
|
|
use ErrorException; |
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use GeoTimeZone\Quadrant\Indexer; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class UpdaterData |
12
|
|
|
{ |
13
|
|
|
const DOWNLOAD_DIR = "downloads/"; |
14
|
|
|
const TIMEZONE_FILE_NAME = "timezones"; |
15
|
|
|
const REPO_HOST = "https://api.github.com"; |
16
|
|
|
const REPO_USER = "node-geo-tz"; |
17
|
|
|
const REPO_PATH = "/repos/evansiroky/timezone-boundary-builder/releases/latest"; |
18
|
|
|
const GEO_JSON_DEFAULT_URL = "none"; |
19
|
|
|
const GEO_JSON_DEFAULT_NAME = "geojson"; |
20
|
|
|
|
21
|
|
|
protected $mainDir = null; |
22
|
|
|
protected $downloadDir = null; |
23
|
|
|
protected $timezonesSourcePath = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* UpdaterData constructor. |
27
|
|
|
* @param $dataDirectory |
28
|
|
|
* @throws ErrorException |
29
|
|
|
*/ |
30
|
|
|
public function __construct($dataDirectory = null) |
31
|
|
|
{ |
32
|
|
|
if ($dataDirectory == null) { |
33
|
|
|
throw new ErrorException("ERROR: Ivalid data directory."); |
34
|
|
|
}else{ |
35
|
|
|
$this->mainDir = $dataDirectory; |
36
|
|
|
$this->downloadDir = $dataDirectory . "/" . self::DOWNLOAD_DIR; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get complete json response from repo |
42
|
|
|
* @param $url |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
protected function getResponse($url) |
46
|
|
|
{ |
47
|
|
|
$client = new Client(); |
48
|
|
|
$response = $client->request('GET', $url); |
49
|
|
|
return $response->getBody()->getContents(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Download zip file |
54
|
|
|
* @param $url |
55
|
|
|
* @param string $destinationPath |
56
|
|
|
*/ |
57
|
|
|
protected function getZipResponse($url, $destinationPath = "none") |
58
|
|
|
{ |
59
|
|
|
exec("wget {$url} --output-document={$destinationPath}"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get timezones json url |
64
|
|
|
* @param $data |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function getGeoJsonUrl($data) |
68
|
|
|
{ |
69
|
|
|
$jsonResp = json_decode($data, true); |
70
|
|
|
$geoJsonUrl = self::GEO_JSON_DEFAULT_URL; |
71
|
|
|
foreach ($jsonResp['assets'] as $asset) { |
72
|
|
|
if (strpos($asset['name'], self::GEO_JSON_DEFAULT_NAME)) { |
73
|
|
|
$geoJsonUrl = $asset['browser_download_url']; |
74
|
|
|
break; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return $geoJsonUrl; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Download last version reference repo |
82
|
|
|
*/ |
83
|
|
|
protected function downloadLastVersion() |
84
|
|
|
{ |
85
|
|
|
$response = $this->getResponse(self::REPO_HOST . self::REPO_PATH); |
86
|
|
|
$geoJsonUrl = $this->getGeoJsonUrl($response); |
87
|
|
|
if ($geoJsonUrl != self::GEO_JSON_DEFAULT_URL) { |
88
|
|
|
if (!is_dir($this->mainDir)) { |
89
|
|
|
mkdir($this->mainDir); |
90
|
|
|
} |
91
|
|
|
if (!is_dir($this->downloadDir)) { |
92
|
|
|
mkdir($this->downloadDir); |
93
|
|
|
} |
94
|
|
|
$this->getZipResponse($geoJsonUrl, $this->downloadDir . self::TIMEZONE_FILE_NAME . ".zip"); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Unzip data |
100
|
|
|
* @param $filePath |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
protected function unzipData($filePath) |
104
|
|
|
{ |
105
|
|
|
$zip = new ZipArchive(); |
106
|
|
|
$controlFlag = false; |
107
|
|
|
if ($zip->open($filePath) === TRUE) { |
108
|
|
|
$zipName = basename($filePath, ".zip"); |
109
|
|
|
if (!is_dir($this->downloadDir . $zipName)) { |
110
|
|
|
mkdir($this->downloadDir . $zipName); |
111
|
|
|
} |
112
|
|
|
echo $this->downloadDir . $zipName . "\n"; |
113
|
|
|
$zip->extractTo($this->downloadDir . $zipName); |
114
|
|
|
$zip->close(); |
115
|
|
|
$controlFlag = true; |
116
|
|
|
unlink($filePath); |
117
|
|
|
} |
118
|
|
|
return $controlFlag; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Rename downloaded timezones json file |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
protected function renameTimezoneJson() |
126
|
|
|
{ |
127
|
|
|
$path = realpath($this->downloadDir . self::TIMEZONE_FILE_NAME . "/"); |
128
|
|
|
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path)); |
129
|
|
|
$jsonPath = ""; |
130
|
|
|
foreach ($files as $pathFile => $file) { |
131
|
|
|
if (strpos($pathFile, ".json")) { |
132
|
|
|
$jsonPath = $pathFile; |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
$this->timezonesSourcePath = dirname($jsonPath) . "/" . self::TIMEZONE_FILE_NAME . ".json"; |
137
|
|
|
echo $this->timezonesSourcePath . "\n"; |
138
|
|
|
return rename($jsonPath, $this->timezonesSourcePath); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Remove all directories tree in a particular data folder |
143
|
|
|
* @param $path |
144
|
|
|
* @param $validDir |
145
|
|
|
*/ |
146
|
|
|
protected function removeData($path, $validDir = null) |
147
|
|
|
{ |
148
|
|
|
$removeAll = !$validDir ? true : false; |
149
|
|
|
|
150
|
|
|
if (is_dir($path)) { |
151
|
|
|
$objects = scandir($path); |
152
|
|
|
foreach ($objects as $object) { |
153
|
|
|
$objectPath = $path . "/" . $object; |
154
|
|
|
if ($object != "." && $object != "..") { |
155
|
|
|
if (is_dir($objectPath)) { |
156
|
|
|
if (in_array(basename($object), $validDir) || $removeAll) { |
157
|
|
|
$this->removeData($objectPath, $validDir); |
158
|
|
|
} |
159
|
|
|
} else { |
160
|
|
|
unlink($objectPath); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
if (in_array(basename($path), $validDir) || $removeAll) { |
165
|
|
|
rmdir($path); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Remove data tree |
173
|
|
|
*/ |
174
|
|
|
protected function removeDataTree() |
175
|
|
|
{ |
176
|
|
|
$validDir = [ |
177
|
|
|
Indexer::LEVEL_A, |
178
|
|
|
Indexer::LEVEL_B, |
179
|
|
|
Indexer::LEVEL_C, |
180
|
|
|
Indexer::LEVEL_D |
181
|
|
|
]; |
182
|
|
|
$this->removeData($this->mainDir . "/", $validDir); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Remove downloaded data |
188
|
|
|
*/ |
189
|
|
|
protected function removeDownloadedData() |
190
|
|
|
{ |
191
|
|
|
$validDir = array("downloads", "timezones", "dist"); |
192
|
|
|
$this->removeData($this->downloadDir, $validDir); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Add folder to zip file |
197
|
|
|
* @param $mainDir |
198
|
|
|
* @param $zip |
199
|
|
|
* @param $exclusiveLength |
200
|
|
|
*/ |
201
|
|
|
protected function folderToZip($mainDir, &$zip, $exclusiveLength) |
202
|
|
|
{ |
203
|
|
|
$handle = opendir($mainDir); |
204
|
|
|
while (false !== $f = readdir($handle)) { |
205
|
|
|
if ($f != '.' && $f != '..') { |
206
|
|
|
$filePath = "$mainDir/$f"; |
207
|
|
|
$localPath = substr($filePath, $exclusiveLength); |
208
|
|
|
if (is_file($filePath)) { |
209
|
|
|
$zip->addFile($filePath, $localPath); |
210
|
|
|
} elseif (is_dir($filePath)) { |
211
|
|
|
$zip->addEmptyDir($localPath); |
212
|
|
|
$this->folderToZip($filePath, $zip, $exclusiveLength); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
closedir($handle); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Compress directory |
221
|
|
|
* @param $sourcePath |
222
|
|
|
* @param $outZipPath |
223
|
|
|
*/ |
224
|
|
|
protected function zipDir($sourcePath, $outZipPath) |
225
|
|
|
{ |
226
|
|
|
$pathInfo = pathInfo($sourcePath); |
227
|
|
|
$parentPath = $pathInfo['dirname']; |
228
|
|
|
$dirName = $pathInfo['basename']; |
229
|
|
|
|
230
|
|
|
$z = new ZipArchive(); |
231
|
|
|
$z->open($outZipPath, ZIPARCHIVE::CREATE); |
232
|
|
|
$z->addEmptyDir($dirName); |
233
|
|
|
$this->folderToZip($sourcePath, $z, strlen("$parentPath/")); |
234
|
|
|
$z->close(); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Main function that runs all updating process |
239
|
|
|
*/ |
240
|
|
|
public function updateData() |
241
|
|
|
{ |
242
|
|
|
echo "Downloading data...\n"; |
243
|
|
|
$this->downloadLastVersion(); |
244
|
|
|
echo "Unzip data...\n"; |
245
|
|
|
$this->unzipData($this->downloadDir . self::TIMEZONE_FILE_NAME . ".zip"); |
246
|
|
|
echo "Rename timezones json...\n"; |
247
|
|
|
$this->renameTimezoneJson(); |
248
|
|
|
echo "Remove previous data...\n"; |
249
|
|
|
$this->removeDataTree(); |
250
|
|
|
echo "Creating quadrant tree data...\n"; |
251
|
|
|
$geoIndexer = new Indexer($this->mainDir, $this->timezonesSourcePath); |
252
|
|
|
$geoIndexer->createQuadrantTreeData(); |
253
|
|
|
echo "Remove downloaded data...\n"; |
254
|
|
|
$this->removeDownloadedData(); |
255
|
|
|
echo "Zipping quadrant tree data..."; |
256
|
|
|
$this->zipDir($this->mainDir, $this->mainDir . ".zip"); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|