making mig-loadbibs a bit more defensive with marc-8 data
[migration-tools.git] / mig-bin / mig-loadbibs
1 #!/usr/bin/perl
2
3 ###############################################################################
4 =pod
5
6 =item B<loadbibs> --stage_file foo.mrc 
7
8 Takes a load of bibs from a binary marc file and loads them into mig staging table 
9 of bibio_record_entry.
10
11 Takes these optional arguments:
12
13 --append 
14
15 When used it does not drop the staging table and instead adds onto it.  
16
17 --source
18
19 Sets an x_source value on the staging table to the one supplied instead of the 
20 default of none.
21
22 --xml 
23
24 By default the program assumes a USMARC file.  This flag will identify it as 
25 a MARCXML file instead.
26
27 =back
28
29 =cut
30
31 ###############################################################################
32
33 use strict;
34 use warnings;
35
36 use DBI;
37 use Data::Dumper;
38 use MARC::Record;
39 use MARC::Batch;
40 use MARC::File;
41 use MARC::File::XML;
42 use MARC::Charset 'marc8_to_utf8';
43 binmode STDIN, ':bytes';
44 use Env qw(
45     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
46     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
47 );
48 use Pod::Usage;
49 use Switch;
50 use Cwd 'abs_path';
51 use FindBin;
52 use UNIVERSAL;
53 use Unicode::Normalize;
54 my $mig_bin = "$FindBin::Bin/";
55 use lib "$FindBin::Bin/";
56 use Mig;
57
58 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
59 pod2usage(-verbose => 1) if ! $ARGV[1];
60
61 my $next_arg_is_file = 0;
62 my $append_is_false = 1;
63 my $next_arg_is_source = 0;
64 my $source = 'default';
65 my $file_is_xml = 0;
66 my $dbh = Mig::db_connect();
67 my $infile;
68 my $i = 0;
69 my $batch;
70 binmode STDIN, ':utf8';
71
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);
77
78 foreach my $arg (@ARGV) {
79     if ($arg eq '--stage_file') {
80         $next_arg_is_file = 1;
81         next;
82     }
83     if ($next_arg_is_file) {
84         $infile = $arg;
85         $next_arg_is_file = 0;
86         next;
87     }
88     if ($arg eq '--source') {
89         $next_arg_is_source = 1;
90         next;
91     }
92     if ($next_arg_is_file) {
93         $source = $arg;
94         $next_arg_is_source = 0;
95         next;
96     }
97         if ($arg eq '--append') {
98                 $append_is_false = 0;
99                 next;
100         }
101     if ($arg eq '--xml') {
102         $file_is_xml = 1;
103         next;
104     }
105 }
106
107 create_child_table($dbh);
108
109 # normal stage table creation
110 if ($append_is_false) { create_stage_table($dbh); }
111
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);
115 $qsth->execute();
116 my $f;
117 while (my @row = $qsth->fetchrow_array) { $f = $row[0]; }
118 if ($f eq 'f') { create_stage_table($dbh); }
119
120 if ($append_is_false == 0) { create_stage_table($dbh); }
121
122 if ($file_is_xml) {
123         $batch = MARC::Batch->new('XML',$infile);
124 } else {
125         $batch = MARC::Batch->new('USMARC',$infile);
126
127 $batch->strict_off();
128
129 while ( my $record = $batch->next() ) {
130         my $xml;
131         if ($file_is_xml) { $xml = $record; } 
132                 else { $xml = $record->as_xml_record(); } 
133         $i++;
134         $xml = clean_marc($xml);
135         $xml = '$_$' . $xml . '$_$';
136         my @warnings = $batch->warnings();
137         my $warning_string;
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);
141     $sth->execute();
142         report_progress("Records staged", $i) if 0 != $i % 100;
143 }
144
145 $dbh->do(qq/
146     CREATE INDEX ${MIGSCHEMA}_biblio_record_entry_stage_idx ON
147         $MIGSCHEMA.biblio_record_entry_stage (id);
148 /);
149
150 print "Finis.\n";
151
152 sub create_stage_table {
153         my $dbh = shift;
154
155     $dbh->do("DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_stage;");
156     $dbh->do("CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_stage (
157             l_bib_id    TEXT,
158             x_source    TEXT,
159             x_warnings  TEXT,
160             x_migrate   BOOLEAN DEFAULT TRUE
161         ) INHERITS ($MIGSCHEMA.biblio_record_entry);");
162
163     return();
164 }
165
166 sub create_child_table {
167     my $dbh = shift;
168
169     $dbh->do("DO \$\$ 
170         DECLARE
171             t   BOOLEAN;
172         BEGIN
173         SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
174         IF t = FALSE THEN
175             PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
176         END IF;
177         END \$\$;");
178
179     return ();
180 }
181
182 sub clean_marc {
183     my $xml = shift;
184     $xml = marc8_to_utf8($xml);
185     $xml =~ s/\n//sog;
186     $xml =~ s/^<\?xml.+\?\s*>//go;
187     $xml =~ s/>\s+</></go;
188     $xml =~ s/\p{Cc}//go;
189     $xml = NFC($xml);
190     $xml =~ s/&(?!\S+;)/&amp;/gso;
191     $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
192     $xml =~ s/[\x00-\x1f]//go;
193     return $xml;
194 }
195
196
197 sub abort {
198     my $msg = shift;
199     print STDERR "$0: $msg", "\n";
200     exit 1;
201 }
202
203 sub report_progress {
204     my ($msg, $counter) = @_;
205     if (defined $counter) {
206         print STDERR "$msg: $counter\n";
207     } else {
208         print STDERR "$msg\n";
209     }
210 }