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/";
53 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
54 pod2usage(-verbose => 1) if ! $ARGV[1];
57 my $base_table = 'biblio_record_entry';
58 my $stage_table = 'biblio_record_entry_legacy';
61 my $source = 'default';
62 my $dbh = Mig::db_connect();
66 binmode STDIN, ':utf8';
70 'serial:s' => \$serial,
72 'source:s' => \$source,
73 'base_table:s' => \$base_table,
74 'stage_table:s' => \$stage_table
77 #if in file is empty then fail
78 #if auth and serial = 1 fail
81 $base_table = 'authority_record_entry';
82 $stage_table = 'authority_record_entry_legacy';
86 $base_table = 'serial_record_entry';
87 $stage_table = 'serial_record_entry_legacy';
90 if ($auth == 1 and $serial == 1) { abort('are you sure you want to load these as authorities and serials?'); }
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); }
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'); }
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'); }
103 #flatten out MARC XML FILE
104 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
107 while(my $line = <$xml>) {
108 if ($line =~ /^<\/?collection/) { next; }
110 $record = $record . $line;
111 if ($line =~ /<\/record>$/) {
112 stage_record($dbh,$record,$source);
115 if (($i % 100) == 0) { report_progress('Records stage', $i); }
120 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
123 # beyond here be functions
131 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$base_table') INTO t;
133 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA',REGEXP_REPLACE('$base_table','_','.'));
140 sub create_child_bre {
144 CREATE TABLE $MIGSCHEMA.$stage_table (x_migrate BOOLEAN DEFAULT TRUE, x_source TEXT) INHERITS ($MIGSCHEMA.$base_table);
152 print STDERR "$0: $msg", "\n";
156 sub report_progress {
157 my ($msg, $counter) = @_;
158 if (defined $counter) {
159 print STDERR "$msg: $counter\n";
161 print STDERR "$msg\n";
169 my $last_xact = "'$MIGSCHEMA'";
170 $record = '$_$' . $record . '$_$';
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);
179 sub check_for_table {
182 my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
183 my $sth = $dbh->prepare($sql);
185 my @sqlresult = $sth->fetchrow_array;
186 my $r = pop @sqlresult;
187 if ($r) { return $r; } else { return 0; }
190 sub check_for_column {
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);
197 my @sqlresult = $sth->fetchrow_array;
198 my $r = pop @sqlresult;
199 if ($r) { return $r; } else { return 0; }
206 my $column_type = shift;
207 my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
208 my $sth = $dbh->prepare($sql);
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; }