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