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
29 open X, '>', ($c->{prefix} . "-HOLDINGS-privnotes.pg");
30 open Z, '>', ($c->{prefix} . "-HOLDINGS-pubnotes.pg");
33 while (my $rec = $m->parse_record) {
34 # for each holdings tag in the record...
35 for my $holdidx ( @{$rec->{tmap}{ $c->{holdings} }} ) {
36 my $tagid = $rec->{tags}[$holdidx]{tag};
38 print STDOUT "\r", $m->recno;
39 my @out = (); # clear the output buffer
40 push @out, $rec->{egid}; # slug in the egid first thing
41 print "BEGIN;\n\negid\t" if ($m->recno == 1);
43 # grab the unary mappings and slug 'em in
44 for my $sub ( sort keys %{$rec->{tags}[$holdidx]{uni}} ) {
45 push @out, $rec->{tags}[$holdidx]{uni}{$sub};
46 print "l_", $m->name($tagid, $sub),"\t"
49 for my $x (@{$rec->{tags}[$holdidx]{multi}{x}} ) {
50 print X $rec->{egid}, "\t",
51 $rec->{tags}[$holdidx]{uni}{ $c->{copyid} },
54 for my $z (@{$rec->{tags}[$holdidx]{multi}{z}} ) {
55 print Z $rec->{egid}, "\t",
56 $rec->{tags}[$holdidx]{uni}{ $c->{copyid} },
61 # now get everything else in the mapping
62 for my $othertag ( sort keys %{$rec->{tmap}} ) {
63 next if $othertag eq $c->{holdings}; # ignoring the holdings, o'course
64 my $idx = $rec->{tmap}{$othertag}[0]; # get index into tags struct
65 for my $sub ( sort keys %{$rec->{tags}[$idx]{uni}} ) {
66 push @out, $rec->{tags}[$idx]{uni}{$sub};
67 print "l_", $m->name($rec->{tags}[$idx]{tag}, $sub), "\t"
73 print "\n" if ($m->recno == 1);
74 print join("\t", @out);
83 #--------------------------
88 if ($c->{samplemap}) {
89 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
90 mapfile => $c->{samplemap});
91 } elsif ($c->{samplestr}) {
92 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
93 mapstring => $c->{samplestr});
95 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
97 print "Parsing records for sampling... ";
100 dump_sample_overview($c, $s) if $c->{sample};
101 dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
104 sub dump_sample_detail {
106 my $tags = $s->{data}{samp};
107 my $count = $s->{data}{rcnt};
109 open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
111 for my $tag (sort keys %{$tags}) {
112 print ">>>>> TAG $tag\n";
113 for my $subkey (sort keys %{$tags->{$tag}}) {
114 my $sub = $tags->{$tag}{$subkey};
115 print " Subfield: $subkey\n";
116 print " Sample: '", $sub->{value}, "'\n";
117 print " Count: ", $sub->{count}, " in ", $sub->{tcnt}, " tags\n\n";
118 #print "(", int($sub->{count} / $sub->{rcnt}), "%)\n";
126 sub dump_sample_overview {
128 my $tags = $s->{data}{tags};
129 my $count = $s->{data}{rcnt};
131 my @tagsbyname = sort keys %{$tags};
132 my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
134 open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
136 print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
137 print "FOUND TAGS (BY TAG) FOUND TAGS (BY COUNT)\n";
138 print "------------------------ --------------------------\n";
139 for my $i (0 .. @tagsbyname - 1) {
140 print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
141 $tags->{ $tagsbyname[$i] };
142 print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
144 print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
145 $tags->{ $tagsbycount[$i] };
146 print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
153 #--------------------------
159 # set mode on existing filehandles
160 binmode(STDIN, ':utf8');
162 my $rc = GetOptions( $c,
174 show_help() unless $rc;
175 show_help() if ($c->{help});
176 show_help("Nothing to do!")
177 unless ($c->{map} or $c->{sample} or $c->{samplemap} or $c->{samplestr});
178 show_help("map, holdings, and copyid must be specified together!")
179 if ($c->{map} and !$c->{holdings} and !$c->{copyid});
180 show_version() if $c->{version};
182 my @keys = keys %{$c};
183 for my $key ('prefix', 'marcfile')
184 { push @missing, $key unless $c->{$key} }
186 print "Required option: ", join(', ', @missing), " missing!\n";
195 print "\nERROR - $msg\n" if $msg;
198 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
201 --prefix -p Prefix string for output filenames
202 --marcfile -m MARCXML to use as source data
205 --sample -s Generate a report of all tags in the MARC data
206 --samplemap -sm Specify a E::M::STL map file which will be used to generate
207 subfield breakdown reports about specific tags in the MARC
209 --samplestr -ss As above, but with a one-liner map specified on the command
210 line as a string (e.g. '-ss "852 999"')
212 If --samplemap and --samplestr are both specified, --samplemap wins.
214 HOLDINGS EXTRACTION ARGUMENTS
215 --map E::M::SM map file which will be used to extract holdings data
216 from the input MARC file
217 --holdings -h Specifies actual holdings tag
218 --copyid -c Specifies subfield of holdings with unique copy identifier
220 Both these must be given together.
225 sub show_version { print "extract_holdings v$VERSION\n"; exit }