]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/extras/import/direct_ingest.pl
bbec8bf817cb70010c9c5afd0e8163cad6fe78a2
[working/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 OpenSRF::Utils::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 ($max_uri, $max_cn, $auth, $config, $quiet) =
30         (0, 0, 0, '/openils/conf/opensrf_core.xml');
31
32 GetOptions(
33         'config=s'      => \$config,
34         'authority'     => \$auth,
35         'quiet'         => \$quiet,
36         'max_uri=i'     => \$max_uri,   
37         'max_cn=i'      => \$max_cn,    
38 );
39
40 my @ses;
41
42 open NEWERR,     ">&STDERR";
43
44 select NEWERR; $| = 1;
45 select STDERR; $| = 1;
46 select STDOUT; $| = 1;
47
48 OpenSRF::System->bootstrap_client( config_file => $config );
49 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
50
51 OpenILS::Application::Ingest->use;
52
53 if (!$auth) {
54     die "We have no more use for biblio ingest ... just insert the bre objects and you're done!\n";
55 }
56
57 my $meth = 'open-ils.ingest.full.biblio.object.readonly';
58 $meth = 'open-ils.ingest.full.authority.object.readonly' if ($auth);
59
60 $meth = OpenILS::Application::Ingest->method_lookup( $meth );
61
62 my $count = 0;
63 my $starttime = time;
64 while (my $rec = <>) {
65         next unless ($rec);
66
67         my $bib = OpenSRF::Utils::JSON->JSON2perl($rec);
68         my $data;
69
70         try {
71                 ($data) = $meth->run( $bib => $max_cn => $max_uri );
72         } catch Error with {
73                 my $e = shift;
74                 warn "Couldn't process record: $e\n >>> $rec\n";
75         };
76
77         next unless $data;
78
79         postprocess( { bib => $bib, ingest_data => $data } );
80
81         if (!$quiet && !($count % 20)) {
82                 print NEWERR "\r$count\t". $count / (time - $starttime);
83         }
84
85         $count++;
86 }
87
88 sub postprocess {
89         my $data = shift;
90
91         my ($field_entries, $fp, $rd, $uri);
92
93         my $bib = $data->{bib};
94         my $full_rec = $data->{ingest_data}->{full_rec};
95
96         if (!$auth) {
97                 $field_entries = $data->{ingest_data}->{field_entries};
98                 $fp = $data->{ingest_data}->{fingerprint};
99                 $rd = $data->{ingest_data}->{descriptor};
100                 $uri = $data->{ingest_data}->{uri};
101
102                 $bib->fingerprint( $fp->{fingerprint} );
103                 $bib->quality( $fp->{quality} );
104         }
105
106         print( OpenSRF::Utils::JSON->perl2JSON($bib)."\n" );
107         if (!$auth) {
108                 print( OpenSRF::Utils::JSON->perl2JSON($rd)."\n" );
109                 print( OpenSRF::Utils::JSON->perl2JSON($_)."\n" ) for (@$field_entries);
110                 for my $u (@$uri) {
111                         print( OpenSRF::Utils::JSON->perl2JSON($u->{call_number})."\n" ) if $u->{call_number}->isnew;
112                         print( OpenSRF::Utils::JSON->perl2JSON($u->{uri})."\n" ) if $u->{uri}->isnew;
113
114                         my $umap = Fieldmapper::asset::uri_call_number_map->new;
115                         $umap->uri($u->{uri}->id);
116                         $umap->call_number($u->{call_number}->id);
117                         print( OpenSRF::Utils::JSON->perl2JSON($umap)."\n" );
118
119                         $max_cn = $u->{call_number}->id + 1 if $u->{call_number}->isnew;
120                         $max_uri = $u->{uri}->id + 1 if $u->{uri}->isnew;
121                 }
122         }
123
124         print( OpenSRF::Utils::JSON->perl2JSON($_)."\n" ) for (@$full_rec);
125 }
126