5 use lib '/openils/lib/perl5/';
8 use OpenILS::Utils::Fieldmapper;
9 use Digest::MD5 qw/md5_hex/;
10 use OpenSRF::Utils::JSON;
12 use Unicode::Normalize;
16 use Time::HiRes qw/time/;
19 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
23 #MARC::Charset->ignore_errors(1);
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');
28 my ($db_driver,$db_host,$db_name,$db_user,$db_pw) =
29 ('Pg','localhost','evergreen','postgres','postgres');
32 'marctype=s' => \$marctype,
33 'startid=i' => \$recid,
34 'idfield=s' => \$id_field,
35 'idsubfield=s' => \$id_subfield,
37 'encoding=s' => \$enc,
38 'hard_encoding' => \$force_enc,
39 'keyfile=s' => \$keyfile,
40 'config=s' => \$config,
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,
54 MARC::Charset->ignore_errors(1);
55 MARC::Charset->assume_encoding($enc);
58 if (uc($marctype) eq 'XML') {
64 @files = @ARGV if (!@files);
70 my $dsn = "dbi:$db_driver:host=$db_host;dbname=$db_name";
73 my $table = 'biblio_record_entry';
74 $table = 'biblio.record_entry' if ($db_driver eq 'Pg');
76 my $dbh = DBI->connect($dsn,$db_user,$db_pw);
77 my $sth = $dbh->prepare("SELECT MAX(id) + 1 FROM $table");
80 $sth->bind_col(1, \$recid);
95 Fieldmapper->import(IDL => $idlfile);
99 open F, $keyfile or die "Couldn't open key file $keyfile";
101 if ( /^(\d+)\|(\S+)/o ) {
110 open F, $dontuse_file or die "Couldn't open used-id file $dontuse_file";
120 select STDERR; $| = 1;
121 select STDOUT; $| = 1;
123 my $batch = new MARC::Batch ( $marctype, @files );
124 $batch->strict_off();
125 $batch->warnings_off();
128 my $starttime = time;
131 while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
132 next if ($rec == -1);
136 while (exists $used_ids{$recid}) {
139 $used_ids{$recid} = 1;
142 my $field = $rec->field($id_field);
144 if ($field->is_control_field) {
147 $id = $field->subfield($id_subfield);
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 ) );
169 ($rec, $tcn) = preprocess($rec, $id);
171 $tcn->add_subfields(c => $id);
173 $rec->delete_field( $_ ) for ($rec->field($id_field));
174 $rec->append_fields( $tcn );
180 my $tcn_value = $rec->subfield('901' => 'a') || "SYS$id";
181 my $tcn_source = $rec->subfield('901' => 'b') || 'System';
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;
190 my $bib = new Fieldmapper::biblio::record_entry;
195 $bib->creator($user);
196 $bib->create_date('now');
198 $bib->edit_date('now');
199 $bib->tcn_source($tcn_source);
200 $bib->tcn_value($tcn_value);
201 $bib->last_xact_id('IMPORT-'.$starttime);
203 print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
204 $dontuse_id{$tcn_value} = 1;
208 if (!$quiet && !($count % 50)) {
209 print STDERR "\r$count\t". $count / (time - $starttime);
217 my ($source, $value) = ('','','');
220 my $f = $rec->field('001');
221 $id = $f->data if ($f);
222 $id = '' if (exists $dontuse_id{$id});
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
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);
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);
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);
249 $rec->delete_field($_) for ($rec->field('901', $id_field, @trash_fields));
254 $id =~ s/^(\S+).*$/$1/o;
256 $id = $source.$id if ($source);
258 ($source, $value) = $id =~ /^(.)(.+)$/o;
259 if ($id =~ /^o(\d+)$/o) {
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";
275 my $tcn = MARC::Field->new(
278 b => do { $source_map{$source} || 'System' },
288 if ($form and $form eq 'D') {
289 $stuff = NFD($stuff);
291 $stuff = NFC($stuff);
294 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;