]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/extras/import/marc2sre.pl
Increase support for serials in Evergreen.
[working/Evergreen.git] / Open-ILS / src / extras / import / marc2sre.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 ($idfield, $count, $user, $password, $config, $marctype, @files, @trash_fields, $quiet) =
29         ('001', 1, 'admin', 'open-ils', '/openils/conf/opensrf_core.xml', 'USMARC');
30
31 GetOptions(
32         'idfield=i'     => \$idfield,
33         'startid=i'     => \$count,
34         'user=s'        => \$user,
35         'password=s'    => \$password,
36         'config=s'      => \$config,
37         'marctype=s'    => \$marctype,
38         'file=s'        => \@files,
39         'quiet'         => \$quiet,
40 );
41
42 @files = @ARGV if (!@files);
43
44 my @ses;
45 my @req;
46 my %processing_cache;
47
48 OpenSRF::System->bootstrap_client( config_file => $config );
49 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
50
51 $user = OpenILS::Application::AppUtils->check_user_session( login($user,$password) )->id;
52
53 select STDERR; $| = 1;
54 select STDOUT; $| = 1;
55
56 my $batch = new MARC::Batch ( $marctype, @files );
57 $batch->strict_off();
58 $batch->warnings_off();
59
60 my $starttime = time;
61 my $rec;
62 while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
63         next if ($rec == -1);
64         my $id = $count;
65         my $record_field = $rec->field($idfield);
66         my $record = $count;
67
68         # On some systems, the 001 actually points to the record ID
69         # We need to attach to the call number to handle holdings in different libraries
70         # but we can work out call numbers later in SQL by the record ID + call number text
71         if ($record_field) {
72                 $record = $record_field->data;
73                 $record =~ s/(\d+)/$1/;
74         }
75
76         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
77         $xml =~ s/^<\?xml.+\?\s*>//go;
78         $xml =~ s/>\s+</></go;
79         $xml =~ s/\p{Cc}//go;
80         $xml = OpenILS::Application::AppUtils->entityize($xml,'D');
81         $xml =~ s/[\x00-\x1f]//go;
82
83         my $bib = new Fieldmapper::serial::record_entry;
84         $bib->id($id);
85         $bib->record($record);
86         $bib->active('t');
87         $bib->deleted('f');
88         $bib->marc($xml);
89         $bib->creator($user);
90         $bib->create_date('now');
91         $bib->editor($user);
92         $bib->edit_date('now');
93         $bib->last_xact_id('IMPORT-'.$starttime);
94
95         print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
96
97         $count++;
98
99         if (!$quiet && !($count % 20)) {
100                 print STDERR "\r$count\t". $count / (time - $starttime);
101         }
102 }
103
104 sub login {        
105         my( $username, $password, $type ) = @_;
106
107         $type |= "staff"; 
108
109         my $seed = OpenILS::Application::AppUtils->simplereq(
110                 'open-ils.auth',
111                 'open-ils.auth.authenticate.init',
112                 $username
113         );
114
115         die("No auth seed. Couldn't talk to the auth server") unless $seed;
116
117         my $response = OpenILS::Application::AppUtils->simplereq(
118                 'open-ils.auth',
119                 'open-ils.auth.authenticate.complete',
120                 {       username => $username,
121                         password => md5_hex($seed . md5_hex($password)),
122                         type => $type });
123
124         die("No auth response returned on login.") unless $response;
125
126         my $authtime = $response->{payload}->{authtime};
127         my $authtoken = $response->{payload}->{authtoken};
128
129         die("Login failed for user $username!") unless $authtoken;
130
131         return $authtoken;
132 }       
133