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;
62 my $append_is_false = 1;
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_file) {
94 $next_arg_is_source = 0;
97 if ($arg eq '--append') {
101 if ($arg eq '--xml') {
107 create_child_table($dbh);
109 # normal stage table creation
110 if ($append_is_false) { create_stage_table($dbh); }
112 #sanity check and create stage table if it doesn't exist
113 my $query = "SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry_stage')";
114 my $qsth = $dbh->prepare($query);
117 while (my @row = $qsth->fetchrow_array) { $f = $row[0]; }
118 if ($f eq 'f') { create_stage_table($dbh); }
120 if ($append_is_false == 0) { create_stage_table($dbh); }
123 $batch = MARC::Batch->new('XML',$infile);
125 $batch = MARC::Batch->new('USMARC',$infile);
127 $batch->strict_off();
129 while ( my $record = $batch->next() ) {
131 if ($file_is_xml) { $xml = $record; }
132 else { $xml = $record->as_xml_record(); }
134 $xml = clean_marc($xml);
135 $xml = '$_$' . $xml . '$_$';
136 my @warnings = $batch->warnings();
138 if (@warnings) { $warning_string = "'" . join(':',@warnings) . "'"; } else { $warning_string = "'none'"; }
139 my $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry_stage (marc,x_source,x_warnings) VALUES ($xml,$source,$warning_string);";
140 my $sth = $dbh->prepare($sql);
142 report_progress("Records staged", $i) if 0 != $i % 100;
146 CREATE INDEX ${MIGSCHEMA}_biblio_record_entry_stage_idx ON
147 $MIGSCHEMA.biblio_record_entry_stage (id);
152 sub create_stage_table {
155 $dbh->do("DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_stage;");
156 $dbh->do("CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_stage (
160 x_migrate BOOLEAN DEFAULT TRUE
161 ) INHERITS ($MIGSCHEMA.biblio_record_entry);");
166 sub create_child_table {
173 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
175 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
184 $xml = marc8_to_utf8($xml);
186 $xml =~ s/^<\?xml.+\?\s*>//go;
187 $xml =~ s/>\s+</></go;
188 $xml =~ s/\p{Cc}//go;
190 $xml =~ s/&(?!\S+;)/&/gso;
191 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
192 $xml =~ s/[\x00-\x1f]//go;
199 print STDERR "$0: $msg", "\n";
203 sub report_progress {
204 my ($msg, $counter) = @_;
205 if (defined $counter) {
206 print STDERR "$msg: $counter\n";
208 print STDERR "$msg\n";