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