tweaks to mig stagebibs
[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 $next_arg_is_base_table = 0;
52 my $next_arg_is_stage_table = 0;
53 my $base_table = 'biblio_record_entry';
54 my $stage_table = 'biblio_record_entry_legacy';
55 my $source = 'default';
56 my $file_is_xml = 0;
57 my $dbh = Mig::db_connect();
58 my $infile;
59 my $i = 0;
60 my $batch;
61 binmode STDIN, ':utf8';
62
63 foreach my $arg (@ARGV) {
64     if ($arg eq '--file') {
65         $next_arg_is_file = 1;
66         next;
67     }
68     if ($next_arg_is_file) {
69         $infile = $arg;
70         $next_arg_is_file = 0;
71         next;
72     }
73     if ($arg eq '--source') {
74         $next_arg_is_source = 1;
75         next;
76     }
77     if ($next_arg_is_source) {
78         $source = $arg;
79         $next_arg_is_source = 0;
80         next;
81     }
82     if ($arg eq '--base-table') {
83         $next_arg_is_base_table = 1;
84         next;
85     }
86     if ($next_arg_is_base_table) {
87         $base_table = $arg;
88         $next_arg_is_base_table = 0;
89         next;
90     }
91     if ($arg eq '--stage-table') {
92         $next_arg_is_stage_table = 1;
93         next;
94     }
95     if ($next_arg_is_stage_table) {
96         $stage_table = $arg;
97         $next_arg_is_stage_table = 0;
98         next;
99     }
100 }
101
102 my $bre_test = check_for_table($dbh,$base_table);
103 my $bre_legacy_test = check_for_table($dbh,$stage_table);
104 if ($bre_test == 0 and $bre_legacy_test == 0 ) { create_bre($dbh); create_child_bre($dbh); }
105 if ($bre_test == 1 and $bre_legacy_test == 0 ) { create_child_bre($dbh); }
106
107 my $xmig_test = check_for_column($dbh,$stage_table,'x_migrate');
108 if ($xmig_test == 0) { add_column($dbh,$stage_table,'x_migrate','BOOLEAN DEFAULT TRUE'); }
109
110 my $xsource_test = check_for_column($dbh,$stage_table,'x_source');
111 if ($xsource_test == 0) { add_column($dbh,$stage_table,'x_source','TEXT'); }
112
113 #flatten out MARC XML FILE
114 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
115 $i = 0;
116 my $record = '';
117 while(my $line = <$xml>) {
118         if ($line =~ /^<\/?collection/) { next; }
119         chomp $line;
120         $record = $record . $line;
121         if ($line =~ /<\/record>$/) {
122                 stage_record($dbh,$record,$source); 
123                 $record = '';
124                 $i++;
125                 if (($i % 100) == 0) { report_progress('Records stage', $i); }
126         }
127 }
128 close $xml;
129
130 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
131 print "Finis.\n";
132
133 # beyond here be functions 
134
135 sub create_bre {
136     my $dbh = shift;
137     $dbh->do("DO \$\$ 
138         DECLARE
139             t   BOOLEAN;
140         BEGIN
141         SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$base_table') INTO t;
142         IF t = FALSE THEN
143             PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA',REGEXP_REPLACE('$base_table','_','.'));
144         END IF;
145         END \$\$;");
146
147     return ();
148 }
149
150 sub create_child_bre {
151     my $dbh = shift;
152     $dbh->do("DO \$\$ 
153         BEGIN
154         CREATE TABLE $MIGSCHEMA.$stage_table (x_migrate BOOLEAN DEFAULT TRUE, x_source TEXT) INHERITS ($MIGSCHEMA.$base_table);
155         END \$\$;");
156
157     return ();
158 }
159
160 sub abort {
161     my $msg = shift;
162     print STDERR "$0: $msg", "\n";
163     exit 1;
164 }
165
166 sub report_progress {
167     my ($msg, $counter) = @_;
168     if (defined $counter) {
169         print STDERR "$msg: $counter\n";
170     } else {
171         print STDERR "$msg\n";
172     }
173 }
174
175 sub stage_record {
176     my $dbh = shift;
177     my $record = shift;
178         my $source = shift;
179         my $last_xact = "'$MIGSCHEMA'";
180         $record = '$_$' . $record . '$_$';
181         my $sql;
182         if ($source eq 'default') { $sql = "INSERT INTO $MIGSCHEMA.$stage_table (last_xact_id,marc) VALUES ($last_xact,$record);"; }
183                 else { $sql = "INSERT INTO $MIGSCHEMA.$stage_table (last_xact_id,marc,x_source) VALUES ($last_xact,$record,'$source');";  }
184     my $sth = $dbh->prepare($sql);
185     $sth->execute();
186         return;
187 }
188
189 sub check_for_table {
190     my $dbh = shift;
191     my $table = shift;
192     my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
193     my $sth = $dbh->prepare($sql);
194     $sth->execute();
195     my @sqlresult = $sth->fetchrow_array;
196     my $r = pop @sqlresult;
197     if ($r) { return $r; } else { return 0; }
198 }
199
200 sub check_for_column {
201     my $dbh = shift;
202     my $table = shift;
203         my $column = shift;
204     my $sql = "SELECT 1 FROM information_schema.columns WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table' AND column_name = '$column';";
205     my $sth = $dbh->prepare($sql);
206     $sth->execute();
207     my @sqlresult = $sth->fetchrow_array;
208     my $r = pop @sqlresult;
209     if ($r) { return $r; } else { return 0; }
210 }
211
212 sub add_column {
213     my $dbh = shift;
214     my $table = shift;
215     my $column = shift;
216         my $column_type = shift;
217     my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
218     my $sth = $dbh->prepare($sql);
219     $sth->execute();
220     my @sqlresult = $sth->fetchrow_array;
221         my $r = check_for_column($dbh,$table,$column);
222         if ($r == 0) { abort('failed to create column'); } else { return $r; }
223 }
224