3 ###############################################################################
6 =item B<stagebibs> --file foo.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. This is done with no checking of file validity
10 so records should be checked before hand and cleaned.
12 Takes one optional arguments:
16 Sets an x_source value on the staging table to the one supplied instead of the
23 ###############################################################################
29 #binmode STDIN, ':bytes';
31 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
32 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
40 my $mig_bin = "$FindBin::Bin/";
41 use lib "$FindBin::Bin/";
44 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
45 pod2usage(-verbose => 1) if ! $ARGV[1];
47 my $next_arg_is_file = 0;
49 my $next_arg_is_source = 0;
50 my $next_arg_is_stage = 0;
51 my $stage_table = 'biblio_record_entry';
52 my $source = 'default';
54 my $dbh = Mig::db_connect();
58 binmode STDIN, ':utf8';
60 foreach my $arg (@ARGV) {
61 if ($arg eq '--file') {
62 $next_arg_is_file = 1;
65 if ($next_arg_is_file) {
67 $next_arg_is_file = 0;
70 if ($arg eq '--source') {
71 $next_arg_is_source = 1;
74 if ($next_arg_is_source) {
76 $next_arg_is_source = 0;
81 my $bre_test = check_for_table($dbh,'biblio_record_entry');
82 if ($bre_test == 0) { create_child_bre($dbh); }
84 my $xmig_test = check_for_column($dbh,'biblio_record_entry','x_migrate');
85 if ($xmig_test == 0) { add_column($dbh,'biblio_record_entry','x_migrate','BOOLEAN DEFAULT TRUE'); }
87 my $xsource_test = check_for_column($dbh,'biblio_record_entry','x_source');
88 if ($xsource_test == 0) { add_column($dbh,'biblio_record_entry','x_source','TEXT'); }
90 #flatten out MARC XML FILE
91 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
94 while(my $line = <$xml>) {
95 if ($line =~ /^<\/?collection/) { next; }
97 $record = $record . $line;
98 if ($line =~ /<\/record>$/) {
99 stage_record($dbh,$record,$source);
102 if (($i % 100) == 0) { report_progress('Records stage', $i); }
109 # beyond here be functions
111 sub create_child_bre {
117 SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
119 PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
128 print STDERR "$0: $msg", "\n";
132 sub report_progress {
133 my ($msg, $counter) = @_;
134 if (defined $counter) {
135 print STDERR "$msg: $counter\n";
137 print STDERR "$msg\n";
145 my $last_xact = "'$MIGSCHEMA'";
146 $record = '$_$' . $record . '$_$';
148 if ($source eq 'default') { $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry (last_xact_id,marc) VALUES ($last_xact,$record);"; }
149 else { $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry (last_xact_id,marc,x_source) VALUES ($last_xact,$record,'$source');"; }
150 my $sth = $dbh->prepare($sql);
155 sub check_for_table {
158 my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
159 my $sth = $dbh->prepare($sql);
161 my @sqlresult = $sth->fetchrow_array;
162 my $r = pop @sqlresult;
163 if ($r) { return $r; } else { return 0; }
166 sub check_for_column {
170 my $sql = "SELECT 1 FROM information_schema.columns WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table' AND column_name = '$column';";
171 my $sth = $dbh->prepare($sql);
173 my @sqlresult = $sth->fetchrow_array;
174 my $r = pop @sqlresult;
175 if ($r) { return $r; } else { return 0; }
182 my $column_type = shift;
183 my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
184 my $sth = $dbh->prepare($sql);
186 my @sqlresult = $sth->fetchrow_array;
187 my $r = check_for_column($dbh,$table,$column);
188 if ($r == 0) { abort('failed to create column'); } else { return $r; }