aa290235ae41594c3b9f136da0115a80d52117f1
[migration-tools.git] / extract_loadset
1 #!/usr/bin/perl
2
3 # Copyright 2009-2012, Equinox Software, Inc.
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 use strict;
20 use warnings;
21 use open ':utf8';
22
23 use Getopt::Long;
24
25 my $conf  = {}; # configuration hashref
26 initialize($conf);
27
28 # build exclusion hash
29 open FP, '<', shift or die "Can't open matchset file: $!\n";
30 my %exclude = ();
31 while (<FP>) {
32     chomp;
33     my ($lead,$sub) = split /\t/;
34     $sub =~ s/\s//g; # any whitespace is extraneous
35     $exclude{$sub}  = 1 unless ($sub < $conf->{lowerbound});
36 }
37 close FP;
38
39 # strip exclusions from marcxml file
40 open MI, '<', $conf->{input} or die "Can't open input file: $!\n";
41 open MO, '>', $conf->{output} or die "Can't open output file: $!\n";
42 while (<MI>) {
43     m/tag="$conf->{tag}".+?<subfield code="$conf->{subfield}">(\d+)</;
44     if ($conf->{reverse}) {
45         print MO if     $exclude{$1};
46     } else {
47         print MO unless $exclude{$1};
48     }
49 }
50
51
52 sub initialize {
53     my ($c) = @_;
54     my @missing = ();
55
56     # set mode on existing filehandles
57     binmode(STDIN, ':utf8');
58
59     my $rc = GetOptions( $c,
60                          'lowerbound|l=i',
61                          'input|i=s',
62                          'output|o=s',
63                          'tag|t=i',
64                          'subfield|s=s',
65                          'reverse|r',
66                          'help|h',
67                        );
68     show_help() unless $rc;
69     show_help() if ($c->{help});
70
71     $conf->{tag} = $conf->{tag} || 903;
72     $conf->{subfield} = $conf->{subfield} || 'a';
73
74     my @keys = keys %{$c};
75     show_help() unless (@ARGV and @keys);
76     for my $key ('output', 'lowerbound', 'input')
77       { push @missing, $key unless $c->{$key} }
78     if (@missing) {
79         print "Required option: ", join(', ', @missing), " missing!\n";
80         show_help();
81     }
82 }
83
84 sub show_help {
85     print <<HELP;
86 Usage is: extract_loadset -l BOUND -i INPUTXML -o OUTPUTXML MATCHSET
87
88   --lowerbound  -l  Lowest record ID which will be included in the loadset
89   --input       -i  MARCXML input file
90   --output      -o  MARCXML output file
91   --tag         -t  MARC tag to use as identifier (default: 903)
92   --subfield    -s  Subfield of --tag argument (default: 'a')
93   --reverse     -r  Output subordinate bibs rather than lead bibs
94 HELP
95 exit;
96 }