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