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
14 --ignore_filetype true will have it not care what file returns as the type and
15 always treat it as marc21
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
21 --exportbarcodes ils_name is used if you want to export the barcodes associated
22 with one of the ILSes so provide the name
24 --exportbarcodesfile will use this file name for a barcode export instead
25 of the generic 'barcodes_export.txt'
30 ###############################################################################
37 HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
38 MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
48 use List::MoreUtils qw(uniq);
50 my $mig_bin = "$FindBin::Bin/";
51 use lib "$FindBin::Bin/";
54 use open ':encoding(utf8)';
56 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
57 pod2usage(-verbose => 1) if ! $ARGV[1];
60 my $uri_threshold = 1;
62 my $p_barcode_subfield;
64 my $holding_threshold = 50;
65 my $p_ignore_filetype = 'false';
69 my $exportbarcodesfile;
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
83 if ($exportbarcodesfile and !defined $exportbarcodes) { abort('You have to provide an ILS name if you want a barcode export file.'); }
85 if ($p_holding_code and length $p_holding_code != 3) { abort('Holdings codes must be three characters.'); }
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.'); }
92 # ils name, holding tag, barcode subfield
94 ['Mandarin','852','p'],
95 ['Evergreen','852','p'],
96 ['Polaris','852','p'],
99 ['Symphony','999','i'],
100 ['Destiny','852','p']
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];
111 #to do - add a check for exportbarcodes being in @ilses
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."); }
121 my $uri_valid_count = 0;
122 my $uri_sub9_count = 0;
129 my @holding_code_strings;
134 $holding_counts{@$_[0]} = 0;
135 $barcode_counts{@$_[0]} = 0;
138 while ( my $record = $batch->next() ) {
140 #check holdings, bit time consuming but more future proof
144 my $barcode = @$_[2];
145 my @holding_fields = $record->field($hcode);
146 foreach my $hf (@holding_fields) {
148 my $barcode_string = $hf->subfield($barcode);
150 push @h, $barcode_string;
151 push @holdings, [@h];
153 my $l = scalar @holding_fields;
154 my $v = $holding_counts{$ils};
155 if ($l) { $holding_counts{$ils} = $v + $l; }
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;
163 foreach my $f (@fields) {
164 my $u = $f->subfield('u');
165 my $n = $f->subfield('9');
166 if (defined $n) { $uri_sub9_count++; }
169 my $ind1 = $f->indicator('1');
170 my $ind2 = $f->indicator('2');
172 if ($ind2 eq '0' or $ind2 eq '1') { $uri_valid_count++; }
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;
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++; }
188 @fields = $record->field('245');
189 foreach my $f (@fields) {
190 my $t = $f->subfield('0');
191 if (defined $t) { $author_sub0++; }
193 if(($i % 1000) == 0) { print "Processing bib $i.\n"; }
199 foreach my $h (@holdings) {
200 my $temp_ils_name = @$h[0];
201 if ($temp_ils_name eq $ils) { push @temp_barcodes, @$h[1]; }
203 my @uniq_barcodes = uniq @temp_barcodes;;
204 $barcode_counts{$ils} = scalar @uniq_barcodes;
208 $uri_counts{$_}++ for @uris;
211 $encoding_counts{$_}++ for @encodings;
214 $type_counts{$_}++ for @types;
216 print "\n$filetype\n";
217 print "$i bibs read in file\n\n";
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";
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";
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";
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) {
245 if ($key ne 'Symphony') { print " $key - $holding_counts{$key} holdings in $i bibs with $barcode_counts{$key} unique barcodes\n"; }
246 else { print " $key - $holding_counts{$key} holdings in $i bibs with $barcode_counts{$key} unique barcodes - 999 FIELDS MUST BE MOVED WITH kmig prepbibs\n"; }
250 print "\n===== URI values are domains and filtered to only show those with more than $uri_threshold\n";
251 foreach my $key (keys %uri_counts) {
252 my $value = $uri_counts{$key};
253 if ($value > $uri_threshold) { print " $key $value\n"; }
256 if ($exportbarcodes) {
259 if ($exportbarcodesfile) { $outfile = $exportbarcodesfile; } else { $outfile = 'barcodes_export.txt'; }
260 open my $out_fh, '>:utf8', $outfile or abort('can not open output file for barcode list');
261 foreach my $h (@holdings) {
262 my $temp_ils_name = @$h[0];
263 my $barcode = @$h[1];
264 if (!defined $barcode) { $barcode = 'no barcode found'; }
265 if ($temp_ils_name eq $exportbarcodes) { print $out_fh "@$h[1]\n" }
268 } else { print "No barcodes being exported.\n"; }
272 ########### functions
276 print STDERR "$0: $msg", "\n";
282 if ($type eq 'a') { return 'Language material'; }
283 if ($type eq 'c') { return 'Notated Music'; }
284 if ($type eq 'd') { return 'Manuscript notated music'; }
285 if ($type eq 'e') { return 'Cartographic material'; }
286 if ($type eq 'f') { return 'Manuscript cartographic material'; }
287 if ($type eq 'g') { return 'Projected Medium'; }
288 if ($type eq 'i') { return 'Nonmusical sound recording'; }
289 if ($type eq 'j') { return 'Musical sound recording'; }
290 if ($type eq 'k') { return 'Two-dimensional nonprojectable graphic'; }
291 if ($type eq 'm') { return 'Computer file'; }
292 if ($type eq 'o') { return 'Kit'; }
293 if ($type eq 'p') { return 'Mixed materials'; }
294 if ($type eq 'r') { return 'Three-dimensaional artifact or naturally occurring object'; }
295 if ($type eq 't') { return 'Manuscript language material'; }
296 if ($type eq 'z') { return 'Authority'; }