cleanup code a bit
[koha-eprints] / tsv2eprints.pl
1 #!/usr/bin/perl -I /usr/share/eprints3/perl_lib
2 use warnings;
3 use strict;
4 use autodie;
5 use utf8;
6
7 # export single record to get structure:
8 # sudo -u eprints /usr/share/eprints3/bin/export snz archive XMLFiles 20 > /tmp/20.xml
9
10 # Import procedure:
11
12 # 3. import with:
13 # sudo -u eprints /usr/share/eprints3/bin/import --verbose --migration --enable-file-imports --update --enable-import-fields grf eprint XML /tmp/xml
14 #
15 # 4. re-run view generation
16 # sudo -u eprints /usr/share/eprints3/bin/generate_views grf --verbose
17
18 use Encode;
19 use Data::Dump qw(dump);
20 use Storable;
21 use LWP::Simple;
22
23 use EPrints;
24
25 my $ep = EPrints->new();
26 my $repo = $ep->repository( 'snz' );
27 #$repo->{config}->{enable_file_imports} = 1;
28 #$repo->{config}->{enable_web_imports} = 1;
29 my $dataset = $repo->dataset( 'eprint' );
30 my $list = $dataset->search;
31 my $count = $list->count;
32 warn "# found [$count] eprints\n";
33
34 warn ref( $list );
35
36 #warn "# ids = ",dump( $list->ids );
37
38 my $info = { count => 0 };
39 $list->map( sub {
40         my( $session, $dataset, $eprint, $info ) = @_;
41
42         my $biblionumber = $eprint->get_value('biblionumber');
43
44         $info->{biblionumber}->{$biblionumber}++;
45         $info->{count}++;
46
47 }, $info );
48 warn dump( $info );
49
50 =for update
51
52 #my $eprint = $dataset->dataobj( 21 );
53 #warn dump( $eprint->get_value('institution'), $institution );
54
55 warn dump( $eprint );
56
57 if ( $eprint->get_value( 'institution' ) ne $institution ) {
58         $eprint->set_value( 'institution' => $institution );
59         $eprint->save_revision();
60         $eprint->commit();
61 }
62
63 $repo->terminate();
64
65 =cut
66
67 my $files;
68
69 my $mkp_path = "/mnt/share/MKP/ELEKTRONIÄŒKI DOKUMENTI/EL.DOKUMENTI PO BIBLIOBROJU/";
70 my $koha_path = "/tmp/koha_ffzg";
71
72 open(my $fh, '-|:encoding(UTF-8)', 'find "' . $mkp_path . '" -iname "*.pdf"');
73 while(my $full_path = <$fh>) {
74         chomp $full_path;
75
76         my $file = $1 if $full_path =~ m{/([^/]+)\.pdf}i;
77
78         my $file_id;
79         if ( $file =~ m/(\d+)/ ) {
80                 $file_id = $1;
81         } else {
82                 $file_id = $file;
83         }
84
85         warn "# $file_id\t$full_path\n";
86         $files->{ $file_id } = $full_path;
87
88 }
89
90 warn "# got ", scalar keys %$files, " files\n";
91
92 store $files, "$koha_path.biblionumber.file";
93
94 my $stat;
95
96 open(my $tsv_fh,  '<:encoding(UTF-8)', "$koha_path.tsv");
97 open(my $marc_fh, '<', "$koha_path.marc");
98 open(my $import_fh, '>', "$koha_path.import.marc");
99
100 my $last_offset = 0;
101 my @cols;
102
103 while(<$tsv_fh>) {
104         chomp;
105         my $line = $_;
106         $line =~ s/[\n\r]+$//;
107
108         if ( ! @cols && $line =~ m/#(.+)/ ) {
109                 @cols = split(/\t/, $1);
110                 next;
111         }
112
113         my @v = split(/\t/, $line, $#cols + 1);
114         my %row;
115         @row{@cols} = @v;
116 #warn "## row = ",dump( \%row );
117
118         my $offset = $row{offset} // die "no offset";
119         my $biblionumber = $row{biblionumber} || die "no biblionumber";
120
121 #       warn "# ", join(' ', map { $row{$_} } qw(biblionumber title)), "\n";
122
123         if ( delete $files->{$biblionumber} ) {
124                 $stat->{file}++;
125
126                 if ( $info->{biblionumber}->{$biblionumber} ) {
127                         $stat->{existing}++;
128                         warn "EXISTING $biblionumber found in eprints\n";
129                 } else {
130
131                         $stat->{new}++;
132
133                         seek $marc_fh, $last_offset, 0;
134                         read $marc_fh, my $marc, $offset - $last_offset;
135                         print $import_fh $marc;
136                         warn "# NEW ", join(' ', map { $row{$_} } qw(biblionumber title)), "\n";
137 #                       warn "# NEW $biblionumber\n";
138
139                 }
140
141         } else {
142                 $stat->{missing}++;
143         }
144
145         $last_offset = $offset;
146
147 }
148
149 warn "# files left ", dump($files);
150
151 foreach my $biblionumber ( keys %$files ) {
152
153         if ( $info->{biblionumber}->{$biblionumber} ) {
154                 $stat->{existing}++;
155                 warn "EXISTING $biblionumber found in eprints\n";
156                 next;
157         }
158
159         if ( my $marc = get("https://koha.ffzg.hr/cgi-bin/koha/opac-export.pl?op=export&bib=$biblionumber&format=utf8") ) {
160                 print $import_fh $marc;
161                 warn "## marc $biblionumber from koha!";
162                 $stat->{koha}++;
163         } else {
164                 warn "ERROR: can't fetch $biblionumber from koha";
165         }
166 }
167
168 warn "# stat ", dump($stat);