3 ###############################################################################
6 =item B<stagebibs> --file foo.mrc.xml
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.
12 Takes three optional arguments:
16 Sets an x_source value on the staging table to the one supplied instead of the
21 This will load bibs into the authority_record_entry_legacy.
25 This will load bibs into the serial_record_entry_legacy.
31 ###############################################################################
37 #binmode STDIN, ':bytes';
39 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
40 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
48 my $mig_bin = "$FindBin::Bin/";
49 use lib "$FindBin::Bin/";
52 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
53 pod2usage(-verbose => 1) if ! $ARGV[1];
55 my $next_arg_is_file = 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';
65 my $dbh = Mig::db_connect();
69 binmode STDIN, ':utf8';
71 foreach my $arg (@ARGV) {
72 if ($arg eq '--auth') {
73 $base_table = 'authority_record_entry';
74 $stage_table = 'authority_record_entry_legacy';
76 if ($arg eq '--serial') {
77 $base_table = 'serial_record_entry';
78 $stage_table = 'serial_record_entry_legacy';
80 if ($arg eq '--file') {
81 $next_arg_is_file = 1;
84 if ($next_arg_is_file) {
86 $next_arg_is_file = 0;
89 if ($arg eq '--source') {
90 $next_arg_is_source = 1;
93 if ($next_arg_is_source) {
95 $next_arg_is_source = 0;
98 if ($arg eq '--base-table') {
99 $next_arg_is_base_table = 1;
102 if ($next_arg_is_base_table) {
104 $next_arg_is_base_table = 0;
107 if ($arg eq '--stage-table') {
108 $next_arg_is_stage_table = 1;
111 if ($next_arg_is_stage_table) {
113 $next_arg_is_stage_table = 0;
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); }
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'); }
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'); }
129 #flatten out MARC XML FILE
130 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
133 while(my $line = <$xml>) {
134 if ($line =~ /^<\/?collection/) { next; }
136 $record = $record . $line;
137 if ($line =~ /<\/record>$/) {
138 stage_record($dbh,$record,$source);
141 if (($i % 100) == 0) { report_progress('Records stage', $i); }
146 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
149 # beyond here be functions
157 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$base_table') INTO t;
159 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA',REGEXP_REPLACE('$base_table','_','.'));
166 sub create_child_bre {
170 CREATE TABLE $MIGSCHEMA.$stage_table (x_migrate BOOLEAN DEFAULT TRUE, x_source TEXT) INHERITS ($MIGSCHEMA.$base_table);
178 print STDERR "$0: $msg", "\n";
182 sub report_progress {
183 my ($msg, $counter) = @_;
184 if (defined $counter) {
185 print STDERR "$msg: $counter\n";
187 print STDERR "$msg\n";
195 my $last_xact = "'$MIGSCHEMA'";
196 $record = '$_$' . $record . '$_$';
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);
205 sub check_for_table {
208 my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
209 my $sth = $dbh->prepare($sql);
211 my @sqlresult = $sth->fetchrow_array;
212 my $r = pop @sqlresult;
213 if ($r) { return $r; } else { return 0; }
216 sub check_for_column {
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);
223 my @sqlresult = $sth->fetchrow_array;
224 my $r = pop @sqlresult;
225 if ($r) { return $r; } else { return 0; }
232 my $column_type = shift;
233 my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
234 my $sth = $dbh->prepare($sql);
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; }