6 use OpenILS::Utils::Fieldmapper;
7 use Digest::MD5 qw/md5_hex/;
8 use OpenSRF::Utils::JSON;
9 use OpenILS::Application::AppUtils;
11 use Unicode::Normalize;
15 use Time::HiRes qw/time/;
18 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
22 #MARC::Charset->ignore_errors(1);
24 my ($id_field, $id_subfield, $recid, $user, $config, $idlfile, $marctype, $tcn_offset, $tcn_mapfile, $tcn_dumpfile, $used_id_file, $used_tcn_file, $enc, @files, @trash_fields, @req_fields, $use901, $quiet, $tcn_field, $tcn_subfield) =
25 ('', 'a', 0, 1, '@sysconfdir@/opensrf_core.xml', '@sysconfdir@/fm_IDL.xml', 'USMARC', 0);
27 my ($db_driver, $db_host, $db_port, $db_name, $db_user, $db_pw) =
28 ('Pg', 'localhost', 5432, 'evergreen', 'postgres', 'postgres');
31 'marctype=s' => \$marctype, # format of MARC files being processed defaults to USMARC, often set to XML
32 'startid=i' => \$recid, # id number to start with when auto-assigning id numbers, defaults to highest id in database + 1
33 'idfield=s' => \$id_field, # field containing the record's desired internal id, NOT tcn
34 'idsubfield=s' => \$id_subfield, # subfield of above record id field
35 'tcnfield=s' => \$tcn_field, # field containing the record's desired tcn, NOT the internal id
36 'tcnsubfield=s' => \$tcn_subfield, # subfield of above record tcn field
37 'tcnoffset=i' => \$tcn_offset, # optionally skip characters at beginning of supplied tcn (e.g. to remove '(Sirsi)')
38 'user=s' => \$user, # set creator/editor values for records in database
39 'encoding=s' => \$enc, # set assumed MARC encoding for MARC::Charset
40 'keyfile=s' => \$tcn_mapfile, # DEPRECATED, use tcn_mapfile instead
41 'tcn_mapfile=s' => \$tcn_mapfile, # external file which allows for matching specific record tcns to specific record ids, format = one id_number|tcn_number combo per line
42 'tcnfile=s' => \$tcn_dumpfile, # DEPRECATED, use tcn_dumpfile instead
43 'tcn_dumpfile=s' => \$tcn_dumpfile, # allows specification of a dumpfile for all used tcn values
44 'config=s' => \$config, # location of OpenSRF core config file, defaults to @sysconfdir@/opensrf_core.xml
45 'file=s' => \@files, # files to process (or you can simple list the files as unnamed arguments, i.e. @ARGV)
46 'required_fields=s' => \@req_fields, # skip any records missing these fields
47 'trash=s' => \@trash_fields, # fields to remove from all processed records
48 'xml_idl=s' => \$idlfile, # location of XML IDL file, defaults to @sysconfdir@/fm_IDL.xml
49 'dontuse=s' => \$used_id_file, # DEPRECATED, use used_id_file instead
50 'used_id_file=s' => \$used_id_file, # external file which prevents id collisions by specifying ids already in use in the database, format = one id number per line
51 'used_tcn_file=s' => \$used_tcn_file, # external file which prevents tcn collisions by specifying tcns already in use in the database, format = one tcn number per line
52 "db_driver=s" => \$db_driver, # database driver type, usually 'Pg'
53 "db_host=s" => \$db_host, # database hostname
54 "db_port=i" => \$db_port, # database port
55 "db_name=s" => \$db_name, # database name
56 "db_user=s" => \$db_user, # database username
57 "db_pw=s" => \$db_pw, # database password
58 'use901' => \$use901, # use values from previously created 901 fields and skip all other processing
59 'quiet' => \$quiet # do not output progress count
62 @trash_fields = split(/,/,join(',',@trash_fields));
63 @req_fields = split(/,/,join(',',@req_fields));
66 MARC::Charset->ignore_errors(1);
67 MARC::Charset->assume_encoding($enc);
70 if (uc($marctype) eq 'XML') {
76 @files = @ARGV if (!@files);
82 my $dsn = "dbi:$db_driver:host=$db_host;port=$db_port;dbname=$db_name";
85 my $table = 'biblio_record_entry';
86 $table = 'biblio.record_entry' if ($db_driver eq 'Pg');
88 my $dbh = DBI->connect($dsn,$db_user,$db_pw);
89 my $sth = $dbh->prepare("SELECT MAX(id) + 1 FROM $table");
92 $sth->bind_col(1, \$recid);
97 # In a clean Evergreen schema, the maximum ID will be -1; but sequences
98 # have to start at 1, so handle the clean Evergreen schema situation
104 my %tcn_source_map = (
114 Fieldmapper->import(IDL => $idlfile);
118 open F, $tcn_mapfile or die "Couldn't open key file $tcn_mapfile";
120 if ( /^(\d+)\|(\S+)/o ) {
129 open F, $used_id_file or die "Couldn't open used-id file $used_id_file";
134 $used_recids{$_} = 1;
140 if ($used_tcn_file) {
141 open F, $used_tcn_file or die "Couldn't open used-tcn file $used_tcn_file";
151 select STDERR; $| = 1;
152 select STDOUT; $| = 1;
154 my $batch = new MARC::Batch ( $marctype, @files );
155 $batch->strict_off();
156 $batch->warnings_off();
158 my $starttime = time;
161 PROCESS: while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
162 next if ($rec == -1);
166 # Skip records that don't contain a required field (like '245', for example)
167 foreach my $req_field (@req_fields) {
168 if (!$rec->field("$req_field")) {
169 warn "\n!!! Record $count missing required field $req_field, skipping record.\n";
177 # If $use901 is set, use it for the id, the tcn, and the tcn source without ANY further processing (i.e. no error checking)
179 $rec->delete_field($_) for ($rec->field(@trash_fields));
180 $tcn_value = $rec->subfield('901' => 'a');
181 $tcn_source = $rec->subfield('901' => 'b');
182 $id = $rec->subfield('901' => 'c');
184 # This section of code deals with the record's 'id', which is a system-level, numeric, internal identifier
185 # It is often convenient but not necessary to carry over the internal ids from your previous ILS, so here is where that happens
187 my $field = $rec->field($id_field);
189 if ($field->is_control_field) {
192 $id = $field->subfield($id_subfield);
194 # ensure internal record ids are numeric only
195 $id =~ s/\D+//gso if $id;
200 warn "\n!!! Record $count has missing or invalid id field $id_field, assigning new id.\n";
202 } elsif (exists $used_recids{$id}) {
203 warn "\n!!! Record $count has a duplicate id in field $id_field, assigning new id.\n";
206 $used_recids{$id} = 1;
210 # id field not specified or found to be invalid, assign auto id
212 while (exists $used_recids{$recid}) {
215 $used_recids{$recid} = 1;
220 # This section of code deals with the record's 'tcn', or title control number, which is a record-level, possibly alpha-numeric, sometimes user-supplied value
223 if (my $tcn = $tcn_map{$id}) {
224 $rec->delete_field( $_ ) for ($rec->field($tcn_field));
225 $rec->append_fields( MARC::Field->new( $tcn_field, '', '', $tcn_subfield, $tcn ) );
227 warn "\n!!! ID $id not found in tcn_mapfile, skipping record.\n";
233 my $field = $rec->field($tcn_field);
235 if ($field->is_control_field) {
236 $tcn_value = $field->data;
238 $tcn_value = $field->subfield($tcn_subfield);
240 # $tcn_offset is another Sirsi influence, as it will allow you to remove '(Sirsi)'
241 # from exported tcns, but was added more generically to perhaps support other use cases
243 $tcn_value = substr($tcn_value, $tcn_offset);
250 # turn our id and tcn into a 901 field, and also create a tcn and/or figure out the tcn source
251 ($tcn_value, $tcn_source) = preprocess($rec, $tcn_value, $id);
252 # delete the old identifier and trash fields
253 $rec->delete_field($_) for ($rec->field('901', $tcn_field, $id_field, @trash_fields));
256 (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
257 $xml =~ s/^<\?xml.+\?\s*>//go;
258 $xml =~ s/>\s+</></go;
259 $xml =~ s/\p{Cc}//go;
260 $xml = OpenILS::Application::AppUtils->entityize($xml);
261 $xml =~ s/[\x00-\x1f]//go;
263 my $bib = new Fieldmapper::biblio::record_entry;
268 $bib->creator($user);
269 $bib->create_date('now');
271 $bib->edit_date('now');
272 $bib->tcn_source($tcn_source);
273 $bib->tcn_value($tcn_value);
274 $bib->last_xact_id('IMPORT-'.$starttime);
276 print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
277 $used_tcns{$tcn_value} = 1;
279 if (!$quiet && !($count % 50)) {
280 print STDERR "\r$count\t". $count / (time - $starttime);
285 open TCN_DUMPFILE, '>', $tcn_dumpfile;
286 print TCN_DUMPFILE "$_\n" for (keys %used_tcns);
292 my $tcn_value = shift;
296 # in the following code, $tcn_number represents the portion of the tcn following the source code-letter
301 # this preprocess subroutine is optimized for Sirsi-created tcns, that is, those with a single letter
302 # followed by some digits (and maybe 'x' in older systems). If using user supplied tcns, try to identify
303 # the source here, otherwise set to 'z' ('Unknown')
304 if ($tcn_value =~ /([a-z])([0-9xX]+)/) {
311 # save and warn if a passed in TCN is replaced
312 if ($tcn_value && exists $used_tcns{$tcn_value}) {
313 $passed_tcn = $tcn_value;
320 # we didn't have a user supplied tcn, or it was a duplicate, so let's derive one from commonly unique record fields
322 my $f = $rec->field('001');
323 $tcn_value = despace($f->data) if ($f);
326 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
327 my $f = $rec->field('000');
329 $tcn_number = despace($f->data);
330 $tcn_source = 'g'; # only Project Gutenberg seems to use this
331 $tcn_value = $tcn_source.$tcn_number;
335 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
336 my $f = $rec->field('020');
338 $tcn_number = despace($f->subfield('a'));
340 $tcn_value = $tcn_source.$tcn_number;
344 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
345 my $f = $rec->field('022');
347 $tcn_number = despace($f->subfield('a'));
349 $tcn_value = $tcn_source.$tcn_number;
353 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
354 my $f = $rec->field('010');
356 $tcn_number = despace($f->subfield('a'));
358 $tcn_value = $tcn_source.$tcn_number;
362 # special case to catch possibly passed in full OCLC numbers and those derived from the 001 field
363 if ($tcn_value =~ /^oc(m|n)(\d+)$/o) {
366 $tcn_value = $tcn_source.$tcn_number;
369 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
372 $tcn_value = $tcn_source.$tcn_number;
377 # expand $tcn_source from code letter to full name
378 $tcn_source = do { $tcn_source_map{$tcn_source} || 'Unknown' };
381 warn "\n!!! TCN $passed_tcn is already in use, using TCN ($tcn_value) derived from $tcn_source ID.\n";
384 return ($tcn_value, $tcn_source);
390 # remove all leading/trailing spaces and trucate at first internal space if present
393 $value =~ s/^(\S+).*$/$1/o;