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:
17 Takes a numeric value and set the x_source of the bib record to that. Defaults to
18 2 which is local system.
22 Sets an x_source value on the staging table to the one supplied instead of the
27 This will load bibs into the authority_record_entry_legacy.
31 This will load bibs into the serial_record_entry_legacy.
37 ###############################################################################
43 #binmode STDIN, ':bytes';
45 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
46 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
54 my $mig_bin = "$FindBin::Bin/";
55 use lib "$FindBin::Bin/";
59 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
60 pod2usage(-verbose => 1) if ! $ARGV[1];
63 my $base_table = 'm_biblio_record_entry';
64 my $stage_table = 'm_biblio_record_entry_legacy';
68 my $x_source = 'default';
69 my $dbh = Mig::db_connect();
73 binmode STDIN, ':utf8';
77 'serial:s' => \$serial,
79 'x_source:s' => \$x_source,
80 'source:i' => \$source,
81 'base_table:s' => \$base_table,
82 'stage_table:s' => \$stage_table
85 #if in file is empty then fail
86 #if auth and serial = 1 fail
89 $base_table = 'm_authority_record_entry';
90 $stage_table = 'm_authority_record_entry_legacy';
94 $base_table = 'm_serial_record_entry';
95 $stage_table = 'm_serial_record_entry_legacy';
98 if ($auth == 1 and $serial == 1) { abort('are you sure you want to load these as authorities and serials?'); }
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); }
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'); }
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'); }
111 #flatten out MARC XML FILE
112 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
115 while(my $line = <$xml>) {
116 if ($line =~ /^<\/?collection/) { next; }
118 $record = $record . $line;
119 if ($line =~ /<\/record>$/) {
120 stage_record($dbh,$record,$x_source,$source);
123 if (($i % 100) == 0) { report_progress('Records stage', $i); }
128 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
131 # beyond here be functions
139 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$base_table') INTO t;
141 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA',REGEXP_REPLACE('$base_table','_','.'));
148 sub create_child_bre {
152 CREATE TABLE $MIGSCHEMA.$stage_table (x_migrate BOOLEAN DEFAULT TRUE, x_source TEXT) INHERITS ($MIGSCHEMA.$base_table);
160 print STDERR "$0: $msg", "\n";
164 sub report_progress {
165 my ($msg, $counter) = @_;
166 if (defined $counter) {
167 print STDERR "$msg: $counter\n";
169 print STDERR "$msg\n";
176 my $x_source = shift;
178 my $last_xact = "'$MIGSCHEMA'";
179 $record = '$_$' . $record . '$_$';
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);
188 sub check_for_table {
191 my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
192 my $sth = $dbh->prepare($sql);
194 my @sqlresult = $sth->fetchrow_array;
195 my $r = pop @sqlresult;
196 if ($r) { return $r; } else { return 0; }
199 sub check_for_column {
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);
206 my @sqlresult = $sth->fetchrow_array;
207 my $r = pop @sqlresult;
208 if ($r) { return $r; } else { return 0; }
215 my $column_type = shift;
216 my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
217 my $sth = $dbh->prepare($sql);
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; }