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