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,
75 'captions:s' => \$captions,
79 #if in file is empty then fail
80 #if auth and serial = 1 fail
83 $base_table = 'authority_record_entry';
84 $stage_table = 'authority_record_entry_legacy';
88 $base_table = 'serial_record_entry';
89 $stage_table = 'serial_record_entry_legacy';
92 if ($auth == 1 and $serial == 1) { abort('are you sure you want to load these as authorities and serials?'); }
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); }
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'); }
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'); }
105 #flatten out MARC XML FILE
106 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
109 while(my $line = <$xml>) {
110 if ($line =~ /^<\/?collection/) { next; }
112 $record = $record . $line;
113 if ($line =~ /<\/record>$/) {
114 stage_record($dbh,$record,$source);
117 if (($i % 100) == 0) { report_progress('Records stage', $i); }
122 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
125 # beyond here be functions
133 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$base_table') INTO t;
135 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA',REGEXP_REPLACE('$base_table','_','.'));
142 sub create_child_bre {
146 CREATE TABLE $MIGSCHEMA.$stage_table (x_migrate BOOLEAN DEFAULT TRUE, x_source TEXT) INHERITS ($MIGSCHEMA.$base_table);
154 print STDERR "$0: $msg", "\n";
158 sub report_progress {
159 my ($msg, $counter) = @_;
160 if (defined $counter) {
161 print STDERR "$msg: $counter\n";
163 print STDERR "$msg\n";
171 my $last_xact = "'$MIGSCHEMA'";
172 $record = '$_$' . $record . '$_$';
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);
181 sub check_for_table {
184 my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
185 my $sth = $dbh->prepare($sql);
187 my @sqlresult = $sth->fetchrow_array;
188 my $r = pop @sqlresult;
189 if ($r) { return $r; } else { return 0; }
192 sub check_for_column {
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);
199 my @sqlresult = $sth->fetchrow_array;
200 my $r = pop @sqlresult;
201 if ($r) { return $r; } else { return 0; }
208 my $column_type = shift;
209 my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
210 my $sth = $dbh->prepare($sql);
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; }