strict_add_sf9
[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 = 0;
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 #causes issues sometimes 
75 #may be useful other times ... still in flux
76 #my $setting = MARC::Charset->assume_unicode();
77 #MARC::Charset->assume_unicode(1); 
78 MARC::Charset->ignore_errors(1);
79
80 foreach my $arg (@ARGV) {
81     if ($arg eq '--stage_file') {
82         $next_arg_is_file = 1;
83         next;
84     }
85     if ($next_arg_is_file) {
86         $infile = $arg;
87         $next_arg_is_file = 0;
88         next;
89     }
90     if ($arg eq '--source') {
91         $next_arg_is_source = 1;
92         next;
93     }
94     if ($next_arg_is_source) {
95         $source = $arg;
96         $next_arg_is_source = 0;
97         next;
98     }
99         if ($arg eq '--append') {
100                 $append = 1;
101                 next;
102         }
103     if ($arg eq '--xml') {
104         $file_is_xml = 1;
105         next;
106     }
107 }
108
109 create_child_table($dbh); #and test to see if it exists 
110
111 # normal stage table creation
112 if ($append == 0) { 
113     drop_stage_table($dbh); 
114     create_stage_table($dbh);
115     }
116 if ($file_is_xml) {
117         $batch = MARC::Batch->new('XML',$infile);
118 } else {
119         $batch = MARC::Batch->new('USMARC',$infile);
120
121 $batch->strict_off();
122
123 while ( my $record = $batch->next() ) {
124         my $xml = $record->as_xml_record();
125     $xml = marc8_to_utf8($xml);
126         $i++;
127         $xml = clean_marc($xml);
128         $xml = '$_$' . $xml . '$_$';
129         my @warnings = $batch->warnings();
130         my $warning_string;
131         if (@warnings) { $warning_string = "'" . join(':',@warnings) . "'"; } else { $warning_string = "'none'"; }
132         my $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry_legacy (marc,x_source,x_warnings) VALUES ($xml,'$source',$warning_string);";
133     my $sth = $dbh->prepare($sql);
134     $sth->execute();
135         report_progress("Records staged", $i) if 0 != $i % 100;
136 }
137
138 $dbh->do(qq/
139     CREATE INDEX ${MIGSCHEMA}_biblio_record_entry_legacy_idx ON
140         $MIGSCHEMA.biblio_record_entry_legacy (id);
141 /);
142
143 print "Finis.\n";
144
145
146 sub drop_stage_table {
147     my $dbh = shift;
148     $dbh->do("DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_legacy;");
149     return();
150 }
151
152 sub create_stage_table {
153         my $dbh = shift;
154
155     $dbh->do("CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_legacy (
156             l_bib_id    TEXT,
157             x_source    TEXT,
158             x_warnings  TEXT,
159             x_migrate   BOOLEAN DEFAULT TRUE
160         ) INHERITS ($MIGSCHEMA.biblio_record_entry);");
161
162     return();
163 }
164
165 sub create_child_table {
166     my $dbh = shift;
167
168     $dbh->do("DO \$\$ 
169         DECLARE
170             t   BOOLEAN;
171         BEGIN
172         SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
173         IF t = FALSE THEN
174             PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
175         END IF;
176         END \$\$;");
177
178     return ();
179 }
180
181 sub clean_marc {
182     my $xml = shift;
183     $xml = marc8_to_utf8($xml);
184     $xml =~ s/\n//sog;
185     $xml =~ s/^<\?xml.+\?\s*>//go;
186     $xml =~ s/>\s+</></go;
187     $xml =~ s/\p{Cc}//go;
188     $xml = NFC($xml);
189     $xml =~ s/&(?!\S+;)/&amp;/gso;
190     $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
191     $xml =~ s/[\x00-\x1f]//go;
192     return $xml;
193 }
194
195
196 sub abort {
197     my $msg = shift;
198     print STDERR "$0: $msg", "\n";
199     exit 1;
200 }
201
202 sub report_progress {
203     my ($msg, $counter) = @_;
204     if (defined $counter) {
205         print STDERR "$msg: $counter\n";
206     } else {
207         print STDERR "$msg\n";
208     }
209 }