3 use MARC::File::XML ( BinaryEncoding => 'utf-8' );
5 use Unicode::Normalize;
9 my $id_tag = $ARGV[1]; my $id_subfield = $ARGV[2];
11 binmode(STDOUT, ':utf8');
12 binmode(STDIN, ':utf8');
14 foreach $argnum ( 3 .. $#ARGV ) {
16 print STDERR "Processing " . $ARGV[$argnum] . "\n";
18 my $batch = MARC::Batch->new('XML',$ARGV[$argnum]);
20 $batch->warnings_off();
26 $record = $batch->next();
28 my $id = $record->field($id_tag);
30 print STDERR "ERROR: This record is missing a $id_tag field.\n" . $record->as_formatted() . "\n=====\n";
33 $id = $id->as_string($id_subfield);
34 print STDERR "WARNINGS: Record id " . $id . " : " . join(":",@warnings) . " : continuing...\n" if ( @warnings );
36 my $leader = $record->leader();
37 my $record_type = substr($leader,6,1);
38 my $bib_lvl = substr($leader,7,1);
40 my $my_008 = $record->field('008');
41 $my_008 = $my_008->as_string() if ($my_008);
42 my $date1 = substr($my_008,7,4) if ($my_008);
43 my $date2 = substr($my_008,11,4) if ($my_008);
45 if ( $record_type =~ /[gkroef]/ ) { # MAP, VIS
46 $item_form = substr($my_008,29,1) if ($my_008);
48 $item_form = substr($my_008,23,1) if ($my_008);
51 my $title = $record->field('245');
52 if ( $title ) { $title = $title->subfield('a'); }
55 my @isbns_020; if ($record->field('020')) { @isbns_020 = $record->field('020'); }
56 foreach my $f ( @isbns_020 ) { if ($f->subfield('a')) { if ( $f->subfield('a')=~/(\S+)/ ) { push @isbns, $1; } } }
57 my @isbns_024; if ($record->field('024')) { @isbns_024 = $record->field('024'); }
58 foreach my $f ( @isbns_024 ) { if ($f->subfield('a')) { if ( $f->subfield('a')=~/(\S+)/ ) { push @isbns, $1; } } }
60 my $issn = $record->field('022');
61 if ( $issn ) { $issn = $issn->subfield('a'); }
62 my $lccn = $record->field('010');
63 if ( $lccn ) { $lccn = $lccn->subfield('a'); }
65 if ($record->field('100')) { $author = $record->field('100')->subfield('a'); }
67 if ($record->field('110')) { $author = $record->field('110')->subfield('a'); }
70 if ($record->field('111')) { $author = $record->field('111')->subfield('a'); }
72 my $desc = $record->field('300');
73 if ( $desc ) { $desc = $desc->subfield('a'); }
75 if ($desc =~ /(\d+)/) { $pages = $1; }
76 my $my_260 = $record->field('260');
77 my $publisher = $my_260->subfield('b') if ( $my_260 );
78 my $pubyear = $my_260->subfield('c') if ( $my_260 );
80 if ( $pubyear =~ /(\d\d\d\d)/ ) { $pubyear = $1; } else { $pubyear = ''; }
82 my $edition = $record->field('250');
83 if ( $edition ) { $edition = $edition->subfield('a'); }
86 if ($record_type == ' ') { $record_type = 'a'; }
88 $title = NFD($title); $title =~ s/[\x{80}-\x{ffff}]//go;
93 $author = NFD($author); $author =~ s/[\x{80}-\x{ffff}]//go;
94 $author = lc($author);
95 $author =~ s/\W+$//go;
96 if ($author =~ /^(\w+)/) {
101 $publisher = NFD($publisher); $publisher =~ s/[\x{80}-\x{ffff}]//go;
102 $publisher = lc($publisher);
103 $publisher =~ s/\W+$//go;
104 if ($publisher =~ /^(\w+)/) {
109 # SPIT OUT FINGERPRINTS FROM THE "LOIS ALGORITHM"
110 # If we're not getting good matches, we may want to change this. The same thing goes for some other fields.
111 if ($item_form && ($date1 =~ /\d\d\d\d/) && $record_type && $bib_lvl && $title) {
113 if ($which eq "primary") {
114 print STDOUT join("\t",$id,$item_form,$date1,$record_type,$bib_lvl,$title) . "\n";
117 # case a : isbn and pages
118 if (scalar(@isbns)>0 && $pages) {
119 foreach my $isbn ( @isbns ) {
120 print STDOUT join("\t",$id,"case a",$item_form,$date1,$record_type,$bib_lvl,$title,$isbn,$pages) . "\n";
126 print STDOUT join("\t",$id,"case b",$item_form,$date1,$record_type,$bib_lvl,$title,$edition) . "\n";
131 print STDOUT join("\t",$id,"case c",$item_form,$date1,$record_type,$bib_lvl,$title,$issn) . "\n";
136 print STDOUT join("\t",$id,"case d",$item_form,$date1,$record_type,$bib_lvl,$title,$lccn) . "\n";
139 # case e : author, publisher, pubyear, pages
140 if ($author && $publisher && $pubyear && $pages) {
141 print STDOUT join("\t",$id,"case e",$item_form,$date1,$record_type,$bib_lvl,$title,$author,$publisher,$pubyear,$pages) . "\n";
147 print STDERR "Record " . $id . " did not make the cut: ";
148 print STDERR "Missing item_form. " unless ($item_form);
149 print STDERR "Missing valid date1. " unless ($date1 =~ /\d\d\d\d/);
150 print STDERR "Missing record_type. " unless ($record_type);
151 print STDERR "Missing bib_lvl. " unless ($bib_lvl);
152 print STDERR "Missing title. " unless ($title);
157 print STDERR "Trapped exception for MARC::Batch->next on record $count: $@\n" if ($@);
159 print STDERR "Processed $count records\n";