]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/extras/import/marc2are.pl.in
install command-line MARC import tools in @prefix@/bin
[working/Evergreen.git] / Open-ILS / src / extras / import / marc2are.pl.in
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use OpenSRF::System;
6 use OpenSRF::Application;
7 use OpenSRF::EX qw/:try/;
8 use OpenSRF::AppSession;
9 use OpenSRF::MultiSession;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenILS::Application::AppUtils;
12 use OpenILS::Utils::Fieldmapper;
13 use Digest::MD5 qw/md5_hex/;
14 use OpenSRF::Utils::JSON;
15 use Data::Dumper;
16 use Unicode::Normalize;
17
18 use Time::HiRes qw/time/;
19 use Getopt::Long;
20 use MARC::Batch;
21 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
22 use MARC::Charset;
23
24 MARC::Charset->ignore_errors(1);
25
26 my ($count, $user, $password, $config, $marctype, $keyfile, @files, $quiet) =
27         (1, 'admin', 'open-ils', '@sysconfdir@/opensrf_core.xml', 'USMARC');
28
29 GetOptions(
30         'startid=i'     => \$count,
31         'user=s'        => \$user,
32         'marctype=s'    => \$marctype,
33         'password=s'    => \$password,
34         'config=s'      => \$config,
35         'file=s'        => \@files,
36         'quiet'         => \$quiet,
37 );
38
39 @files = @ARGV if (!@files);
40
41 my @ses;
42 my @req;
43 my %processing_cache;
44
45 OpenSRF::System->bootstrap_client( config_file => $config );
46 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
47
48 $user = OpenILS::Application::AppUtils->check_user_session( login($user,$password) )->id;
49
50 select STDERR; $| = 1;
51 select STDOUT; $| = 1;
52
53 my $batch = new MARC::Batch ( $marctype, @files );
54 $batch->strict_off();
55 $batch->warnings_off();
56
57 my $starttime = time;
58 my $rec;
59 while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
60         next if ($rec == -1);
61         my $id = $count;
62
63         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
64         $xml =~ s/^<\?xml.+\?\s*>//go;
65         $xml =~ s/>\s+</></go;
66         $xml =~ s/\p{Cc}//go;
67         $xml = OpenILS::Application::AppUtils->entityize($xml);
68         $xml =~ s/[\x00-\x1f]//go;
69
70         my $bib = new Fieldmapper::authority::record_entry;
71         $bib->id($id);
72         $bib->active('t');
73         $bib->deleted('f');
74         $bib->marc($xml);
75         $bib->creator($user);
76         $bib->create_date('now');
77         $bib->editor($user);
78         $bib->edit_date('now');
79         $bib->last_xact_id('IMPORT-'.$starttime);
80
81         print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
82
83         $count++;
84
85         if (!$quiet && !($count % 20)) {
86                 print STDERR "\r$count\t". $count / (time - $starttime);
87         }
88 }
89
90 sub login {        
91         my( $username, $password, $type ) = @_;
92
93         $type |= "staff"; 
94
95         my $seed = OpenILS::Application::AppUtils->simplereq(
96                 'open-ils.auth',
97                 'open-ils.auth.authenticate.init',
98                 $username
99         );
100
101         die("No auth seed. Couldn't talk to the auth server") unless $seed;
102
103         my $response = OpenILS::Application::AppUtils->simplereq(
104                 'open-ils.auth',
105                 'open-ils.auth.authenticate.complete',
106                 {       username => $username,
107                         password => md5_hex($seed . md5_hex($password)),
108                         type => $type });
109
110         die("No auth response returned on login.") unless $response;
111
112         my $authtime = $response->{payload}->{authtime};
113         my $authtoken = $response->{payload}->{authtoken};
114
115         die("Login failed for user $username!") unless $authtoken;
116
117         return $authtoken;
118 }       
119