2 # -*- coding: iso-8859-15 -*-
3 ###############################################################################
6 =item B<bibstats> --file foo.mrc
8 Reads through a marc file to generate statistical information about the file
11 --uri_threshold defaults to 1, only shows URI values with more than that
18 ###############################################################################
25 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
26 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
37 my $mig_bin = "$FindBin::Bin/";
38 use lib "$FindBin::Bin/";
40 use open ':encoding(utf8)';
42 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
43 pod2usage(-verbose => 1) if ! $ARGV[1];
46 my $uri_threshold = 1;
48 my $p_barcode_subfield;
49 my $p_ils_name = 'Runtime ILS';
50 my $holding_threshold = 50;
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
61 if ($p_holding_code and length $p_holding_code != 3) { abort('Holdings codes must be three characters.'); }
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.'); }
69 ['Mandarin','852','p'],
70 ['Evergreen','852','p'],
71 ['Polaris','852','p'],
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; }
87 my $batch = MARC::Batch->new('USMARC', $file);
89 my $filetype = `file $file`;
90 if ($filetype =~ m/MARC21/) { print "$filetype.\n" }
91 else { abort("File is not MARC21."); }
95 my $uri_valid_count = 0;
96 my $uri_sub9_count = 0;
102 my @holding_code_strings;
107 $holding_counts{@$_[0]} = 0;
108 $barcode_counts{@$_[0]} = 0;
111 while ( my $record = $batch->next() ) {
113 #check holdings, bit time consuming but more future proof
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; }
124 @fields = $record->field('856');
125 my $ldr = substr $record->leader(), 9, 1;
127 foreach my $f (@fields) {
128 my $u = $f->subfield('u');
129 my $n = $f->subfield('9');
130 if (defined $n) { $uri_sub9_count++; }
133 my $ind1 = $f->indicator('1');
134 my $ind2 = $f->indicator('2');
136 if ($ind2 eq '0' or $ind2 eq '1') { $uri_valid_count++; }
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;
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++; }
152 @fields = $record->field('245');
153 foreach my $f (@fields) {
154 my $t = $f->subfield('0');
155 if (defined $t) { $author_sub0++; }
157 if(($i % 1000) == 0) { print "Processing bib $i.\n"; }
161 $uri_counts{$_}++ for @uris;
164 $code_counts{$_}++ for @codes;
166 print "\n$filetype\n";
167 print "$i bibs read in file\n\n";
169 print "=== Leader 09 codes\n";
170 foreach my $key (keys %code_counts) {
171 my $value = $code_counts{$key};
172 print "=== $key $value\n";
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";
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"; }
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"; }
196 ########### functions
200 print STDERR "$0: $msg", "\n";