1.001 changed sample detail format to one which can be dumped directly into a dokuwik...
[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.001';
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\n";
113         for my $subkey (sort keys %{$tags->{$tag}}) {
114             my $sub = $tags->{$tag}{$subkey};
115             print "|| $subkey | ", $sub->{value}, " | ", 
116               $sub->{count}, "/", $sub->{tcnt}, " |  ||\n";
117         }
118     }
119     print "\n";
120     select STDOUT;
121     close DETAIL;
122 }
123
124 sub dump_sample_overview {
125     my ($c, $s) = @_;
126     my $tags = $s->{data}{tags};
127     my $count = $s->{data}{rcnt};
128
129     my @tagsbyname  = sort keys %{$tags};
130     my @tagsbycount = reverse sort { $tags->{$a} <=> $tags->{$b} } keys %{$tags};
131
132     open SAMPLE, '>', ($c->{prefix} . "-HOLDINGS-OVERVIEW.txt");
133     select SAMPLE;
134     print "SAMPLE REPORT FOR ", $c->{prefix},": $count records\n\n";
135     print "FOUND TAGS (BY TAG)           FOUND TAGS (BY COUNT)\n";
136     print "------------------------      --------------------------\n";
137     for my $i (0 .. @tagsbyname - 1) {
138         print $tagsbyname[$i], (" " x (14 - length $tags->{ $tagsbyname[$i] })),
139           $tags->{ $tagsbyname[$i] };
140         print " (", sprintf("%03d", int($tags->{ $tagsbyname[$i] } / $count * 100)), "%)";
141         print "      ";
142         print $tagsbycount[$i], (" " x (16 - length $tags->{ $tagsbycount[$i] })),
143           $tags->{ $tagsbycount[$i] };
144         print " (", sprintf("%03d", int($tags->{ $tagsbycount[$i] } / $count * 100)), "%)\n";
145     }
146     select STDOUT;
147     print "\n";
148     close SAMPLE;
149 }
150
151 #--------------------------
152
153 sub initialize {
154     my $c = {};
155     my @missing = ();
156
157     # set mode on existing filehandles
158     binmode(STDIN, ':utf8');
159
160     my $rc = GetOptions( $c,
161                          'sample|s',
162                          'samplemap|sm=s',
163                          'samplestr|ss=s',
164                          'marcfile|m=s',
165                          'map=s',
166                          'holdings|h=i',
167                          'copyid|c=s',
168                          'prefix|p=s',
169                          'version|v',
170                          'help',
171                        );
172     show_help() unless $rc;
173     show_help() if ($c->{help});
174     show_help("Nothing to do!")
175       unless ($c->{map} or $c->{sample} or $c->{samplemap} or $c->{samplestr});
176     show_help("map, holdings, and copyid must be specified together!")
177       if ($c->{map} and !$c->{holdings} and !$c->{copyid});
178     show_version() if $c->{version};
179
180     my @keys = keys %{$c};
181     for my $key ('prefix', 'marcfile')
182       { push @missing, $key unless $c->{$key} }
183     if (@missing) {
184         print "Required option: ", join(', ', @missing), " missing!\n";
185         show_help();
186     }
187
188     return $c;
189 }
190
191 sub show_help {
192     my ($msg) = @_;
193     print "\nERROR - $msg\n" if $msg;
194     print <<HELP;
195
196 Usage is: extract_holdings -p PREFIX -m MARCFILE [ARGUMENTS]
197
198 REQUIRED ARGUMENTS
199   --prefix   -p  Prefix string for output filenames
200   --marcfile -m  MARCXML to use as source data
201
202 SAMPLING ARGUMENTS
203   --sample    -s   Generate a report of all tags in the MARC data
204   --samplemap -sm  Specify a E::M::STL map file which will be used to generate
205                    subfield breakdown reports about specific tags in the MARC
206                    data
207   --samplestr -ss  As above, but with a one-liner map specified on the command
208                    line as a string (e.g. '-ss "852 999"')
209
210   If --samplemap and --samplestr are both specified, --samplemap wins.
211
212 HOLDINGS EXTRACTION ARGUMENTS
213   --map          E::M::SM map file which will be used to extract holdings data
214                  from the input MARC file
215   --holdings -h  Specifies actual holdings tag
216   --copyid   -c  Specifies subfield of holdings with unique copy identifier
217
218   Both these must be given together.
219 HELP
220     exit;
221 }
222
223 sub show_version { print "extract_holdings v$VERSION\n"; exit }