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 next if ($line =~ m|</record>|);
68 # catch empty datafield elements
69 if ($line =~ m|</datafield>|) {
70 if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
71 pop @record; pop @record;
72 message("Empty datafield scrubbed");
78 if ($line =~ m|<leader>(.+?)</leader>|) {
80 if (length $leader < 24) {
81 $leader .= ' ' x (20 - length($leader));
83 $line = "<leader>$leader</leader>\n";
84 message("Short leader padded");
88 # clean misplaced dollarsigns
89 if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
90 $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
91 message("Dollar sign corrected");
94 # clean up tags with spaces in them
95 $line =~ s/tag=" /tag="00/g;
96 $line =~ s/tag=" /tag="0/g;
97 $line =~ s/tag="-/tag="0/g;
98 $line =~ s/tag="(\d\d) /tag="0$1/g;
100 # stow tag data if we're looking at it
101 if ($line =~ m/<datafield tag="(.{3})" ind1="(.)" ind2="(.)">/) {
107 # and stow line back in record
111 if ($line =~ /&/ && $line !~ /&\w+?;/)
112 { edit("Naked ampersand", $line); next }
114 # tags must be numeric
115 if ($line =~ /<datafield tag="(.+?)"/) {
117 if ($match =~ /\D/) {
118 edit("Non-numerics in tag", $line);
123 # subfields can't be non-alphanumeric
124 if ($line =~ /<subfield code="(.+?)"/) {
126 if ($match =~ /\P{IsAlnum}/) {
127 edit("Junk in subfield code", $line);
133 print $NUMARC "</collection>\n";
134 print $EXMARC "</collection>\n";
135 print $OUT "\nDone. \n";
139 Handles the Term::ReadLine loop
144 my ($msg, $line_in) = @_;
145 return if $trash{$recmeta{tag}};
150 my $line = $term->readline('marc-cleanup>');
151 my @chunks = split /\s+/, $line;
153 if (length $chunks[0] == 1)
154 { next unless (defined $commands{$chunks[0]}) }
156 if (defined $commands{$chunks[0]}) {
157 my $term = $commands{$chunks[0]}->($line_in, @chunks[1..$#chunks]);
160 if ($context[3] eq " [LINE KILLED]\n") {
161 push @record, "$line\n"
163 $record[-1] = "$line\n";
165 $context[3] = "$line\n";
173 Reads from the incoming MARC file; returns lines into the driver
174 loop. Batches records for output, and maintains the context listing.
182 if ($l =~ /<record>/) {
186 } elsif ($l =~ m|</record>|) {
187 write_record($NUMARC) if $reccount;
197 $oreccount++ if ($FH eq $NUMARC);
198 print $FH '<!-- ', $recmeta{explanation}, " -->\n"
199 if(defined $recmeta{explanation});
201 # LOOP OVER %trash TO EXCISE UNWANTED TAGS1
205 for my $line (@record) {
207 $istrash = 0 if $line =~ m|</datafield|;
210 if ($line =~ m/<datafield tag="(.{3})"/) {
212 if ($trash{$tag} or $tag =~ /\D/) {
217 push @trimmed, $line;
222 print $FH '</record>\n';
225 sub update_linecontext {
227 push @context, $line2;
228 shift @context if (@context > 5);
233 print $OUT "\r$msg at record $reccount/",$oreccount + 1,"\n";
237 #-----------------------------------------------------------------------------------
239 #-----------------------------------------------------------------------------------
242 my ($line_in, @chunks) = @_;
243 my $ofrom = shift @chunks;
244 if ($ofrom =~ /^'/ or !@chunks) {
245 until ($ofrom =~ /'$/)
246 { $ofrom .= join(' ','',shift @chunks) }
247 $ofrom =~ s/^'//; $ofrom =~ s/'$//;
249 my $to = shift @chunks;
251 until ($to =~ /'$/ or !@chunks)
252 { $to .= join(' ','',shift @chunks) }
253 $to =~ s/^'//; $to =~ s/'$//;
257 for my $char (split(//,$ofrom)) {
258 $char = "\\" . $char if ($char =~ /\W/);
259 $from = join('', $from, $char);
261 $record[-1] =~ s/$from/$to/;
262 $context[3] = $record[-1];
268 my $last = pop @record;
270 $record[-1] =~ s/\n//;
271 $record[-1] = join('', $record[-1], $last);
273 push @temp, @context[0..1];
274 $temp[3] = $record[-1];
275 $temp[4] = $context[4];
283 $context[3] = " [LINE KILLED]\n";
289 my ($line_in, @explanation) = @_;
290 $recmeta{explanation} = join(' ', 'Tag', $recmeta{tag}, @explanation);
291 my $line = <MARC>; $count++;
292 update_linecontext();
293 until ($line =~ m|</record>|) {
295 $line = <MARC>; $count++;
296 update_linecontext();
299 write_record($EXMARC);
303 sub commit_edit { return 1 }
306 print "\n Tag:",$recmeta{tag}, " Ind1:'",
307 $recmeta{ind1},"' Ind2:'", $recmeta{ind2}, "'";
312 sub print_linecontext {
313 print $OUT "\n", join(' |','',@context[0..2]);
314 print $OUT '==> |', $context[3];
315 print $OUT ' |', $context[4],"\n";
321 print $OUT "\n$line_in\n";
328 Type a replacement for the indicated line, or enter a command.
330 Commands: c Show record context ('C' for brief context)
331 k Kill indicated line (remove from record)
332 m Merge indicated line with previous line
334 s Substitute ARG1 for ARG2 in indicated line
335 t Commit changes and resume stream edit
336 x Write this record to the exception file instead of output
345 #-----------------------------------------------------------------------------------
347 #-----------------------------------------------------------------------------------
348 # defined a domain-specific language for specifying MARC tags to be dropped from
349 # records during processing. it is line oriented, and is specified as follows:
351 # each line may specify any number of tags to be included, either singly (\d{1,3})
352 # or as a range (\d{1,3}\.\.\d{1,3}
354 # if a single number is given, it must be between '000' and '999', inclusive.
356 # ranges obey the previous rule, and also the first number of the range must be less
357 # than the second number
359 # finally, any single range in a line may be followed by the keyword 'except'. every
360 # number or range after 'except' is excluded from the range specified. all these
361 # numbers must actually be within the range.
363 # specifying a tag twice is an error, to help prevent typos
366 open TRASH, '<', '.trashtags';
368 my $lastwasrange = 0;
369 my %lastrange = ( high => 0, low => 0);
372 my @chunks = split /\s+/;
373 while (my $chunk = shift @chunks) {
376 if ($chunk =~ /^\d{1,3}$/) {
377 trash_add($chunk, $except);
383 if ($chunk =~ /^\d{1,3}\.\.\d{1,3}$/) {
384 my ($low, $high) = trash_add_range($chunk, $except, \%lastrange);
386 %lastrange = (low => $low, high => $high)
392 if ($chunk eq 'except') {
393 die "Keyword 'except' can only follow a range (line $.)\n"
394 unless $lastwasrange;
395 die "Keyword 'except' may only occur once per line (line $.)\n"
401 die "Unknown chunk $chunk in .trashtags file (line $.)\n";
406 sub trash_add_range {
407 my ($chunk, $except, $range) = @_;
408 my ($low,$high) = split /\.\./, $chunk;
409 die "Ranges must be 'low..high' ($low is greater than $high on line $.)\n"
412 die "Exception ranges must be within last addition range (line $.)\n"
413 if ($low < $range->{low} or $high > $range->{high});
415 for my $tag ($low..$high) {
416 trash_add($tag, $except)
422 my ($tag, $except) = @_;
423 die "Trash values must be valid tags (000-999)\n"
424 unless ($tag >= 0 and $tag <= 999);
428 die "Trash tag '$tag' specified twice (line $.)\n"