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