]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/extras/import/direct_ingest.pl
e46de4a2a405501bc179121945a59996a09c7783
[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 my $meth = 'open-ils.ingest.full.biblio.object.readonly';
54 $meth = 'open-ils.ingest.full.authority.object.readonly' if ($auth);
55
56 $meth = OpenILS::Application::Ingest->method_lookup( $meth );
57
58 my $count = 0;
59 my $starttime = time;
60 while (my $rec = <>) {
61         next unless ($rec);
62
63         my $bib = OpenSRF::Utils::JSON->JSON2perl($rec);
64         my $data;
65
66         try {
67                 ($data) = $meth->run( $bib => $max_cn => $max_uri );
68         } catch Error with {
69                 my $e = shift;
70                 warn "Couldn't process record: $e\n >>> $rec\n";
71         };
72
73         next unless $data;
74
75         postprocess( { bib => $bib, ingest_data => $data } );
76
77         if (!$quiet && !($count % 20)) {
78                 print NEWERR "\r$count\t". $count / (time - $starttime);
79         }
80
81         $count++;
82 }
83
84 sub postprocess {
85         my $data = shift;
86
87         my $bib = $data->{bib};
88         my $full_rec = $data->{ingest_data}->{full_rec};
89
90         my $field_entries = $data->{ingest_data}->{field_entries} unless ($auth);
91         my $fp = $data->{ingest_data}->{fingerprint} unless ($auth);
92         my $rd = $data->{ingest_data}->{descriptor} unless ($auth);
93         my $uri = $data->{ingest_data}->{uri} unless ($auth);
94
95         $bib->fingerprint( $fp->{fingerprint} ) unless ($auth);
96         $bib->quality( $fp->{quality} ) unless ($auth);
97
98         print( OpenSRF::Utils::JSON->perl2JSON($bib)."\n" );
99         unless ($auth) {
100                 print( OpenSRF::Utils::JSON->perl2JSON($rd)."\n" );
101                 print( OpenSRF::Utils::JSON->perl2JSON($_)."\n" ) for (@$field_entries);
102                 for my $u (@$uri) {
103                         print( OpenSRF::Utils::JSON->perl2JSON($u->{call_number})."\n" ) if $u->{call_number}->isnew;
104                         print( OpenSRF::Utils::JSON->perl2JSON($u->{uri})."\n" ) if $u->{uri}->isnew;
105
106                         my $umap = Fieldmapper::asset::uri_call_number_map->new;
107                         $umap->uri($u->{uri}->id);
108                         $umap->call_number($u->{call_number}->id);
109                         print( OpenSRF::Utils::JSON->perl2JSON($umap)."\n" );
110
111                         $max_cn = $u->{call_number}->id + 1 if $u->{call_number}->isnew;
112                         $max_uri = $u->{uri}->id + 1 if $u->{uri}->isnew;
113                 }
114         }
115
116         print( OpenSRF::Utils::JSON->perl2JSON($_)."\n" ) for (@$full_rec);
117 }
118