improved docs for mig-stagebibs
[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 --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 --auth foo.mrc.xml
20
21 This will load bibs into the authority_record_entry_legacy.
22
23 --serial foo.mrc.xml
24
25 This will load bibs into the serial_record_entry_legacy.
26
27 =back
28
29 =cut
30
31 ###############################################################################
32
33 use strict;
34 use warnings;
35
36 use DBI;
37 #binmode STDIN, ':bytes';
38 use Env qw(
39     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
40     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
41 );
42 use Data::Dumper;
43 use Pod::Usage;
44 use Switch;
45 use Cwd 'abs_path';
46 use FindBin;
47 use UNIVERSAL;
48 my $mig_bin = "$FindBin::Bin/";
49 use lib "$FindBin::Bin/";
50 use Mig;
51
52 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
53 pod2usage(-verbose => 1) if ! $ARGV[1];
54
55 my $next_arg_is_file = 0;
56 my $append = 0;
57 my $next_arg_is_source = 0;
58 my $next_arg_is_stage = 0;
59 my $next_arg_is_base_table = 0;
60 my $next_arg_is_stage_table = 0;
61 my $base_table = 'biblio_record_entry';
62 my $stage_table = 'biblio_record_entry_legacy';
63 my $source = 'default';
64 my $file_is_xml = 0;
65 my $dbh = Mig::db_connect();
66 my $infile;
67 my $i = 0;
68 my $batch;
69 binmode STDIN, ':utf8';
70
71 foreach my $arg (@ARGV) {
72     if ($arg eq '--auth') {
73         $base_table = 'authority_record_entry';
74         $stage_table = 'authority_record_entry_legacy';
75     }
76     if ($arg eq '--serial') {
77         $base_table = 'serial_record_entry';
78         $stage_table = 'serial_record_entry_legacy';
79     }
80     if ($arg eq '--file') {
81         $next_arg_is_file = 1;
82         next;
83     }
84     if ($next_arg_is_file) {
85         $infile = $arg;
86         $next_arg_is_file = 0;
87         next;
88     }
89     if ($arg eq '--source') {
90         $next_arg_is_source = 1;
91         next;
92     }
93     if ($next_arg_is_source) {
94         $source = $arg;
95         $next_arg_is_source = 0;
96         next;
97     }
98     if ($arg eq '--base-table') {
99         $next_arg_is_base_table = 1;
100         next;
101     }
102     if ($next_arg_is_base_table) {
103         $base_table = $arg;
104         $next_arg_is_base_table = 0;
105         next;
106     }
107     if ($arg eq '--stage-table') {
108         $next_arg_is_stage_table = 1;
109         next;
110     }
111     if ($next_arg_is_stage_table) {
112         $stage_table = $arg;
113         $next_arg_is_stage_table = 0;
114         next;
115     }
116 }
117
118 my $bre_test = check_for_table($dbh,$base_table);
119 my $bre_legacy_test = check_for_table($dbh,$stage_table);
120 if ($bre_test == 0 and $bre_legacy_test == 0 ) { create_bre($dbh); create_child_bre($dbh); }
121 if ($bre_test == 1 and $bre_legacy_test == 0 ) { create_child_bre($dbh); }
122
123 my $xmig_test = check_for_column($dbh,$stage_table,'x_migrate');
124 if ($xmig_test == 0) { add_column($dbh,$stage_table,'x_migrate','BOOLEAN DEFAULT TRUE'); }
125
126 my $xsource_test = check_for_column($dbh,$stage_table,'x_source');
127 if ($xsource_test == 0) { add_column($dbh,$stage_table,'x_source','TEXT'); }
128
129 #flatten out MARC XML FILE
130 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
131 $i = 0;
132 my $record = '';
133 while(my $line = <$xml>) {
134         if ($line =~ /^<\/?collection/) { next; }
135         chomp $line;
136         $record = $record . $line;
137         if ($line =~ /<\/record>$/) {
138                 stage_record($dbh,$record,$source); 
139                 $record = '';
140                 $i++;
141                 if (($i % 100) == 0) { report_progress('Records stage', $i); }
142         }
143 }
144 close $xml;
145
146 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
147 print "Finis.\n";
148
149 # beyond here be functions 
150
151 sub create_bre {
152     my $dbh = shift;
153     $dbh->do("DO \$\$ 
154         DECLARE
155             t   BOOLEAN;
156         BEGIN
157         SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$base_table') INTO t;
158         IF t = FALSE THEN
159             PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA',REGEXP_REPLACE('$base_table','_','.'));
160         END IF;
161         END \$\$;");
162
163     return ();
164 }
165
166 sub create_child_bre {
167     my $dbh = shift;
168     $dbh->do("DO \$\$ 
169         BEGIN
170         CREATE TABLE $MIGSCHEMA.$stage_table (x_migrate BOOLEAN DEFAULT TRUE, x_source TEXT) INHERITS ($MIGSCHEMA.$base_table);
171         END \$\$;");
172
173     return ();
174 }
175
176 sub abort {
177     my $msg = shift;
178     print STDERR "$0: $msg", "\n";
179     exit 1;
180 }
181
182 sub report_progress {
183     my ($msg, $counter) = @_;
184     if (defined $counter) {
185         print STDERR "$msg: $counter\n";
186     } else {
187         print STDERR "$msg\n";
188     }
189 }
190
191 sub stage_record {
192     my $dbh = shift;
193     my $record = shift;
194         my $source = shift;
195         my $last_xact = "'$MIGSCHEMA'";
196         $record = '$_$' . $record . '$_$';
197         my $sql;
198         if ($source eq 'default') { $sql = "INSERT INTO $MIGSCHEMA.$stage_table (last_xact_id,marc) VALUES ($last_xact,$record);"; }
199                 else { $sql = "INSERT INTO $MIGSCHEMA.$stage_table (last_xact_id,marc,x_source) VALUES ($last_xact,$record,'$source');";  }
200     my $sth = $dbh->prepare($sql);
201     $sth->execute();
202         return;
203 }
204
205 sub check_for_table {
206     my $dbh = shift;
207     my $table = shift;
208     my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
209     my $sth = $dbh->prepare($sql);
210     $sth->execute();
211     my @sqlresult = $sth->fetchrow_array;
212     my $r = pop @sqlresult;
213     if ($r) { return $r; } else { return 0; }
214 }
215
216 sub check_for_column {
217     my $dbh = shift;
218     my $table = shift;
219         my $column = shift;
220     my $sql = "SELECT 1 FROM information_schema.columns WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table' AND column_name = '$column';";
221     my $sth = $dbh->prepare($sql);
222     $sth->execute();
223     my @sqlresult = $sth->fetchrow_array;
224     my $r = pop @sqlresult;
225     if ($r) { return $r; } else { return 0; }
226 }
227
228 sub add_column {
229     my $dbh = shift;
230     my $table = shift;
231     my $column = shift;
232         my $column_type = shift;
233     my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
234     my $sth = $dbh->prepare($sql);
235     $sth->execute();
236     my @sqlresult = $sth->fetchrow_array;
237         my $r = check_for_column($dbh,$table,$column);
238         if ($r == 0) { abort('failed to create column'); } else { return $r; }
239 }
240