a6840dec8a8d76f8d89559d409c26ba0a6d461df
[migration-tools.git] / mig-bin / mig-loadbibs
1 #!/usr/bin/perl
2
3 ###############################################################################
4 =pod
5
6 =item B<loadbibs> --stage_file foo.mrc 
7
8 Takes a load of bibs from a binary marc file and loads them into mig staging table 
9 of bibio_record_entry.
10
11 Takes these optional arguments:
12
13 --append 
14
15 When used it does not drop the staging table and instead adds onto it.  
16
17 --source
18
19 Sets an x_source value on the staging table to the one supplied instead of the 
20 default of none.
21
22 --xml 
23
24 By default the program assumes a USMARC file.  This flag will identify it as 
25 a MARCXML file instead.
26
27 =back
28
29 =cut
30
31 ###############################################################################
32
33 use strict;
34 use warnings;
35
36 use DBI;
37 use Data::Dumper;
38 use MARC::Record;
39 use MARC::Batch;
40 use MARC::File;
41 use MARC::File::XML;
42 use Env qw(
43     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
44     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
45 );
46 use Pod::Usage;
47 use Switch;
48 use Cwd 'abs_path';
49 use FindBin;
50 use UNIVERSAL;
51 use Unicode::Normalize;
52 my $mig_bin = "$FindBin::Bin/";
53 use lib "$FindBin::Bin/";
54 use Mig;
55
56 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
57 pod2usage(-verbose => 1) if ! $ARGV[1];
58
59 my $next_arg_is_file = 0;
60 my $append_is_false = 1;
61 my $next_arg_is_source = 0;
62 my $source = 'default';
63 my $file_is_xml = 0;
64 my $dbh = Mig::db_connect();
65 my $infile;
66 my $i = 0;
67 my $batch;
68 binmode STDIN, ':utf8';
69
70 foreach my $arg (@ARGV) {
71     if ($arg eq '--stage_file') {
72         $next_arg_is_file = 1;
73         next;
74     }
75     if ($next_arg_is_file) {
76         $infile = $arg;
77         $next_arg_is_file = 0;
78         next;
79     }
80     if ($arg eq '--source') {
81         $next_arg_is_source = 1;
82         next;
83     }
84     if ($next_arg_is_file) {
85         $source = $arg;
86         $next_arg_is_source = 0;
87         next;
88     }
89         if ($arg eq '--append') {
90                 $append_is_false = 0;
91                 next;
92         }
93     if ($arg eq '--xml') {
94         $file_is_xml = 1;
95         next;
96     }
97 }
98
99 if ($append_is_false) {
100         $dbh->do(qq{
101         DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_stage;
102         CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_stage (
103                 l_bib_id    TEXT,
104                         x_source        TEXT,
105                         x_warnings      TEXT,
106                 x_migrate   BOOLEAN DEFAULT TRUE
107         ) INHERITS ($MIGSCHEMA.biblio_record_entry);
108         });
109 }
110
111 if ($file_is_xml) {
112         $batch = MARC::Batch->new('XML',$infile);
113 } else {
114         $batch = MARC::Batch->new('USMARC',$infile);
115
116 $batch->strict_off();
117
118 while ( my $record = $batch->next() ) {
119         my $xml;
120         if ($file_is_xml) { $xml = $record; } 
121                 else { $xml = $record->as_xml_record(); } 
122         $i++;
123         $xml = clean_marc($xml);
124         $xml = '$_$' . $xml . '$_$';
125         my @warnings = $batch->warnings();
126         my $warning_string;
127         if (@warnings) { $warning_string = "'" . join(':',@warnings) . "'"; } else { $warning_string = "'none'"; }
128         my $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry_stage (marc,x_source,x_warnings) VALUES ($xml,$source,$warning_string);";
129     my $sth = $dbh->prepare($sql);
130     $sth->execute();
131         report_progress("Records staged", $i) if 0 != $i % 100;
132 }
133
134 $dbh->do(qq/
135     CREATE INDEX ${MIGSCHEMA}_biblio_record_entry_stage_idx ON
136         $MIGSCHEMA.biblio_record_entry_stage (id);
137 /);
138
139 print "Finis.\n";
140
141 sub clean_marc {
142     my $xml = shift;
143     $xml =~ s/\n//sog;
144     $xml =~ s/^<\?xml.+\?\s*>//go;
145     $xml =~ s/>\s+</></go;
146     $xml =~ s/\p{Cc}//go;
147     $xml = NFC($xml);
148     $xml =~ s/&(?!\S+;)/&amp;/gso;
149     $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
150     $xml =~ s/[\x00-\x1f]//go;
151     return $xml;
152 }
153
154
155 sub abort {
156     my $msg = shift;
157     print STDERR "$0: $msg", "\n";
158     exit 1;
159 }
160
161 sub report_progress {
162     my ($msg, $counter) = @_;
163     if (defined $counter) {
164         print STDERR "$msg: $counter\n";
165     } else {
166         print STDERR "$msg\n";
167     }
168 }