]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/marc2bre.pl
4ba4028c5458fe664c97285f8852acc157203f9e
[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 Error qw/:try/;
8 use OpenILS::Utils::Fieldmapper;
9 use Digest::MD5 qw/md5_hex/;
10 use OpenSRF::Utils::JSON;
11 use Data::Dumper;
12 use Unicode::Normalize;
13 use Encode;
14
15 use FileHandle;
16 use Time::HiRes qw/time/;
17 use Getopt::Long;
18 use MARC::Batch;
19 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
20 use MARC::Charset;
21 use DBI;
22
23 #MARC::Charset->ignore_errors(1);
24
25 my ($id_field, $recid, $user, $config, $idlfile, $marctype, $keyfile, $dontuse_file, $enc, $force_enc, @files, @trash_fields, $quiet) =
26         ('', 0, 1, '/openils/conf/opensrf_core.xml', '/openils/conf/fm_IDL.xml', 'USMARC');
27
28 my ($db_driver,$db_host,$db_name,$db_user,$db_pw) =
29         ('Pg','localhost','evergreen','postgres','postgres');
30
31 GetOptions(
32         'marctype=s'    => \$marctype,
33         'startid=i'     => \$recid,
34         'idfield=s'     => \$id_field,
35         'user=s'        => \$user,
36         'encoding=s'    => \$enc,
37         'hard_encoding' => \$force_enc,
38         'keyfile=s'     => \$keyfile,
39         'config=s'      => \$config,
40         'file=s'        => \@files,
41         'trash=s'       => \@trash_fields,
42         'xml_idl=s'     => \$idlfile,
43         'dontuse=s'     => \$dontuse_file,
44         "db_driver=s"           => \$db_driver,
45         "db_host=s"             => \$db_host,
46         "db_name=s"             => \$db_name,
47         "db_user=s"             => \$db_user,
48         "db_pw=s"               => \$db_pw,
49         'quiet'         => \$quiet
50 );
51
52 if ($enc) {
53         MARC::Charset->ignore_errors(1);
54         MARC::Charset->assume_encoding($enc);
55 }
56
57 if (uc($marctype) eq 'XML') {
58         'open'->use(':utf8');
59 } else {
60         bytes->use();
61 }
62
63 @files = @ARGV if (!@files);
64
65 my @ses;
66 my @req;
67 my %processing_cache;
68
69 my $dsn = "dbi:$db_driver:host=$db_host;dbname=$db_name";
70
71 if (!$recid) {
72     my $table = 'biblio_record_entry';
73     $table = 'biblio.record_entry' if ($db_driver eq 'Pg');
74
75         my $dbh = DBI->connect($dsn,$db_user,$db_pw);
76         my $sth = $dbh->prepare("SELECT MAX(id) + 1 FROM $table");
77
78         $sth->execute;
79         $sth->bind_col(1, \$recid);
80         $sth->fetch;
81         $sth->finish;
82         $recid++;
83         $dbh->disconnect;
84 }
85
86 my %source_map = (      
87         o  => 'OCLC',
88         i  => 'ISxN',    
89         l  => 'LCCN',
90         s  => 'System',  
91         g  => 'Gutenberg',  
92 );                              
93
94 Fieldmapper->import(IDL => $idlfile);
95
96 my %keymap;
97 if ($keyfile) {
98         open F, $keyfile or die "Couldn't open key file $keyfile";
99         while (<F>) {
100                 if ( /^(\d+)\|(\S+)/o ) {
101                         $keymap{$1} = $2;
102                 }
103         }
104         close(F);
105 }
106
107 my %dontuse_id;
108 if ($dontuse_file) {
109         open F, $dontuse_file or die "Couldn't open used-id file $dontuse_file";
110         while (<F>) {
111                 chomp;
112                 s/^\s*//;
113                 s/\s*$//;
114                 $dontuse_id{$_} = 1;
115         }
116         close(F);
117 }
118
119 select STDERR; $| = 1;
120 select STDOUT; $| = 1;
121
122 my $batch = new MARC::Batch ( $marctype, @files );
123 $batch->strict_off();
124 $batch->warnings_off();
125
126 my %used_ids;
127 my $starttime = time;
128 my $rec;
129 my $count = 0;
130 while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
131         next if ($rec == -1);
132         my $id;
133
134         $recid++;
135         while (exists $used_ids{$recid}) {
136                 $recid++;
137         }
138         $used_ids{$recid} = 1;
139
140         if ($id_field) {
141                 my $field = $rec->field($id_field);
142                 if ($field) {
143                         if ($field->is_control_field) {
144                                 $id = $field->data;
145                         } else {
146                                 $id = $field->subfield('a');
147                         }
148
149                         $id =~ s/\D+//gso;
150                 }
151         }
152
153         if (!$id) {
154                 $id = $recid;
155         }
156
157         if ($keyfile) {
158                 if (my $tcn = $keymap{$id}) {
159                         $rec->delete_field( $_ ) for ($rec->field($id_field));
160                         $rec->append_fields( MARC::Field->new( $id_field, '', '', 'a', $tcn ) );
161                 } else {
162                         $count++;
163                         next;
164                 }
165         }
166
167         my $tcn;
168         ($rec, $tcn) = preprocess($rec);
169
170     $tcn->add_subfields(c => $id);
171
172         $rec->delete_field( $_ ) for ($rec->field($id_field));
173         $rec->append_fields( $tcn );
174
175         if (!$rec) {
176                 next;
177         }
178
179         my $tcn_value = $rec->subfield('901' => 'a') || "SYS$id";
180         my $tcn_source = $rec->subfield('901' => 'b') || 'System';
181
182         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
183         $xml =~ s/^<\?xml.+\?\s*>//go;
184         $xml =~ s/>\s+</></go;
185         $xml =~ s/\p{Cc}//go;
186         $xml = entityize($xml);
187         $xml =~ s/[\x00-\x1f]//go;
188
189         my $bib = new Fieldmapper::biblio::record_entry;
190         $bib->id($id);
191         $bib->active('t');
192         $bib->deleted('f');
193         $bib->marc($xml);
194         $bib->creator($user);
195         $bib->create_date('now');
196         $bib->editor($user);
197         $bib->edit_date('now');
198         $bib->tcn_source($tcn_source);
199         $bib->tcn_value($tcn_value);
200         $bib->last_xact_id('IMPORT-'.$starttime);
201
202         print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
203         $dontuse_id{$tcn_value} = 1;
204
205         $count++;
206
207         if (!$quiet && !($count % 50)) {
208                 print STDERR "\r$count\t". $count / (time - $starttime);
209         }
210 }
211
212 sub preprocess {
213         my $rec = shift;
214
215         my ($id, $source, $value) = ('','','');
216
217         if (!$id) {
218                 my $f = $rec->field('001');
219                 $id = $f->data if ($f);
220         $id = '' if (exists $dontuse_id{$id});
221         }
222
223         if (!$id || exists $dontuse_id{$source.$id}) {
224                 my $f = $rec->field('000');
225                 $id = $f->data if ($f);
226                 $source = 'g' if ($f); # only PG seems to use this
227         }
228
229         if (!$id || exists $dontuse_id{$source.$id}) {
230                 my $f = $rec->field('020');
231                 $id = $f->subfield('a') if ($f);
232                 $source = 'i' if ($f);
233         }
234
235         if (!$id || exists $dontuse_id{$source.$id}) {
236                 my $f = $rec->field('022');
237                 $id = $f->subfield('a') if ($f);
238                 $source = 'i' if ($f);
239         }
240
241         if (!$id || exists $dontuse_id{$source.$id}) {
242                 my $f = $rec->field('010');
243                 $id = $f->subfield('a') if ($f);
244                 $source = 'l' if ($f);
245         }
246
247         $rec->delete_field($_) for ($rec->field('901', $id_field, @trash_fields));
248
249         if ($id) {
250                 $id =~ s/\s*$//o;
251                 $id =~ s/^\s*//o;
252                 $id =~ s/^(\S+).*$/$1/o;
253
254                 $id = $source.$id if ($source);
255
256                 ($source, $value) = $id =~ /^(.)(.+)$/o;
257                 if ($id =~ /^o(\d+)$/o) {
258                         $id = "ocm$1";
259                         $source = 'o';
260                 }
261         }
262
263         if ($id && exists $dontuse_id{$id}) {
264                 warn "\n!!! TCN $id is already in use.  Using the record ID ($recid) as a system-generated TCN.\n";
265                 $id = '';
266         }
267
268         if (!$id) {
269                 $source = 's';
270                 $id = 's'.$recid;
271         }
272
273         my $tcn = MARC::Field->new(
274                 '901' => ('', ''),
275                 a => $id,
276                 b => do { $source_map{$source} || 'System' },
277         );
278
279         return ($rec,$tcn);
280 }
281
282 sub entityize {
283         my $stuff = shift;
284         my $form = shift;
285
286         if ($form and $form eq 'D') {
287                 $stuff = NFD($stuff);
288         } else {
289                 $stuff = NFC($stuff);
290         }
291
292         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
293         return $stuff;
294 }
295