9 my $term = new Term::ReadLine 'yaz-cleanup';
10 my $OUT = $term->OUT || \*STDOUT;
12 # initialization and setup
15 populate_trash() if ($conf->{trash});
17 my @record = (); # current record storage
18 my %recmeta = (); # metadata about current record
19 my $ptr = 0; # record index pointer
21 my $input = shift || 'incoming.marc.xml';
23 open MARC, '<:utf8', $input;
24 open my $NUMARC, '>:utf8', $conf->{output};
25 print $NUMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
26 print $NUMARC '<collection xmlns="http://www.loc.gov/MARC21/slim">',"\n";
28 open my $EXMARC, '>:utf8', $conf->{exception};
30 # this is the dispatch table which drives command selection in
32 my %commands = ( c => \&print_fullcontext,
35 '<' => \&widen_window,
36 '>' => \&narrow_window,
38 o => \&insert_original,
52 my @spinner = qw(- / | \\);
55 while ( buildrecord() ) {
56 unless ($conf->{ricount} % 100) {
57 print "\rWorking... ", $spinner[$sidx];
58 $sidx = ($sidx == $#spinner) ? 0 : $sidx + 1;
61 do_automated_cleanups();
64 until ($ptr == $#record) {
66 if ($record[$ptr] =~ /&/ && $record[$ptr] !~ /&\w+?;/)
67 { edit("Naked ampersand"); $ptr= 0; next }
69 if ($record[$ptr] =~ /<datafield tag="(.+?)"/) {
71 # tags must be numeric
73 edit("Non-numerics in tag") unless $conf->{autoscrub};
76 # test for existing 901/903 unless we're autocleaning them
77 unless ($conf->{'strip-nines'}) {
78 if ($match == 901 or $match == 903) {
79 edit("Incoming 901/903 found in data");
85 # subfields can't be non-alphanumeric
86 if ($record[$ptr] =~ /<subfield code="(.*?)"/) {
88 if ($match =~ /\P{IsAlnum}/ or $match eq '') {
89 edit("Junk in subfield code/Null subfield code");
96 write_record($NUMARC);
98 print $NUMARC "</collection>\n";
99 print $OUT "\nDone. \n";
102 #-----------------------------------------------------------------------------------
104 #-----------------------------------------------------------------------------------
106 sub do_automated_cleanups {
108 until ($ptr == $#record) {
109 # catch empty datafield elements
110 if ($record[$ptr] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
111 if ($record[$ptr + 1] =~ m|</datafield>|) {
112 my @a = @record[0 .. $ptr - 1];
113 my @b = @record[$ptr + 2 .. $#record];
115 message("Empty datafield scrubbed");
120 # and quasi-empty subfields
121 if ($record[$ptr] =~ m|<subfield code="(.*?)">(.*?)</sub|) {
122 my $code = $1; my $content = $2;
123 if ($code =~ /\W/ and ($content =~ /\s+/ or $content eq '')) {
124 my @a = @record[0 .. $ptr - 1];
125 my @b = @record[$ptr + 1 .. $#record];
127 message("Empty subfield scrubbed");
136 for $ptr (0 .. $#record) {
138 if ($record[$ptr] =~ m|<leader>(.+?)</leader>|) {
140 if (length $leader < 24) {
141 $leader .= ' ' x (20 - length($leader));
143 $record[$ptr] = "<leader>$leader</leader>\n";
144 message("Short leader padded");
147 if ($record[$ptr] =~ m|<controlfield tag="008">(.+?)</control|) {
150 if (length $content < 40) {
151 $content .= ' ' x (40 - length($content));
152 $record[$ptr] = "<controlfield tag=\"008\">$content</controlfield>\n";
153 message("Short 008 padded");
157 # clean misplaced dollarsigns
158 if ($record[$ptr] =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
159 $record[$ptr] =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
160 message("Dollar sign corrected");
163 # clean up tags with spaces in them
164 $record[$ptr] =~ s/tag=" /tag="00/g;
165 $record[$ptr] =~ s/tag=" /tag="0/g;
166 $record[$ptr] =~ s/tag="-/tag="0/g;
167 $record[$ptr] =~ s/tag="(\d\d) /tag="0$1/g;
169 # stow tag data if we're looking at it
170 if ($record[$ptr] =~ m/<datafield tag="(.{3})" ind1="(.)" ind2="(.)">/) {
176 # automatable subfield maladies
177 $record[$ptr] =~ s/code=" ">c/code="c">/;
178 $record[$ptr] =~ s/code=" ">\$/code="c"$>/;
182 #-----------------------------------------------------------------------------------
184 #-----------------------------------------------------------------------------------
189 Handles the Term::ReadLine loop
196 return if $conf->{trash}{ $recmeta{tag} };
197 $conf->{editmsg} = $msg;
200 # stow original problem line
201 $conf->{origline} = $record[$ptr];
204 my $line = $term->readline('marc-cleanup>');
205 my @chunks = split /\s+/, $line;
207 # lines with single-character first chunks are commands.
208 # make sure they exist.
209 if (length $chunks[0] == 1) {
210 unless (defined $commands{$chunks[0]}) {
211 print $OUT "No such command '", $chunks[0], "'\n";
216 if (defined $commands{$chunks[0]}) {
217 my $term = $commands{$chunks[0]}->(@chunks[1..$#chunks]);
220 $record[$ptr] = "$line\n";
224 # set pointer to top on the way out
230 Constructs record arrays from the incoming MARC file and returns them
237 $l = <MARC> while (defined $l and $l !~ /<record>/);
238 return $l unless defined $l;
243 until ($l =~ m|</record>|)
244 { push @record, $l; $l = <MARC>; }
251 my $trash = $conf->{trash};
253 $conf->{rocount}++ if ($FH eq $NUMARC);
254 print $FH '<!-- ', $recmeta{explanation}, " -->\n"
255 if(defined $recmeta{explanation});
257 # excise unwanted tags
258 if (keys %{$trash} or $conf->{autoscrub}) {
261 for my $line (@record) {
263 $istrash = 0 if $line =~ m|</datafield|;
266 if ($line =~ m/<datafield tag="(.{3})"/) {
268 if ($trash->{$tag} or ($conf->{autoscrub} and $tag =~ /\D/)) {
273 push @trimmed, $line;
278 # add 903(?) with new record id
280 if ($conf->{'renumber-from'}) {
281 $renumber = join('', ' <datafield tag="', $conf->{'renumber-tag'},
282 '" ind1=" " ind2=" "> <subfield code="',
283 $conf->{'renumber-subfield'}, '">',
284 $conf->{'renumber-from'}, "</subfield></datafield>\n");
285 my @tmp = @record[0 .. $#record - 1];
286 my $last = $record[$#record];
287 @record = (@tmp, $renumber, $last);
288 $conf->{'renumber-from'}++;
292 unless ($conf->{nocollapse})
293 { s/\n// for (@record) }
298 sub print_fullcontext {
299 print $OUT "\r", ' ' x 72, "\n";
300 print $OUT $conf->{editmsg},"\n";
301 print $OUT "\r Tag:",$recmeta{tag}, " Ind1:'",
302 $recmeta{ind1},"' Ind2:'", $recmeta{ind2}, "'";
308 my $upper = int($conf->{window} / 2) + 1;
309 my $lower = int($conf->{window} / 2) - 1;
310 my $start = ($ptr - $upper < 0) ? 0 : $ptr - $upper;
311 my $stop = ($ptr + $lower > $#record) ? $#record : $ptr + $lower;
313 print $OUT ' |', $record[$_] for ($start .. $ptr - 1);
314 print $OUT '==> |', $record[$ptr];
315 print $OUT ' |', $record[$_] for ($ptr + 1 .. $stop);
322 print $OUT "\r$msg at ",$conf->{ricount},"/",$conf->{rocount} + 1,"\n";
325 #-----------------------------------------------------------------------------------
327 #-----------------------------------------------------------------------------------
330 my ($line_in, @chunks) = @_;
332 my $ofrom = shift @chunks;
333 if ($ofrom =~ /^'/ or !@chunks) {
334 until ($ofrom =~ /'$/)
335 { $ofrom .= join(' ','',shift @chunks) }
336 $ofrom =~ s/^'//; $ofrom =~ s/'$//;
338 my $to = shift @chunks;
340 until ($to =~ /'$/ or !@chunks)
341 { $to .= join(' ','',shift @chunks) }
342 $to =~ s/^'//; $to =~ s/'$//;
346 for my $char (split(//,$ofrom)) {
347 $char = "\\" . $char if ($char =~ /\W/);
348 $from = join('', $from, $char);
351 $conf->{prevline} = $record[$ptr];
352 $record[$ptr] =~ s/$from/$to/;
358 $conf->{prevline} = $record[$ptr];
359 # remove <subfield stuff; extract (probably wrong) subfield code
360 $record[$ptr] =~ s/^\s*<subfield code="(.*?)">//;
361 # and move to front of line
362 $record[$ptr] = join(' ', $1 , $record[$ptr]);
363 # tear off trailing subfield tag from preceeding line
364 $record[$ptr - 1] =~ s|</subfield>\n||;
365 # join current line onto preceeding line
366 $record[$ptr - 1] = join('', $record[$ptr - 1], $record[$ptr]);
368 my @a = @record[0 .. $ptr - 1];
369 my @b = @record[$ptr + 1 .. $#record];
371 # move record pointer to previous line
378 unless ($conf->{prevline})
379 { print $OUT "No previously edited line to flip\n"; return }
380 my $temp = $record[$ptr];
381 $record[$ptr] = $conf->{prevline};
382 $conf->{prevline} = $temp;
388 $conf->{killline} = $record[$ptr];
389 my @a = @record[0 .. $ptr - 1];
390 my @b = @record[$ptr + 1 .. $#record];
397 unless ($conf->{killline})
398 { print $OUT "No killed line to yank\n"; return }
399 my @a = @record[0 .. $ptr - 1];
400 my @b = @record[$ptr .. $#record];
401 @record = (@a, $conf->{killline}, @b);
407 print $OUT "\nOrig. edit line :", $conf->{origline};
408 print $OUT "Current flip line:", $conf->{prevline} if $conf->{prevline};
409 print $OUT "Last killed line :", $conf->{killline} if $conf->{killline};
415 my ($line_in, @explanation) = @_;
416 $recmeta{explanation} = join(' ', 'Tag', $recmeta{tag}, @explanation);
417 write_record($EXMARC);
422 $ptr++ unless ($ptr == $#record);;
428 $ptr-- unless ($ptr == 0);
433 sub commit_edit { return 1 }
436 if ($conf->{window} == 15)
437 { print $OUT "Window can't be bigger than 15 lines\n"; return }
438 $conf->{window} += 2;
443 if ($conf->{window} == 5)
444 { print $OUT "Window can't be smaller than 5 lines\n"; return }
445 $conf->{window} -= 2;
451 Type a replacement for the indicated line, or enter a command.
453 DISPLAY COMMANDS | LINE AUTO-EDIT COMMANDS
454 < Expand context window | k Kill current line
455 > Contract context window | y Yank last killed line
456 p Move pointer to prev line | m Merge current line into preceding line
457 n Move pointer to next line | o Insert original line
458 c Print line context | f Flip current line and last edited line
459 d Print current saved lines |
460 -----------------------------+-------------------------------------------
461 s Subtitute; replace ARG1 in current line with ARG2. If either ARG
462 contains spaces, it must be single-quoted
463 t Commit changes and resume automated operations
464 x Dump record to exception file
473 #-----------------------------------------------------------------------------------
475 #-----------------------------------------------------------------------------------
476 # defined a domain-specific language for specifying MARC tags to be dropped from
477 # records during processing. it is line oriented, and is specified as follows:
479 # each line may specify any number of tags to be included, either singly (\d{1,3})
480 # or as a range (\d{1,3}\.\.\d{1,3}
482 # if a single number is given, it must be between '000' and '999', inclusive.
484 # ranges obey the previous rule, and also the first number of the range must be less
485 # than the second number
487 # finally, any single range in a line may be followed by the keyword 'except'. every
488 # number or range after 'except' is excluded from the range specified. all these
489 # numbers must actually be within the range.
491 # specifying a tag twice is an error, to help prevent typos
494 print $OUT ">>> TRASHTAGS FILE FOUND. LOADING TAGS TO BE STRIPPED FROM OUTPUT\n";
495 open TRASH, '<', $conf->{trash}
496 or die "Can't open trash tags file!\n";
498 my $lastwasrange = 0;
499 my %lastrange = ( high => 0, low => 0);
502 my @chunks = split /\s+/;
503 while (my $chunk = shift @chunks) {
506 if ($chunk =~ /^\d{1,3}$/) {
507 trash_add($chunk, $except);
513 if ($chunk =~ /^\d{1,3}\.\.\d{1,3}$/) {
514 my ($low, $high) = trash_add_range($chunk, $except, \%lastrange);
516 %lastrange = (low => $low, high => $high)
522 if ($chunk eq 'except') {
523 die "Keyword 'except' can only follow a range (line $.)\n"
524 unless $lastwasrange;
525 die "Keyword 'except' may only occur once per line (line $.)\n"
531 die "Unknown chunk $chunk in .trashtags file (line $.)\n";
535 # remove original id sequence tag from trash hash if we know it
536 trash_add($conf->{'renumber-orig'}, 1)
537 if ($conf->{'renumber-orig'} and $conf->{trash}{ $conf->{'renumber-orig'} });
540 sub trash_add_range {
541 my ($chunk, $except, $range) = @_;
542 my ($low,$high) = split /\.\./, $chunk;
543 die "Ranges must be 'low..high' ($low is greater than $high on line $.)\n"
546 die "Exception ranges must be within last addition range (line $.)\n"
547 if ($low < $range->{low} or $high > $range->{high});
549 for my $tag ($low..$high) {
550 trash_add($tag, $except)
556 my ($tag, $except) = @_;
557 my $trash = $conf->{trash};
559 die "Trash values must be valid tags (000-999)\n"
560 unless ($tag >= 0 and $tag <= 999);
563 delete $trash->{$tag};
565 die "Trash tag '$tag' specified twice (line $.)\n"
571 #-----------------------------------------------------------------------
575 Performs boring script initialization. Handles argument parsing,
584 # set mode on existing filehandles
585 binmode(STDIN, ':utf8');
587 my $rc = GetOptions( $c,
592 'renumber-from|rf=i',
593 'renumber-orig|ro=i',
595 'renumber-subfield|rs=i',
601 show_help() unless $rc;
602 show_help() if ($c->{help});
603 show_trashhelp() if ($c->{trashhelp});
606 $c->{output} = 'incoming.cleaned.marc.xml' unless defined $c->{output};
607 $c->{exception} = 'incoming.exception.marc.xml' unless defined $c->{exception};
608 $c->{'renumber-tag'} = 903 unless defined $c->{'renumber-tag'};
609 $c->{'renumber-subfield'} = 'a' unless defined $c->{'renumber-subfield'};
612 # autotrash 901, 903 if strip-nines
613 if ($c->{'strip-nines'}) {
614 $c->{trash}{901} = 1;
615 $c->{trash}{903} = 1;
618 my @keys = keys %{$c};
619 show_help() unless (@ARGV and @keys);
624 Usage is: marc-cleanup [OPTIONS] <filelist>
626 --output -o Cleaned MARCXML output filename
627 (default: incoming.cleaned.marc.xml)
628 --exception -x Exception (dumped records) MARCXML filename
629 (incoming.exception.marc.xml)
630 --trash File containing trash tag data (see --trashhelp)
632 --renumber-from=NUM -rf Begin renumbering id sequence with this number
633 --renumber-tag -rt Tag to use in renumbering (default: 903)
634 --renumber-subfield -rs Subfield code to use in renumbering (default: a)
635 --renumber-orig -ro Original id tag; will be kept in output even if
636 it appears in the trash file
638 --nocollapse -n Don't compress records to one line on output
639 --autoscrub -a Automatically remove non-numeric tags in data
640 --strip-nines Automatically remove any existing 901/903 tags in data
647 The marc-cleanup trash tags file is a simple plaintext file. It is a
648 line oriented format. There are three basic tokens:
652 * The "except" clause
654 Any number of tags and/or tag ranges can appear on a single line. A
655 tag cannot appear twice in the file, either alone or as part of a
656 range. This is to prevent errors in the trash tag listing. Items do
657 not have to be sorted within a line. These following lines are valid:
659 850 852 870..879 886 890 896..899
662 Ranges must be ordered internally. That is, "870..879" is valid while
665 Finally, there can be only one "except" clause on a line. It is
666 composed of the word "except" followed by one or more tags or
667 ranges. Except clauses must follow a range, and all tags within the
668 clause must be within the range which the clause follows.
670 900..997 except 935 950..959 987 994