| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head2 NAME | ||
| 4 | |||
| 5 | Lintallchecks -- Uses MARC::Lint, MARC::Lintadditions, and MARC::Errorchecks | ||
| 6 | to look for MARC21, AACR2/LCRI coding problems in a file of MARC21 records. | ||
| 7 | |||
| 8 | =head2 DESCRIPTION | ||
| 9 | |||
| 10 | Lint test program prompts for input file of MARC records. | ||
| 11 | Compares the records against coding definitions in Lint module. | ||
| 12 | Also checks against added checks in MARC::Lintadditions. | ||
| 13 | Also checks for errors in multiple fields (vs. individual fields covered by Lint/Lintadditions. | ||
| 14 | Writes out one file: | ||
| 15 | lintwarnings.txt (reported warnings and associated control numbers and titles, tab separation) | ||
| 16 | Differs from linttest.pl (lintcheck.txt) by not outputting raw MARC. | ||
| 17 | Differs from lintcheck2.pl by outputting only one file | ||
| 18 | (vs. separate warnings and control no files), | ||
| 19 | and in checking additional conditions given in Lintadditions.pm. | ||
| 20 | Differs from lintwithadditions.pl in that it incorporates checks which cross multiple fields (such as validation of 008, which requires access to the leader). | ||
| 21 | This code is based on Example V3 of the MARC::Doc::Tutorial, and incorporates code from the following (available on my Web site): | ||
| 22 | |||
| 23 | 003cleanupscript.pl | ||
| 24 | 008checker.pl | ||
| 25 | 010cleanup.pl | ||
| 26 | checkcipforstockno.pl | ||
| 27 | findmultiplespacesafter010.pl | ||
| 28 | |||
| 29 | It uses the MARC::Errorchecks module for checking code not covered in MARC::Lint or MARC::Lintadditions. | ||
| 30 | |||
| 31 | =head2 REQUIREMENTS | ||
| 32 | |||
| 33 | MARC::Record | ||
| 34 | MARC::Lintadditions | ||
| 35 | MARC::Errorchecks | ||
| 36 | MARC::BBMARC (for interface--timing code) | ||
| 37 | |||
| 38 | =head2 TO DO | ||
| 39 | |||
| 40 | Improve interface, particularly file name input. | ||
| 41 | |||
| 42 | For users of this script: Modify section on which errors not to report. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | ########################### | ||
| 47 | ### Initialize includes ### | ||
| 48 | ### and basic needs ### | ||
| 49 | ########################### | ||
| 50 | use strict; | ||
| 51 | use MARC::Batch; | ||
| 52 | use MARC::Lintadditions; | ||
| 53 | use MARC::Errorchecks; | ||
| 54 | use MARC::BBMARC; | ||
| 55 | |||
| 56 | ########################## | ||
| 57 | ## Time coding routines ## | ||
| 58 | ## Print start time and ## | ||
| 59 | ## set start variable ## | ||
| 60 | ########################## | ||
| 61 | |||
| 62 | use Time::HiRes qw( tv_interval ); | ||
| 63 | # measure elapsed time | ||
| 64 | my $t0 = [Time::HiRes::time()]; | ||
| 65 | my $startingtime = MARC::BBMARC::startstop_time(); | ||
| 66 | ######################### | ||
| 67 | ### Start main program ## | ||
| 68 | ######################### | ||
| 69 | |||
| 70 | print ("Welcome to Lint All Checks\n"); | ||
| 71 | |||
| 72 | ##### File handling initialization ###### | ||
| 73 | #prompt for input file | ||
| 74 | print ("What is the input file? "); | ||
| 75 | my $inputfile=<>; | ||
| 76 | chomp $inputfile; | ||
| 77 | $inputfile =~ s/^\"(.*)\"$/$1/; | ||
| 78 | print ("What is the export file? "); | ||
| 79 | my $exportfile = <>; | ||
| 80 | chomp $exportfile; | ||
| 81 | $exportfile =~ s/^\"(.*)\"$/$1/; | ||
| 82 | #protect against overwriting input file | ||
| 83 | if ($inputfile =~ /^\Q$exportfile\E$/i) { | ||
| 84 | print "Input file and export file are identical.\nProgram will exit now. Press Enter to continue\n"; | ||
| 85 | <>; | ||
| 86 | die; | ||
| 87 | } | ||
| 88 | #check to see if export file exists | ||
| 89 | if (-f $exportfile) { | ||
| 90 | print "That file, $exportfile exists already.\nOverwrite? "; | ||
| 91 | my $continue = <>; | ||
| 92 | chomp $continue; | ||
| 93 | unless ($continue =~ /^y(es)?$/i) { | ||
| 94 | #exit program if user typed anything other than y or yes (in any cap) | ||
| 95 | print "Exiting (press Enter)\n"; <>; die; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | open(OUT, ">$exportfile") or die "Can not open $exportfile, $!"; | ||
| 99 | |||
| 100 | #if using MacPerl, set creator and type to BBEdit and Text | ||
| 101 | if ($^O eq 'MacOS') { | ||
| 102 | MacPerl::SetFileInfo('R*ch', 'TEXT', $exportfile); | ||
| 103 | } | ||
| 104 | |||
| 105 | #initialize $batch as new MARC::Batch object | ||
| 106 | my $batch = MARC::Batch->new('USMARC', "$inputfile"); | ||
| 107 | ########## Start extraction ######### | ||
| 108 | |||
| 109 | ############################################ | ||
| 110 | # Set start time for main calculation loop # | ||
| 111 | ############################################ | ||
| 112 | my $t1 = [Time::HiRes::time()]; | ||
| 113 | my $runningrecordcount=0; | ||
| 114 | ################################################### | ||
| 115 | |||
| 116 | my $linter = MARC::Lintadditions->new(); | ||
| 117 | |||
| 118 | my $counter = 0; | ||
| 119 | my $errorcount = 0; | ||
| 120 | |||
| 121 | while (my $record = $batch->next()) { | ||
| 122 | $counter++; | ||
| 123 | my @haswarnings = (); | ||
| 124 | |||
| 125 | #call MARC::Lintadditions (which also performs MARC::Lint checks) | ||
| 126 | $linter->check_record($record); | ||
| 127 | |||
| 128 | #get control number for error reporting | ||
| 129 | my $controlno =$record->field('001')->as_string() if $record->field('001'); | ||
| 130 | |||
| 131 | my $titlea = $record->field('245')->subfield('a'); | ||
| 132 | |||
| 133 | #if controlnumber doesn't exist, report as error and use title | ||
| 134 | unless ($controlno) { | ||
| 135 | push @haswarnings, "001: Control number field not found."; | ||
| 136 | $controlno = $titlea; | ||
| 137 | } #unless controlno was found | ||
| 138 | |||
| 139 | # Retrieve errors that were found by Lint and Lintadditions | ||
| 140 | push @haswarnings, ($linter->warnings()); | ||
| 141 | |||
| 142 | #call MARC::Errorchecks | ||
| 143 | my @errorstoreturn = (); | ||
| 144 | push @errorstoreturn, (@{MARC::Errorchecks::check_all_subs($record)}); | ||
| 145 | |||
| 146 | #Remove unwanted warnings | ||
| 147 | # e.g. | ||
| 148 | my @errstoreturn = grep {$_ !~ /(has non\-digits)|(Record is coded level 1)|(Record is coded level 2)/} @errorstoreturn; | ||
| 149 | |||
| 150 | #add MARC::Errorchecks warnings to those found by Lint and Lintadditions | ||
| 151 | |||
| 152 | push @haswarnings, @errstoreturn; | ||
| 153 | if (@haswarnings){ | ||
| 154 | print OUT join( "\t", "$controlno", "$titlea", @haswarnings, "\t\n"); | ||
| 155 | $errorcount++ | ||
| 156 | } | ||
| 157 | ################################################### | ||
| 158 | ### add to count for user notification ### | ||
| 159 | $runningrecordcount++; | ||
| 160 | MARC::BBMARC::counting_print ($runningrecordcount); | ||
| 161 | ################################################### | ||
| 162 | } # while | ||
| 163 | |||
| 164 | close $inputfile; | ||
| 165 | close OUT; | ||
| 166 | |||
| 167 | print "$counter records scanned\n$errorcount errors found\n"; | ||
| 168 | ########################## | ||
| 169 | ### Main program done. ## | ||
| 170 | ### Report elapsed time.## | ||
| 171 | ########################## | ||
| 172 | |||
| 173 | my $elapsed = tv_interval ($t0); | ||
| 174 | my $calcelapsed = tv_interval ($t1); | ||
| 175 | print sprintf ("%.4f %s\n", "$elapsed", "seconds from execution\n"); | ||
| 176 | print sprintf ("%.4f %s\n", "$calcelapsed", "seconds to calculate\n"); | ||
| 177 | my $endingtime = MARC::BBMARC::startstop_time(); | ||
| 178 | print "Started at $startingtime\nEnded at $endingtime"; | ||
| 179 | |||
| 180 | |||
| 181 | print "\n\nPress Enter to quit"; | ||
| 182 | <>; | ||
| 183 | |||
| 184 | |||
| 185 | ##################### | ||
| 186 | ### END OF PROGRAM ## | ||
| 187 | ##################### | ||
| 188 | |||
| 189 | =head1 LICENSE | ||
| 190 | |||
| 191 | This code may be distributed under the same terms as Perl itself. | ||
| 192 | |||
| 193 | Please note that this code is not a product of or supported by the | ||
| 194 | employers of the various contributors to the code. | ||
| 195 | |||
| 196 | =head1 AUTHOR | ||
| 197 | |||
| 198 | Bryan Baldus | ||
| 199 | eijabb@cpan.org | ||
| 200 | |||
| 201 | Copyright (c) 2003-2004 | ||
| 202 | |||
| 203 | =cut |