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 ###############################################################################
43 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
44 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
51 use Unicode::Normalize;
52 my $mig_bin = "$FindBin::Bin/";
53 use lib "$FindBin::Bin/";
56 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
57 pod2usage(-verbose => 1) if ! $ARGV[1];
59 my $next_arg_is_file = 0;
60 my $append_is_false = 1;
61 my $next_arg_is_source = 0;
62 my $source = 'default';
64 my $dbh = Mig::db_connect();
68 binmode STDIN, ':utf8';
70 foreach my $arg (@ARGV) {
71 if ($arg eq '--stage_file') {
72 $next_arg_is_file = 1;
75 if ($next_arg_is_file) {
77 $next_arg_is_file = 0;
80 if ($arg eq '--source') {
81 $next_arg_is_source = 1;
84 if ($next_arg_is_file) {
86 $next_arg_is_source = 0;
89 if ($arg eq '--append') {
93 if ($arg eq '--xml') {
99 if ($append_is_false) {
101 DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_stage;
102 CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_stage (
106 x_migrate BOOLEAN DEFAULT TRUE
107 ) INHERITS ($MIGSCHEMA.biblio_record_entry);
112 $batch = MARC::Batch->new('XML',$infile);
114 $batch = MARC::Batch->new('USMARC',$infile);
116 $batch->strict_off();
118 while ( my $record = $batch->next() ) {
120 if ($file_is_xml) { $xml = $record; }
121 else { $xml = $record->as_xml_record(); }
123 $xml = clean_marc($xml);
124 $xml = '$_$' . $xml . '$_$';
125 my @warnings = $batch->warnings();
127 if (@warnings) { $warning_string = "'" . join(':',@warnings) . "'"; } else { $warning_string = "'none'"; }
128 my $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry_stage (marc,x_source,x_warnings) VALUES ($xml,$source,$warning_string);";
129 my $sth = $dbh->prepare($sql);
131 report_progress("Records staged", $i) if 0 != $i % 100;
135 CREATE INDEX ${MIGSCHEMA}_biblio_record_entry_stage_idx ON
136 $MIGSCHEMA.biblio_record_entry_stage (id);
144 $xml =~ s/^<\?xml.+\?\s*>//go;
145 $xml =~ s/>\s+</></go;
146 $xml =~ s/\p{Cc}//go;
148 $xml =~ s/&(?!\S+;)/&/gso;
149 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
150 $xml =~ s/[\x00-\x1f]//go;
157 print STDERR "$0: $msg", "\n";
161 sub report_progress {
162 my ($msg, $counter) = @_;
163 if (defined $counter) {
164 print STDERR "$msg: $counter\n";
166 print STDERR "$msg\n";