store column names in first line prefixed by #
[koha-eprints] / mkp-marc.pl
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5
6 use autodie;
7 use DBI;
8 use Data::Dump qw/dump/;
9
10 my $limit = $ENV{LIMIT} || 50;
11
12 my $dsn = 'DBI:mysql:dbname=koha_ffzg';
13 my $sql = qq{
14 select
15         biblionumber,itemnumber,title,
16         marc
17 from items
18         join biblioitems using (biblionumber)
19         join biblio using (biblionumber)
20 where location = 'MKP'
21         or homebranch = 'MKP'
22 limit $limit
23 };
24
25 my $path = "/dev/shm/$1" if $dsn =~ m/=(.+)/;
26
27 open(my $marc_fh, '>', "$path.marc");
28 open(my $tsv_fh,  '>', "$path.tsv");
29
30 warn "# dsn = $dsn";
31 my $dbh = DBI->connect( $dsn, '', '', { RaiseError => 1 } ) || die $DBI::errstr;
32 #$dbh->do( qq{ set client_encoding='utf-8' } );
33
34 warn "# sql $sql";
35 my $sth = $dbh->prepare( $sql );
36 $sth->execute();
37
38 warn "# ", $sth->rows,"\n";
39 warn "# NAME = ",dump( $sth->{NAME} );
40 print $tsv_fh "#", join("\t", @{ $sth->{NAME} }), "\n";
41
42 my $i = 1;
43
44 while ( my $row = $sth->fetchrow_hashref ) {
45
46 #       warn "# row = ",dump( $row );
47
48         my $rec = tell $marc_fh;
49         foreach ( keys %$row ) {
50                 my $d = $row->{$_};
51
52                 if ( m/marc/ ) {
53                         print $marc_fh $row->{marc};
54                         next;
55                 }
56
57                 next unless defined $d;
58
59 #               $d =~ s{</?\w+>}{ }gs;
60 #               $d =~ s/\s+/ /gs;
61
62                 $rec .= "\t$d";
63
64         }
65
66         print $tsv_fh "$rec\n";
67 }
68
69 system "ls -al $path*";
70 system "rsync -v $path* 10.60.0.82:/tmp/";