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];
65 my $marc_column = 'marc';
69 my $x_source = 'default';
70 my $no_source_or_last_xact_id;
71 my $dbh = EMig::db_connect();
75 binmode STDIN, ':utf8';
79 'serial:s' => \$serial,
81 'x_source:s' => \$x_source,
82 'source:i' => \$source,
83 'base_table:s' => \$base_table,
84 'stage_table:s' => \$stage_table,
85 'marc_column:s' => \$marc_column,
86 'no_source_or_last_xact_id' => \$no_source_or_last_xact_id
89 #if in file is empty then fail
90 #if auth and serial = 1 fail
93 $base_table = 'm_authority_record_entry';
97 $base_table = 'm_serial_record_entry';
100 if ($auth == 1 and $serial == 1) { abort('are you sure you want to load these as authorities and serials?'); }
103 $base_table = 'm_biblio_record_entry';
107 $stage_table = $base_table . '_legacy';
110 my $bre_test = check_for_table($dbh,$base_table);
111 my $bre_legacy_test = check_for_table($dbh,$stage_table);
112 if ($bre_test == 0 and $bre_legacy_test == 0 ) { create_bre($dbh); create_child_bre($dbh); }
113 if ($bre_test == 1 and $bre_legacy_test == 0 ) { create_child_bre($dbh); }
115 my $xmig_test = check_for_column($dbh,$stage_table,'x_migrate');
116 if ($xmig_test == 0) { add_column($dbh,$stage_table,'x_migrate','BOOLEAN DEFAULT TRUE'); }
118 my $xx_source_test = check_for_column($dbh,$stage_table,'x_source');
119 if ($xx_source_test == 0) { add_column($dbh,$stage_table,'x_source','TEXT'); }
121 my $xmarc_test = check_for_column($dbh,$stage_table,$marc_column);
122 if ($xmarc_test == 0) { add_column($dbh,$stage_table,$marc_column,'TEXT'); }
125 #flatten out MARC XML FILE
126 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
129 while(my $line = <$xml>) {
130 if ($line =~ /^<\/?collection/) { next; }
132 $record = $record . $line;
133 if ($line =~ /<\/record>$/) {
134 stage_record($dbh,$record,$x_source,$source);
137 if (($i % 100) == 0) { report_progress('Records stage', $i); }
142 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
145 # beyond here be functions
153 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$base_table') INTO t;
155 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA',REGEXP_REPLACE('$base_table','_','.'));
162 sub create_child_bre {
166 CREATE TABLE $MIGSCHEMA.$stage_table (x_migrate BOOLEAN DEFAULT TRUE, x_source TEXT) INHERITS ($MIGSCHEMA.$base_table);
174 print STDERR "$0: $msg", "\n";
178 sub report_progress {
179 my ($msg, $counter) = @_;
180 if (defined $counter) {
181 print STDERR "$msg: $counter\n";
183 print STDERR "$msg\n";
190 my $x_source = shift;
192 my $last_xact = "'$MIGSCHEMA'";
193 $record = '$_$' . $record . '$_$';
195 if ($no_source_or_last_xact_id) {
196 $sql = "INSERT INTO $MIGSCHEMA.$stage_table ($marc_column) VALUES ($record);";
198 if ($x_source eq 'default') {
199 $sql = "INSERT INTO $MIGSCHEMA.$stage_table (last_xact_id,$marc_column,source) VALUES ($last_xact,$record,$source);";
201 $sql = "INSERT INTO $MIGSCHEMA.$stage_table (last_xact_id,$marc_column,x_source,source) VALUES ($last_xact,$record,'$x_source',$source);";
204 my $sth = $dbh->prepare($sql);
209 sub check_for_table {
212 my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
213 my $sth = $dbh->prepare($sql);
215 my @sqlresult = $sth->fetchrow_array;
216 my $r = pop @sqlresult;
217 if ($r) { return $r; } else { return 0; }
220 sub check_for_column {
224 my $sql = "SELECT 1 FROM information_schema.columns WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table' AND column_name = '$column';";
225 my $sth = $dbh->prepare($sql);
227 my @sqlresult = $sth->fetchrow_array;
228 my $r = pop @sqlresult;
229 if ($r) { return $r; } else { return 0; }
236 my $column_type = shift;
237 my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
238 my $sth = $dbh->prepare($sql);
240 my @sqlresult = $sth->fetchrow_array;
241 my $r = check_for_column($dbh,$table,$column);
242 if ($r == 0) { abort('failed to create column'); } else { return $r; }