Inspired by Don Hamilton, added a --quiet|-q switch to the import-related scripts
[Evergreen.git] / Open-ILS / src / extras / import / marc2are.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::Application;
9 use OpenSRF::EX qw/:try/;
10 use OpenSRF::AppSession;
11 use OpenSRF::MultiSession;
12 use OpenSRF::Utils::SettingsClient;
13 use OpenILS::Application::AppUtils;
14 use OpenILS::Utils::Fieldmapper;
15 use Digest::MD5 qw/md5_hex/;
16 use OpenSRF::Utils::JSON;
17 use Data::Dumper;
18 use Unicode::Normalize;
19
20 use Time::HiRes qw/time/;
21 use Getopt::Long;
22 use MARC::Batch;
23 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
24 use MARC::Charset;
25
26 MARC::Charset->ignore_errors(1);
27
28 my ($utf8, $id_field, $count, $user, $password, $config, $marctype, $keyfile,  @files, @trash_fields, $quiet) =
29         (0, '998', 1, 'admin', 'open-ils', '/openils/conf/opensrf_core.xml', 'USMARC');
30
31 GetOptions(
32         'startid=i'     => \$count,
33         'user=s'        => \$user,
34         'marctype=s'    => \$marctype,
35         'password=s'    => \$password,
36         'config=s'      => \$config,
37         'file=s'        => \@files,
38         'quiet'         => \$quiet,
39 );
40
41 @files = @ARGV if (!@files);
42
43 my @ses;
44 my @req;
45 my %processing_cache;
46
47 OpenSRF::System->bootstrap_client( config_file => $config );
48 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
49
50 $user = OpenILS::Application::AppUtils->check_user_session( login($user,$password) )->id;
51
52 select STDERR; $| = 1;
53 select STDOUT; $| = 1;
54
55 my $batch = new MARC::Batch ( $marctype, @files );
56 $batch->strict_off();
57 $batch->warnings_off();
58
59 my $starttime = time;
60 my $rec;
61 while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
62         next if ($rec == -1);
63         my $id = $count;
64
65         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
66         $xml =~ s/^<\?xml.+\?\s*>//go;
67         $xml =~ s/>\s+</></go;
68         $xml =~ s/\p{Cc}//go;
69         $xml = entityize($xml);
70
71         my $bib = new Fieldmapper::authority::record_entry;
72         $bib->id($id);
73         $bib->active('t');
74         $bib->deleted('f');
75         $bib->marc($xml);
76         $bib->creator($user);
77         $bib->create_date('now');
78         $bib->editor($user);
79         $bib->edit_date('now');
80         $bib->arn_source('LEGACY');
81         $bib->arn_value($count);
82         $bib->last_xact_id('IMPORT-'.$starttime);
83
84         print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
85
86         $count++;
87
88         if (!$quiet && !($count % 20)) {
89                 print STDERR "\r$count\t". $count / (time - $starttime);
90         }
91 }
92
93 sub login {        
94         my( $username, $password, $type ) = @_;
95
96         $type |= "staff"; 
97
98         my $seed = OpenILS::Application::AppUtils->simplereq(
99                 'open-ils.auth',
100                 'open-ils.auth.authenticate.init',
101                 $username
102         );
103
104         die("No auth seed. Couldn't talk to the auth server") unless $seed;
105
106         my $response = OpenILS::Application::AppUtils->simplereq(
107                 'open-ils.auth',
108                 'open-ils.auth.authenticate.complete',
109                 {       username => $username,
110                         password => md5_hex($seed . md5_hex($password)),
111                         type => $type });
112
113         die("No auth response returned on login.") unless $response;
114
115         my $authtime = $response->{payload}->{authtime};
116         my $authtoken = $response->{payload}->{authtoken};
117
118         die("Login failed for user $username!") unless $authtoken;
119
120         return $authtoken;
121 }       
122
123 sub entityize {
124         my $stuff = shift;
125         my $form = shift;
126
127         if ($form and $form eq 'D') {
128                 $stuff = NFD($stuff);
129         } else {
130                 $stuff = NFC($stuff);
131         }
132
133         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
134         return $stuff;
135 }
136