6 use Equinox::Migration::MapDrivenMARCXMLProc 1.003;
7 use Equinox::Migration::MARCXMLSampler;
15 * Have detail mode report on number of subfields per datafield
22 # run samples if we've been asked for them
23 run_samples($c) if ($c->{sample} or $c->{samplemap} or $c->{samplestr});
24 extract_holdings($c) if ($c->{map});
26 #--------------------------
28 sub extract_holdings {
30 print "Parsing records for extraction... ";
31 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( marcfile => $c->{marcfile},
35 print "Writing holdings to output file(s)...\n";
36 # open main holdings file
37 open HOLDINGS, '>', ($c->{prefix} . "-HOLDINGS.pg");
39 my $multis = $m->get_multis;
41 for my $t ( keys %{$multis} ) {
42 for my $s ( keys %{$multis->{$t}})
43 { open my $fh, ">", ($c->{prefix} . "-HOLDINGS-MULT-$t$s.pg"); $MULTIFILE{"$t$s"} = $fh }
46 my $i = 0; # record counter
47 my $j = 0; # holdings counter
49 while ( $m->{data}{recs}[$i] ) {
50 print HOLDINGS "BEGIN;\n\negid\thseq\t" unless $j;
51 my $rec = $m->{data}{recs}[$i];
53 # for each holdings tag in the record...
54 for my $holdidx ( @{$rec->{tmap}{ $c->{holdings} }} ) {
55 my $tagid = $rec->{tags}[$holdidx]{tag};
57 my @out = (); # clear the output buffer
58 push @out, $rec->{egid}; # slug in the egid first thing
59 push @out, $j; # holding seq goes next
61 # grab the unary mappings and slug 'em in
62 for my $sub ( sort keys %{$rec->{tags}[$holdidx]{uni}} ) {
63 push @out, $rec->{tags}[$holdidx]{uni}{$sub};
64 print HOLDINGS "l_", $m->name($tagid, $sub),"\t" unless $j;
67 # handle holdings multis
68 for my $sub ( sort keys %{$multis->{$tagid}} ) {
69 for my $value ( @{$rec->{tags}[$holdidx]{multi}{$sub}} ) {
70 my $fh = $MULTIFILE{"$tagid$sub"};
71 print $fh join("\t", $rec->{egid}, $j, $value), "\n";
76 # now get everything else in the mapping
77 for my $othertag ( sort keys %{$rec->{tmap}} ) {
78 next if $othertag eq $c->{holdings}; # ignoring the holdings, o'course
79 my $idx = $rec->{tmap}{$othertag}[0]; # get index into tags struct
80 for my $sub ( sort keys %{$rec->{tags}[$idx]{uni}} ) {
81 push @out, $rec->{tags}[$idx]{uni}{$sub};
82 print "l_", $m->name($rec->{tags}[$idx]{tag}, $sub), "\t"
88 print HOLDINGS "\n" unless $j;
89 print HOLDINGS join("\t", @out);
99 #--------------------------
104 print "Parsing records for sampling... ";
105 if ($c->{samplemap}) {
106 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
107 mapfile => $c->{samplemap});
108 } elsif ($c->{samplestr}) {
109 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
110 mapstring => $c->{samplestr});
112 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
115 dump_sample_overview($c, $s) if $c->{sample};
116 dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
119 sub dump_sample_detail {
121 my $tags = $s->{data}{samp};
122 my $count = $s->{data}{rcnt};
123 my $scnt = $s->{data}{scnt};
125 open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
127 for my $tag (sort keys %{ $tags }) {
128 print ">>>>> TAG $tag\n\n";
129 for my $subkey (sort keys %{ $tags->{$tag} }) {
130 my $sub = $tags->{$tag}{$subkey};
131 print "|| $subkey | ", $sub->{value}, " | ",
132 $sub->{count}, "/", $sub->{tcnt}, " | ||\n";
137 open SCOUNT, '>', ($c->{prefix} . "-HOLDINGS-SUBCOUNTS.txt");
139 for my $tag (sort keys %{ $scnt }) {
140 print ">>>>> TAG $tag\n\n";
141 for my $len (sort keys %{ $scnt->{$tag} })
142 { print "|| $len | ", $scnt->{$tag}{$len}, " ||\n" }
148 sub dump_sample_overview {
150 my $tags = $s->{data}{tags};
151 my $count = $s->{data}{rcnt};
153 my @tagsbyname = sort keys %{$tags};
154 my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
156 open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
158 print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
159 print "FOUND TAGS (BY TAG) FOUND TAGS (BY COUNT)\n";
160 print "------------------------ --------------------------\n";
161 for my $i (0 .. @tagsbyname - 1) {
162 print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
163 $tags->{ $tagsbyname[$i] };
164 print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
166 print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
167 $tags->{ $tagsbycount[$i] };
168 print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
175 #--------------------------
181 # set mode on existing filehandles
182 binmode(STDIN, ':utf8');
184 my $rc = GetOptions( $c,
196 show_help() unless $rc;
197 show_help() if ($c->{help});
198 show_help("Nothing to do!")
199 unless ($c->{map} or $c->{sample} or $c->{samplemap} or $c->{samplestr});
200 show_help("map, holdings, and copyid must be specified together!")
201 if ($c->{map} and !($c->{holdings} and $c->{copyid}));
202 show_version() if $c->{version};
204 my @keys = keys %{$c};
205 for my $key ('prefix', 'marcfile')
206 { push @missing, $key unless $c->{$key} }
208 print "Required option: ", join(', ', @missing), " missing!\n";
217 print "\nERROR - $msg\n" if $msg;
220 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
223 --prefix -p Prefix string for output filenames
224 --marcfile -m MARCXML to use as source data
227 --sample -s Generate a report of all tags in the MARC data
228 --samplemap -sm Specify a E::M::STL map file which will be used to generate
229 subfield breakdown reports about specific tags in the MARC
231 --samplestr -ss As above, but with a one-liner map specified on the command
232 line as a string (e.g. '-ss "852 999"')
234 If --samplemap and --samplestr are both specified, --samplemap wins.
236 HOLDINGS EXTRACTION ARGUMENTS
237 --map E::M::SM map file which will be used to extract holdings data
238 from the input MARC file
239 --holdings -h Specifies actual holdings tag
240 --copyid -c Specifies subfield of holdings with unique copy identifier
242 Both these must be given together.
247 sub show_version { print "extract_holdings v$VERSION\n"; exit }