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 binmode STDIN, ':bytes';
44 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
45 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
52 use Unicode::Normalize;
53 my $mig_bin = "$FindBin::Bin/";
54 use lib "$FindBin::Bin/";
57 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
58 pod2usage(-verbose => 1) if ! $ARGV[1];
60 my $next_arg_is_file = 0;
61 my $append_is_false = 1;
62 my $next_arg_is_source = 0;
63 my $source = 'default';
65 my $dbh = Mig::db_connect();
69 binmode STDIN, ':utf8';
71 foreach my $arg (@ARGV) {
72 if ($arg eq '--stage_file') {
73 $next_arg_is_file = 1;
76 if ($next_arg_is_file) {
78 $next_arg_is_file = 0;
81 if ($arg eq '--source') {
82 $next_arg_is_source = 1;
85 if ($next_arg_is_file) {
87 $next_arg_is_source = 0;
90 if ($arg eq '--append') {
94 if ($arg eq '--xml') {
100 create_child_table($dbh);
102 # normal stage table creation
103 if ($append_is_false) { create_stage_table($dbh); }
105 #sanity check and create stage table if it doesn't exist
106 my $query = "SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry_stage')";
107 my $qsth = $dbh->prepare($query);
110 while (my @row = $qsth->fetchrow_array) { $f = $row[0]; }
111 if ($f eq 'f') { create_stage_table($dbh); }
113 if ($append_is_false == 0) { 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);
145 sub create_stage_table {
148 $dbh->do("DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_stage;");
149 $dbh->do("CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_stage (
153 x_migrate BOOLEAN DEFAULT TRUE
154 ) INHERITS ($MIGSCHEMA.biblio_record_entry);");
159 sub create_child_table {
166 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
168 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
178 $xml =~ s/^<\?xml.+\?\s*>//go;
179 $xml =~ s/>\s+</></go;
180 $xml =~ s/\p{Cc}//go;
182 $xml =~ s/&(?!\S+;)/&/gso;
183 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
184 $xml =~ s/[\x00-\x1f]//go;
191 print STDERR "$0: $msg", "\n";
195 sub report_progress {
196 my ($msg, $counter) = @_;
197 if (defined $counter) {
198 print STDERR "$msg: $counter\n";
200 print STDERR "$msg\n";