11 my $term = new Term::ReadLine 'yaz-cleanup';
12 my $OUT = $term->OUT || \*STDOUT;
18 my %trash = (); # hash for tags to be dumped
20 # read in trash tags file if it exists
21 populate_trash() if (-e '.trashtags');
23 my @record = (); # current record storage
24 my %recmeta = (); # metadata about current record
25 my @context= (); # last 5 lines of file
27 my $input = shift || 'incoming.marc.xml';
29 open MARC, '<', $input;
30 open my $NUMARC, '>', 'incoming.clean.marc.xml';
31 print $NUMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
32 print $NUMARC '<collection xmlns="http://www.loc.gov/MARC21/slim">',"\n";
34 open my $EXMARC, '>', 'incoming.exceptions.marc.xml';
35 print $EXMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
36 print $EXMARC '<collection xmlns="http://www.loc.gov/MARC21/slim">',"\n";
37 open MARC2, '<', $input;
40 # this is the dispatch table which drives command selection in
42 my %commands = ( c => \&print_context,
43 C => \&print_linecontext,
56 my @spinner = qw(- / | \\);
59 while (my $line = getline()) {
60 unless ($count % 2000) {
61 print "\rWorking... ", $spinner[$sidx];
62 $sidx = ($sidx == $#spinner) ? 0 : $sidx + 1;
66 # catch empty datafield elements
67 if ($line =~ m|</datafield>|) {
68 if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
69 pop @record; pop @record;
70 message("Empty datafield scrubbed");
76 if ($line =~ m|<leader>(.+?)</leader>|) {
78 if (length $leader < 24) {
79 $leader .= ' ' x (20 - length($leader));
81 $line = "<leader>$leader</leader>\n";
82 message("Short leader padded");
86 # clean misplaced dollarsigns
87 if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
88 $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
89 message("Dollar sign corrected");
92 # clean up tags with spaces in them
93 $line =~ s/tag=" /tag="00/g;
94 $line =~ s/tag=" /tag="0/g;
95 $line =~ s/tag="-/tag="0/g;
96 $line =~ s/tag="(\d\d) /tag="0$1/g;
98 # stow tag data if we're looking at it
99 if ($line =~ m/<datafield tag="(.{3})" ind1="(.)" ind2="(.)">/) {
105 # and stow line back in record
109 if ($line =~ /&/ && $line !~ /&\w+?;/)
110 { edit("Naked ampersand", $line); next }
112 # tags must be numeric
113 if ($line =~ /<datafield tag="(.+?)"/) {
115 if ($match =~ /\D/) {
116 edit("Non-numerics in tag", $line);
121 # subfields can't be non-alphanumeric
122 if ($line =~ /<subfield code="(.+?)"/) {
124 if ($match =~ /\P{IsAlnum}/) {
125 edit("Junk in subfield code", $line);
131 print $NUMARC "</collection>\n";
132 print $EXMARC "</collection>\n";
133 print $OUT "\nDone. \n";
137 Handles the Term::ReadLine loop
142 my ($msg, $line_in) = @_;
143 return if $trash{$recmeta{tag}};
148 my $line = $term->readline('marc-cleanup>');
149 my @chunks = split /\s+/, $line;
151 if (length $chunks[0] == 1)
152 { next unless (defined $commands{$chunks[0]}) }
154 if (defined $commands{$chunks[0]}) {
155 my $term = $commands{$chunks[0]}->($line_in, @chunks[1..$#chunks]);
158 if ($context[3] eq " [LINE KILLED]\n") {
159 push @record, "$line\n"
161 $record[-1] = "$line\n";
163 $context[3] = "$line\n";
171 Reads from the incoming MARC file; returns lines into the driver
172 loop. Batches records for output, and maintains the context listing.
180 if ($l =~ /<record>/) {
184 } elsif ($l =~ m|</record>|) {
186 write_record($NUMARC) if $reccount;
196 $oreccount++ if ($FH eq $NUMARC);
197 print $FH '<!-- ', $recmeta{explanation}, " -->\n"
198 if(defined $recmeta{explanation});
202 sub update_linecontext {
204 push @context, $line2;
205 shift @context if (@context > 5);
210 print $OUT "\r$msg at record $reccount/",$oreccount + 1,"\n";
214 #-----------------------------------------------------------------------------------
216 #-----------------------------------------------------------------------------------
219 my ($line_in, @chunks) = @_;
220 my $ofrom = shift @chunks;
221 if ($ofrom =~ /^'/ or !@chunks) {
222 until ($ofrom =~ /'$/)
223 { $ofrom .= join(' ','',shift @chunks) }
224 $ofrom =~ s/^'//; $ofrom =~ s/'$//;
226 my $to = shift @chunks;
228 until ($to =~ /'$/ or !@chunks)
229 { $to .= join(' ','',shift @chunks) }
230 $to =~ s/^'//; $to =~ s/'$//;
234 for my $char (split(//,$ofrom)) {
235 $char = "\\" . $char if ($char =~ /\W/);
236 $from = join('', $from, $char);
238 $record[-1] =~ s/$from/$to/;
239 $context[3] = $record[-1];
245 my $last = pop @record;
247 $record[-1] =~ s/\n//;
248 $record[-1] = join('', $record[-1], $last);
250 push @temp, @context[0..1];
251 $temp[3] = $record[-1];
252 $temp[4] = $context[4];
260 $context[3] = " [LINE KILLED]\n";
266 my ($line_in, @explanation) = @_;
267 $recmeta{explanation} = join(' ', @explanation);
268 my $line = <MARC>; $count++;
269 update_linecontext();
270 until ($line =~ m|</record>|) {
272 $line = <MARC>; $count++;
273 update_linecontext();
276 write_record($EXMARC);
280 sub commit_edit { return 1 }
283 print "\n Tag:",$recmeta{tag}, " Ind1:'",
284 $recmeta{ind1},"' Ind2:'", $recmeta{ind2}, "'";
289 sub print_linecontext {
290 print $OUT "\n", join(' |','',@context[0..2]);
291 print $OUT '==> |', $context[3];
292 print $OUT ' |', $context[4],"\n";
298 print $OUT "\n$line_in\n";
305 Type a replacement for the indicated line, or enter a command.
307 Commands: c Show record context ('C' for brief context)
308 k Kill indicated line (remove from record)
309 m Merge indicated line with previous line
311 s Substitute ARG1 for ARG2 in indicated line
312 t Commit changes and resume stream edit
313 x Write this record to the exception file instead of output
322 #-----------------------------------------------------------------------------------
324 #-----------------------------------------------------------------------------------
325 # defined a domain-specific language for specifying MARC tags to be dropped from
326 # records during processing. it is line oriented, and is specified as follows:
328 # each line may specify any number of tags to be included, either singly (\d{1,3})
329 # or as a range (\d{1,3}\.\.\d{1,3}
331 # if a single number is given, it must be between '000' and '999', inclusive.
333 # ranges obey the previous rule, and also the first number of the range must be less
334 # than the second number
336 # finally, any single range in a line may be followed by the keyword 'except'. every
337 # number or range after 'except' is excluded from the range specified. all these
338 # numbers must actually be within the range.
340 # specifying a tag twice is an error, to help prevent typos
343 open TRASH, '<', '.trashtags';
345 my $lastwasrange = 0;
346 my %lastrange = ( high => 0, low => 0);
349 my @chunks = split /\s+/;
350 while (my $chunk = shift @chunks) {
353 if ($chunk =~ /^\d{1,3}$/) {
354 trash_add($chunk, $except);
360 if ($chunk =~ /^\d{1,3}\.\.\d{1,3}$/) {
361 my ($low, $high) = trash_add_range($chunk, $except, \%lastrange);
363 %lastrange = (low => $low, high => $high)
369 if ($chunk eq 'except') {
370 die "Keyword 'except' can only follow a range (line $.)\n"
371 unless $lastwasrange;
372 die "Keyword 'except' may only occur once per line (line $.)\n"
378 die "Unknown chunk $chunk in .trashtags file (line $.)\n";
381 print $OUT join ",", (sort keys %trash);
385 sub trash_add_range {
386 my ($chunk, $except, $range) = @_;
387 my ($low,$high) = split /\.\./, $chunk;
388 die "Ranges must be 'low..high' ($low is greater than $high on line $.)\n"
391 die "Exception ranges must be within last addition range (line $.)\n"
392 if ($low < $range->{low} or $high > $range->{high});
394 for my $tag ($low..$high) {
395 trash_add($tag, $except)
401 my ($tag, $except) = @_;
402 die "Trash values must be valid tags (000-999)\n"
403 unless ($tag >= 0 and $tag <= 999);
407 die "Trash tag '$tag' specified twice (line $.)\n"