]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/marc2bre.pl
more import pipeline improvement
[Evergreen.git] / Open-ILS / src / extras / import / marc2bre.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 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;
24 use MARC::Charset;
25 use UNIVERSAL::require;
26
27 MARC::Charset->ignore_errors(1);
28
29 my ($id_field, $count, $user, $password, $config, $keyfile,  @files, @trash_fields) =
30         ('998', 1, 'admin', 'open-ils', '/openils/conf/bootstrap.conf');
31
32 GetOptions(
33         'startid=i'     => \$count,
34         'idfield=s'     => \$id_field,
35         'user=s'        => \$user,
36         'password=s'    => \$password,
37         'keyfile=s'     => \$keyfile,
38         'config=s'      => \$config,
39         'file=s'        => \@files,
40         'trash=s'       => \@trash_fields,
41 );
42
43 @files = @ARGV if (!@files);
44
45 my @ses;
46 my @req;
47 my %processing_cache;
48
49 my %source_map = (      
50         o  => 'OCLC',
51         i  => 'ISxN',    
52         l  => 'Local',
53         s  => 'System',  
54         g  => 'Gutenberg',  
55 );                              
56
57
58 OpenSRF::System->bootstrap_client( config_file => $config );
59 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
60
61 $user = OpenILS::Application::AppUtils->check_user_session( login($user,$password) )->id;
62
63 my %keymap;
64 if ($keyfile) {
65         open F, $keyfile or die "Couldn't open key file $keyfile";
66         while (<F>) {
67                 if ( /^(\d+)\|(\S+)/o ) {
68                         $keymap{$1} = $2;
69                 }
70         }
71 }
72
73 select STDERR; $| = 1;
74 select STDOUT; $| = 1;
75
76 my $batch = new MARC::Batch ( 'USMARC', @files );
77 $batch->strict_off();
78 $batch->warnings_off();
79
80 my $starttime = time;
81 while ( my $rec = $batch->next ) {
82
83         my $id;
84         my $field = $rec->field($id_field);
85
86         if ($field) {
87                 if ($field->is_control_field) {
88                         $id = $field->data;
89                 } else {
90                         $id = $field->subfield('a');
91                 }
92         } else {
93                 $id = $count;
94         }
95                 
96         if ($id =~ /(\d+)/o) {
97                 $id = $1;
98         }
99
100         if ($keyfile) {
101                 if (my $tcn = $keymap{$id}) {
102                         $rec->delete_field( $_ ) for ($rec->field($id_field));
103                         $rec->append_fields( MARC::Field->new( $id_field, '', '', 'a', $tcn ) );
104                 } else {
105                         $count++;
106                         next;
107                 }
108         }
109
110         $rec = preprocess($rec);
111
112         if (!$rec) {
113                 next;
114         }
115
116         my $tcn_value = $rec->subfield($id_field => 'a');
117         my $tcn_source = $rec->subfield($id_field => 'b');
118
119         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
120         $xml =~ s/^<\?xml.+\?\s*>//go;
121         $xml =~ s/>\s+</></go;
122         $xml =~ s/\p{Cc}//go;
123         $xml = entityize($xml);
124
125         my $bib = new Fieldmapper::biblio::record_entry;
126         $bib->id($id);
127         $bib->active('t');
128         $bib->deleted('f');
129         $bib->marc($xml);
130         $bib->creator($user);
131         $bib->create_date('now');
132         $bib->editor($user);
133         $bib->edit_date('now');
134         $bib->tcn_source($tcn_source);
135         $bib->tcn_value($tcn_value);
136         $bib->last_xact_id('IMPORT-'.$starttime);
137
138         print JSON->perl2JSON($bib)."\n";
139
140         $count++;
141
142         if (!($count % 20)) {
143                 print STDERR "\r$count\t". $count / (time - $starttime);
144         }
145 }
146
147 sub preprocess {
148         my $rec = shift;
149
150         my ($id, $source, $value);
151
152         if (!$id) {
153                 my $f = $rec->field('001');
154                 $id = $f->data if ($f);
155         }
156
157         if (!$id) {
158                 my $f = $rec->field('000');
159                 $id = 'g'.$f->data if ($f);
160                 $source = 'g';
161         }
162
163         if (!$id) {
164                 my $f = $rec->field('020');
165                 $id = $f->subfield('a') if ($f);
166                 $source = 'i';
167         }
168
169         if (!$id) {
170                 my $f = $rec->field('022');
171                 $id = $f->subfield('a') if ($f);
172                 $source = 'i';
173         }
174
175         if (!$id) {
176                 my $f = $rec->field('010');
177                 $id = $f->subfield('a') if ($f);
178                 $source = 'l';
179         }
180
181         if (!$id) {
182                 my $f = $rec->field($id_field);
183                 $id = $f->subfield('a') if ($f);
184                 $source = 's';
185         }
186
187         if (!$id) {
188                 $count++;
189                 warn "\n !!! Record with no TCN : $count\n".$rec->as_formatted;
190                 return undef;
191         }
192
193         $rec->delete_field($_) for ($rec->field($id_field, @trash_fields));
194
195         $id =~ s/\s*$//o;
196         $id =~ s/^\s*//o;
197         $id =~ s/(\S+)$/$1/o;
198
199         $id = $source.$id if ($source);
200
201         ($source, $value) = $id =~ /^(.)(.+)$/o;
202         if ($id =~ /^o(\d+)$/o) {
203                 $id = "ocm$1";
204                 $source = 'o';
205         }
206
207         my $tcn = MARC::Field->new(
208                 $id_field,
209                 '', '',
210                 'a', $id,
211                 'b', do { $source_map{$source} || 'System' },
212         );
213
214         $rec->append_fields($tcn);
215
216         return $rec;
217 }
218
219 sub login {        
220         my( $username, $password, $type ) = @_;
221
222         $type |= "staff"; 
223
224         my $seed = OpenILS::Application::AppUtils->simplereq(
225                 'open-ils.auth',
226                 'open-ils.auth.authenticate.init',
227                 $username
228         );
229
230         die("No auth seed. Couldn't talk to the auth server") unless $seed;
231
232         my $response = OpenILS::Application::AppUtils->simplereq(
233                 'open-ils.auth',
234                 'open-ils.auth.authenticate.complete',
235                 {       username => $username,
236                         password => md5_hex($seed . md5_hex($password)),
237                         type => $type });
238
239         die("No auth response returned on login.") unless $response;
240
241         my $authtime = $response->{payload}->{authtime};
242         my $authtoken = $response->{payload}->{authtoken};
243
244         die("Login failed for user $username!") unless $authtoken;
245
246         return $authtoken;
247 }       
248
249 sub entityize {
250         my $stuff = shift;
251         my $form = shift;
252
253         if ($form and $form eq 'D') {
254                 $stuff = NFD($stuff);
255         } else {
256                 $stuff = NFC($stuff);
257         }
258
259         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
260         return $stuff;
261 }
262