1450fc38a22800b7b6c5ca1444931ef1eb79bb00
[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 print $OUT "Interactive MARC Stream Editor starting up\n";
14
15 my $count = 0;
16 my $reccount = 0;
17 my $line = '';
18
19 my @record = (); # current record storage
20 my @context= (); # 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 open MARC2, '<', 'incoming.marc.xml';
27 <MARC2>;
28
29 # this is the dispatch table which drives command selection in
30 # edit(), below
31 my %commands = ( c => \&print_context,
32                  k => \&kill_line,
33                  o => \&show_original,
34                  t => \&commit_edit,
35                  x => \&dump_record,
36                  q => \&quit,
37                  '?' => \&help,
38                  h   => \&help,
39                  help => \&help,
40                );
41
42 my @spinner = qw(- / | \\);
43 my $sidx = 0;
44
45 while (my $line = getline()) {
46     unless ($count % 2000) {
47         print "\rWorking... ", $spinner[$sidx];
48         $sidx++;
49         $sidx = 0 if ($sidx > $#spinner);
50     }
51     update_context();
52
53     # catch empty datafield elements
54     if ($line =~ m|</datafield>|) {
55         if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
56             pop @record; pop @record;
57             print $OUT "\rEmpty datafield scrubbed at line $count\n";
58             next;
59         }
60     }
61
62     # clean misplaced dollarsigns
63     if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
64         $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
65         print $OUT "\rDollar sign in subfield code corrected at line $count\n";
66     }
67
68     # clean up tags with spaces in them
69     $line =~ s/tag="  /tag="00/g;
70     $line =~ s/tag=" /tag="0/g;
71     $line =~ s/tag="-/tag="0/g;
72     $line =~ s/tag="(\d\d) /tag="0$1/g;
73
74     # naked ampersands
75     if ($line =~ /&/ && $line !~ /&\w{1,7};/)
76       { edit("Looks like naked ampersand", $line); next }
77
78     # subfields can't be non-alphanumeric
79       if ($line =~ /<subfield code="[^[:alnum:]]"/)
80         { edit("Junk in subfield", $line); next }
81
82 }
83 print $NUMARC "</xml>\n";
84
85 =head2 edit
86
87 Handles the Term::ReadLine loop
88
89 =cut
90
91 sub edit {
92     my ($msg, $line_in) = @_;
93     print $OUT "\r".$msg, " at line $count:\n";
94     print_context();
95     while (1) {
96         my $line = $term->readline('yaz-cleanup>');
97         if (defined $commands{$line}) {
98             my $term = $commands{$line}->($line_in);
99             last if $term;
100         } else {
101             if ($context[3] eq " [LINE KILLED\n]") {
102                 push @record, "$line\n"
103             } else {
104                 $record[-1] = "$line\n";
105             }
106             $context[3] = "$line\n";
107             print_context();
108         }
109     }
110 }
111
112 =head2 getline
113
114 Reads from the incoming MARC file; returns lines into the driver
115 loop. Batches records for output, and maintains the context listing.
116
117 =cut
118
119 sub getline {
120     my $l = <MARC>;
121     $count++;
122     if (defined $l) {
123         if ($l =~ /<record>/) {
124             @record = ($l);
125             $reccount++;
126         } elsif ($l =~ m|</record>|) {
127             write_record($NUMARC) if $reccount;
128         } else {
129             push @record, $l;
130         }
131     }
132     return $l;
133 }
134
135 sub write_record {
136     my ($FH) = @_;
137     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";
138     print $FH @record;
139     print $FH "</collection>\n";
140 }
141
142 sub update_context {
143     my $line2 = <MARC2>;
144     push @context, $line2;
145     shift @context if (@context > 5);
146 }
147
148 #-----------------------------------------------------------------------------------
149 # command routines
150 #-----------------------------------------------------------------------------------
151
152 sub print_context {
153     print $OUT "\n", join('    |','',@context[0..2]);
154     print $OUT '==> |', $context[3];
155     print $OUT '    |', $context[4],"\n";
156     return 0;
157 }
158
159 sub show_original {
160     my ($line_in) = @_;
161     print $OUT "\n$line_in\n";
162     return 0;
163 }
164
165 sub commit_edit { return 1 }
166
167 sub kill_line {
168     pop @record;
169     $context[3] = " [LINE KILLED]\n";
170     print_context();
171     return 0;
172 }
173
174 sub dump_record {
175     my $line = <MARC>;
176     until ($line =~ m|</record>|) {
177         push @record, $line;
178         $line = <MARC>;
179         update_context;
180     }
181     push @record, $line;
182     write_record($EXMARC);
183 }
184
185 sub help {
186 print $OUT <<HELP;
187
188 Type a replacement for the indicated line, or enter a command.
189
190 Commands: c  Show line context
191           k  Kill this line (remove from record)
192           o  Show original line
193           t  Commit changes and resume stream edit
194           x  Write this record to the exception file instead of output
195           q  Quit
196
197 HELP
198 return 0;
199 }
200
201 sub quit { exit }