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 my $ignore = MARC::Charset->ignore_errors();
73 MARC::Charset->ignore_errors(1);
74 my $setting = MARC::Charset->assume_unicode();
75 MARC::Charset->assume_unicode(1);
76 MARC::Charset->ignore_errors(1);
78 foreach my $arg (@ARGV) {
79 if ($arg eq '--stage_file') {
80 $next_arg_is_file = 1;
83 if ($next_arg_is_file) {
85 $next_arg_is_file = 0;
88 if ($arg eq '--source') {
89 $next_arg_is_source = 1;
92 if ($next_arg_is_source) {
94 $next_arg_is_source = 0;
97 if ($arg eq '--append') {
101 if ($arg eq '--xml') {
107 create_child_table($dbh); #and test to see if it exists
109 # normal stage table creation
111 drop_stage_table($dbh);
112 create_stage_table($dbh);
116 $batch = MARC::Batch->new('XML',$infile);
118 $batch = MARC::Batch->new('USMARC',$infile);
120 $batch->strict_off();
122 while ( my $record = $batch->next() ) {
124 if ($file_is_xml) { $xml = $record; }
125 else { $xml = $record->as_xml_record(); }
127 $xml = clean_marc($xml);
128 $xml = '$_$' . $xml . '$_$';
129 my @warnings = $batch->warnings();
131 if (@warnings) { $warning_string = "'" . join(':',@warnings) . "'"; } else { $warning_string = "'none'"; }
132 my $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry_stage (marc,x_source,x_warnings) VALUES ($xml,'$source',$warning_string);";
133 my $sth = $dbh->prepare($sql);
135 report_progress("Records staged", $i) if 0 != $i % 100;
139 CREATE INDEX ${MIGSCHEMA}_biblio_record_entry_stage_idx ON
140 $MIGSCHEMA.biblio_record_entry_stage (id);
146 sub drop_stage_table {
148 $dbh->do("DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_stage;");
152 sub create_stage_table {
155 $dbh->do("CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_stage (
159 x_migrate BOOLEAN DEFAULT TRUE
160 ) INHERITS ($MIGSCHEMA.biblio_record_entry);");
165 sub create_child_table {
172 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
174 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
183 $xml = marc8_to_utf8($xml);
185 $xml =~ s/^<\?xml.+\?\s*>//go;
186 $xml =~ s/>\s+</></go;
187 $xml =~ s/\p{Cc}//go;
189 $xml =~ s/&(?!\S+;)/&/gso;
190 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
191 $xml =~ s/[\x00-\x1f]//go;
198 print STDERR "$0: $msg", "\n";
202 sub report_progress {
203 my ($msg, $counter) = @_;
204 if (defined $counter) {
205 print STDERR "$msg: $counter\n";
207 print STDERR "$msg\n";