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" }
173 print "Saved results as ", ($c->{prefix} . "-HOLDINGS-DETAIL.txt"), " and ",
174 ($c->{prefix} . "-HOLDINGS-SUBCOUNTS.txt"), "\n";
177 sub dump_sample_overview {
179 my $tags = $s->{data}{tags};
180 my $count = $s->{data}{rcnt};
182 my @tagsbyname = sort keys %{$tags};
183 my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
185 open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
187 print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
188 print "FOUND TAGS (BY TAG) FOUND TAGS (BY COUNT)\n";
189 print "------------------------ --------------------------\n";
190 for my $i (0 .. @tagsbyname - 1) {
191 print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
192 $tags->{ $tagsbyname[$i] };
193 print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
195 print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
196 $tags->{ $tagsbycount[$i] };
197 print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
200 print "Saved results as ", ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt"), "\n";
204 #--------------------------
210 # set mode on existing filehandles
211 binmode(STDIN, ':utf8');
213 my $rc = GetOptions( $c,
222 'disable-pg-normalization',
226 show_help() unless $rc;
227 show_help() if ($c->{help});
228 show_help("Nothing to do!")
229 unless ($c->{map} or $c->{sample} or $c->{samplemap} or $c->{samplestr});
230 show_help("map, holdings, and copyid must be specified together!")
231 if ($c->{map} and !($c->{holdings} and $c->{copyid}));
232 show_version() if $c->{version};
234 if ($c->{prefix} and !$c->{marcfile}) {
235 $c->{marcfile} = $c->{prefix} . ".clean.marc.xml";
238 my @keys = keys %{$c};
239 for my $key ('prefix', 'marcfile')
240 { push @missing, $key unless $c->{$key} }
242 print "Required option: ", join(', ', @missing), " missing!\n";
249 sub normalize_output {
251 $str =~ s!\\!\\\\!g unless $c->{'disable-pg-normalization'};
257 print "\nERROR - $msg\n" if $msg;
260 Usage is: extract_holdings --prefix PREFIX --marcfile MARCFILE [ARGUMENTS]
263 --prefix -p Prefix string for output filenames
264 --marcfile MARCXML to use as source data
265 Defaults to 'PREFIX.clean.marc.xml'
268 --sample -s Generate a report of all tags in the MARC data
269 --samplemap -sm Specify a Equinox::Migration::SimpleTagList map file which
270 will be used to generate subfield breakdown reports about
271 specific tags in the MARC data
272 --samplestr -ss As above, but with a one-liner map specified on the command
273 line as a string (e.g. '-ss "852 999"')
275 If --samplemap and --samplestr are both specified, --samplemap wins.
277 HOLDINGS EXTRACTION ARGUMENTS
278 --map -m Equinox::Migration::SubfieldMapper map file which will be
279 used to extract holdings data from the input MARC file
280 --holdings -h Specifies actual holdings tag
281 --copyid -c Specifies subfield of holdings with unique copy identifier
282 --disable-pg-normalization By default, output is normalized so that a Postgres
283 copy or \\copy can import the data without choking on
284 backslashes; use this command-line option if
285 output is not meant to be consumed by psql.
287 All three of these must be given together.
292 sub show_version { print "extract_holdings v$VERSION\n"; exit }