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