]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/extras/import/marc2bre.pl
Enable custom specified ID to be retained in preprocess subroutine
[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         }
153
154         if (!$id) {
155                 $id = $recid;
156         }
157
158         if ($keyfile) {
159                 if (my $tcn = $keymap{$id}) {
160                         $rec->delete_field( $_ ) for ($rec->field($id_field));
161                         $rec->append_fields( MARC::Field->new( $id_field, '', '', $id_subfield, $tcn ) );
162                 } else {
163                         $count++;
164                         next;
165                 }
166         }
167
168         my $tcn;
169         ($rec, $tcn) = preprocess($rec, $id);
170
171     $tcn->add_subfields(c => $id);
172
173         $rec->delete_field( $_ ) for ($rec->field($id_field));
174         $rec->append_fields( $tcn );
175
176         if (!$rec) {
177                 next;
178         }
179
180         my $tcn_value = $rec->subfield('901' => 'a') || "SYS$id";
181         my $tcn_source = $rec->subfield('901' => 'b') || 'System';
182
183         (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
184         $xml =~ s/^<\?xml.+\?\s*>//go;
185         $xml =~ s/>\s+</></go;
186         $xml =~ s/\p{Cc}//go;
187         $xml = entityize($xml);
188         $xml =~ s/[\x00-\x1f]//go;
189
190         my $bib = new Fieldmapper::biblio::record_entry;
191         $bib->id($id);
192         $bib->active('t');
193         $bib->deleted('f');
194         $bib->marc($xml);
195         $bib->creator($user);
196         $bib->create_date('now');
197         $bib->editor($user);
198         $bib->edit_date('now');
199         $bib->tcn_source($tcn_source);
200         $bib->tcn_value($tcn_value);
201         $bib->last_xact_id('IMPORT-'.$starttime);
202
203         print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
204         $dontuse_id{$tcn_value} = 1;
205
206         $count++;
207
208         if (!$quiet && !($count % 50)) {
209                 print STDERR "\r$count\t". $count / (time - $starttime);
210         }
211 }
212
213 sub preprocess {
214         my $rec = shift;
215         my $id = shift;
216
217         my ($source, $value) = ('','','');
218
219         if (!$id) {
220                 my $f = $rec->field('001');
221                 $id = $f->data if ($f);
222         $id = '' if (exists $dontuse_id{$id});
223         }
224
225         if (!$id || exists $dontuse_id{$source.$id}) {
226                 my $f = $rec->field('000');
227                 $id = $f->data if ($f);
228                 $source = 'g' if ($f); # only PG seems to use this
229         }
230
231         if (!$id || exists $dontuse_id{$source.$id}) {
232                 my $f = $rec->field('020');
233                 $id = $f->subfield('a') if ($f);
234                 $source = 'i' if ($f);
235         }
236
237         if (!$id || exists $dontuse_id{$source.$id}) {
238                 my $f = $rec->field('022');
239                 $id = $f->subfield('a') if ($f);
240                 $source = 'i' if ($f);
241         }
242
243         if (!$id || exists $dontuse_id{$source.$id}) {
244                 my $f = $rec->field('010');
245                 $id = $f->subfield('a') if ($f);
246                 $source = 'l' if ($f);
247         }
248
249         $rec->delete_field($_) for ($rec->field('901', $id_field, @trash_fields));
250
251         if ($id) {
252                 $id =~ s/\s*$//o;
253                 $id =~ s/^\s*//o;
254                 $id =~ s/^(\S+).*$/$1/o;
255
256                 $id = $source.$id if ($source);
257
258                 ($source, $value) = $id =~ /^(.)(.+)$/o;
259                 if ($id =~ /^o(\d+)$/o) {
260                         $id = "ocm$1";
261                         $source = 'o';
262                 }
263         }
264
265         if ($id && exists $dontuse_id{$id}) {
266                 warn "\n!!! TCN $id is already in use.  Using the record ID ($recid) as a system-generated TCN.\n";
267                 $id = '';
268         }
269
270         if (!$id) {
271                 $source = 's';
272                 $id = 's'.$recid;
273         }
274
275         my $tcn = MARC::Field->new(
276                 '901' => ('', ''),
277                 a => $id,
278                 b => do { $source_map{$source} || 'System' },
279         );
280
281         return ($rec,$tcn);
282 }
283
284 sub entityize {
285         my $stuff = shift;
286         my $form = shift;
287
288         if ($form and $form eq 'D') {
289                 $stuff = NFD($stuff);
290         } else {
291                 $stuff = NFC($stuff);
292         }
293
294         $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
295         return $stuff;
296 }
297