-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbuild-data.country-mapping
278 lines (250 loc) · 9.88 KB
/
build-data.country-mapping
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env perl
# THIS SCRIPT IS NOT INTENDED FOR END USERS OR FOR PEOPLE INSTALLING
# THE MODULES, BUT FOR THE AUTHOR'S USE WHEN UPDATING THE DATA FROM OFCOM'S
# PUBLISHED DATA.
use strict;
use warnings;
# use lib 'lib';
# use Number::Phone::NANP::Data;
use lib 'buildtools';
use Number::Phone::BuildHelpers;
my %known_non_country_codes = known_non_country_codes();
$| = 1;
use XML::XPath;
my $xml = XML::XPath->new(filename => 'libphonenumber/resources/PhoneNumberMetadata.xml');
my @territories = $xml->find('/phoneNumberMetadata/territories/territory')->get_nodelist();
mkdir('lib/Number/Phone/Country');
my %idd_codes = ();
my %prefix_codes = ();
TERRITORY: foreach my $territory (@territories) {
my $IDD_country_code = ''.$territory->find('@countryCode');
my $national_code = ''.$territory->find('@nationalPrefix');
my $international_rx = ''.$territory->find('@internationalPrefix');
my $international_code = ''.$territory->find('@preferredInternationalPrefix') ||
$international_rx;
my $ISO_country_code = ''.$territory->find('@id');
if(is_dodgy_unknown_country($ISO_country_code, $IDD_country_code)) {
warn("skipping 'country' $ISO_country_code (+$IDD_country_code)\n");
next TERRITORY;
}
$international_code =~ s/~//; # ex-Soviet republics
if($international_code && $international_code !~ /^\d+$/) {
warn("For $ISO_country_code found idd code $international_code\n");
# some of these privelege a particular carrier. I don't care. I
# just want something that works.
$international_code = (grep { $_ =~ /^$international_code$/ } qw(00 000 001 002 008 009 0012 011 191200))[0];
if($international_code) {
warn(" fixed it to $international_code\n");
} else {
exit(1);
}
}
my $idd_rx = eval { qr/\A $international_rx /xms };
if ($@ || !$idd_rx) {
warn("For $ISO_country_code found invalid idd regex $international_rx\n$@\n");
exit(1);
}
elsif (length $international_code) {
if ($international_code !~ $idd_rx) {
warn("idd regex $idd_rx for $ISO_country_code does not match" .
"against its idd code $international_code. Exiting.\n");
exit(1);
}
}
if($national_code && $national_code !~ /^\d+$/) {
warn("For $ISO_country_code found ndd code $national_code\n");
exit(1);
}
if(exists($idd_codes{$IDD_country_code})) {
$idd_codes{$IDD_country_code} = ref($idd_codes{$IDD_country_code})
? [@{$idd_codes{$IDD_country_code}}, $ISO_country_code]
: [$idd_codes{$IDD_country_code}, $ISO_country_code];
} else {
$idd_codes{$IDD_country_code} = $ISO_country_code;
}
$prefix_codes{$ISO_country_code} = [
$IDD_country_code,
(length($international_code) ? $international_code : undef),
(length($national_code) ? $national_code : undef),
(length($international_rx) ? $international_rx : undef),
];
}
open(my $module_fh, '>:encoding(UTF-8)', "lib/Number/Phone/Country/Data.pm")
|| die("Can't write lib/Number/Phone/Country/Data.pm: $!\n");
print $module_fh q{
# automatically generated file, don't edit
package Number::Phone::Country::Data;
$VERSION = '2.}.
join("",
(gmtime())[5] + 1900,
sprintf('%02d', (gmtime())[4] + 1),
map { sprintf('%02d', $_) } (gmtime())[3, 2, 1, 0]
)
.q{';
%Number::Phone::Country::idd_codes = (
};
foreach my $idd_code (sort keys %idd_codes) {
next if($idd_code eq '1');
print $module_fh ''.$idd_code . ' => ' .
(
ref($idd_codes{$idd_code})
? '['.join(', ', map { "'$_'" } @{$idd_codes{$idd_code}})."],\n"
: "'$idd_codes{$idd_code}',\n"
);
}
print $module_fh q{
);
%Number::Phone::Country::prefix_codes = (
};
foreach my $ISO_code (sort keys %prefix_codes) {
print $module_fh
"'$ISO_code' => ['".
$prefix_codes{$ISO_code}->[0].
"', ".
join(', ', map {
defined($_)
? "'$_'"
: 'undef'
} $prefix_codes{$ISO_code}->[1], $prefix_codes{$ISO_code}->[2]).
', ' .
(defined $prefix_codes{$ISO_code}->[3]
? 'qr/\A ' . $prefix_codes{$ISO_code}->[3] . ' /xms'
: 'undef'
).
"],\n";
}
print $module_fh q{
);
$Number::Phone::Country::prefix_codes{UK} = $Number::Phone::Country::prefix_codes{GB};
};
# and now for non-geo "country" codes, sub-countries, countries "borrowing"
# from other countries, and Russia/Kazakhstan
foreach my $tuple (
# for some "shared space" codes we need to ensure that the "host" country comes first.
# libphonenumber has them in alphabetical order which we don't want.
[ 212 => ['MA', 'EH' ]],
[ 358 => ['FI', 'AX' ]],
[ 47 => ['NO', 'BV', 'SJ' ]], # no BV in libphonenumber
[ 500 => ['FK', 'GS' ]], # no GS in libphonenumber
[ 590 => ['GP', 'BL', 'MF' ]],
[ 596 => ['MQ', 'TF' ]], # no TF in libphonenumber
[ 672 => ['AQ', 'NF' ]], # no AQ in libphonenumber
[ 7 => ['RU', 'KZ' ]],
# checked on 2024-12-11
# next check due 2025-12-01 (annually)
[ 2125288 => 'EH' ], # see https://en.wikipedia.org/wiki/Telephone_numbers_in_Western_Sahara, may overlap with MA (Morocco)
[ 2125289 => 'EH' ], # see https://en.wikipedia.org/wiki/Telephone_numbers_in_Western_Sahara, may overlap with MA (Morocco)
[ 262269 => 'YT' ], # Mayotte fixed lines, see https://en.wikipedia.org/wiki/Telephone_numbers_in_France
[ 262639 => 'YT' ], # Mayotte GSM, see https://en.wikipedia.org/wiki/Telephone_numbers_in_France
[ 35348 => 'GB' ], # https://en.wikipedia.org/wiki/Telephone_numbers_in_the_Republic_of_Ireland#Calls_to_Northern_Ireland
[ 379 => 'VA' ], # Vatican (not in use, missing from libphonenumber), see https://en.wikipedia.org/wiki/Telephone_numbers_in_Vatican_City
[ 3906698 => 'VA' ], # Vatican in Italian dialling plan, see https://en.wikipedia.org/wiki/Telephone_numbers_in_Vatican_City
[ 390549 => 'SM' ], # San Marino in Italian dialling plan, see https://en.wikipedia.org/wiki/Telephone_numbers_in_San_Marino
(
map {
[ '44'.$_ => 'GG' ]
} (
1481, 7781, 7839, 79111, 79117
)
),
(
map {
[ '44'.$_ => 'IM' ]
} (
1624,
762450,
762456,
74576,
7524,
7924,
76240,
76241,
76242,
76243,
76244,
76246,
76248,
76249,
808162,
8440406, 8440906,
872299,
845624, 870624,
900624, 901624, 906624, 907624,
)
),
(
map {
[ '44'.$_ => 'JE' ]
} (
1534,
7509, 7829, 7937,
77003, 77007, 77008,
77977, 77978, 77979,
800735, 800781,
8089012, 8089013, 8089019,
)
),
# see https://en.wikipedia.org/wiki/Cocos_(Keeling)_Islands#Communications
# NB +61 406 listed there for mobiles may be a pan-Australia area code
# see https://en.wikipedia.org/wiki/Telephone_numbers_in_Christmas_Island
# see https://en.wikipedia.org/wiki/Telephone_numbers_in_the_Australian_Antarctic_Territory
# see https://en.wikipedia.org/wiki/Telephone_numbers_in_Norfolk_Island
# checked on 2024-12-11
# next check due 2025-12-01 (annually)
[ 5999 => 'CW' ], # Curacao see https://en.wikipedia.org/wiki/Cura%C3%A7ao
[ 6189162 => 'CC' ], # Cocos (Keeling) Islands
[ 6189164 => 'CX' ], # Christmas Island
[ 67210 => 'AQ' ], # Davis station \
[ 67211 => 'AQ' ], # Mawson |
[ 67212 => 'AQ' ], # Casey | Australian Antarctic bases
[ 67213 => 'AQ' ], # Macquarie Island |
[ 67214 => 'AQ' ], # Wilkins, mobiles /
[ 6723 => 'NF' ], # Norfolk Island
# see http://en.wikipedia.org/wiki/Telephone_numbers_in_Kazakhstan
# see https://en.wikipedia.org/wiki/%2B7
# checked on 2024-12-11
# next check due 2025-12-01 (annually)
[ 76 => 'KZ' ],
[ 77 => 'KZ' ],
# see https://en.wikipedia.org/wiki/Telephone_numbers_in_Kosovo
# checked on 2024-12-11
# next check due 2026-12-01 (bi-annually)
#
# in the Serbian country code ... https://en.wikipedia.org/wiki/Telephone_numbers_in_Serbia#Kosovo
[ 38128 => 'XK' ],
[ 38129 => 'XK' ],
[ 38138 => 'XK' ],
[ 38139 => 'XK' ],
do {
map {
[ $_ => $known_non_country_codes{$_} ]
} keys %known_non_country_codes
},
) {
print $module_fh
'$Number::Phone::Country::idd_codes{'.
$tuple->[0].
"} = ".
(ref($tuple->[1])
? '['.join(', ', map { "'$_'" } @{$tuple->[1]}).']'
: "'".$tuple->[1]."'"
).
";\n";
}
foreach my $IDD (keys %known_non_country_codes) {
my $name = $known_non_country_codes{$IDD};
print $module_fh qq{
\$Number::Phone::Country::prefix_codes{'$name'} = ['$IDD'];
};
}
# countries that don't exist in libphonenumber
# FIXME unchecked
print $module_fh q{
$Number::Phone::Country::prefix_codes{VA} = ['379', '00', undef, qr/\A 00 /xms];
$Number::Phone::Country::prefix_codes{BV} = ['47', '00', undef, qr/\A 00 /xms];
$Number::Phone::Country::prefix_codes{GS} = ['500', '00', undef, qr/\A 00 /xms];
$Number::Phone::Country::prefix_codes{TF} = ['596', '00', '0', qr/\A 00 /xms];
# 0011 from https://en.wikipedia.org/wiki/Telephone_numbers_in_the_Australian_Antarctic_Territory
# but note that Norfolk Island is also 672 but apparently uses 00 as per libphonenumber
$Number::Phone::Country::prefix_codes{AQ} = ['672', '0011', undef, qr/\A 0011 /xms];
};