fix biblionumber
[google-map-tiles.git] / koha-import.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use DBI;
6 use XML::Simple;
7 use Data::Dump qw(dump);
8
9 our $koha_dsn    = 'dbi:oursql:dbname=koha';
10 our $koha_user   = 'kohaadmin';
11 our $koha_passwd = '';
12
13 our $geo_dsn = 'dbi:Pg:dbname=koha';
14 our $geo_user = '';
15 our $geo_passwd = '';
16
17 our $opts = { RaiseError => 1 }; #, AutoCommit => 0, pg_enable_utf8 => 1, oursql_enable_utf8 => 1 };
18
19 require '/srv/koha-config.pl';
20
21 my $k_dbh = DBI->connect($koha_dsn, $koha_user, $koha_passwd, $opts) || die $DBI::errstr;
22 my $g_dbh = DBI->connect($geo_dsn, $geo_user, $geo_passwd, $opts) || die $DBI::errstr;
23
24 sub fetch_table {
25         my ( $table, $pk ) = @_;
26
27         warn "# clean $table";
28         $g_dbh->do( "delete from $table" );
29
30         my $offset = 0;
31
32         my $sql = "select * from $table order by $pk";
33
34         warn "# import $table";
35
36         do {
37
38                 my $sth = $k_dbh->prepare( "$sql limit 1000 offset $offset" );
39                 print STDERR "$table [$offset] ";
40                 $sth->execute;
41
42                 $offset = 0 if ! $sth->rows; # exit
43
44                 my @cols = @{ $sth->{NAME_lc} };
45                 my $sql_insert = "insert into $table (" . join(',',@cols) . ") values (" . join(',', map { '?' } @cols ) . ")";
46                 my $sth_insert = $g_dbh->prepare( $sql_insert );
47
48                 while( my $row = $sth->fetchrow_arrayref ) {
49                         eval { $sth_insert->execute( @$row ) };
50                         $offset++;
51                         print STDERR "$offset " if $offset % 100 == 0;
52                 }
53
54                 print STDERR "\n";
55
56         } while $offset;
57 }
58
59 #fetch_table 'biblio' => 'biblionumber' ;
60 #fetch_table 'biblioitems' => 'biblioitemnumber' ;
61
62 sub geo_biblioitems {
63
64 warn "# drop geo_biblioitems";
65 eval { $g_dbh->do(qq{ drop table geo_biblioitems }) };
66
67 warn "# create geo_biblioitems";
68 $g_dbh->do(qq{
69 create table geo_biblioitems (
70         biblioitemnumber integer not null references biblioitems(biblioitemnumber),
71         biblionumber integer not null references biblio(biblionumber),
72         city text
73 )
74 });
75 my $sth_insert = $g_dbh->prepare(qq{
76         insert into geo_biblioitems values (?,?,?)
77 });
78
79 warn "# select bibiloitems";
80 my $sth = $g_dbh->prepare(qq{
81 SELECT
82         biblioitemnumber, biblionumber, isbn, issn, marcxml
83 from biblioitems
84 });
85 $sth->execute;
86
87 warn $sth->rows, " rows\n";
88
89 sub warn_dump {
90         warn dump @_,$/ if $ENV{DEBUG};
91 }
92
93 my $i = 0;
94
95 while ( my $row = $sth->fetchrow_hashref ) {
96         my $xml = XMLin( delete $row->{marcxml}, ForceArray => [ 'datafield', 'subfield' ] );
97
98         warn_dump($row, $xml);
99
100         my @tag_260 = grep { $_->{tag} eq '260' } @{ $xml->{datafield} };
101
102         next unless @tag_260;
103
104         warn_dump @tag_260 if $ENV{DEBUG};
105
106         foreach my $sf ( @{ $tag_260[0]->{subfield} } ) {
107                 $row->{ 'tag_260_' . $sf->{code} } = $sf->{content};
108         }
109
110         $row->{city} = $row->{tag_260_a};
111         $row->{city} =~ s/['"]+//g;
112         $row->{city} =~ s/\s*\[etc.*\].*$//;
113         $row->{city} =~ s/\s*[:]\s*$//;
114         $row->{city} =~ s/[\[\]]+//g;
115
116         warn_dump $row;
117
118         warn "# $i ", $row->{city}, $/ if $i++ % 100 == 0;
119
120         $sth_insert->execute( $row->{biblioitemnumber}, $row->{biblionumber}, $row->{city} );
121 }
122
123 } # geo_biblioitems;
124
125 #geo_biblioitems;
126
127 eval {
128 $g_dbh->do(qq{
129 create table geo_city (
130         city_koha text not null primary key,
131         city text,
132         country text,
133         county text,
134         lat float,
135         lng float,
136         radius int,
137         dump text
138 )
139 });
140 };
141
142 my $sth_insert = $g_dbh->prepare(qq{insert into geo_city values (?,?,?,?,?,?,?,?)});
143
144 my $sth = $g_dbh->prepare(qq{
145 select count(*),city from geo_biblioitems where city not in (select city_koha from geo_city) group by city order by count(city) desc
146 });
147 $sth->execute;
148
149 warn $sth->rows, " cities to geolocate";
150
151 use Geo::Coder::PlaceFinder;
152
153 my $geocoder = Geo::Coder::PlaceFinder->new( appid => 'eQWEGC58' );
154
155 while( my $row = $sth->fetchrow_hashref ) {
156         my $location  = $geocoder->geocode(location => $row->{city});
157         warn dump($location);
158         $sth_insert->execute(
159                 $row->{city}
160                 , $location->{city},
161                 , $location->{country}
162                 , $location->{county}
163                 , $location->{latitude}
164                 , $location->{longitude}
165                 , $location->{radius}
166                 , dump($location)
167         );
168 }
169
170 eval { $g_dbh->do(qq{ drop table geo_count }) };
171 $g_dbh->do(qq{
172
173 select
174         count(biblionumber)
175         ,point(lat,lng)
176         ,geo_city.city
177         ,geo_city.city_koha
178         ,country
179 into geo_count
180 from geo_biblioitems
181 join geo_city on city_koha = geo_biblioitems.city
182 where length(geo_city.city) > 1
183 group by geo_city.city, country, lat, lng, city_koha
184 order by count(biblionumber) desc
185
186 });