skip records with tag 260
[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 warn "# drop geo_biblioitems";
63 eval { $g_dbh->do(qq{ drop table geo_biblioitems }) };
64
65 warn "# create geo_biblioitems";
66 $g_dbh->do(qq{
67 create table geo_biblioitems (
68         biblioitemnumber integer not null references biblioitems(biblioitemnumber),
69         biblionumber integer not null references biblio(biblionumber),
70         city text
71 )
72 });
73 my $sth_insert = $g_dbh->prepare(qq{
74         insert into geo_biblioitems values (?,?,?)
75 });
76
77 warn "# select bibiloitems";
78 my $sth = $g_dbh->prepare(qq{
79 SELECT
80         biblioitemnumber, biblionumber, isbn, issn, marcxml
81 from biblioitems
82 });
83 $sth->execute;
84
85 warn $sth->rows, " rows\n";
86
87 sub warn_dump {
88         warn dump @_,$/ if $ENV{DEBUG};
89 }
90
91 while ( my $row = $sth->fetchrow_hashref ) {
92         my $xml = XMLin( delete $row->{marcxml} );
93
94         warn_dump($row, $xml);
95
96         my @tag_260 = grep { $_->{tag} eq '260' } @{ $xml->{datafield} };
97
98         next unless @tag_260;
99
100         warn_dump @tag_260 if $ENV{DEBUG};
101
102         foreach my $sf ( @{ $tag_260[0]->{subfield} } ) {
103                 $row->{ 'tag_260_' . $sf->{code} } = $sf->{content};
104         }
105
106         $row->{city} = $row->{tag_260_a};
107         $row->{city} =~ s/['"]+//g;
108         $row->{city} =~ s/\s*\[etc.*\].*$//;
109         $row->{city} =~ s/\s*[:]\s*$//;
110
111         warn_dump $row;
112
113         print $row->{city}, $/;
114
115         $sth_insert->execute( $row->{biblioitemnumber}, $row->{biblionumber}, $row->{city} );
116 }