5 #use lib '/openils/lib/perl5/';
8 use OpenILS::Utils::Fieldmapper;
9 use Digest::MD5 qw/md5_hex/;
10 use OpenSRF::Utils::JSON;
11 use OpenILS::Application::AppUtils;
13 use Unicode::Normalize;
17 use Time::HiRes qw/time/;
20 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
24 #MARC::Charset->ignore_errors(1);
26 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) =
27 ('', 'a', 0, 1, '/openils/conf/opensrf_core.xml', '/openils/conf/fm_IDL.xml', 'USMARC', 0);
29 my ($db_driver, $db_host, $db_port, $db_name, $db_user, $db_pw) =
30 ('Pg', 'localhost', 5432, 'evergreen', 'postgres', 'postgres');
33 'marctype=s' => \$marctype, # format of MARC files being processed defaults to USMARC, often set to XML
34 'startid=i' => \$recid, # id number to start with when auto-assigning id numbers, defaults to highest id in database + 1
35 'idfield=s' => \$id_field, # field containing the record's desired internal id, NOT tcn
36 'idsubfield=s' => \$id_subfield, # subfield of above record id field
37 'tcnfield=s' => \$tcn_field, # field containing the record's desired tcn, NOT the internal id
38 'tcnsubfield=s' => \$tcn_subfield, # subfield of above record tcn field
39 'tcnoffset=i' => \$tcn_offset, # optionally skip characters at beginning of supplied tcn (e.g. to remove '(Sirsi)')
40 'user=s' => \$user, # set creator/editor values for records in database
41 'encoding=s' => \$enc, # set assumed MARC encoding for MARC::Charset
42 'keyfile=s' => \$tcn_mapfile, # DEPRECATED, use tcn_mapfile instead
43 '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
44 'tcnfile=s' => \$tcn_dumpfile, # DEPRECATED, use tcn_dumpfile instead
45 'tcn_dumpfile=s' => \$tcn_dumpfile, # allows specification of a dumpfile for all used tcn values
46 'config=s' => \$config, # location of OpenSRF core config file, defaults to /openils/conf/opensrf_core.xml
47 'file=s' => \@files, # files to process (or you can simple list the files as unnamed arguments, i.e. @ARGV)
48 'required_fields=s' => \@req_fields, # skip any records missing these fields
49 'trash=s' => \@trash_fields, # fields to remove from all processed records
50 'xml_idl=s' => \$idlfile, # location of XML IDL file, defaults to /openils/conf/fm_IDL.xml
51 'dontuse=s' => \$used_id_file, # DEPRECATED, use used_id_file instead
52 '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
53 '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
54 "db_driver=s" => \$db_driver, # database driver type, usually 'Pg'
55 "db_host=s" => \$db_host, # database hostname
56 "db_port=i" => \$db_port, # database port
57 "db_name=s" => \$db_name, # database name
58 "db_user=s" => \$db_user, # database username
59 "db_pw=s" => \$db_pw, # database password
60 'use901' => \$use901, # use values from previously created 901 fields and skip all other processing
61 'quiet' => \$quiet # do not output progress count
64 @trash_fields = split(/,/,join(',',@trash_fields));
65 @req_fields = split(/,/,join(',',@req_fields));
68 MARC::Charset->ignore_errors(1);
69 MARC::Charset->assume_encoding($enc);
72 if (uc($marctype) eq 'XML') {
78 @files = @ARGV if (!@files);
84 my $dsn = "dbi:$db_driver:host=$db_host;port=$db_port;dbname=$db_name";
87 my $table = 'biblio_record_entry';
88 $table = 'biblio.record_entry' if ($db_driver eq 'Pg');
90 my $dbh = DBI->connect($dsn,$db_user,$db_pw);
91 my $sth = $dbh->prepare("SELECT MAX(id) + 1 FROM $table");
94 $sth->bind_col(1, \$recid);
99 # In a clean Evergreen schema, the maximum ID will be -1; but sequences
100 # have to start at 1, so handle the clean Evergreen schema situation
106 my %tcn_source_map = (
116 Fieldmapper->import(IDL => $idlfile);
120 open F, $tcn_mapfile or die "Couldn't open key file $tcn_mapfile";
122 if ( /^(\d+)\|(\S+)/o ) {
131 open F, $used_id_file or die "Couldn't open used-id file $used_id_file";
136 $used_recids{$_} = 1;
142 if ($used_tcn_file) {
143 open F, $used_tcn_file or die "Couldn't open used-tcn file $used_tcn_file";
153 select STDERR; $| = 1;
154 select STDOUT; $| = 1;
156 my $batch = new MARC::Batch ( $marctype, @files );
157 $batch->strict_off();
158 $batch->warnings_off();
160 my $starttime = time;
163 PROCESS: while ( try { $rec = $batch->next } otherwise { $rec = -1 } ) {
164 next if ($rec == -1);
168 # Skip records that don't contain a required field (like '245', for example)
169 foreach my $req_field (@req_fields) {
170 if (!$rec->field("$req_field")) {
171 warn "\n!!! Record $count missing required field $req_field, skipping record.\n";
179 # If $use901 is set, use it for the id, the tcn, and the tcn source without ANY further processing (i.e. no error checking)
181 $rec->delete_field($_) for ($rec->field(@trash_fields));
182 $tcn_value = $rec->subfield('901' => 'a');
183 $tcn_source = $rec->subfield('901' => 'b');
184 $id = $rec->subfield('901' => 'c');
186 # This section of code deals with the record's 'id', which is a system-level, numeric, internal identifier
187 # It is often convenient but not necessary to carry over the internal ids from your previous ILS, so here is where that happens
189 my $field = $rec->field($id_field);
191 if ($field->is_control_field) {
194 $id = $field->subfield($id_subfield);
196 # ensure internal record ids are numeric only
197 $id =~ s/\D+//gso if $id;
202 warn "\n!!! Record $count has missing or invalid id field $id_field, assigning new id.\n";
204 } elsif (exists $used_recids{$id}) {
205 warn "\n!!! Record $count has a duplicate id in field $id_field, assigning new id.\n";
208 $used_recids{$id} = 1;
212 # id field not specified or found to be invalid, assign auto id
214 while (exists $used_recids{$recid}) {
217 $used_recids{$recid} = 1;
222 # 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
225 if (my $tcn = $tcn_map{$id}) {
226 $rec->delete_field( $_ ) for ($rec->field($tcn_field));
227 $rec->append_fields( MARC::Field->new( $tcn_field, '', '', $tcn_subfield, $tcn ) );
229 warn "\n!!! ID $id not found in tcn_mapfile, skipping record.\n";
235 my $field = $rec->field($tcn_field);
237 if ($field->is_control_field) {
238 $tcn_value = $field->data;
240 $tcn_value = $field->subfield($tcn_subfield);
242 # $tcn_offset is another Sirsi influence, as it will allow you to remove '(Sirsi)'
243 # from exported tcns, but was added more generically to perhaps support other use cases
245 $tcn_value = substr($tcn_value, $tcn_offset);
252 # turn our id and tcn into a 901 field, and also create a tcn and/or figure out the tcn source
253 ($tcn_value, $tcn_source) = preprocess($rec, $tcn_value, $id);
254 # delete the old identifier and trash fields
255 $rec->delete_field($_) for ($rec->field('901', $tcn_field, $id_field, @trash_fields));
258 (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
259 $xml =~ s/^<\?xml.+\?\s*>//go;
260 $xml =~ s/>\s+</></go;
261 $xml =~ s/\p{Cc}//go;
262 $xml = OpenILS::Application::AppUtils->entityize($xml);
263 $xml =~ s/[\x00-\x1f]//go;
265 my $bib = new Fieldmapper::biblio::record_entry;
270 $bib->creator($user);
271 $bib->create_date('now');
273 $bib->edit_date('now');
274 $bib->tcn_source($tcn_source);
275 $bib->tcn_value($tcn_value);
276 $bib->last_xact_id('IMPORT-'.$starttime);
278 print OpenSRF::Utils::JSON->perl2JSON($bib)."\n";
279 $used_tcns{$tcn_value} = 1;
281 if (!$quiet && !($count % 50)) {
282 print STDERR "\r$count\t". $count / (time - $starttime);
287 open TCN_DUMPFILE, '>', $tcn_dumpfile;
288 print TCN_DUMPFILE "$_\n" for (keys %used_tcns);
294 my $tcn_value = shift;
298 # in the following code, $tcn_number represents the portion of the tcn following the source code-letter
303 # this preprocess subroutine is optimized for Sirsi-created tcns, that is, those with a single letter
304 # followed by some digits (and maybe 'x' in older systems). If using user supplied tcns, try to identify
305 # the source here, otherwise set to 'z' ('Unknown')
306 if ($tcn_value =~ /([a-z])([0-9xX]+)/) {
313 # save and warn if a passed in TCN is replaced
314 if ($tcn_value && exists $used_tcns{$tcn_value}) {
315 $passed_tcn = $tcn_value;
322 # we didn't have a user supplied tcn, or it was a duplicate, so let's derive one from commonly unique record fields
324 my $f = $rec->field('001');
325 $tcn_value = despace($f->data) if ($f);
328 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
329 my $f = $rec->field('000');
331 $tcn_number = despace($f->data);
332 $tcn_source = 'g'; # only Project Gutenberg seems to use this
333 $tcn_value = $tcn_source.$tcn_number;
337 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
338 my $f = $rec->field('020');
340 $tcn_number = despace($f->subfield('a'));
342 $tcn_value = $tcn_source.$tcn_number;
346 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
347 my $f = $rec->field('022');
349 $tcn_number = despace($f->subfield('a'));
351 $tcn_value = $tcn_source.$tcn_number;
355 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
356 my $f = $rec->field('010');
358 $tcn_number = despace($f->subfield('a'));
360 $tcn_value = $tcn_source.$tcn_number;
364 # special case to catch possibly passed in full OCLC numbers and those derived from the 001 field
365 if ($tcn_value =~ /^oc(m|n)(\d+)$/o) {
368 $tcn_value = $tcn_source.$tcn_number;
371 if (!$tcn_value || exists $used_tcns{$tcn_value}) {
374 $tcn_value = $tcn_source.$tcn_number;
379 # expand $tcn_source from code letter to full name
380 $tcn_source = do { $tcn_source_map{$tcn_source} || 'Unknown' };
383 warn "\n!!! TCN $passed_tcn is already in use, using TCN ($tcn_value) derived from $tcn_source ID.\n";
386 return ($tcn_value, $tcn_source);
392 # remove all leading/trailing spaces and trucate at first internal space if present
395 $value =~ s/^(\S+).*$/$1/o;