]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/direct_ingest.pl
6fcae1ad1b8f853ea146560e5fae90013b7e6d9a
[Evergreen.git] / Open-ILS / src / extras / import / direct_ingest.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use lib '/openils/lib/perl5/';
6
7 use OpenSRF::System;
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::AppSession;
10 use OpenSRF::Application;
11 use OpenSRF::MultiSession;
12 use OpenSRF::Utils::SettingsClient;
13 use OpenILS::Application::Ingest;
14 use OpenILS::Application::AppUtils;
15 use OpenILS::Utils::Fieldmapper;
16 use Digest::MD5 qw/md5_hex/;
17 use JSON;
18 use Data::Dumper;
19 use FileHandle;
20
21 use Time::HiRes qw/time/;
22 use Getopt::Long;
23 use MARC::Batch;
24 use MARC::File::XML;
25 use MARC::Charset;
26
27 MARC::Charset->ignore_errors(1);
28
29 my ($auth, $workers, $config, $prefix) =
30         (0, 1, '/openils/conf/bootstrap.conf', 'marc-out-');
31
32 GetOptions(
33         'threads=i'     => \$workers,
34         'config=s'      => \$config,
35         'prefix=s'      => \$prefix,
36         'authority'     => \$auth,
37 );
38
39 my @ses;
40
41 open NEWERR,     ">&STDERR";
42
43 select NEWERR; $| = 1;
44 select STDERR; $| = 1;
45 select STDOUT; $| = 1;
46
47 for (1 .. $workers) {
48         my ($r,$w);
49         pipe($r,$w);
50         if (fork) {
51                 push @ses, $w;
52         } else {
53                 $0 = "Local Ingest Worker $_";
54                 if ($workers == 1) {
55                         worker($r, -1);
56                 } else {
57                         worker($r, $_);
58                 }
59                 exit;
60         }
61 }
62 $0 = "Local Ingest Master";
63
64 sub worker {
65         my $pipe = shift;
66         my $file = shift;
67
68         OpenSRF::System->bootstrap_client( config_file => $config );
69         Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
70
71         sleep 1;
72
73         OpenILS::Application::Ingest->use;
74
75         my $fname = "${prefix}$file";
76         if ($file == -1) {
77                 $fname = '&STDOUT';
78         }
79
80         my $f = new FileHandle(">$fname");
81
82         my $meth = 'open-ils.ingest.full.biblio.object.readonly';
83         $meth = 'open-ils.ingest.full.authority.object.readonly' if ($auth);
84
85         $meth = OpenILS::Application::Ingest->method_lookup( $meth );
86
87         while (my $rec = <$pipe>) {
88
89                 my $bib = JSON->JSON2perl($rec);
90                 my $data;
91
92                 try {
93                         ($data) = $meth->run( $bib );
94                 } catch Error with {
95                         my $e = shift;
96                         warn "Couldn't process record: $e\n >>> $rec\n";
97                 };
98
99                 next unless $data;
100
101                 postprocess(
102                         { bib           => $bib,
103                         worm_data       => $data,
104                         },
105                         $f
106                 );
107         }
108 }
109
110 my $count = 0;
111 my $starttime = time;
112 while ( my $rec = <> ) {
113         next unless ($rec);
114         my $session_index = $count % $workers;
115
116         $ses[$session_index]->printflush( $rec );
117
118         if (!($count % 20)) {
119                 print NEWERR "\r$count\t". $count / (time - $starttime);
120         }
121
122         $count++;
123 }
124
125 $ses[$_]->close for (@ses);
126 sub postprocess {
127         my $data = shift;
128         my $f = shift;
129
130         my $bib = $data->{bib};
131         my $field_entries = $data->{worm_data}->{field_entries} unless ($auth);
132         my $full_rec = $data->{worm_data}->{full_rec};
133         my $fp = $data->{worm_data}->{fingerprint} unless ($auth);
134         my $rd = $data->{worm_data}->{descriptor} unless ($auth);
135
136         $bib->fingerprint( $fp->{fingerprint} ) unless ($auth);
137         $bib->quality( $fp->{quality} ) unless ($auth);
138
139         $f->printflush( JSON->perl2JSON($bib)."\n" );
140         unless ($auth) {
141                 $f->printflush( JSON->perl2JSON($rd)."\n" );
142                 $f->printflush( JSON->perl2JSON($_)."\n" ) for (@$field_entries);
143         }
144         $f->printflush( JSON->perl2JSON($_)."\n" ) for (@$full_rec);
145 }
146