cleanup before new work
[migration-tools.git] / extract_holdings
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use Getopt::Long;
6 use Equinox::Migration::MapDrivenMARCXMLProc;
7 use Equinox::Migration::MARCXMLSampler;
8
9 my $VERSION = '1.000';
10
11 my $c = initialize();
12 $| = 1;
13
14 # run samples if we've been asked for them
15 run_samples($c) if ($c->{sample} or $c->{samplemap} or $c->{samplestr});
16 extract_holdings($c) if ($c->{map});
17
18 #--------------------------
19
20 sub extract_holdings {
21     my ($c) = @_;
22     print "Parsing records for extraction... ";
23     my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( marcfile => $c->{marcfile},
24                                                            mapfile  => $c->{map} );
25     # open main holdings file
26     open HOLDINGS, '>', ($c->{prefix} . "-HOLDINGS.pg");
27     # open the files for multi mappings
28     # FIXME DO THIS
29     select HOLDINGS;
30
31     while (my $rec = $m->parse_record) {
32         # for each holdings tag in the record...
33         for my $holdidx ( @{$rec->{tmap}{ $c->{holdings} }} ) {
34             print STDOUT "\r", $m->recno;
35             my @out = ();            # clear the output buffer
36             push @out, $rec->{egid}; # slug in the egid first thing
37             print "BEGIN;\n\negid\t" if ($m->recno == 1);
38
39             # grab the unary mappings and slug 'em in
40             for my $sub ( sort keys %{$rec->{tags}[$holdidx]{uni}} ) {
41                 push @out, $rec->{tags}[$holdidx]{uni}{$sub};
42                 print "l_", $m->name($rec->{tags}[$holdidx]{tag}, $sub),"\t"
43                   if ($m->recno == 1);
44             }
45
46             # now get everything else in the mapping
47             for my $othertag ( sort keys %{$rec->{tmap}} ) {
48                 next if $othertag eq $c->{holdings};  # ignoring the holdings, o'course
49                 my $idx = $rec->{tmap}{$othertag}[0]; # get index into tags struct
50                 for my $sub ( sort keys %{$rec->{tags}[$idx]{uni}} ) {
51                     push @out, $rec->{tags}[$idx]{uni}{$sub};
52                     print "l_", $m->name($rec->{tags}[$idx]{tag}, $sub), "\t"
53                       if ($m->recno == 1);
54                 }
55             }
56
57             # and dump it
58             print "\n" if ($m->recno == 1);
59             print join("\t", @out);
60             print "\n";
61         }
62
63     }
64     select STDOUT;
65     print "\n";
66 }
67
68 #--------------------------
69
70 sub run_samples {
71     my ($c) = @_;
72     my $s;
73     if ($c->{samplemap}) {
74         $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
75                                                       mapfile  => $c->{samplemap});
76     } elsif ($c->{samplestr}) {
77         $s = Equinox::Migration::MARCXMLSampler->new( marcfile  => $c->{marcfile},
78                                                       mapstring => $c->{samplestr});
79     } else {
80         $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
81     }
82     print "Parsing records for sampling... ";
83     $s->parse_records;
84
85     dump_sample_overview($c, $s) if $c->{sample};
86     dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
87 }
88
89 sub dump_sample_detail {
90     my ($c, $s) = @_;
91     my $tags = $s->{data}{samp};
92     my $count = $s->{data}{rcnt};
93
94     open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
95     select DETAIL;
96     for my $tag (sort keys %{$tags}) {
97         print ">>>>> TAG $tag\n";
98         for my $subkey (sort keys %{$tags->{$tag}}) {
99             my $sub = $tags->{$tag}{$subkey};
100             print "  Subfield: $subkey\n";
101             print "  Sample:   '", $sub->{value}, "'\n";
102             print "  Count:    ", $sub->{count}, " in ", $sub->{tcnt}, " tags\n\n";
103             #print "(", int($sub->{count} / $sub->{rcnt}), "%)\n";
104         }
105     }
106     select STDOUT;
107     print "\n";
108     close DETAIL;
109 }
110
111 sub dump_sample_overview {
112     my ($c, $s) = @_;
113     my $tags = $s->{data}{tags};
114     my $count = $s->{data}{rcnt};
115
116     my @tagsbyname  = sort keys %{$tags};
117     my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
118
119     open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
120     select SAMPLE;
121     print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
122     print "FOUND TAGS (BY TAG)           FOUND TAGS (BY COUNT)\n";
123     print "------------------------      --------------------------\n";
124     for my $i (0 .. @tagsbyname - 1) {
125         print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
126           $tags->{ $tagsbyname[$i] };
127         print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
128         print "      ";
129         print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
130           $tags->{ $tagsbycount[$i] };
131         print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
132     }
133     select STDOUT;
134     print "\n";
135     close SAMPLE;
136 }
137
138 #--------------------------
139
140 sub initialize {
141     my $c = {};
142     my @missing = ();
143
144     # set mode on existing filehandles
145     binmode(STDIN, ':utf8');
146
147     my $rc = GetOptions( $c,
148                          'sample|s',
149                          'samplemap|sm=s',
150                          'samplestr|ss=s',
151                          'marcfile|m=s',
152                          'map=s',
153                          'holdings|h=i',
154                          'prefix|p=s',
155                          'version|v',
156                          'help',
157                        );
158     show_help() unless $rc;
159     show_help() if ($c->{help});
160     show_help("map and holdings must be specified together!")
161       if ($c->{map} and !$c->{holdings});
162     show_version() if $c->{version};
163
164     my @keys = keys %{$c};
165     for my $key ('prefix')
166       { push @missing, $key unless $c->{$key} }
167     if (@missing) {
168         print "Required option: ", join(', ', @missing), " missing!\n";
169         show_help();
170     }
171
172     return $c;
173 }
174
175 sub show_help {
176     my ($msg) = @_;
177     print "\nERROR - $msg\n" if $msg;
178     print <<HELP;
179
180 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
181
182 REQUIRED ARGUMENTS
183   --prefix   -p  Prefix string for output filenames
184   --marcfile -m  MARCXML to use as source data
185
186 SAMPLING ARGUMENTS
187   --sample    -s   Generate a report of all tags in the MARC data
188   --samplemap -sm  Specify a E::M::STL map file which will be used to generate
189                    subfield breakdown reports about specific tags in the MARC
190                    data
191   --samplestr -ss  As above, but with a one-liner map specified on the command
192                    line as a string (e.g. '-ss "852 999"')
193
194   If --samplemap and --samplestr are both specified, --samplemap wins.
195
196 HOLDINGS EXTRACTION ARGUMENTS
197   --map          E::M::SM map file which will be used to extract holdings data
198                  from the input MARC file
199   --holdings -h  Specifies actual holdings tag
200
201   Both these must be given together.
202 HELP
203     exit;
204 }
205
206 sub show_version { print "extract_holdings v$VERSION\n"; exit }