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