11 my $term = new Term::ReadLine 'yaz-cleanup';
12 my $OUT = $term->OUT || \*STDOUT;
18 my @record = (); # current record storage
20 my @linecontext= (); # last 5 lines of file
22 my $input = shift || 'incoming.marc.xml';
24 open MARC, '<', $input;
25 open my $NUMARC, '>', 'incoming.clean.marc.xml';
26 print $NUMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
27 open my $EXMARC, '>', 'incoming.exceptions.marc.xml';
28 print $EXMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
29 open MARC2, '<', $input;
32 # this is the dispatch table which drives command selection in
34 my %commands = ( c => \&print_context,
35 C => \&print_linecontext,
48 my @spinner = qw(- / | \\);
51 while (my $line = getline()) {
52 unless ($count % 2000) {
53 print "\rWorking... ", $spinner[$sidx];
54 $sidx = ($sidx == $#spinner) ? 0 : $sidx + 1;
58 # catch empty datafield elements
59 if ($line =~ m|</datafield>|) {
60 if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
61 pop @record; pop @record;
62 print $OUT "\rEmpty datafield scrubbed at line $count\n";
67 # clean misplaced dollarsigns
68 if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
69 $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
70 print $OUT "\rDollar sign in subfield code corrected at line $count\n";
73 # clean up tags with spaces in them
74 $line =~ s/tag=" /tag="00/g;
75 $line =~ s/tag=" /tag="0/g;
76 $line =~ s/tag="-/tag="0/g;
77 $line =~ s/tag="(\d\d) /tag="0$1/g;
79 # stow tag data if we're looking at it
80 if ($line =~ m/<datafield tag="(.{3})" ind1="(.)" ind2="(.)">/) {
81 $reccontext{tag} = $1;
82 $reccontext{ind1} = $2;
83 $reccontext{ind2} = $3;
87 if ($line =~ /&/ && $line !~ /&\w+?;/)
88 { edit("Naked ampersand", $line); next }
90 # tags must be numeric
91 if ($line =~ /<datafield tag="(.+?)"/) {
94 edit("Non-numerics in tag", $line);
99 # subfields can't be non-alphanumeric
100 if ($line =~ /<subfield code="(.+?)"/) {
102 if ($match =~ /\P{IsAlnum}/) {
103 edit("Junk in subfield code", $line);
109 print $NUMARC "</xml>\n";
110 print $EXMARC "</xml>\n";
111 print $OUT "\nDone. \n";
115 Handles the Term::ReadLine loop
120 my ($msg, $line_in) = @_;
121 print $OUT "\r".$msg, " at line $count (record $reccount)\n";
125 my $line = $term->readline('marc-cleanup>');
126 my @chunks = split /\s+/, $line;
128 if (length $chunks[0] == 1)
129 { next unless (defined $commands{$chunks[0]}) }
131 if (defined $commands{$chunks[0]}) {
132 my $term = $commands{$chunks[0]}->($line_in, @chunks[1..$#chunks]);
135 if ($linecontext[3] eq " [LINE KILLED]\n") {
136 push @record, "$line\n"
138 $record[-1] = "$line\n";
140 $linecontext[3] = "$line\n";
148 Reads from the incoming MARC file; returns lines into the driver
149 loop. Batches records for output, and maintains the linecontext listing.
157 if ($l =~ /<record>/) {
161 } elsif ($l =~ m|</record>|) {
163 write_record($NUMARC) if $reccount;
173 print $FH '<collection xmlns="http://www.loc.gov/MARC21/slim">',"\n";
174 print $FH '<!-- ', $reccontext{explanation}, " -->\n"
175 if(defined $reccontext{explanation});
177 print $FH "</collection>\n";
180 sub update_linecontext {
182 push @linecontext, $line2;
183 shift @linecontext if (@linecontext > 5);
186 #-----------------------------------------------------------------------------------
188 #-----------------------------------------------------------------------------------
191 my ($line_in, $ofrom, $to) = @_;
193 for my $char (split(//,$ofrom)) {
194 $char = "\\" . $char if ($char =~ /\W/);
195 $from = join('', $from, $char);
197 $record[-1] =~ s/$from/$to/;
198 $linecontext[3] = $record[-1];
204 my $last = pop @record;
206 $record[-1] =~ s/\n//;
207 $record[-1] = join('', $record[-1], $last);
209 push @temp, @linecontext[0..1];
210 $temp[3] = $record[-1];
211 $temp[4] = $linecontext[4];
212 @linecontext = @temp;
219 $linecontext[3] = " [LINE KILLED]\n";
225 my ($line_in, @explanation) = @_;
226 $reccontext{explanation} = join(' ', @explanation);
227 my $line = <MARC>; $count++;
228 update_linecontext();
229 until ($line =~ m|</record>|) {
231 $line = <MARC>; $count++;
232 update_linecontext();
235 write_record($EXMARC);
239 sub commit_edit { return 1 }
242 print "\n Tag:",$reccontext{tag}, " Ind1:'",
243 $reccontext{ind1},"' Ind2:'", $reccontext{ind2}, "'";
248 sub print_linecontext {
249 print $OUT "\n", join(' |','',@linecontext[0..2]);
250 print $OUT '==> |', $linecontext[3];
251 print $OUT ' |', $linecontext[4],"\n";
257 print $OUT "\n$line_in\n";
264 Type a replacement for the indicated line, or enter a command.
266 Commands: c Show record context ('C' for brief context)
267 k Kill indicated line (remove from record)
268 m Merge indicated line with previous line
270 s Substitute ARG1 for ARG2 in indicated line
271 t Commit changes and resume stream edit
272 x Write this record to the exception file instead of output