]>
jfr.im git - irc/SurrealServices/srsv.git/blob - tags/0.4.3.1-pre2/utils/country-table3.pl
3 # This file is part of SurrealServices.
5 # SurrealServices is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # SurrealServices is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with SurrealServices; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 # SurrealChat.net does not provide the Country/Allocation data,
20 # is in no way associated with maxmind.com,
21 # nor are we providing a license to download/use it.
22 # Be sure to direct availability/accuracy/licensing questions to maxmind.com
28 use Cwd
qw( abs_path getcwd );
32 PREFIX
=> abs_path
(dirname
(abs_path
($0)).'/..'),
34 require constant; import
constant(\
%constants);
42 use SrSv
::Conf2Consts
qw( sql );
45 countrydb_url
=> 'rsync://countries-ns.mdc.dk/zone/zz.countries.nerd.dk.rbldnsd',
46 srcname
=> 'zz.countries.nerd.dk.rbldnsd',
54 print "Synching country-data file...\n";
56 print "Connecting to database...\n";
57 my $dbh = dbConnect
();
58 print "Creating new table...\n";
60 print "Inserting data... ";
62 print "Removing old table...\n";
65 print "Country table update complete.\n";
69 my $srcPath = PREFIX
.'/data/'.srcname
;
70 system('rsync -azvv --progress '.countrydb_url
.' '.$srcPath);
72 print STDERR
"FATAL: Download failed.\n";
81 $dbh = DBI-
>connect("DBI:mysql:"..sql_conf_mysql_db
, sql_conf_mysql_user
, sql_conf_mysql_pass
,
82 { AutoCommit
=> 1, RaiseError
=> 1, PrintError
=> 1 })
86 print STDERR
"FATAL: Can't connect to database:\n$@\n";
87 print STDERR
"You must edit config/sql.conf and create a corresponding\nMySQL user and database!\n\n";
97 $dbh->do("DROP TABLE IF EXISTS newcountry");
99 "CREATE TABLE `newcountry` (
100 `low` int unsigned NOT NULL default 0,
101 `high` int unsigned NOT NULL default 0,
102 `country` char(2) NOT NULL default '-',
103 PRIMARY KEY (`low`, `high`)
110 my $add_entry = $dbh->prepare("INSERT IGNORE INTO newcountry SET low=?, high=?, country=?");
113 my $unpackPath = PREFIX
.'/data/'.srcname
;
114 my ($lines) = qx{wc -l $unpackPath};
115 my $div = int($lines/100);
118 open ((my $COUNTRYTABLE), '<', $unpackPath);
119 $dbh->do("ALTER TABLE `newcountry` DISABLE KEYS");
120 $dbh->do("LOCK TABLES newcountry WRITE");
121 while(my $x = <$COUNTRYTABLE>) {
122 if($i == 0 or !($i % $div)) {
123 printf("\b\b\b\b%3d%", ($i/$lines)*100);
127 #85.10.224.152/29 :127.0.0.20:ad
128 if ($x =~ /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/(\d
{1,2}) \
:(\S
+)\
:([a-z
]{1,2})$/) {
129 my $low = $1 << 24 | $2 << 16 | $3 << 8 | $4;
130 my $high = $low + ((2 << (31 - $5)));
132 next if lc $country eq 'eu';
133 push @entries, '('.$dbh->quote($low).','.$dbh->quote($high).','.$dbh->quote($country).')';
134 if(scalar(@entries) >= 100) { #1000 only gives another 10% boost for 10x as much memory
135 $dbh->do("INSERT IGNORE INTO newcountry (low, high, country) VALUES ".join(',', @entries));
142 $dbh->do("INSERT IGNORE INTO newcountry (low, high, country) VALUES ".join(',', @entries)) if scalar(@entries);
144 $dbh->do("UNLOCK TABLES");
145 $dbh->do("ALTER TABLE `newcountry` ENABLE KEYS");
147 print "\b\b\b\bdone.\n";
153 $dbh->do("DROP TABLE IF EXISTS `oldcountry`");
154 print "Renaming new table...\n";
155 $dbh->{RaiseError
} = 0;
156 $dbh->do("OPTIMIZE TABLE `newcountry`");
157 $dbh->do("ANALYZE TABLE `newcountry`");
158 # Doing the renames cannot be done atomically
159 # as sometimes `country` doesn't exist yet.
160 $dbh->do("RENAME TABLE `country` TO `oldcountry`");
161 $dbh->do("RENAME TABLE `newcountry` TO `country`");
162 $dbh->do("DROP TABLE `oldcountry`");