add config import/export logic to add new rows for config.rule_*
[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     print MO unless $exclude{$1};
45 }
46
47
48 sub initialize {
49     my ($c) = @_;
50     my @missing = ();
51
52     # set mode on existing filehandles
53     binmode(STDIN, ':utf8');
54
55     my $rc = GetOptions( $c,
56                          'lowerbound|l=i',
57                          'input|i=s',
58                          'output|o=s',
59                          'tag|t=i',
60                          'subfield|s=s',
61                          'help|h',
62                        );
63     show_help() unless $rc;
64     show_help() if ($c->{help});
65
66     $conf->{tag} = $conf->{tag} || 903;
67     $conf->{subfield} = $conf->{subfield} || 'a';
68
69     my @keys = keys %{$c};
70     show_help() unless (@ARGV and @keys);
71     for my $key ('output', 'lowerbound', 'input')
72       { push @missing, $key unless $c->{$key} }
73     if (@missing) {
74         print "Required option: ", join(', ', @missing), " missing!\n";
75         show_help();
76     }
77 }
78
79 sub show_help {
80     print <<HELP;
81 Usage is: extract_loadset -l BOUND -i INPUTXML -o OUTPUTXML MATCHSET
82
83   --lowerbound  -l  Lowest record ID which will be included in the loadset
84   --input       -i  MARCXML input file
85   --output      -o  MARCXML output file
86   --tag         -t  MARC tag to use as identifier (default: 903)
87   --subfield    -s  Subfield of --tag argument (default: 'a')
88 HELP
89 exit;
90 }