]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/marc2bre.pl
import tweaks
[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 ($utf8, $id_field, $count, $user, $password, $config, $keyfile,  @files, @trash_fields) =
30         (0, '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  => 'LCCN',
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 %seen;
81
82 my $starttime = time;
83 my $rec;
84 while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
85         next if ($rec == -1);
86         my $id;
87
88         if ($id_field) {
89                 my $field = $rec->field($id_field);
90                 if ($field) {
91                         if ($field->is_control_field) {
92                                 $id = $field->data;
93                         } else {
94                                 $id = $field->subfield('a');
95                         }
96                 }
97         }
98                 
99         if ($id && $id =~ /(\d+)/o) {
100                 $id = $1;
101         } else {
102                 $id = $count;
103         }
104
105         if ($keyfile) {
106                 if (my $tcn = $keymap{$id}) {
107                         $rec->delete_field( $_ ) for ($rec->field($id_field));
108                         $rec->append_fields( MARC::Field->new( $id_field, '', '', 'a', $tcn ) );
109                 } else {
110                         $count++;
111                         next;
112                 }
113         }
114
115         $rec = preprocess($rec);
116         $rec->delete_field( $_ ) for ($rec->field($id_field));
117
118         if (!$rec) {
119                 next;
120         }
121
122         my $tcn_value = $rec->subfield('901' => 'a');
123         my $tcn_source = $rec->subfield('901' => 'b');
124
125         next if ($seen{$tcn_value});
126         $seen{$tcn_value} = 1;
127
128         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
129         $xml =~ s/^<\?xml.+\?\s*>//go;
130         $xml =~ s/>\s+</></go;
131         $xml =~ s/\p{Cc}//go;
132         $xml = entityize($xml);
133
134         my $bib = new Fieldmapper::biblio::record_entry;
135         $bib->id($id);
136         $bib->active('t');
137         $bib->deleted('f');
138         $bib->marc($xml);
139         $bib->creator($user);
140         $bib->create_date('now');
141         $bib->editor($user);
142         $bib->edit_date('now');
143         $bib->tcn_source($tcn_source);
144         $bib->tcn_value($tcn_value);
145         $bib->last_xact_id('IMPORT-'.$starttime);
146
147         print JSON->perl2JSON($bib)."\n";
148
149         $count++;
150
151         if (!($count % 20)) {
152                 print STDERR "\r$count\t". $count / (time - $starttime);
153         }
154 }
155
156 sub preprocess {
157         my $rec = shift;
158
159         my ($id, $source, $value);
160
161         if (!$id) {
162                 my $f = $rec->field($id_field);
163                 $id = $f->subfield('a') if ($f);
164         }
165
166         if (!$id) {
167                 my $f = $rec->field('001');
168                 $id = $f->data if ($f);
169         }
170
171         if (!$id) {
172                 my $f = $rec->field('000');
173                 $id = $f->data if ($f);
174                 $source = 'g'; # only PG seems to use this
175         }
176
177         if (!$id) {
178                 my $f = $rec->field('020');
179                 $id = $f->subfield('a') if ($f);
180                 $source = 'i';
181         }
182
183         if (!$id) {
184                 my $f = $rec->field('022');
185                 $id = $f->subfield('a') if ($f);
186                 $source = 'i';
187         }
188
189         if (!$id) {
190                 my $f = $rec->field('010');
191                 $id = $f->subfield('a') if ($f);
192                 $source = 'l';
193         }
194
195         if (!$id) {
196                 $count++;
197                 warn "\n !!! Record with no TCN : $count\n".$rec->as_formatted;
198                 return undef;
199         }
200
201         $rec->delete_field($_) for ($rec->field($id_field, @trash_fields));
202
203         $id =~ s/\s*$//o;
204         $id =~ s/^\s*//o;
205         $id =~ s/^(\S+).*$/$1/o;
206
207         $id = $source.$id if ($source);
208
209         ($source, $value) = $id =~ /^(.)(.+)$/o;
210         if ($id =~ /^o(\d+)$/o) {
211                 $id = "ocm$1";
212                 $source = 'o';
213         }
214
215         my $tcn = MARC::Field->new(
216                 '901' => ('', ''),
217                 a => $id,
218                 b => do { $source_map{$source} || 'System' },
219         );
220
221         $rec->append_fields($tcn);
222
223         return $rec;
224 }
225
226 sub login {        
227         my( $username, $password, $type ) = @_;
228
229         $type |= "staff"; 
230
231         my $seed = OpenILS::Application::AppUtils->simplereq(
232                 'open-ils.auth',
233                 'open-ils.auth.authenticate.init',
234                 $username
235         );
236
237         die("No auth seed. Couldn't talk to the auth server") unless $seed;
238
239         my $response = OpenILS::Application::AppUtils->simplereq(
240                 'open-ils.auth',
241                 'open-ils.auth.authenticate.complete',
242                 {       username => $username,
243                         password => md5_hex($seed . md5_hex($password)),
244                         type => $type });
245
246         die("No auth response returned on login.") unless $response;
247
248         my $authtime = $response->{payload}->{authtime};
249         my $authtoken = $response->{payload}->{authtoken};
250
251         die("Login failed for user $username!") unless $authtoken;
252
253         return $authtoken;
254 }       
255
256 sub entityize {
257         my $stuff = shift;
258         my $form = shift;
259
260         if ($form and $form eq 'D') {
261                 $stuff = NFD($stuff);
262         } else {
263                 $stuff = NFC($stuff);
264         }
265
266         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
267         return $stuff;
268 }
269