adding mergeline capability
[migration-tools.git] / yaz-cleanup
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long;
7 use Term::ReadLine;
8
9 $| = 1;
10
11 my $term = new Term::ReadLine 'yaz-cleanup';
12 my $OUT = $term->OUT || \*STDOUT;
13
14 my $count = 0;
15 my $reccount = 0;
16 my $line = '';
17
18 my @record = (); # current record storage
19 my %reccontext = ();
20 my @linecontext= (); # last 5 lines of file
21
22 open MARC, '<', 'incoming.marc.xml';
23 open my $NUMARC, '>', 'incoming.clean.marc.xml';
24 print $NUMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
25 open my $EXMARC, '>', 'incoming.exceptions.marc.xml';
26 print $EXMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
27 open MARC2, '<', 'incoming.marc.xml';
28 <MARC2>;
29
30 # this is the dispatch table which drives command selection in
31 # edit(), below
32 my %commands = ( c => \&print_context,
33                  C => \&print_linecontext,
34                  k => \&kill_line,
35                  o => \&show_original,
36                  m => \&merge_lines,
37                  t => \&commit_edit,
38                  x => \&dump_record,
39                  q => \&quit,
40                  '?' => \&help,
41                  h   => \&help,
42                  help => \&help,
43                );
44
45 my @spinner = qw(- / | \\);
46 my $sidx = 0;
47
48 while (my $line = getline()) {
49     unless ($count % 2000) {
50         print "\rWorking... ", $spinner[$sidx];
51         $sidx = $sidx > $#spinner ? 0 : $sidx++;
52     }
53     update_linecontext();
54
55     # catch empty datafield elements
56     if ($line =~ m|</datafield>|) {
57         if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
58             pop @record; pop @record;
59             print $OUT "\rEmpty datafield scrubbed at line $count\n";
60             next;
61         }
62     }
63
64     # clean misplaced dollarsigns
65     if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
66         $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
67         print $OUT "\rDollar sign in subfield code corrected at line $count\n";
68     }
69
70     # clean up tags with spaces in them
71     $line =~ s/tag="  /tag="00/g;
72     $line =~ s/tag=" /tag="0/g;
73     $line =~ s/tag="-/tag="0/g;
74     $line =~ s/tag="(\d\d) /tag="0$1/g;
75
76     # stow tag data if we're looking at it
77     if ($line =~ m/<datafield tag="(\d{3})" ind1="(.)" ind2="(.)">/) {
78         $reccontext{tag}  = $1;
79         $reccontext{ind1} = $2;
80         $reccontext{ind2} = $3;
81     }
82
83     # naked ampersands
84     if ($line =~ /&/ && $line !~ /&\w{1,7};/)
85       { edit("Looks like naked ampersand", $line); next }
86
87     # subfields can't be non-alphanumeric
88       if ($line =~ /<subfield code="[^[:alnum:]]"/)
89         { edit("Junk in subfield", $line); next }
90
91 }
92 print $NUMARC "</xml>\n";
93 print $EXMARC "</xml>\n";
94
95 =head2 edit
96
97 Handles the Term::ReadLine loop
98
99 =cut
100
101 sub edit {
102     my ($msg, $line_in) = @_;
103     print $OUT "\r".$msg, " at line $count:\n";
104     print_context();
105     while (1) {
106         my $line = $term->readline('yaz-cleanup>');
107         if (length $line < 2)
108           { next unless (defined $commands{$line}) }
109         if (defined $commands{$line}) {
110             my $term = $commands{$line}->($line_in);
111             last if $term;
112         } else {
113             if ($linecontext[3] eq " [LINE KILLED]\n") {
114                 push @record, "$line\n"
115             } else {
116                 $record[-1] = "$line\n";
117             }
118             $linecontext[3] = "$line\n";
119             print_linecontext();
120         }
121     }
122 }
123
124 =head2 getline
125
126 Reads from the incoming MARC file; returns lines into the driver
127 loop. Batches records for output, and maintains the linecontext listing.
128
129 =cut
130
131 sub getline {
132     my $l = <MARC>;
133     $count++;
134     if (defined $l) {
135         if ($l =~ /<record>/) {
136             @record = ($l);
137             %reccontext = ();
138             $reccount++;
139         } elsif ($l =~ m|</record>|) {
140             write_record($NUMARC) if $reccount;
141         } else {
142             push @record, $l;
143         }
144     }
145     return $l;
146 }
147
148 sub write_record {
149     my ($FH) = @_;
150     print $FH '<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" xmlns="http://www.loc.gov/MARC21/slim">',"\n";
151     print $FH @record;
152     print $FH "</collection>\n";
153 }
154
155 sub update_linecontext {
156     my $line2 = <MARC2>;
157     push @linecontext, $line2;
158     shift @linecontext if (@linecontext > 5);
159 }
160
161 #-----------------------------------------------------------------------------------
162 # command routines
163 #-----------------------------------------------------------------------------------
164
165 sub print_context {
166     print "\n Tag:",$reccontext{tag}, " Ind1:'",
167       $reccontext{ind1},"' Ind2:'", $reccontext{ind2}, "'";
168     print_linecontext();
169     return 0;
170 }
171
172 sub print_linecontext {
173     print $OUT "\n", join('    |','',@linecontext[0..2]);
174     print $OUT '==> |', $linecontext[3];
175     print $OUT '    |', $linecontext[4],"\n";
176     return 0;
177 }
178
179 sub show_original {
180     my ($line_in) = @_;
181     print $OUT "\n$line_in\n";
182     return 0;
183 }
184
185 sub merge_lines {
186     my $last = pop @record;
187     $last =~ s/^\s+//;
188     $record[-1] =~ s/\n//;
189     $record[-1] = join('', $record[-1], $last);
190     my @temp = ("\n");
191     push @temp, @linecontext[0..1];
192     $temp[3] = $record[-1];
193     $temp[4] = $linecontext[4];
194     @linecontext = @temp;
195     print_linecontext();
196     return 0;
197 }
198
199 sub kill_line {
200     pop @record;
201     $linecontext[3] = " [LINE KILLED]\n";
202     print_linecontext();
203     return 0;
204 }
205
206 sub dump_record {
207     my $line = <MARC>; $count++;
208     update_linecontext();
209     until ($line =~ m|</record>|) {
210         push @record, $line;
211         $line = <MARC>; $count++;
212         update_linecontext();
213     }
214     push @record, $line;
215     write_record($EXMARC);
216     return 1;
217 }
218
219 sub commit_edit { return 1 }
220
221 sub help {
222 print $OUT <<HELP;
223
224 Type a replacement for the indicated line, or enter a command.
225
226 Commands: c  Show record context ('C' for brief context)
227           k  Kill this line (remove from record)
228           m  Merge indicated line with previous line
229           o  Show original line
230           t  Commit changes and resume stream edit
231           x  Write this record to the exception file instead of output
232           q  Quit
233
234 HELP
235 return 0;
236 }
237
238 sub quit { exit }