various enhacnements to emig bibstats including adding it to kmig
[migration-tools.git] / emig.d / bin / mig-bibstats
1 #!/usr/bin/perl
2 # -*- coding: iso-8859-15 -*-
3 ###############################################################################
4 =pod
5
6 =item B<bibstats> --file foo.mrc
7
8 Reads through a marc file to generate statistical information about the file 
9 for quick analysis.
10
11 --uri_threshold defaults to 1, only shows URI values with more than that 
12 frequency
13
14 --ignore_filetype true will have it not care what file returns as the type and 
15 always treat it as marc21
16
17 --ils --holding_code --barcode_subfield work together to pass an new ILS 
18 definnition without it being hardcode in the script and can test arbitary 
19 fields 
20
21 --exportbarcodes ils_name is used if you want to export the barcodes associated 
22 with one of the ILSes so provide the name 
23
24 --exportbarcodesfile will use this file name for a barcode export instead 
25 of the generic 'barcodes_export.txt'
26
27 =back
28 =cut
29
30 ###############################################################################
31
32 use strict;
33 use warnings;
34
35 use Data::Dumper;
36 use Env qw(
37     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
38     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
39 );
40 use Pod::Usage;
41 use Switch;
42 use Getopt::Long;
43 use MARC::Batch;
44 use MARC::Record;
45 use MARC::Field;
46 use Cwd 'abs_path';
47 use Cwd qw(getcwd);
48 use List::MoreUtils qw(uniq);
49 use FindBin;
50 my $mig_bin = "$FindBin::Bin/";
51 use lib "$FindBin::Bin/";
52 use EMig;
53 #use KMig;
54 use open ':encoding(utf8)';
55
56 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
57 pod2usage(-verbose => 1) if ! $ARGV[1];
58
59 my $file;
60 my $uri_threshold = 1;
61 my $p_holding_code;
62 my $p_barcode_subfield;
63 my $p_ils_name = '';
64 my $holding_threshold = 50;
65 my $p_ignore_filetype = 'false';
66 my @holdings;
67 my %unique_barcodes;
68 my $exportbarcodes;
69 my $exportbarcodesfile;
70
71 my $ret = GetOptions(
72     'file:s'                     => \$file,
73         'uri_threshold:i'                => \$uri_threshold,
74         'holding_code:s'                 => \$p_holding_code,
75         'barcode_subfield:s'     => \$p_barcode_subfield,
76         'ignore_filetype:s'              => \$p_ignore_filetype,
77         'ils:s'                          => \$p_ils_name,
78         'exportbarcodes:s'               => \$exportbarcodes,
79         'exportbarcodesfile:s'   => \$exportbarcodesfile,
80         'holding_threshold:s'    => \$holding_threshold
81 );
82
83 if ($exportbarcodesfile and !defined $exportbarcodes) { abort('You have to provide an ILS name if you want a barcode export file.'); }
84
85 if ($p_holding_code and length $p_holding_code != 3) { abort('Holdings codes must be three characters.'); }
86
87 if ($p_barcode_subfield) {
88         if (!defined $p_holding_code) { abort('A barcode field can not be used without a holding code.'); }
89         if (length $p_barcode_subfield != 1) { abort('Barcode subfields must be a single character code.'); }
90 }
91
92 # ils name, holding tag, barcode subfield 
93 my @ilses = (
94         ['Mandarin','852','p'],
95         ['Evergreen','852','p'],
96         ['Polaris','852','p'],
97         ['TLC','949','g'],
98         ['Koha','952','p'],
99         ['Sympony','999','i'],
100     ['Destiny','852','p']
101 );
102
103 my @temp;
104 if ($p_holding_code) {
105         push @temp, $p_ils_name;
106         push @temp, $p_holding_code;
107         if ($p_barcode_subfield) { push @temp, lc $p_barcode_subfield; }
108         push @ilses, [@temp];
109 }
110
111 #to do - add a check for exportbarcodes being in @ilses
112
113 my $batch = MARC::Batch->new('USMARC', $file);
114 $batch->strict_off();
115 my $filetype = `file $file`;
116 if ($filetype =~ m/MARC21/ or $p_ignore_filetype eq 'true') { print "$filetype.\n" }
117     else { abort("File is not MARC21."); }
118
119 my $i = 0;
120 my $uri_count = 0;
121 my $uri_valid_count = 0;
122 my $uri_sub9_count = 0;
123 my $author_sub0 = 0;
124 my $title_sub0 = 0;
125 my @uris;
126 my @fields;
127 my @encodings;
128 my @types;
129 my @holding_code_strings;
130 my %holding_counts;
131 my %barcode_counts;
132
133 foreach (@ilses) { 
134         $holding_counts{@$_[0]} = 0; 
135         $barcode_counts{@$_[0]} = 0;
136 }
137
138 while ( my $record = $batch->next() ) {
139     $i++;
140         #check holdings, bit time consuming but more future proof
141         foreach (@ilses) {
142                 my $ils = @$_[0];
143                 my $hcode = @$_[1];
144                 my $barcode = @$_[2];
145                 my @holding_fields = $record->field($hcode);
146         foreach my $hf (@holding_fields) {
147                         my @h;
148                         my $barcode_string = $hf->subfield($barcode);
149                 push @h, $ils;
150                 push @h, $barcode_string;
151                         push @holdings, [@h];
152                 }
153                 my $l = scalar @holding_fields;
154                 my $v = $holding_counts{$ils};
155                 if ($l) { $holding_counts{$ils} = $v + $l; }
156         }
157     #process 856s
158         @fields = $record->field('856');
159         my $enc = substr $record->leader(), 9, 1;
160         push @encodings, $enc;
161     my $type = substr $record->leader(), 6, 1;
162     push @types, $type;
163         foreach my $f (@fields) {
164                 my $u = $f->subfield('u');
165         my $n = $f->subfield('9');
166         if (defined $n) { $uri_sub9_count++; }
167                 if (defined $u) {
168                         $uri_count++;
169                         my $ind1 = $f->indicator('1');
170                         my $ind2 = $f->indicator('2');
171                         if ($ind1 eq '4') {
172                                 if ($ind2 eq '0' or $ind2 eq '1') { $uri_valid_count++; }
173                         }
174                         my $ustring = lc $f->as_string('u');
175                         $ustring =~ s/http:\/\///;
176             $ustring =~ s/ftp:\/\///;
177                         $ustring =~ s/https:\/\///;
178                         $ustring =~ s/\/.*//;
179                         push @uris, $ustring;
180                 }
181         }
182     #check for authority linking on 100s and 245s, if present may need to scrub them
183         @fields = $record->field('100');
184         foreach my $f (@fields) {
185                 my $t = $f->subfield('0');
186                 if (defined $t) { $title_sub0++; }      
187         }
188     @fields = $record->field('245');
189     foreach my $f (@fields) {
190         my $t = $f->subfield('0');
191         if (defined $t) { $author_sub0++; }
192     }
193     if(($i % 1000) == 0) { print "Processing bib $i.\n"; }
194 }
195
196 foreach (@ilses) {
197         my $ils = @$_[0];
198     my @temp_barcodes;
199     foreach my $h (@holdings) {
200                 my $temp_ils_name = @$h[0];
201         if ($temp_ils_name eq $ils) { push @temp_barcodes, @$h[1]; }
202     }
203     my @uniq_barcodes = uniq @temp_barcodes;;
204     $barcode_counts{$ils} = scalar @uniq_barcodes;
205 }
206
207 my %uri_counts;
208 $uri_counts{$_}++ for @uris;
209
210 my %encoding_counts;
211 $encoding_counts{$_}++ for @encodings;
212
213 my %type_counts;
214 $type_counts{$_}++ for @types;
215
216 print "\n$filetype\n";
217 print "$i bibs read in file\n\n";
218
219 print "===== Leader 09, # = MARC-8, a = UCS/Unicode\n";
220 foreach my $key (keys %encoding_counts) {
221     my $value = $encoding_counts{$key};
222     print "  $key   $value\n"; 
223 }
224 print "\n";
225
226 print "===== Leader 06\n";
227 foreach my $key (keys %type_counts) {
228     my $value = $type_counts{$key};
229     my $type = give_type($key);
230     print "  $key   $value $type\n";
231 }
232 print "\n";
233
234 print "===== Summary of Select Field Counts\n";
235 print "  $uri_count 856 fields with a subfield u\n";
236 print "  $uri_valid_count 856 fields with a subfield u and valid indicators\n";
237 print "  $uri_sub9_count 856 fields have a subfield 9\n";
238 print "  $title_sub0 100 fields have a subfield 0\n";
239 print "  $author_sub0 245 fields have a subfield 0\n";
240
241 print "\n===== Holdings Analysis\n";
242 foreach my $key (keys %holding_counts) {
243         my $c = $holding_counts{$key};
244         if (((100/$i)*$c) >= $holding_threshold) { print "  $key $holding_counts{$key} holdings in $i bibs with $barcode_counts{$key} unique barcodes\n"; }
245 }
246
247 print "\n===== URI values are domains and filtered to only show those with more than $uri_threshold\n";
248 foreach my $key (keys %uri_counts) {
249         my $value = $uri_counts{$key};
250         if ($value > $uri_threshold) { print "  $key   $value\n"; } 
251 }
252
253 if ($exportbarcodes) {
254     my @temp_barcodes;
255     my $outfile;
256     if ($exportbarcodesfile) { $outfile = $exportbarcodesfile; } else { $outfile = 'barcodes_export.txt'; }
257     open my $out_fh, '>:utf8', $outfile or abort('can not open output file for barcode list');
258     foreach my $h (@holdings) {
259         my $temp_ils_name = @$h[0];
260                 my $barcode = @$h[1];
261                 if (!defined $barcode) { $barcode = 'no barcode found'; }
262         if ($temp_ils_name eq $exportbarcodes) { print $out_fh "@$h[1]\n" }
263     }
264     close $out_fh;
265 } else { print "frack\n"; }
266
267 close $file;
268
269 ########### functions
270
271 sub abort {
272     my $msg = shift;
273     print STDERR "$0: $msg", "\n";
274     exit 1;
275 }
276
277 sub give_type {
278         my $type = shift;
279     if ($type eq 'a') { return 'Language material'; }
280     if ($type eq 'c') { return 'Notated Music'; }
281     if ($type eq 'd') { return 'Manuscript notated music'; }
282     if ($type eq 'e') { return 'Cartographic material'; }
283     if ($type eq 'f') { return 'Manuscript cartographic material'; }
284     if ($type eq 'g') { return 'Projected Medium'; }
285     if ($type eq 'i') { return 'Nonmusical sound recording'; }
286     if ($type eq 'j') { return 'Musical sound recording'; }
287     if ($type eq 'k') { return 'Two-dimensional nonprojectable graphic'; }
288     if ($type eq 'm') { return 'Computer file'; }
289     if ($type eq 'o') { return 'Kit'; }
290     if ($type eq 'p') { return 'Mixed materials'; }
291     if ($type eq 'r') { return 'Three-dimensaional artifact or naturally occurring object'; }
292     if ($type eq 't') { return 'Manuscript language material'; }
293     if ($type eq 'z') { return 'Authority'; }
294     return 'unknown';
295 }