sample stuffs working pretty good now
[migration-tools.git] / extract_holdings
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use Getopt::Long;
6 use Equinox::Migration::MapDrivenMARCXMLProc;
7 use Equinox::Migration::MARCXMLSampler;
8
9
10 my $c = initialize();
11
12 # run samples if we've been asked for them
13 run_samples($c) if ($c->{sample} or $c->{samplemap} or $c->{samplestr});
14
15
16 sub run_samples {
17     my ($c) = @_;
18     my $s;
19     if ($c->{samplemap}) {
20         $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile},
21                                                       mapfile  => $c->{samplemap});
22     } elsif ($c->{samplestr}) {
23         $s = Equinox::Migration::MARCXMLSampler->new( marcfile  => $c->{marcfile},
24                                                       mapstring => $c->{samplestr});
25     } else {
26         $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
27     }
28     $s->parse_records;
29
30     dump_sample_overview($c, $s) if $c->{sample};
31     dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
32 }
33
34 sub dump_sample_detail {
35     my ($c, $s) = @_;
36     my $tags = $s->{data}{samp};
37     my $count = $s->{data}{rcnt};
38
39     open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
40     select DETAIL;
41     for my $tag (sort keys %{$tags}) {
42         print ">>>>> TAG $tag\n";
43         for my $subkey (sort keys %{$tags->{$tag}}) {
44             my $sub = $tags->{$tag}{$subkey};
45             print "  Subfield: $subkey\n";
46             print "  Sample:   '", $sub->{value}, "'\n";
47             print "  Count:    ", $sub->{count}, " in ", $sub->{tcnt}, " tags\n\n";
48             #print "(", int($sub->{count} / $sub->{rcnt}), "%)\n";
49         }
50     }
51     select STDOUT;
52     close DETAIL;
53 }
54
55 sub dump_sample_overview {
56     my ($c, $s) = @_;
57     my $tags = $s->{data}{tags};
58     my $count = $s->{data}{rcnt};
59
60     my @tagsbyname  = sort keys %{$tags};
61     my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
62
63     open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
64     select SAMPLE;
65     print "FOUND TAGS (BY TAG)    FOUND TAGS (BY COUNT)\n";
66     print "-------------------    ---------------------\n";
67     for my $i (0 .. @tagsbyname - 1) {
68         print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
69           $tags->{ $tagsbyname[$i] }, "      ";
70         print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
71           $tags->{ $tagsbycount[$i] }, "\n";
72     }
73     select STDOUT;
74     close SAMPLE;
75 }
76
77 #--------------------------
78
79 sub initialize {
80     my $c = {};
81     my @missing = ();
82
83     # set mode on existing filehandles
84     binmode(STDIN, ':utf8');
85
86     my $rc = GetOptions( $c,
87                          'sample|s',
88                          'samplemap|sm=s',
89                          'samplestr|ss=s',
90                          'marcfile|m=s',
91                          'ils|i=s',
92                          'library|l=s',
93                          'prefix|p=s',
94                          'help|h',
95                        );
96     show_help() unless $rc;
97     show_help() if ($c->{help});
98
99     my @keys = keys %{$c};
100     for my $key ('prefix', 'ils')
101       { push @missing, $key unless $c->{$key} }
102     if (@missing) {
103         print "Required option: ", join(', ', @missing), " missing!\n";
104         show_help();
105     }
106
107     return $c;
108 }
109
110 sub show_help {
111     print <<HELP;
112 Usage is: extract_holdings -p PREFIX -m MAPFILE -i ILS [OPTIONS] <MARCXML>
113   prefix  p  Prefix string for output filenames
114   map     m  Subfield map file to use
115   ils     i  Legacy ILS name (affects SQL generation)
116
117   sample  s  Comma-delineated list of datafield tags to be sampled
118   library l  Legacy library name (affects SQL generation)
119 HELP
120     exit;
121 }