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,
53 @trash_fields = split(/,/,join(',',@trash_fields));
56 MARC::Charset->ignore_errors(1);
57 MARC::Charset->assume_encoding($enc);
60 if (uc($marctype) eq 'XML') {
66 @files = @ARGV if (!@files);
72 my $dsn = "dbi:$db_driver:host=$db_host;dbname=$db_name";
75 my $table = 'biblio_record_entry';
76 $table = 'biblio.record_entry' if ($db_driver eq 'Pg');
78 my $dbh = DBI->connect($dsn,$db_user,$db_pw);
79 my $sth = $dbh->prepare("SELECT MAX(id) + 1 FROM $table");
82 $sth->bind_col(1, \$recid);
97 Fieldmapper->import(IDL => $idlfile);
101 open F, $keyfile or die "Couldn't open key file $keyfile";
103 if ( /^(\d+)\|(\S+)/o ) {
112 open F, $dontuse_file or die "Couldn't open used-id file $dontuse_file";
122 select STDERR; $| = 1;
123 select STDOUT; $| = 1;
125 my $batch = new MARC::Batch ( $marctype, @files );
126 $batch->strict_off();
127 $batch->warnings_off();
130 my $starttime = time;
133 while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
134 next if ($rec == -1);
138 while (exists $used_ids{$recid}) {
141 $used_ids{$recid} = 1;
144 my $field = $rec->field($id_field);
146 if ($field->is_control_field) {
149 $id = $field->subfield($id_subfield);
154 $id = '' if (exists $dontuse_id{$id});
162 if (my $tcn = $keymap{$id}) {
163 $rec->delete_field( $_ ) for ($rec->field($id_field));
164 $rec->append_fields( MARC::Field->new( $id_field, '', '', $id_subfield, $tcn ) );
172 ($rec, $tcn) = preprocess($rec, $id);
174 $tcn->add_subfields(c => $id);
176 $rec->delete_field( $_ ) for ($rec->field($id_field));
177 $rec->append_fields( $tcn );
183 my $tcn_value = $rec->subfield('901' => 'a') || "SYS$id";
184 my $tcn_source = $rec->subfield('901' => 'b') || 'System';
186 (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
187 $xml =~ s/^<\?xml.+\?\s*>//go;
188 $xml =~ s/>\s+</></go;
189 $xml =~ s/\p{Cc}//go;
190 $xml = entityize($xml);
191 $xml =~ s/[\x00-\x1f]//go;
193 my $bib = new Fieldmapper::biblio::record_entry;
198 $bib->creator($user);
199 $bib->create_date('now');
201 $bib->edit_date('now');
202 $bib->tcn_source($tcn_source);
203 $bib->tcn_value($tcn_value);
204 $bib->last_xact_id('IMPORT-'.$starttime);
206 print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
207 $dontuse_id{$tcn_value} = 1;
211 if (!$quiet && !($count % 50)) {
212 print STDERR "\r$count\t". $count / (time - $starttime);
220 my ($source, $value) = ('','');
222 $id = '' if (exists $dontuse_id{$id});
225 my $f = $rec->field('001');
226 $id = $f->data if ($f);
227 $id = '' if (exists $dontuse_id{$id});
230 if (!$id || exists $dontuse_id{$source.$id}) {
231 my $f = $rec->field('000');
232 $id = $f->data if ($f);
233 $source = 'g' if ($f); # only PG seems to use this
236 if (!$id || exists $dontuse_id{$source.$id}) {
237 my $f = $rec->field('020');
238 $id = $f->subfield('a') if ($f);
239 $source = 'i' if ($f);
242 if (!$id || exists $dontuse_id{$source.$id}) {
243 my $f = $rec->field('022');
244 $id = $f->subfield('a') if ($f);
245 $source = 'i' if ($f);
248 if (!$id || exists $dontuse_id{$source.$id}) {
249 my $f = $rec->field('010');
250 $id = $f->subfield('a') if ($f);
251 $source = 'l' if ($f);
254 $rec->delete_field($_) for ($rec->field('901', $id_field, @trash_fields));
259 $id =~ s/^(\S+).*$/$1/o;
261 $id = $source.$id if ($source);
263 ($source, $value) = $id =~ /^(.)(.+)$/o;
264 if ($id =~ /^o(\d+)$/o) {
270 if ($id && exists $dontuse_id{$id}) {
271 warn "\n!!! TCN $id is already in use. Using the record ID ($recid) as a system-generated TCN.\n";
280 my $tcn = MARC::Field->new(
283 b => do { $source_map{$source} || 'System' },
293 if ($form and $form eq 'D') {
294 $stuff = NFD($stuff);
296 $stuff = NFC($stuff);
299 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;