10 my $term = new Term::ReadLine 'yaz-cleanup';
11 my $OUT = $term->OUT || \*STDOUT;
15 # initialization and setup
18 populate_trash() if ($conf->{trashfile});
20 # set up files, since everything appears to be in order
21 open MARC, '<:utf8', (shift || 'incoming.marc.xml')
22 or die "Can't open input file $!\n";
23 open my $NUMARC, '>:utf8', $conf->{output}
24 or die "Can't open output file $!\n";
25 open my $OLD2NEW, '>', 'old2new.map'
26 if ($conf->{'renumber-from'} and $conf->{'original-subfield'});
28 print $NUMARC "<collection>\n";
30 my @record = (); # current record storage
31 my %recmeta = (); # metadata about current record
32 my $ptr = 0; # record index pointer
34 # this is the dispatch table which drives command selection in
36 my %commands = ( c => \&print_fullcontext,
39 '<' => \&widen_window,
40 '>' => \&narrow_window,
42 o => \&insert_original,
56 my @spinner = qw(- / | \\);
59 while ( buildrecord() ) {
60 unless ($conf->{ricount} % 100) {
61 print "\rWorking... ", $spinner[$sidx];
62 $sidx = ($sidx == $#spinner) ? 0 : $sidx + 1;
65 do_automated_cleanups();
68 until ($ptr == $#record) {
70 if ($record[$ptr] =~ /&/ && $record[$ptr] !~ /&\w+?;/)
71 { edit("Naked ampersand"); $ptr= 0; next }
73 if ($record[$ptr] =~ /<datafield tag="(.+?)"/) {
75 # tags must be numeric
77 edit("Non-numerics in tag") unless $conf->{autoscrub};
80 # test for existing 901/903 unless we're autocleaning them
81 unless ($conf->{'strip-nines'}) {
82 if ($match == 901 or $match == 903) {
83 edit("Incoming 901/903 found in data");
89 # subfields can't be non-alphanumeric
90 if ($record[$ptr] =~ /<subfield code="(.*?)"/) {
91 if ($1 =~ /\P{IsAlnum}/ or $1 eq '') {
92 edit("Junk in subfield code/Null subfield code");
96 # subfields can't be non-alphanumeric
97 if ($record[$ptr] =~ /<subfield code="(\w{2,})"/) {
98 edit("Subfield code larger than 1 char");
104 write_record($NUMARC);
106 print $NUMARC "</collection>\n";
107 print $OUT "\nDone. \n";
110 #-----------------------------------------------------------------------------------
112 #-----------------------------------------------------------------------------------
114 sub do_automated_cleanups {
116 until ($ptr == $#record) {
117 # get datafield/tag data if we have it
120 # catch empty datafield elements
121 if ($record[$ptr] =~ m/<datafield tag="..."/) {
122 if ($record[$ptr + 1] =~ m|</datafield>|) {
123 my @a = @record[0 .. $ptr - 1];
124 my @b = @record[$ptr + 2 .. $#record];
126 message("Empty datafield scrubbed");
131 # and quasi-empty subfields
132 if ($record[$ptr] =~ m|<subfield code="(.*?)">(.*?)</sub|) {
133 my $code = $1; my $content = $2;
134 if ($code =~ /\W/ and ($content =~ /\s+/ or $content eq '')) {
135 my @a = @record[0 .. $ptr - 1];
136 my @b = @record[$ptr + 1 .. $#record];
138 message("Empty subfield scrubbed");
147 for $ptr (0 .. $#record) {
149 if ($record[$ptr] =~ m|<leader>(.+?)</leader>|) {
151 if (length $leader < 24) {
152 $leader .= ' ' x (20 - length($leader));
154 $record[$ptr] = "<leader>$leader</leader>\n";
155 message("Short leader padded");
158 if ($record[$ptr] =~ m|<controlfield tag="008">(.+?)</control|) {
161 if (length $content < 40) {
162 $content .= ' ' x (40 - length($content));
163 $record[$ptr] = "<controlfield tag=\"008\">$content</controlfield>\n";
164 message("Short 008 padded");
168 # clean misplaced dollarsigns
169 if ($record[$ptr] =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
170 $record[$ptr] =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
171 message("Dollar sign corrected");
174 # clean up tags with spaces in them
175 $record[$ptr] =~ s/tag=" /tag="00/g;
176 $record[$ptr] =~ s/tag=" /tag="0/g;
177 $record[$ptr] =~ s/tag="-/tag="0/g;
178 $record[$ptr] =~ s/tag="(\d\d) /tag="0$1/g;
180 # automatable subfield maladies
181 $record[$ptr] =~ s/code=" ">c/code="c">/;
182 $record[$ptr] =~ s/code=" ">\$/code="c">\$/;
186 sub stow_record_data {
187 # get tag data if we're looking at it
189 if ($record[$ptr] =~ m/<datafield tag="(?<TAG>.{3})"/) {
190 $recmeta{tag} = $+{TAG};
191 $record[$ptr] =~ m/ind1="(?<IND1>.)"/;
192 $recmeta{ind1} = $+{IND1} || '';
193 $record[$ptr] =~ m/ind2="(?<IND2>.)"/;
194 $recmeta{ind2} = $+{IND2} || '';
196 unless (defined $recmeta{tag}) {
197 message("Autokill record: no detectable tag");
198 dump_record("No detectable tag") ;
201 # and since we are looking at a tag, see if it's the original id
202 if ($conf->{'original-subfield'} and
203 $recmeta{tag} == $conf->{'original-tag'}) {
204 my $line = $record[$ptr]; my $lptr = $ptr;
205 my $osub = $conf->{'original-subfield'};
206 $recmeta{oid} = 'NONE';
208 until ($line =~ m|</record>|) {
210 $line = $record[$lptr];
211 $recmeta{oid} = $+{TAG}
212 if ($line =~ /<subfield code="$osub">(.+?)</);
218 #-----------------------------------------------------------------------------------
220 #-----------------------------------------------------------------------------------
224 Handles the Term::ReadLine loop
231 return if $conf->{trash}{ $recmeta{tag} };
232 $conf->{editmsg} = $msg;
235 # stow original problem line
236 $recmeta{origline} = $record[$ptr];
239 my $line = $term->readline('marc-cleanup>');
240 my @chunks = split /\s+/, $line;
242 # lines with single-character first chunks are commands.
243 # make sure they exist.
244 if (length $chunks[0] == 1) {
245 unless (defined $commands{$chunks[0]}) {
246 print $OUT "No such command '", $chunks[0], "'\n";
251 if (defined $commands{$chunks[0]}) {
252 my $term = $commands{$chunks[0]}->(@chunks[1..$#chunks]);
255 $recmeta{prevline} = $record[$ptr];
256 $record[$ptr] = "$line\n";
260 # set pointer to top on the way out
266 Constructs record arrays from the incoming MARC file and returns them
273 $l = <MARC> while (defined $l and $l !~ /<record>/);
274 return $l unless defined $l;
279 until ($l =~ m|</record>|)
280 { push @record, $l; $l = <MARC>; }
287 my $trash = $conf->{trash};
291 open $EXMARC, '>:utf8', $conf->{exception}
292 or die "Can't open exception file $!\n";
296 $conf->{rocount}++ if ($FH eq $NUMARC);
297 print $FH '<!-- ', $recmeta{explanation}, " -->\n"
298 if(defined $recmeta{explanation});
300 # excise unwanted tags
301 if (keys %{$trash} or $conf->{autoscrub}) {
304 for my $line (@record) {
306 $istrash = 0 if $line =~ m|</datafield|;
309 if ($line =~ m/<datafield tag="(.{3})"/) {
311 if ($trash->{$tag} or ($conf->{autoscrub} and $tag =~ /\D/)) {
316 push @trimmed, $line;
321 # add 903(?) with new record id
323 if ($conf->{'renumber-from'}) {
324 $recmeta{nid} = $conf->{'renumber-from'};
325 $renumber = join('', ' <datafield tag="', $conf->{'renumber-tag'},
326 '" ind1=" " ind2=" "> <subfield code="',
327 $conf->{'renumber-subfield'},
328 '">', $recmeta{nid}, "</subfield></datafield>\n");
329 my @tmp = @record[0 .. $#record - 1];
330 my $last = $record[$#record];
331 @record = (@tmp, $renumber, $last);
332 $conf->{'renumber-from'}++;
335 # scrub newlines (unless told not to or writing exception record)
336 unless ($conf->{nocollapse} or $FH eq $EXMARC)
337 { s/\n// for (@record) }
339 # write to old->new map file if needed
340 if ($conf->{'renumber-from'} and $conf->{'original-subfield'}) {
341 unless (defined $recmeta{oid}) {
342 my $msg = join(' ', "No old id num found");
345 print $OLD2NEW $recmeta{oid}, "\t", $recmeta{nid}, "\n"
349 # actually write the record
350 print $FH @record,"\n";
352 # if we were dumping to exception file, nuke the record and set ptr
353 # to terminate processing loop
358 sub print_fullcontext {
359 print $OUT "\r", ' ' x 72, "\n";
360 print $OUT $conf->{editmsg},"\n";
361 print $OUT "\r Tag:",$recmeta{tag}, " Ind1:'",
362 $recmeta{ind1},"' Ind2:'", $recmeta{ind2}, "'";
363 print $OUT " @ ", $conf->{ricount}, "/", $conf->{rocount} + 1;
369 my $upper = int($conf->{window} / 2) + 1;
370 my $lower = int($conf->{window} / 2) - 1;
371 my $start = ($ptr - $upper < 0) ? 0 : $ptr - $upper;
372 my $stop = ($ptr + $lower > $#record) ? $#record : $ptr + $lower;
374 print $OUT ' |', $record[$_] for ($start .. $ptr - 1);
375 print $OUT '==> |', $record[$ptr];
376 print $OUT ' |', $record[$_] for ($ptr + 1 .. $stop);
383 print $OUT "\r$msg at ",$conf->{ricount},"/",$conf->{rocount} + 1,"\n";
386 #-----------------------------------------------------------------------------------
388 #-----------------------------------------------------------------------------------
393 my $ofrom = shift @chunks;
394 if ($ofrom =~ /^'/) {
395 until ($ofrom =~ /'$/ or !@chunks)
396 { $ofrom .= join(' ','',shift @chunks) }
397 $ofrom =~ s/^'//; $ofrom =~ s/'$//;
399 my $to = shift @chunks;
401 until ($to =~ /'$/ or !@chunks)
402 { $to .= join(' ','',shift @chunks) }
403 $to =~ s/^'//; $to =~ s/'$//;
407 for my $char (split(//,$ofrom)) {
408 $char = "\\" . $char if ($char =~ /\W/);
409 $from = join('', $from, $char);
412 $recmeta{prevline} = $record[$ptr];
413 $record[$ptr] =~ s/$from/$to/;
419 $recmeta{prevline} = $record[$ptr];
420 # remove <subfield stuff; extract (probably wrong) subfield code
421 $record[$ptr] =~ s/^\s*<subfield code="(.*?)">//;
422 # and move to front of line
423 $record[$ptr] = join(' ', $1 , $record[$ptr]);
424 # tear off trailing subfield tag from preceeding line
425 $record[$ptr - 1] =~ s|</subfield>\n||;
426 # join current line onto preceeding line
427 $record[$ptr - 1] = join('', $record[$ptr - 1], $record[$ptr]);
429 my @a = @record[0 .. $ptr - 1];
430 my @b = @record[$ptr + 1 .. $#record];
432 # move record pointer to previous line
439 unless ($recmeta{prevline})
440 { print $OUT "No previously edited line to flip\n"; return }
441 my $temp = $record[$ptr];
442 $record[$ptr] = $recmeta{prevline};
443 $recmeta{prevline} = $temp;
449 $recmeta{killline} = $record[$ptr];
450 my @a = @record[0 .. $ptr - 1];
451 my @b = @record[$ptr + 1 .. $#record];
458 unless ($recmeta{killline})
459 { print $OUT "No killed line to yank\n"; return }
460 my @a = @record[0 .. $ptr - 1];
461 my @b = @record[$ptr .. $#record];
462 @record = (@a, $conf->{killline}, @b);
467 sub insert_original {
468 $record[$ptr] = $recmeta{origline};
474 print $OUT "\nOrig. edit line :", $recmeta{origline};
475 print $OUT "Current flip line:", $recmeta{prevline} if $recmeta{prevline};
476 print $OUT "Last killed line :", $recmeta{killline} if $recmeta{killline};
482 my (@explanation) = @_;
483 print $OUT @explanation;
484 $recmeta{explanation} = join(' ', 'Tag', $recmeta{tag}, @explanation);
485 write_record($EXMARC);
490 $ptr++ unless ($ptr == $#record);;
496 $ptr-- unless ($ptr == 0);
501 sub commit_edit { return 1 }
504 if ($conf->{window} == 15)
505 { print $OUT "Window can't be bigger than 15 lines\n"; return }
506 $conf->{window} += 2;
511 if ($conf->{window} == 5)
512 { print $OUT "Window can't be smaller than 5 lines\n"; return }
513 $conf->{window} -= 2;
519 Type a replacement for the indicated line, or enter a command.
521 DISPLAY COMMANDS | LINE AUTO-EDIT COMMANDS
522 < Expand context window | k Kill current line
523 > Contract context window | y Yank last killed line
524 p Move pointer to prev line | m Merge current line into preceding line
525 n Move pointer to next line | o Insert original line
526 c Print line context | f Flip current line and last edited line
527 d Print current saved lines |
528 -----------------------------+-------------------------------------------
529 s Subtitute; replace ARG1 in current line with ARG2. If either ARG
530 contains spaces, it must be single-quoted
531 t Commit changes and resume automated operations
532 x Dump record to exception file
541 #-----------------------------------------------------------------------------------
543 #-----------------------------------------------------------------------------------
544 # defined a domain-specific language for specifying MARC tags to be dropped from
545 # records during processing. it is line oriented, and is specified as follows:
547 # each line may specify any number of tags to be included, either singly (\d{1,3})
548 # or as a range (\d{1,3}\.\.\d{1,3}
550 # if a single number is given, it must be between '000' and '999', inclusive.
552 # ranges obey the previous rule, and also the first number of the range must be less
553 # than the second number
555 # finally, any single range in a line may be followed by the keyword 'except'. every
556 # number or range after 'except' is excluded from the range specified. all these
557 # numbers must actually be within the range.
559 # specifying a tag twice is an error, to help prevent typos
562 print $OUT ">>> TRASHTAGS FILE FOUND. LOADING TAGS TO BE STRIPPED FROM OUTPUT\n";
563 open TRASH, '<', $conf->{trashfile}
564 or die "Can't open trash tags file!\n";
566 my $lastwasrange = 0;
567 my %lastrange = ( high => 0, low => 0);
570 my @chunks = split /\s+/;
571 while (my $chunk = shift @chunks) {
574 if ($chunk =~ /^\d{1,3}$/) {
575 trash_add($chunk, $except);
581 if ($chunk =~ /^\d{1,3}\.\.\d{1,3}$/) {
582 my ($low, $high) = trash_add_range($chunk, $except, \%lastrange);
584 %lastrange = (low => $low, high => $high)
590 if ($chunk eq 'except') {
591 die "Keyword 'except' can only follow a range (line $.)\n"
592 unless $lastwasrange;
593 die "Keyword 'except' may only occur once per line (line $.)\n"
599 die "Unknown chunk $chunk in .trashtags file (line $.)\n";
603 # remove original id sequence tag from trash hash if we know it
604 trash_add($conf->{'original-tag'}, 1)
605 if ($conf->{'original-tag'} and $conf->{trash}{ $conf->{'original-tag'} });
608 sub trash_add_range {
609 my ($chunk, $except, $range) = @_;
610 my ($low,$high) = split /\.\./, $chunk;
611 die "Ranges must be 'low..high' ($low is greater than $high on line $.)\n"
614 die "Exception ranges must be within last addition range (line $.)\n"
615 if ($low < $range->{low} or $high > $range->{high});
617 for my $tag ($low..$high) {
618 trash_add($tag, $except)
624 my ($tag, $except) = @_;
625 my $trash = $conf->{trash};
627 die "Trash values must be valid tags (000-999)\n"
628 unless ($tag >= 0 and $tag <= 999);
631 delete $trash->{$tag};
633 die "Trash tag '$tag' specified twice (line $.)\n"
639 #-----------------------------------------------------------------------
643 Performs boring script initialization. Handles argument parsing,
652 # set mode on existing filehandles
653 binmode(STDIN, ':utf8');
655 my $rc = GetOptions( $c,
661 'renumber-from|rf=i',
663 'renumber-subfield|rs=s',
665 'original-subfield|os=s',
672 show_help() unless $rc;
673 show_help() if ($c->{help});
674 show_trashhelp() if ($c->{trashhelp});
677 my $pfx = $c->{prefix} // "bibs";
678 $c->{output} = join('.',$c->{prefix},'clean','marc','xml');
679 $c->{exception} = join('.',$c->{prefix},'exception','marc','xml');
680 $c->{'renumber-tag'} = 903 unless defined $c->{'renumber-tag'};
681 $c->{'renumber-subfield'} = 'a' unless defined $c->{'renumber-subfield'};
684 # autotrash 901, 903 if strip-nines
685 if ($c->{'strip-nines'}) {
686 $c->{trash}{901} = 1;
687 $c->{trash}{903} = 1;
690 my @keys = keys %{$c};
691 show_help() unless (@ARGV and @keys);
696 Usage is: marc-cleanup [OPTIONS] <filelist>
698 --output -o Cleaned MARCXML output filename
699 --exception -x Exception (dumped records) MARCXML filename
701 --prefix=<PREFIX>> -p Shared prefix for output/exception files. Will produce
702 PREFIX.clean.marc.xml and PREFIX.exception.marc.xml
704 --renumber-from -rf Begin renumbering id sequence with this number
705 --renumber-tag -rt Tag to use in renumbering (default: 903)
706 --renumber-subfield -rs Subfield code to use in renumbering (default: a)
707 --original-tag -ot Original id tag; will be kept in output even if
708 it appears in the trash file
709 --original-subfield -os Original id subfield code. If this is specified
710 and renumbering is in effect, an old-to-new mapping
711 file (old2new.map) will be generated.
713 --autoscrub -a Automatically remove non-numeric tags in data
714 --nocollapse -n Don't compress records to one line on output
715 --strip-nines Automatically remove any existing 901/903 tags in data
716 --trashfile -t File containing trash tag data (see --trashhelp)
719 --script Store human-initiated ops in scriptfile (.mcscript)
727 The marc-cleanup trash tags file is a simple plaintext file. It is a
728 line oriented format. There are three basic tokens:
732 * The "except" clause
734 Any number of tags and/or tag ranges can appear on a single line. A
735 tag cannot appear twice in the file, either alone or as part of a
736 range. This is to prevent errors in the trash tag listing. Items do
737 not have to be sorted within a line. These following lines are valid:
739 850 852 870..879 886 890 896..899
742 Ranges must be ordered internally. That is, "870..879" is valid while
745 Finally, there can be only one "except" clause on a line. It is
746 composed of the word "except" followed by one or more tags or
747 ranges. Except clauses must follow a range, and all tags within the
748 clause must be within the range which the clause follows.
750 900..997 except 935 950..959 987 994