254f51d03f5f4914e9966ddb4aa461c4a7b89e23
[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 =for eprints-api
24
25 use EPrints;
26 my $institution = 'Grafički fakultet';
27
28 my $ep = EPrints->new();
29 my $repo = $ep->repository( 'grf' );
30 $repo->{config}->{enable_file_imports} = 1;
31 $repo->{config}->{enable_web_imports} = 1;
32
33 my $dataset = $repo->dataset( 'eprint' );
34 my $list = $dataset->search;
35 my $count = $list->count;
36 warn "# found [$count] eprints\n";
37
38 my $eprint = $dataset->dataobj( 21 );
39 warn dump( $eprint->get_value('institution'), $institution );
40
41 warn dump( $eprint );
42
43 if ( $eprint->get_value( 'institution' ) ne $institution ) {
44         $eprint->set_value( 'institution' => $institution );
45         $eprint->save_revision();
46         $eprint->commit();
47 }
48
49 $repo->terminate();
50
51 =cut
52
53 our $eprintid = 1;
54
55 my $files;
56
57 my $mkp_path = "/mnt/share/MKP/ELEKTRONIČKI DOKUMENTI/EL.DOKUMENTI PO BIBLIOBROJU/";
58 my $koha_path = "/tmp/koha_ffzg";
59
60 open(my $fh, '-|:encoding(UTF-8)', 'find "' . $mkp_path . '" -iname "*.pdf"');
61 while(my $full_path = <$fh>) {
62         chomp $full_path;
63
64         my $file = $1 if $full_path =~ m{/([^/]+)\.pdf}i;
65
66         my $file_id;
67         if ( $file =~ m/(\d+)/ ) {
68                 $file_id = $1;
69         } else {
70                 $file_id = $file;
71         }
72
73         warn "# $file_id\t$full_path\n";
74         $files->{ $file_id } = $full_path;
75
76 }
77
78 warn "# got ", scalar keys %$files, " files\n";
79
80 store $files, "$koha_path.biblionumber.file";
81
82 my $stat;
83
84 open(my $tsv_fh,  '<:encoding(UTF-8)', "$koha_path.tsv");
85 open(my $marc_fh, '<', "$koha_path.marc");
86 open(my $import_fh, '>', "$koha_path.import.marc");
87
88 my $last_offset = 0;
89 my @cols;
90
91 while(<$tsv_fh>) {
92         my $line = $_;
93         $line =~ s/[\n\r]+$//;
94
95         if ( ! @cols && $line =~ m/#(.+)/ ) {
96                 @cols = split(/\t/, $1);
97                 next;
98         }
99
100         my @v = split(/\t/, $_, $#cols + 1);
101         my %row;
102         @row{@cols} = @v;
103 warn "## row = ",dump( \%row );
104
105         my $offset = $row{offset} // die "no offset";
106         my $biblionumber = $row{biblionumber} || die "no biblionumber";
107
108         warn "# ", join(' ', map { $row{$_} } qw(offset biblionumber title)), "\n";
109
110         exit if $ENV{LAST} && $eprintid >= $ENV{LAST};
111
112         if ( delete $files->{$biblionumber} ) {
113                 $stat->{file}++;
114
115                 seek $marc_fh, $last_offset, 0;
116                 read $marc_fh, my $marc, $offset - $last_offset;
117                 print $import_fh $marc;
118                 warn "# marc $biblionumber\n";
119
120         } else {
121                 $stat->{missing}++;
122         }
123
124         $last_offset = $offset;
125
126 }
127
128 warn "# files left ", dump($files);
129
130 foreach my $biblionumber ( keys %$files ) {
131         if ( my $marc = get("https://koha.ffzg.hr/cgi-bin/koha/opac-export.pl?op=export&bib=$biblionumber&format=utf8") ) {
132                 print $import_fh $marc;
133                 warn "## marc $biblionumber from koha!";
134         } else {
135                 warn "ERROR: can't fetch $biblionumber from koha";
136         }
137 }
138
139 warn "# stat ", dump($stat);