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