3 # Copyright 2009-2012, Equinox Software, Inc.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 use Equinox::Migration::MapDrivenMARCXMLProc 1.005;
24 use Equinox::Migration::MARCXMLSampler;
26 my $VERSION = '1.001';
32 * Have detail mode report on number of subfields per datafield
39 # run samples if we've been asked for them
40 run_samples($c) if ($c->{sample} or $c->{samplemap} or $c->{samplestr});
41 extract_holdings($c) if ($c->{map});
44 #--------------------------
46 sub extract_holdings {
48 print "Parsing records for extraction:\n";
49 my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( marcfile => $c->{marcfile},
54 print "Writing holdings to output file(s)...\n";
55 # open main holdings file
56 open HOLDINGS, '>', ($c->{prefix} . "-HOLDINGS.pg");
58 my $multis = $m->get_multis;
60 for my $t ( keys %{$multis} ) {
61 for my $s ( keys %{$multis->{$t}})
62 { open my $fh, ">", ($c->{prefix} . "-HOLDINGS-MULT-$t$s.pg"); $MULTIFILE{"$t$s"} = $fh }
65 my $parallel_fields = $m->get_parallel_fields;
67 my $i = 0; # record counter
68 my $j = 0; # holdings counter
70 while ( $m->{data}{recs}[$i] ) {
71 print HOLDINGS "BEGIN;\n\negid, hseq, " unless $j;
72 my $rec = $m->{data}{recs}[$i];
73 my $k = 0; # holding-within-record pointer
75 for my $holdidx ( @{ $rec->{tmap}{ $c->{holdings} } } ) {
76 # for each holdings tag in the record...
77 my $tagid = $rec->{tags}[$holdidx]{tag};
80 my @out = (); # clear the output buffer
81 push @out, $rec->{egid}; # slug in the egid first thing
82 push @out, $j; # holding seq goes next
84 # grab the unary mappings and slug 'em in
85 for my $sub ( sort keys %{$rec->{tags}[$holdidx]{uni}} ) {
86 push @out, $rec->{tags}[$holdidx]{uni}{$sub};
87 print HOLDINGS "l_", $m->name($tagid, $sub),", " unless $j;
90 # handle holdings multis
91 for my $sub ( sort keys %{$multis->{$tagid}} ) {
92 for my $value ( @{$rec->{tags}[$holdidx]{multi}{$sub}} ) {
93 my $fh = $MULTIFILE{"$tagid$sub"};
94 print $fh join("\t", $rec->{egid}, $j, $value), "\n";
99 # now get everything else in the mapping
100 for my $othertag ( sort keys %{$rec->{tmap}} ) {
101 next if $othertag eq $c->{holdings}; # ignoring the holdings, o'course
102 my $test_idx = $rec->{tmap}{$othertag}[0]; # get index into tags struct
103 unless (defined $test_idx) {
108 # handle parallel fields
109 if (exists($parallel_fields->{$othertag})) {
110 my $num_fields = $#{ $rec->{tmap}{$othertag} };
112 if ($holdidx > $num_fields) {
115 $tag_idx = $rec->{tmap}{$othertag}[$holdidx];
117 for my $sub ( sort keys %{ $parallel_fields->{$othertag } } ) {
118 push @out, $tag_idx > -1 ? $rec->{tags}[$tag_idx]{parallel}{$sub}->[0] : '';
119 print HOLDINGS "l_", $m->name($rec->{tags}[$tag_idx]{tag}, $sub), ", " unless $j;
123 # handle only first other tag unless it is known to be multi
125 if (exists($multis->{$othertag})) {
126 $limit = $#{ $rec->{tmap}{$othertag} };
128 foreach my $idx (0..$limit) {
129 my $tag_idx = $rec->{tmap}{$othertag}[$idx];
130 for my $sub ( sort keys %{$rec->{tags}[$tag_idx]{uni}} ) {
131 if ($m->first_only($rec->{tags}[$tag_idx]{tag}, $sub)) {
132 push @out, ($k == 1) ? $rec->{tags}[$tag_idx]{uni}{$sub} : '';
134 push @out, $rec->{tags}[$tag_idx]{uni}{$sub};
136 print HOLDINGS "l_", $m->name($rec->{tags}[$tag_idx]{tag}, $sub), ", " unless $j;
138 next unless exists($multis->{$othertag});
139 for my $sub ( sort keys %{$multis->{$othertag}} ) {
140 next if $m->first_only($rec->{tags}[$tag_idx]{tag}, $sub) and ($k > 1);
141 for my $value ( @{$rec->{tags}[$tag_idx]{multi}{$sub}} ) {
142 my $fh = $MULTIFILE{"$othertag$sub"};
143 print $fh normalize_output(join("\t", $rec->{egid}, $j, $value)), "\n";
150 print HOLDINGS "\n" unless $j;
151 print HOLDINGS normalize_output(join("\t", @out));
161 #--------------------------
166 print "Parsing records for sampling... ";
167 if ($c->{samplemap}) {
168 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
169 mapfile => $c->{samplemap});
170 } elsif ($c->{samplestr}) {
171 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
172 mapstring => $c->{samplestr});
174 $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
177 dump_sample_overview($c, $s) if $c->{sample};
178 dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
181 sub dump_sample_detail {
183 my $tags = $s->{data}{samp};
184 my $count = $s->{data}{rcnt};
185 my $scnt = $s->{data}{scnt};
187 open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
189 for my $tag (sort keys %{ $tags }) {
190 print ">>>>> TAG $tag\n\n";
191 for my $subkey (sort keys %{ $tags->{$tag} }) {
192 my $sub = $tags->{$tag}{$subkey};
193 print "|| $subkey | ", $sub->{value}, " | ",
194 $sub->{count}, "/", $sub->{tcnt}, " | ", ($sub->{count} > $sub->{tcnt}) ? "MULTI" : "", " ||\n";
199 open SCOUNT, '>', ($c->{prefix} . "-HOLDINGS-SUBCOUNTS.txt");
201 for my $tag (sort keys %{ $scnt }) {
202 print ">>>>> TAG $tag\n\n";
203 for my $len (sort keys %{ $scnt->{$tag} })
204 { print "|| $len | ", $scnt->{$tag}{$len}, " ||\n" }
208 print "Saved results as ", ($c->{prefix} . "-HOLDINGS-DETAIL.txt"), " and ",
209 ($c->{prefix} . "-HOLDINGS-SUBCOUNTS.txt"), "\n";
212 sub dump_sample_overview {
214 my $tags = $s->{data}{tags};
215 my $count = $s->{data}{rcnt};
217 my @tagsbyname = sort keys %{$tags};
218 my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
220 open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
222 print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
223 print "FOUND TAGS (BY TAG) FOUND TAGS (BY COUNT)\n";
224 print "------------------------ --------------------------\n";
225 for my $i (0 .. @tagsbyname - 1) {
226 print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
227 $tags->{ $tagsbyname[$i] };
228 print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
230 print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
231 $tags->{ $tagsbycount[$i] };
232 print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
235 print "Saved results as ", ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt"), "\n";
239 #--------------------------
245 # set mode on existing filehandles
246 binmode(STDIN, ':utf8');
248 my $rc = GetOptions( $c,
257 'disable-pg-normalization',
261 show_help() unless $rc;
262 show_help() if ($c->{help});
263 show_help("Nothing to do!")
264 unless ($c->{map} or $c->{sample} or $c->{samplemap} or $c->{samplestr});
265 show_help("map, holdings, and copyid must be specified together!")
266 if ($c->{map} and !($c->{holdings} and $c->{copyid}));
267 show_version() if $c->{version};
269 if ($c->{prefix} and !$c->{marcfile}) {
270 $c->{marcfile} = $c->{prefix} . ".clean.marc.xml";
273 my @keys = keys %{$c};
274 for my $key ('prefix', 'marcfile')
275 { push @missing, $key unless $c->{$key} }
277 print "Required option: ", join(', ', @missing), " missing!\n";
284 sub normalize_output {
286 $str =~ s!\\!\\\\!g unless $c->{'disable-pg-normalization'};
292 print "\nERROR - $msg\n" if $msg;
295 Usage is: extract_holdings --prefix PREFIX --marcfile MARCFILE [ARGUMENTS]
298 --prefix -p Prefix string for output filenames
299 --marcfile MARCXML to use as source data
300 Defaults to 'PREFIX.clean.marc.xml'
303 --sample -s Generate a report of all tags in the MARC data
304 --samplemap -sm Specify a Equinox::Migration::SimpleTagList map file which
305 will be used to generate subfield breakdown reports about
306 specific tags in the MARC data
307 --samplestr -ss As above, but with a one-liner map specified on the command
308 line as a string (e.g. '-ss "852 999"')
310 If --samplemap and --samplestr are both specified, --samplemap wins.
312 HOLDINGS EXTRACTION ARGUMENTS
313 --map -m Equinox::Migration::SubfieldMapper map file which will be
314 used to extract holdings data from the input MARC file
315 --holdings -h Specifies actual holdings tag
316 --copyid -c Specifies subfield of holdings with unique copy identifier
317 --disable-pg-normalization By default, output is normalized so that a Postgres
318 copy or \\copy can import the data without choking on
319 backslashes; use this command-line option if
320 output is not meant to be consumed by psql.
322 All three of these must be given together.
327 sub show_version { print "extract_holdings v$VERSION\n"; exit }