further enchancements to mig-bibstats
[migration-tools.git] / mig-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 =back
15
16 =cut
17
18 ###############################################################################
19
20 use strict;
21 use warnings;
22
23 use Data::Dumper;
24 use Env qw(
25     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
26     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
27 );
28 use Pod::Usage;
29 use Switch;
30 use Getopt::Long;
31 use MARC::Batch;
32 use MARC::Record;
33 use MARC::Field;
34 use Cwd 'abs_path';
35 use Cwd qw(getcwd);
36 use FindBin;
37 my $mig_bin = "$FindBin::Bin/";
38 use lib "$FindBin::Bin/";
39 use Mig;
40 use open ':encoding(utf8)';
41
42 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
43 pod2usage(-verbose => 1) if ! $ARGV[1];
44
45 my $file;
46 my $uri_threshold = 1;
47 my $p_holding_code;
48 my $p_barcode_subfield;
49 my $p_ils_name = 'Runtime ILS';
50 my $holding_threshold = 50;
51
52 my $ret = GetOptions(
53     'file:s'                    => \$file,
54         'uri_threshold:i'               => \$uri_threshold,
55         'holding_code:s'                => \$p_holding_code,
56         'barcode:s'                     => \$p_barcode_subfield,
57         'ils_name:s'                    => \$p_ils_name,
58         'holding_threshold:s'   => \$holding_threshold
59 );
60
61 if ($p_holding_code and length $p_holding_code != 3) { abort('Holdings codes must be three characters.'); }
62
63 if ($p_barcode_subfield) {
64         if (!defined $p_holding_code) { abort('A barcode field can not be used without a holding code.'); }
65         if (length $p_barcode_subfield != 1) { abort('Barcode subfields must be a single character code.'); }
66 }
67
68 my @ilses = (
69         ['Mandarin','852','p'],
70         ['Evergreen','852','p'],
71         ['Polaris','852','p'],
72         ['TLC','949','g'],
73         ['Koha','952','p'],
74         ['Sympony','999','i']
75 );
76
77 my @temp;
78 if ($p_holding_code) {
79         push @temp, $p_ils_name;
80         push @temp, $p_holding_code;
81         if ($p_barcode_subfield) { push @temp, lc $p_barcode_subfield; }
82 }
83 push @ilses, @temp;
84
85
86
87 my $batch = MARC::Batch->new('USMARC', $file);
88 $batch->strict_off();
89 my $filetype = `file $file`;
90 if ($filetype =~ m/MARC21/) { print "$filetype.\n" }
91     else { abort("File is not MARC21."); }
92
93 my $i = 0;
94 my $uri_count = 0;
95 my $uri_valid_count = 0;
96 my $uri_sub9_count = 0;
97 my $author_sub0 = 0;
98 my $title_sub0 = 0;
99 my @uris;
100 my @fields;
101 my @codes;
102 my @holding_code_strings;
103 my %holding_counts;
104 my %barcode_counts;
105
106 foreach (@ilses) { 
107         $holding_counts{@$_[0]} = 0; 
108         $barcode_counts{@$_[0]} = 0;
109 }
110
111 while ( my $record = $batch->next() ) {
112     $i++;
113         #check holdings, bit time consuming but more future proof
114         foreach (@ilses) {
115                 my $ils = @$_[0];
116                 my $hcode = @$_[1];
117                 my $barcode = @$_[2];
118                 my @holding_fields = $record->field($hcode);
119                 my $l = scalar @holding_fields;
120                 my $v = $holding_counts{$ils};
121                 if ($l) { $holding_counts{$ils} = $v + $l; }
122         }
123     #process 856s
124         @fields = $record->field('856');
125         my $ldr = substr $record->leader(), 9, 1;
126         push @codes, $ldr;
127         foreach my $f (@fields) {
128                 my $u = $f->subfield('u');
129         my $n = $f->subfield('9');
130         if (defined $n) { $uri_sub9_count++; }
131                 if (defined $u) {
132                         $uri_count++;
133                         my $ind1 = $f->indicator('1');
134                         my $ind2 = $f->indicator('2');
135                         if ($ind1 eq '4') {
136                                 if ($ind2 eq '0' or $ind2 eq '1') { $uri_valid_count++; }
137                         }
138                         my $ustring = lc $f->as_string('u');
139                         $ustring =~ s/http:\/\///;
140             $ustring =~ s/ftp:\/\///;
141                         $ustring =~ s/https:\/\///;
142                         $ustring =~ s/\/.*//;
143                         push @uris, $ustring;
144                 }
145         }
146     #check for authority linking on 100s and 245s, if present may need to scrub them
147         @fields = $record->field('100');
148         foreach my $f (@fields) {
149                 my $t = $f->subfield('0');
150                 if (defined $t) { $title_sub0++; }      
151         }
152     @fields = $record->field('245');
153     foreach my $f (@fields) {
154         my $t = $f->subfield('0');
155         if (defined $t) { $author_sub0++; }
156     }
157     if(($i % 1000) == 0) { print "Processing bib $i.\n"; }
158 }
159
160 my %uri_counts;
161 $uri_counts{$_}++ for @uris;
162
163 my %code_counts;
164 $code_counts{$_}++ for @codes;
165
166 print "\n$filetype\n";
167 print "$i bibs read in file\n\n";
168
169 print "=== Leader 09 codes\n";
170 foreach my $key (keys %code_counts) {
171     my $value = $code_counts{$key};
172     print "=== $key   $value\n"; 
173 }
174 print "\n";
175
176 print "$uri_count 856 fields with a subfield u\n";
177 print "$uri_valid_count 856 fields with a subfield u and valid indicators\n";
178 print "$uri_sub9_count 856 fields have subfield 9s\n";
179 print "$title_sub0 100 fields have a subfield 0\n";
180 print "$author_sub0 245 fields have a subfield 0\n";
181
182 print "\n=== Holdings Analysis\n";
183 foreach my $key (keys %holding_counts) {
184         my $c = $holding_counts{$key};
185         if (((100/$i)*$c) >= $holding_threshold) { print "Could be $key $holding_counts{$key} holdings tags\n"; }
186 }
187
188 print "\nURI values are domains and filtered to only show those with more than $uri_threshold\n";
189 foreach my $key (keys %uri_counts) {
190         my $value = $uri_counts{$key};
191         if ($value > $uri_threshold) { print "=== $key   $value\n"; } 
192 }
193
194 close $file;
195
196 ########### functions
197
198 sub abort {
199     my $msg = shift;
200     print STDERR "$0: $msg", "\n";
201     exit 1;
202 }