6 use Equinox::Migration::MapDrivenMARCXMLProc;
7 use Equinox::Migration::MARCXMLSampler;
13 # run samples if we've been asked for them
14 run_samples($c) if ($c->{sample} or $c->{samplemap} or $c->{samplestr});
15 extract_holdings($c) if ($c->{map});
17 #--------------------------
19 sub extract_holdings {
21 print "Parsing records for extraction... ";
22 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( marcfile => $c->{marcfile},
23 mapfile => $c->{map} );
24 # open main holdings file
25 open HOLDINGS, '>', ($c->{prefix} . "-HOLDINGS.pg");
26 # open the files for multi mappings
30 while (my $rec = $m->parse_record) {
31 # for each holdings tag in the record...
32 for my $holdidx ( @{$rec->{tmap}{ $c->{holdings} }} ) {
33 print STDOUT "\r", $m->recno;
34 my @out = (); # clear the output buffer
35 push @out, $rec->{egid}; # slug in the egid first thing
36 print "BEGIN;\n\negid\t" if ($m->recno == 1);
38 # grab the unary mappings and slug 'em in
39 for my $sub ( sort keys %{$rec->{tags}[$holdidx]{uni}} ) {
40 push @out, $rec->{tags}[$holdidx]{uni}{$sub};
41 print "l_", $m->name($rec->{tags}[$holdidx]{tag}, $sub),"\t"
45 # now get everything else in the mapping
46 for my $othertag ( sort keys %{$rec->{tmap}} ) {
47 next if $othertag eq $c->{holdings}; # ignoring the holdings, o'course
48 my $idx = $rec->{tmap}{$othertag}[0]; # get index into tags struct
49 for my $sub ( sort keys %{$rec->{tags}[$idx]{uni}} ) {
50 push @out, $rec->{tags}[$idx]{uni}{$sub};
51 print "l_", $m->name($rec->{tags}[$idx]{tag}, $sub), "\t"
57 print "\n" if ($m->recno == 1);
58 print join("\t", @out);
67 #--------------------------
72 if ($c->{samplemap}) {
73 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
74 mapfile => $c->{samplemap});
75 } elsif ($c->{samplestr}) {
76 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
77 mapstring => $c->{samplestr});
79 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
81 print "Parsing records for sampling... ";
84 dump_sample_overview($c, $s) if $c->{sample};
85 dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
88 sub dump_sample_detail {
90 my $tags = $s->{data}{samp};
91 my $count = $s->{data}{rcnt};
93 open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
95 for my $tag (sort keys %{$tags}) {
96 print ">>>>> TAG $tag\n";
97 for my $subkey (sort keys %{$tags->{$tag}}) {
98 my $sub = $tags->{$tag}{$subkey};
99 print " Subfield: $subkey\n";
100 print " Sample: '", $sub->{value}, "'\n";
101 print " Count: ", $sub->{count}, " in ", $sub->{tcnt}, " tags\n\n";
102 #print "(", int($sub->{count} / $sub->{rcnt}), "%)\n";
110 sub dump_sample_overview {
112 my $tags = $s->{data}{tags};
113 my $count = $s->{data}{rcnt};
115 my @tagsbyname = sort keys %{$tags};
116 my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
118 open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
120 print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
121 print "FOUND TAGS (BY TAG) FOUND TAGS (BY COUNT)\n";
122 print "------------------------ --------------------------\n";
123 for my $i (0 .. @tagsbyname - 1) {
124 print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
125 $tags->{ $tagsbyname[$i] };
126 print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
128 print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
129 $tags->{ $tagsbycount[$i] };
130 print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
137 #--------------------------
143 # set mode on existing filehandles
144 binmode(STDIN, ':utf8');
146 my $rc = GetOptions( $c,
156 show_help() unless $rc;
157 show_help() if ($c->{help});
158 show_help("map and holdings must be specified together!")
159 if ($c->{map} and !$c->{holdings});
161 my @keys = keys %{$c};
162 for my $key ('prefix')
163 { push @missing, $key unless $c->{$key} }
165 print "Required option: ", join(', ', @missing), " missing!\n";
174 print "\nERROR - $msg\n" if $msg;
177 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
180 --prefix -p Prefix string for output filenames
181 --marcfile -m MARCXML to use as source data
184 --sample -s Generate a report of all tags in the MARC data
185 --samplemap -sm Specify a E::M::STL map file which will be used to generate
186 subfield breakdown reports about specific tags in the MARC
188 --samplestr -ss As above, but with a one-liner map specified on the command
189 line as a string (e.g. '-ss "852 999"')
191 If --samplemap and --samplestr are both specified, --samplemap wins.
193 HOLDINGS EXTRACTION ARGUMENTS
194 --map E::M::SM map file which will be used to extract holdings data
195 from the input MARC file
196 --holdings -h Specifies actual holdings tag
198 Both these must be given together.