3 ###############################################################################
6 =item B<loadbibs> --stage_file foo.mrc
8 Takes a load of bibs from a binary marc file and loads them into mig staging table
11 Takes these optional arguments:
15 When used it does not drop the staging table and instead adds onto it.
19 Sets an x_source value on the staging table to the one supplied instead of the
24 By default the program assumes a USMARC file. This flag will identify it as
25 a MARCXML file instead.
31 ###############################################################################
42 use MARC::Charset 'marc8_to_utf8';
43 #binmode STDIN, ':bytes';
45 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
46 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
53 use Unicode::Normalize;
54 my $mig_bin = "$FindBin::Bin/";
55 use lib "$FindBin::Bin/";
58 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
59 pod2usage(-verbose => 1) if ! $ARGV[1];
61 my $next_arg_is_file = 0;
63 my $next_arg_is_source = 0;
64 my $source = 'default';
66 my $dbh = Mig::db_connect();
70 binmode STDIN, ':utf8';
72 #MARC::Charset->assume_unicode(1);
73 MARC::Charset->ignore_errors(1);
75 foreach my $arg (@ARGV) {
76 if ($arg eq '--stage_file') {
77 $next_arg_is_file = 1;
80 if ($next_arg_is_file) {
82 $next_arg_is_file = 0;
85 if ($arg eq '--source') {
86 $next_arg_is_source = 1;
89 if ($next_arg_is_source) {
91 $next_arg_is_source = 0;
94 if ($arg eq '--append') {
98 if ($arg eq '--xml') {
104 create_child_table($dbh); #and test to see if it exists
106 # normal stage table creation
108 drop_stage_table($dbh);
109 create_stage_table($dbh);
112 $batch = MARC::Batch->new('XML',$infile);
114 $batch = MARC::Batch->new('USMARC',$infile);
116 $batch->strict_off();
118 while ( my $record = $batch->next() ) {
119 my $xml = $record->as_xml_record();
120 $xml = marc8_to_utf8($xml);
122 $xml = clean_marc($xml);
123 $xml = '$_$' . $xml . '$_$';
124 my @warnings = $batch->warnings();
126 if (@warnings) { $warning_string = "'" . join(':',@warnings) . "'"; } else { $warning_string = "'none'"; }
127 my $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry_legacy (marc,x_source,x_warnings) VALUES ($xml,'$source',$warning_string);";
128 my $sth = $dbh->prepare($sql);
130 report_progress("Records staged", $i) if 0 != $i % 100;
134 CREATE INDEX ${MIGSCHEMA}_biblio_record_entry_legacy_idx ON
135 $MIGSCHEMA.biblio_record_entry_legacy (id);
141 sub drop_stage_table {
143 $dbh->do("DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_legacy;");
147 sub create_stage_table {
150 $dbh->do("CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_legacy (
154 x_migrate BOOLEAN DEFAULT TRUE
155 ) INHERITS ($MIGSCHEMA.biblio_record_entry);");
160 sub create_child_table {
167 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
169 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
178 $xml = marc8_to_utf8($xml);
180 $xml =~ s/^<\?xml.+\?\s*>//go;
181 $xml =~ s/>\s+</></go;
182 $xml =~ s/\p{Cc}//go;
184 $xml =~ s/&(?!\S+;)/&/gso;
185 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
186 $xml =~ s/[\x00-\x1f]//go;
193 print STDERR "$0: $msg", "\n";
197 sub report_progress {
198 my ($msg, $counter) = @_;
199 if (defined $counter) {
200 print STDERR "$msg: $counter\n";
202 print STDERR "$msg\n";