cruddy temporary fix while multi is sorted out
[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 my $VERSION = '1.000';
10
11 my $c = initialize();
12 $| = 1;
13
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});
17
18 #--------------------------
19
20 sub extract_holdings {
21     my ($c) = @_;
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
28     # FIXME DO THIS
29     open X, '>', ($c->{prefix} . "-HOLDINGS-privnotes.pg");
30     open Z, '>', ($c->{prefix} . "-HOLDINGS-pubnotes.pg");
31     select HOLDINGS;
32
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};
37
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);
42
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"
47                   if ($m->recno == 1);
48             }
49             for my $x (@{$rec->{tags}[$holdidx]{multi}{x}} ) {
50                 print X $rec->{egid}, "\t",
51                   $rec->{tags}[$holdidx]{uni}{ $c->{copyid} },
52                     "\t$x\n";
53             }
54             for my $z (@{$rec->{tags}[$holdidx]{multi}{z}} ) {
55                 print Z $rec->{egid}, "\t",
56                   $rec->{tags}[$holdidx]{uni}{ $c->{copyid} },
57                     "\t$z\n";
58             }
59
60
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"
68                       if ($m->recno == 1);
69                 }
70             }
71
72             # and dump it
73             print "\n" if ($m->recno == 1);
74             print join("\t", @out);
75             print "\n";
76         }
77
78     }
79     select STDOUT;
80     print "\n";
81 }
82
83 #--------------------------
84
85 sub run_samples {
86     my ($c) = @_;
87     my $s;
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});
94     } else {
95         $s = Equinox::Migration::MARCXMLSampler->new( marcfile => $c->{marcfile} );
96     }
97     print "Parsing records for sampling... ";
98     $s->parse_records;
99
100     dump_sample_overview($c, $s) if $c->{sample};
101     dump_sample_detail($c, $s) if ($c->{samplemap} or $c->{samplestr});
102 }
103
104 sub dump_sample_detail {
105     my ($c, $s) = @_;
106     my $tags = $s->{data}{samp};
107     my $count = $s->{data}{rcnt};
108
109     open DETAIL, '>', ($c->{prefix} . "-HOLDINGS-DETAIL.txt");
110     select DETAIL;
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";
119         }
120     }
121     select STDOUT;
122     print "\n";
123     close DETAIL;
124 }
125
126 sub dump_sample_overview {
127     my ($c, $s) = @_;
128     my $tags = $s->{data}{tags};
129     my $count = $s->{data}{rcnt};
130
131     my @tagsbyname  = sort keys %{$tags};
132     my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
133
134     open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
135     select SAMPLE;
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)), "%)";
143         print "      ";
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";
147     }
148     select STDOUT;
149     print "\n";
150     close SAMPLE;
151 }
152
153 #--------------------------
154
155 sub initialize {
156     my $c = {};
157     my @missing = ();
158
159     # set mode on existing filehandles
160     binmode(STDIN, ':utf8');
161
162     my $rc = GetOptions( $c,
163                          'sample|s',
164                          'samplemap|sm=s',
165                          'samplestr|ss=s',
166                          'marcfile|m=s',
167                          'map=s',
168                          'holdings|h=i',
169                          'copyid|c=s',
170                          'prefix|p=s',
171                          'version|v',
172                          'help',
173                        );
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};
181
182     my @keys = keys %{$c};
183     for my $key ('prefix', 'marcfile')
184       { push @missing, $key unless $c->{$key} }
185     if (@missing) {
186         print "Required option: ", join(', ', @missing), " missing!\n";
187         show_help();
188     }
189
190     return $c;
191 }
192
193 sub show_help {
194     my ($msg) = @_;
195     print "\nERROR - $msg\n" if $msg;
196     print <<HELP;
197
198 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
199
200 REQUIRED ARGUMENTS
201   --prefix   -p  Prefix string for output filenames
202   --marcfile -m  MARCXML to use as source data
203
204 SAMPLING ARGUMENTS
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
208                    data
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"')
211
212   If --samplemap and --samplestr are both specified, --samplemap wins.
213
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
219
220   Both these must be given together.
221 HELP
222     exit;
223 }
224
225 sub show_version { print "extract_holdings v$VERSION\n"; exit }