first cuts of kmig-*iconv
[migration-tools.git] / mig-bin / mig-stagebibs
1 #!/usr/bin/perl
2
3 ###############################################################################
4 =pod
5
6 =item B<stagebibs> --file foo.mrc.xml
7
8 Takes a load of bibs from a UTF-8 MARC XML  file and loads them into mig staging 
9 table of bibio_record_entry_legacy.  This is done with no checking of file validity 
10 so records should be checked before hand and cleaned.
11
12 Takes three optional arguments:
13
14
15 --source 
16
17 Takes a numeric value and set the x_source of the bib record to that.  Defaults to 
18 2 which is local system.
19
20 --x_source
21
22 Sets an x_source value on the staging table to the one supplied instead of the 
23 default of none.
24
25 --auth foo.mrc.xml
26
27 This will load bibs into the authority_record_entry_legacy.
28
29 --serial foo.mrc.xml
30
31 This will load bibs into the serial_record_entry_legacy.
32
33 =back
34
35 =cut
36
37 ###############################################################################
38
39 use strict;
40 use warnings;
41
42 use DBI;
43 #binmode STDIN, ':bytes';
44 use Env qw(
45     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
46     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
47 );
48 use Data::Dumper;
49 use Pod::Usage;
50 use Switch;
51 use Cwd 'abs_path';
52 use FindBin;
53 use UNIVERSAL;
54 my $mig_bin = "$FindBin::Bin/";
55 use lib "$FindBin::Bin/";
56 use Mig;
57 use Getopt::Long;
58
59 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
60 pod2usage(-verbose => 1) if ! $ARGV[1];
61
62 my $append = 0;
63 my $base_table;
64 my $stage_table;
65 my $marc_column = 'marc';
66 my $auth = '';
67 my $serial = '';
68 my $source = 2;
69 my $x_source = 'default';
70 my $no_source_or_last_xact_id;
71 my $dbh = Mig::db_connect();
72 my $infile;
73 my $i = 0;
74 my $batch;
75 binmode STDIN, ':utf8';
76
77 my $ret = GetOptions(
78     'file:s'              => \$infile,
79     'serial:s'            => \$serial,
80     'auth:s'              => \$auth,
81     'x_source:s'          => \$x_source,
82     'source:i'            => \$source,
83     'base_table:s'        => \$base_table,
84     'stage_table:s'       => \$stage_table,
85     'marc_column:s'       => \$marc_column,
86     'no_source_or_last_xact_id' => \$no_source_or_last_xact_id
87 );
88
89 #if in file is empty then fail
90 #if auth and serial = 1 fail 
91
92 if ($serial == 1) { 
93     $base_table = 'm_authority_record_entry';
94 }
95
96 if ($auth == 1) {
97     $base_table = 'm_serial_record_entry';
98 }
99
100 if ($auth == 1 and $serial == 1) { abort('are you sure you want to load these as authorities and serials?'); }
101
102 if (!$base_table) {
103     $base_table = 'm_biblio_record_entry';
104 }
105
106 if (!$stage_table) {
107     $stage_table = $base_table . '_legacy';
108 }
109
110 my $bre_test = check_for_table($dbh,$base_table);
111 my $bre_legacy_test = check_for_table($dbh,$stage_table);
112 if ($bre_test == 0 and $bre_legacy_test == 0 ) { create_bre($dbh); create_child_bre($dbh); }
113 if ($bre_test == 1 and $bre_legacy_test == 0 ) { create_child_bre($dbh); }
114
115 my $xmig_test = check_for_column($dbh,$stage_table,'x_migrate');
116 if ($xmig_test == 0) { add_column($dbh,$stage_table,'x_migrate','BOOLEAN DEFAULT TRUE'); }
117
118 my $xx_source_test = check_for_column($dbh,$stage_table,'x_source');
119 if ($xx_source_test == 0) { add_column($dbh,$stage_table,'x_source','TEXT'); }
120
121 my $xmarc_test = check_for_column($dbh,$stage_table,$marc_column);
122 if ($xmarc_test == 0) { add_column($dbh,$stage_table,$marc_column,'TEXT'); }
123
124
125 #flatten out MARC XML FILE
126 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
127 $i = 0;
128 my $record = '';
129 while(my $line = <$xml>) {
130         if ($line =~ /^<\/?collection/) { next; }
131         chomp $line;
132         $record = $record . $line;
133         if ($line =~ /<\/record>$/) {
134                 stage_record($dbh,$record,$x_source,$source); 
135                 $record = '';
136                 $i++;
137                 if (($i % 100) == 0) { report_progress('Records stage', $i); }
138         }
139 }
140 close $xml;
141
142 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
143 print "Finis.\n";
144
145 # beyond here be functions 
146
147 sub create_bre {
148     my $dbh = shift;
149     $dbh->do("DO \$\$ 
150         DECLARE
151             t   BOOLEAN;
152         BEGIN
153         SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$base_table') INTO t;
154         IF t = FALSE THEN
155             PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA',REGEXP_REPLACE('$base_table','_','.'));
156         END IF;
157         END \$\$;");
158
159     return ();
160 }
161
162 sub create_child_bre {
163     my $dbh = shift;
164     $dbh->do("DO \$\$ 
165         BEGIN
166         CREATE TABLE $MIGSCHEMA.$stage_table (x_migrate BOOLEAN DEFAULT TRUE, x_source TEXT) INHERITS ($MIGSCHEMA.$base_table);
167         END \$\$;");
168
169     return ();
170 }
171
172 sub abort {
173     my $msg = shift;
174     print STDERR "$0: $msg", "\n";
175     exit 1;
176 }
177
178 sub report_progress {
179     my ($msg, $counter) = @_;
180     if (defined $counter) {
181         print STDERR "$msg: $counter\n";
182     } else {
183         print STDERR "$msg\n";
184     }
185 }
186
187 sub stage_record {
188     my $dbh = shift;
189     my $record = shift;
190         my $x_source = shift;
191     my $source = shift;
192         my $last_xact = "'$MIGSCHEMA'";
193         $record = '$_$' . $record . '$_$';
194         my $sql;
195     if ($no_source_or_last_xact_id) {
196         $sql = "INSERT INTO $MIGSCHEMA.$stage_table ($marc_column) VALUES ($record);";
197     } else {
198         if ($x_source eq 'default') {
199             $sql = "INSERT INTO $MIGSCHEMA.$stage_table (last_xact_id,$marc_column,source) VALUES ($last_xact,$record,$source);";
200         } else {
201             $sql = "INSERT INTO $MIGSCHEMA.$stage_table (last_xact_id,$marc_column,x_source,source) VALUES ($last_xact,$record,'$x_source',$source);";
202         }
203     }
204     my $sth = $dbh->prepare($sql);
205     $sth->execute();
206         return;
207 }
208
209 sub check_for_table {
210     my $dbh = shift;
211     my $table = shift;
212     my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
213     my $sth = $dbh->prepare($sql);
214     $sth->execute();
215     my @sqlresult = $sth->fetchrow_array;
216     my $r = pop @sqlresult;
217     if ($r) { return $r; } else { return 0; }
218 }
219
220 sub check_for_column {
221     my $dbh = shift;
222     my $table = shift;
223         my $column = shift;
224     my $sql = "SELECT 1 FROM information_schema.columns WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table' AND column_name = '$column';";
225     my $sth = $dbh->prepare($sql);
226     $sth->execute();
227     my @sqlresult = $sth->fetchrow_array;
228     my $r = pop @sqlresult;
229     if ($r) { return $r; } else { return 0; }
230 }
231
232 sub add_column {
233     my $dbh = shift;
234     my $table = shift;
235     my $column = shift;
236         my $column_type = shift;
237     my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
238     my $sth = $dbh->prepare($sql);
239     $sth->execute();
240     my @sqlresult = $sth->fetchrow_array;
241         my $r = check_for_column($dbh,$table,$column);
242         if ($r == 0) { abort('failed to create column'); } else { return $r; }
243 }
244