preserve dates we can't parse; we'll punt to postgres
[migration-tools.git] / dump_inverse_select_marc.pl
1 #!/usr/bin/perl
2 use MARC::Batch;
3 use MARC::Record;
4 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
5 use MARC::Field;
6
7 my $format = $ARGV[0];
8
9 my $record_id_file = $ARGV[1];
10 my %record_ids;
11
12 open FILE, $record_id_file;
13 while (my $record_id = <FILE>) {
14     chomp($record_id); $record_ids{ $record_id } = 1;
15 }
16 close FILE;
17
18 my $id_tag = $ARGV[2]; my $id_subfield = $ARGV[3];
19
20 binmode(STDOUT, ':utf8');
21 binmode(STDIN, ':utf8');
22
23 foreach $argnum ( 4 .. $#ARGV ) {
24
25         print STDERR "Processing " . $ARGV[$argnum] . "\n";
26
27         my $batch = MARC::Batch->new('XML',$ARGV[$argnum]);
28         $batch->strict_off();
29         $batch->warnings_off();
30
31     my $count = 0;
32
33         while ( my $record = $batch->next() ) {
34
35         $count++;
36
37                 my $id = $record->field($id_tag);
38                 if (!$id) {
39                         print STDERR "ERROR: This record is missing a $id_tag field.\n" . $record->as_formatted() . "\n=====\n";
40                         next;
41                 }
42                 $id = $id->as_string($id_subfield);
43
44         if (! defined $record_ids{ $id }) {
45             if ($format eq 'text') {
46                 print STDOUT '=-' x 39 . "\n";
47                 print STDOUT $record->as_formatted() . "\n";
48             } else {
49                 print STDOUT $record->as_xml() . "\n";
50             }
51         }
52         }
53     print STDERR "Processed $count records.\n";
54 }