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