6 use Equinox::Migration::MapDrivenMARCXMLProc 1.005;
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});
27 #--------------------------
29 sub extract_holdings {
31 print "Parsing records for extraction:\n";
32 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( marcfile => $c->{marcfile},
37 print "Writing holdings to output file(s)...\n";
38 # open main holdings file
39 open HOLDINGS, '>', ($c->{prefix} . "-HOLDINGS.pg");
41 my $multis = $m->get_multis;
43 for my $t ( keys %{$multis} ) {
44 for my $s ( keys %{$multis->{$t}})
45 { open my $fh, ">", ($c->{prefix} . "-HOLDINGS-MULT-$t$s.pg"); $MULTIFILE{"$t$s"} = $fh }
48 my $i = 0; # record counter
49 my $j = 0; # holdings counter
51 while ( $m->{data}{recs}[$i] ) {
52 print HOLDINGS "BEGIN;\n\negid, hseq, " unless $j;
53 my $rec = $m->{data}{recs}[$i];
54 my $k = 0; # holding-within-record pointer
56 for my $holdidx ( @{ $rec->{tmap}{ $c->{holdings} } } ) {
57 # for each holdings tag in the record...
58 my $tagid = $rec->{tags}[$holdidx]{tag};
61 my @out = (); # clear the output buffer
62 push @out, $rec->{egid}; # slug in the egid first thing
63 push @out, $j; # holding seq goes next
65 # grab the unary mappings and slug 'em in
66 for my $sub ( sort keys %{$rec->{tags}[$holdidx]{uni}} ) {
67 push @out, $rec->{tags}[$holdidx]{uni}{$sub};
68 print HOLDINGS "l_", $m->name($tagid, $sub),", " unless $j;
71 # handle holdings multis
72 for my $sub ( sort keys %{$multis->{$tagid}} ) {
73 for my $value ( @{$rec->{tags}[$holdidx]{multi}{$sub}} ) {
74 my $fh = $MULTIFILE{"$tagid$sub"};
75 print $fh join("\t", $rec->{egid}, $j, $value), "\n";
80 # now get everything else in the mapping
81 for my $othertag ( sort keys %{$rec->{tmap}} ) {
82 next if $othertag eq $c->{holdings}; # ignoring the holdings, o'course
83 my $test_idx = $rec->{tmap}{$othertag}[0]; # get index into tags struct
84 unless (defined $test_idx) {
88 # handle only first other tag unless it is known to be multi
90 if (exists($multis->{$othertag})) {
91 $limit = $#{ $rec->{tmap}{$othertag} };
93 foreach my $idx (0..$limit) {
94 my $tag_idx = $rec->{tmap}{$othertag}[$idx];
95 for my $sub ( sort keys %{$rec->{tags}[$tag_idx]{uni}} ) {
96 if ($m->first_only($rec->{tags}[$tag_idx]{tag}, $sub)) {
97 push @out, ($k == 1) ? $rec->{tags}[$tag_idx]{uni}{$sub} : '';
99 push @out, $rec->{tags}[$tag_idx]{uni}{$sub};
101 print HOLDINGS "l_", $m->name($rec->{tags}[$tag_idx]{tag}, $sub), ", " unless $j;
103 next unless exists($multis->{$othertag});
104 for my $sub ( sort keys %{$multis->{$othertag}} ) {
105 next if $m->first_only($rec->{tags}[$tag_idx]{tag}, $sub) and ($k > 1);
106 for my $value ( @{$rec->{tags}[$tag_idx]{multi}{$sub}} ) {
107 my $fh = $MULTIFILE{"$othertag$sub"};
108 print $fh normalize_output(join("\t", $rec->{egid}, $j, $value)), "\n";
115 print HOLDINGS "\n" unless $j;
116 print HOLDINGS normalize_output(join("\t", @out));
126 #--------------------------
131 print "Parsing records for sampling... ";
132 if ($c->{samplemap}) {
133 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
134 mapfile => $c->{samplemap});
135 } elsif ($c->{samplestr}) {
136 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
137 mapstring => $c->{samplestr});
139 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
142 dump_sample_overview($c, $s) if $c->{sample};
143 dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
146 sub dump_sample_detail {
148 my $tags = $s->{data}{samp};
149 my $count = $s->{data}{rcnt};
150 my $scnt = $s->{data}{scnt};
152 open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
154 for my $tag (sort keys %{ $tags }) {
155 print ">>>>> TAG $tag\n\n";
156 for my $subkey (sort keys %{ $tags->{$tag} }) {
157 my $sub = $tags->{$tag}{$subkey};
158 print "|| $subkey | ", $sub->{value}, " | ",
159 $sub->{count}, "/", $sub->{tcnt}, " | ||\n";
164 open SCOUNT, '>', ($c->{prefix} . "-HOLDINGS-SUBCOUNTS.txt");
166 for my $tag (sort keys %{ $scnt }) {
167 print ">>>>> TAG $tag\n\n";
168 for my $len (sort keys %{ $scnt->{$tag} })
169 { print "|| $len | ", $scnt->{$tag}{$len}, " ||\n" }
175 sub dump_sample_overview {
177 my $tags = $s->{data}{tags};
178 my $count = $s->{data}{rcnt};
180 my @tagsbyname = sort keys %{$tags};
181 my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
183 open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
185 print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
186 print "FOUND TAGS (BY TAG) FOUND TAGS (BY COUNT)\n";
187 print "------------------------ --------------------------\n";
188 for my $i (0 .. @tagsbyname - 1) {
189 print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
190 $tags->{ $tagsbyname[$i] };
191 print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
193 print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
194 $tags->{ $tagsbycount[$i] };
195 print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
202 #--------------------------
208 # set mode on existing filehandles
209 binmode(STDIN, ':utf8');
211 my $rc = GetOptions( $c,
220 'disable-pg-normalization',
224 show_help() unless $rc;
225 show_help() if ($c->{help});
226 show_help("Nothing to do!")
227 unless ($c->{map} or $c->{sample} or $c->{samplemap} or $c->{samplestr});
228 show_help("map, holdings, and copyid must be specified together!")
229 if ($c->{map} and !($c->{holdings} and $c->{copyid}));
230 show_version() if $c->{version};
232 if ($c->{prefix} and !$c->{marcfile}) {
233 $c->{marcfile} = $c->{prefix} . ".clean.marc.xml";
236 my @keys = keys %{$c};
237 for my $key ('prefix', 'marcfile')
238 { push @missing, $key unless $c->{$key} }
240 print "Required option: ", join(', ', @missing), " missing!\n";
247 sub normalize_output {
249 $str =~ s!\\!\\\\!g unless $c->{'disable-pg-normalization'};
255 print "\nERROR - $msg\n" if $msg;
258 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
261 --prefix -p Prefix string for output filenames
262 --marcfile MARCXML to use as source data
263 Defaults to 'PREFIX.clean.marc.xml'
266 --sample -s Generate a report of all tags in the MARC data
267 --samplemap -sm Specify a Equinox::Migration::SimpleTagList map file which
268 will be used to generate subfield breakdown reports about
269 specific tags in the MARC data
270 --samplestr -ss As above, but with a one-liner map specified on the command
271 line as a string (e.g. '-ss "852 999"')
273 If --samplemap and --samplestr are both specified, --samplemap wins.
275 HOLDINGS EXTRACTION ARGUMENTS
276 --map -m Equinox::Migration::SubfieldMapper map file which will be
277 used to extract holdings data from the input MARC file
278 --holdings -h Specifies actual holdings tag
279 --copyid -c Specifies subfield of holdings with unique copy identifier
280 --disable-pg-normalization By default, output is normalized so that a Postgres
281 copy or \\copy can import the data without choking on
282 backslashes; use this command-line option if
283 output is not meant to be consumed by psql.
285 All three of these must be given together.
290 sub show_version { print "extract_holdings v$VERSION\n"; exit }