-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate.php
executable file
·110 lines (97 loc) · 3.16 KB
/
generate.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
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
#!/usr/bin/env php
<?php
function main()
{
$lines = file_get_contents('https://publicsuffix.org/list/public_suffix_list.dat');
$lines = explode("\n", $lines);
$tldMap = [];
$section = 'default'; // ICANN DOMAINS or PRIVATE DOMAINS
foreach ($lines as $line) {
$line = trim($line);
if (!$line) {
continue;
}
if (substr($line, 0, 2) === '//') {
$m = null;
if (preg_match("/===(\w+) ([\w\s]+)===/", $line, $m)) {
if ($m[1] === 'BEGIN') {
$section = $m[2];
} else if ($m[1] === 'END') {
$section = 'default';
}
}
continue; // comment
}
if (substr($line, 0, 2) === '*.') {
// wildcard
$line = substr($line, 2);
$domain = idn_to_ascii($line);
$tldMap[$section][$domain] = 2;
continue;
}
if (substr($line, 0, 1) === '!') {
// exclude from wildcard
$line = substr($line, 1);
$domain = idn_to_ascii($line);
$tldMap[$section][$domain] = 3;
continue;
}
$domain = idn_to_ascii($line);
$tldMap[$section][$domain] = 1;
}
foreach ($tldMap as &$map) {
foreach ($map as $k => $_) {
$fields = explode('.', $k);
$parent = '';
while (count($fields) > 1) {
$s = array_pop($fields);
$parent .= $s . ($parent ? '.' : '') . $parent;
if (!isset($map[$parent])) {
$map[$parent] = 0;
}
}
}
}
$phpMap = var_export($tldMap, true);
$phpMap = str_replace('array (', '[', $phpMap);
$phpMap = str_replace(')', ']', $phpMap);
$phpMap = preg_replace("/=>\s+/m", '=> ', $phpMap);
$phpMap = preg_replace("/\[\s+]/m", '[]', $phpMap);
$now = date('Y-m-d H:i:s');
if (file_exists(__DIR__ . '/php/TldParser.php')) {
echo "generates php code\n";
file_put_contents(__DIR__ . '/php/TldList.data.php', "<?php
// generated on '$now'
return $phpMap;
");
}
if (file_exists(__DIR__ . '/go/tldparser/tld_parser.go')) {
echo "generates go code\n";
$goMap = str_replace(' =>', ':', $phpMap);
$goMap = str_replace("'", '"', $goMap);
$goMap = trim($goMap, "[] \t\n");
$goMap = str_replace(']', '}', $goMap);
$goMap = str_replace('[', 'map[string]int {', $goMap);
file_put_contents(__DIR__ . '/go/tldparser/tld_list_data.go', "package tldparser
// @formatter:off
// generated on '$now'
var tldMap = map[string]map[string]int {
$goMap
}
");
}
if (file_exists(__DIR__ . '/python/tldparser/__init__.py')) {
echo "generates python code\n";
$pythonDict = str_replace(' =>', ':', $phpMap);
$pythonDict = trim($pythonDict, "[] \t\n");
$pythonDict = str_replace(']', '}', $pythonDict);
$pythonDict = str_replace('[', '{', $pythonDict);
file_put_contents(__DIR__ . '/python/tldparser/tldparser_data.py', "# @formatter:off
# generated on '$now'
_tld_map = {
$pythonDict
}
");
}
}
main();