6 use Equinox::Migration::MapDrivenMARCXMLProc;
7 use Equinox::Migration::MARCXMLSampler;
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});
18 #--------------------------
20 sub extract_holdings {
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
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);
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"
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"
58 print "\n" if ($m->recno == 1);
59 print join("\t", @out);
68 #--------------------------
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});
80 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
82 print "Parsing records for sampling... ";
85 dump_sample_overview($c, $s) if $c->{sample};
86 dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
89 sub dump_sample_detail {
91 my $tags = $s->{data}{samp};
92 my $count = $s->{data}{rcnt};
94 open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
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";
111 sub dump_sample_overview {
113 my $tags = $s->{data}{tags};
114 my $count = $s->{data}{rcnt};
116 my @tagsbyname = sort keys %{$tags};
117 my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
119 open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
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)), "%)";
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";
138 #--------------------------
144 # set mode on existing filehandles
145 binmode(STDIN, ':utf8');
147 my $rc = GetOptions( $c,
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};
164 my @keys = keys %{$c};
165 for my $key ('prefix')
166 { push @missing, $key unless $c->{$key} }
168 print "Required option: ", join(', ', @missing), " missing!\n";
177 print "\nERROR - $msg\n" if $msg;
180 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
183 --prefix -p Prefix string for output filenames
184 --marcfile -m MARCXML to use as source data
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
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"')
194 If --samplemap and --samplestr are both specified, --samplemap wins.
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
201 Both these must be given together.
206 sub show_version { print "extract_holdings v$VERSION\n"; exit }