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