-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep3-combine-all.php
68 lines (51 loc) · 1.27 KB
/
step3-combine-all.php
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
<?php
declare(strict_types = 1);
require_once('boot.php');
require_once('console.php');
$O = getopt("r:o:h");
function usage(){
global $argv;
print "\nUsage: $argv[0] -r <ip2country_db_root> -o <combined_db_file> [-h]\n";
print "\n";
print "\t-r Root for ip2country files\n";
print "\t-o File where to save combined database\n";
print "\t-h help\n";
print "\n";
exit(1);
}
if(empty($O['r']) || empty($O['o']) || isset($O['h']))
usage();
$ROOT = realpath($O['r'].DIRECTORY_SEPARATOR);
$COMBINED = $O['o'];
$COMBINED_ROOT = realpath(pathinfo($O['o'], PATHINFO_DIRNAME));
if(!is_readable($ROOT)){
print "Not readable: $ROOT\n";
exit(1);
}
if(!is_writable($COMBINED_ROOT)){
print "Not writeable: $COMBINED_ROOT\n";
exit(1);
}
$db_patt = $ROOT.DIRECTORY_SEPARATOR."*.db";
print "Loading: $db_patt...";
$db = new CountryRangeDB("all");
foreach (glob($db_patt) as $filename) {
$iso = pathinfo($filename, PATHINFO_FILENAME);
$r = new RangeDB($iso);
$r->loadFile($filename);
$db->copyFrom($r);
unset($r);
}
print "DONE\n";
print "De-duplicate...\n";
while($db->equals());
print "DONE\n";
print "Overlaps:\n";
$db->overlapopen();
print "DONE\n";
print "Sorting...";
$db->sort("cmpStart");
print "DONE\n";
print "Saving $COMBINED...";
$db->save($COMBINED);
print "DONE\n";