6 use Equinox::Migration::MapDrivenMARCXMLProc;
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},
32 mapfile => $c->{map} );
33 # open main holdings file
34 open HOLDINGS, '>', ($c->{prefix} . "-HOLDINGS.pg");
35 # open the files for multi mappings
37 open X, '>', ($c->{prefix} . "-HOLDINGS-privnotes.pg");
38 open Z, '>', ($c->{prefix} . "-HOLDINGS-pubnotes.pg");
41 while (my $rec = $m->parse_record) {
42 # for each holdings tag in the record...
43 for my $holdidx ( @{$rec->{tmap}{ $c->{holdings} }} ) {
44 my $tagid = $rec->{tags}[$holdidx]{tag};
46 print STDOUT "\r", $m->recno;
47 my @out = (); # clear the output buffer
48 push @out, $rec->{egid}; # slug in the egid first thing
49 print "BEGIN;\n\negid\t" if ($m->recno == 1);
51 # grab the unary mappings and slug 'em in
52 for my $sub ( sort keys %{$rec->{tags}[$holdidx]{uni}} ) {
53 push @out, $rec->{tags}[$holdidx]{uni}{$sub};
54 print "l_", $m->name($tagid, $sub),"\t"
58 for my $x (@{$rec->{tags}[$holdidx]{multi}{x}} ) {
59 print X $rec->{egid}, "\t",
60 $rec->{tags}[$holdidx]{uni}{ $c->{copyid} },
63 for my $z (@{$rec->{tags}[$holdidx]{multi}{z}} ) {
64 print Z $rec->{egid}, "\t",
65 $rec->{tags}[$holdidx]{uni}{ $c->{copyid} },
70 # now get everything else in the mapping
71 for my $othertag ( sort keys %{$rec->{tmap}} ) {
72 next if $othertag eq $c->{holdings}; # ignoring the holdings, o'course
73 my $idx = $rec->{tmap}{$othertag}[0]; # get index into tags struct
74 for my $sub ( sort keys %{$rec->{tags}[$idx]{uni}} ) {
75 push @out, $rec->{tags}[$idx]{uni}{$sub};
76 print "l_", $m->name($rec->{tags}[$idx]{tag}, $sub), "\t"
82 print "\n" if ($m->recno == 1);
83 print join("\t", @out);
92 #--------------------------
97 if ($c->{samplemap}) {
98 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
99 mapfile => $c->{samplemap});
100 } elsif ($c->{samplestr}) {
101 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
102 mapstring => $c->{samplestr});
104 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
106 print "Parsing records for sampling... ";
109 dump_sample_overview($c, $s) if $c->{sample};
110 dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
113 sub dump_sample_detail {
115 my $tags = $s->{data}{samp};
116 my $count = $s->{data}{rcnt};
117 my $scnt = $s->{data}{scnt};
119 open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
121 for my $tag (sort keys %{ $tags }) {
122 print ">>>>> TAG $tag\n\n";
123 for my $subkey (sort keys %{ $tags->{$tag} }) {
124 my $sub = $tags->{$tag}{$subkey};
125 print "|| $subkey | ", $sub->{value}, " | ",
126 $sub->{count}, "/", $sub->{tcnt}, " | ||\n";
131 open SCOUNT, '>', ($c->{prefix} . "-HOLDINGS-SUBCOUNTS.txt");
133 for my $tag (sort keys %{ $scnt }) {
134 print ">>>>> TAG $tag\n\n";
135 for my $len (sort keys %{ $scnt->{$tag} })
136 { print "|| $len | ", $scnt->{$tag}{$len}, " ||\n" }
142 sub dump_sample_overview {
144 my $tags = $s->{data}{tags};
145 my $count = $s->{data}{rcnt};
147 my @tagsbyname = sort keys %{$tags};
148 my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
150 open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
152 print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
153 print "FOUND TAGS (BY TAG) FOUND TAGS (BY COUNT)\n";
154 print "------------------------ --------------------------\n";
155 for my $i (0 .. @tagsbyname - 1) {
156 print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
157 $tags->{ $tagsbyname[$i] };
158 print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
160 print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
161 $tags->{ $tagsbycount[$i] };
162 print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
169 #--------------------------
175 # set mode on existing filehandles
176 binmode(STDIN, ':utf8');
178 my $rc = GetOptions( $c,
190 show_help() unless $rc;
191 show_help() if ($c->{help});
192 show_help("Nothing to do!")
193 unless ($c->{map} or $c->{sample} or $c->{samplemap} or $c->{samplestr});
194 show_help("map, holdings, and copyid must be specified together!")
195 if ($c->{map} and !$c->{holdings} and !$c->{copyid});
196 show_version() if $c->{version};
198 my @keys = keys %{$c};
199 for my $key ('prefix', 'marcfile')
200 { push @missing, $key unless $c->{$key} }
202 print "Required option: ", join(', ', @missing), " missing!\n";
211 print "\nERROR - $msg\n" if $msg;
214 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
217 --prefix -p Prefix string for output filenames
218 --marcfile -m MARCXML to use as source data
221 --sample -s Generate a report of all tags in the MARC data
222 --samplemap -sm Specify a E::M::STL map file which will be used to generate
223 subfield breakdown reports about specific tags in the MARC
225 --samplestr -ss As above, but with a one-liner map specified on the command
226 line as a string (e.g. '-ss "852 999"')
228 If --samplemap and --samplestr are both specified, --samplemap wins.
230 HOLDINGS EXTRACTION ARGUMENTS
231 --map E::M::SM map file which will be used to extract holdings data
232 from the input MARC file
233 --holdings -h Specifies actual holdings tag
234 --copyid -c Specifies subfield of holdings with unique copy identifier
236 Both these must be given together.
241 sub show_version { print "extract_holdings v$VERSION\n"; exit }