wrong subfield
[migration-tools.git] / unicorn_patron_xml2text.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 use DateTime;
6 use Time::HiRes qw/time/;
7 use XML::LibXML;
8
9 my %s_map;
10
11 my $doc = XML::LibXML->new->parse_file($ARGV[0]);
12
13 my $starttime = time;
14 my $count = 1;
15
16 my @base_elements = (
17     "user_id",
18     "user_altid",
19     "user_pin",
20     "user_profile",
21     "user_status",
22     "user_priv_granted",
23     "user_priv_expires",
24     "user_mailingaddr",
25     "birthdate",
26     "last_name",
27     "first_name",
28     "middle_name",
29     "suffix_name",
30     "note",
31     "comment",
32     "staff",
33     "webcatpref",
34     "user_category1",
35     "user_category2",
36     "user_category3",
37     "dept",
38     "guardian",
39     "user_claims_ret",
40     #"user_environment",
41     #"user_library",
42     "user_department"
43 );
44
45 my @addr_elements = (
46     "std_line1",
47     "std_line2",
48     "std_city",
49     "std_state",
50     "std_zip",
51     "phone",
52     "dayphone",
53     "homephone",
54     "workphone",
55     "email",
56     "location",
57     "usefor",
58     "care_of"
59 );
60
61 print STDOUT join("\t", @base_elements);
62 foreach my $addr ( 1..3 ) {
63     print STDOUT "\t" . join("\t", @addr_elements);
64 }
65 print STDOUT "\tuserid_active\tinactive_barcode1\tinactive_barcode2";
66 print STDOUT "\n";
67
68 for my $patron ( $doc->documentElement->childNodes ) {
69         next if ($patron->nodeType == 3);
70
71         my $bc = $patron->findvalue( 'user_id' ); $bc =~ s/^\s+//; $bc =~ s/\s+$//;
72         if (exists($s_map{$bc})) {
73                 $count++;
74                 warn "\n!!! already saw barcode $bc, skipping\n";
75                 next;
76         } else {
77                 $s_map{$bc} = 1;
78         }
79
80         unless (defined($bc)) {
81                 my $xml = $patron->toString;
82                 warn "\n!!! no barcode found in UMS data, user number $count, xml => $xml \n";
83                 $count++;
84                 next;
85         }
86
87     foreach my $e ( @base_elements ) {
88         my $v = $patron->findvalue( $e ); $v =~ s/^\s+//; $v =~ s/\s+$//;
89         if ( $v && ( $e eq 'birthdate' || $e eq 'user_priv_granted' || $e eq 'user_priv_expires' ) ) { $v = parse_date($v); }
90         print STDOUT ( $v ? $v : '' ) . "\t";
91     }
92
93         my %addresses;
94
95         for my $addr ( $patron->findnodes( "Address" ) ) {
96                 my $addr_type = $addr->getAttribute('addr_type');
97                 $addresses{$addr_type} = $addr;
98         }
99
100     foreach my $t ( 1..3 ) {
101         if ($addresses{$t}) {
102             foreach my $e ( @addr_elements ) {
103                 my $v = $addresses{$t}->findvalue( $e ); $v =~ s/^\s+//; $v =~ s/\s+$//;
104                 print STDOUT ( $v ? $v : '' ) . "\t";
105             }
106         } else {
107             foreach ( @addr_elements ) { print STDOUT "\t"; }
108         }
109     }
110
111     my $inactive_barcode1 = '';
112     my $inactive_barcode2 = '';
113     my $userid_active = 't';
114     my @barcodes = $patron->findnodes( "barcodes" );
115     for my $i_bc ( $barcodes[0]->findnodes( "barcode" ) ) {
116         my $active = $i_bc->getAttribute('active');
117         if ($active eq "0" && $i_bc->textContent eq $bc) {
118             $userid_active = 'f';
119         }
120         if ($active eq "0" && $i_bc->textContent ne $bc) {
121             if (! $inactive_barcode1 ) {
122                 $inactive_barcode1 = $i_bc->textContent;
123                 $inactive_barcode1 =~ s/^\s+//;
124                 $inactive_barcode1 =~ s/\s+$//;
125             } else {
126                 if (! $inactive_barcode2 ) {
127                     $inactive_barcode2 = $i_bc->textContent;
128                     $inactive_barcode2 =~ s/^\s+//;
129                     $inactive_barcode2 =~ s/\s+$//;
130                 } else {
131                     warn "Extra barcode (" . $i_bc->textContent . ") for user with id = " . $bc . "\n";
132                 }
133             }
134         }
135     }
136     print STDOUT "$userid_active\t$inactive_barcode1\t$inactive_barcode2";
137
138     print STDOUT "\n";
139         $count++;
140 }
141
142 sub parse_date {
143         my $string = shift;
144         my $group = shift;
145
146         my ($y,$m,$d);
147
148         if ($string eq 'NEVER') {
149                 my (undef,undef,undef,$d,$m,$y) = localtime();
150                 return sprintf('%04d-%02d-%02d', $y + 1920, $m + 1, $d);
151         } elsif (length($string) == 8 && $string =~ /^(\d{4})(\d{2})(\d{2})$/o) {
152                 ($y,$m,$d) = ($1,$2,$3);
153         } elsif ($string =~ /(\d+)\D(\d+)\D(\d+)/o) { #looks like it's parsable
154                 if ( length($3) > 2 )  { # looks like mm.dd.yyyy
155                         if ( $1 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
156                                 if ($1 > 12 && $1 < 31 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
157                                         ($y,$m,$d) = ($3,$2,$1);
158                                 } elsif ($2 > 12 && $2 < 31 && $1 < 13) {
159                                         ($y,$m,$d) = ($3,$1,$2);
160                                 }
161                         }
162                 } elsif ( length($1) > 3 ) { # format probably yyyy.mm.dd
163                         if ( $3 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
164                                 if ($2 > 12 && $2 < 32 && $3 < 13) { # well, actually it looks like yyyy.dd.mm -- why, I don't konw
165                                         ($y,$m,$d) = ($1,$3,$2);
166                                 } elsif ($3 > 12 && $3 < 31 && $2 < 13) {
167                                         ($y,$m,$d) = ($1,$2,$3);
168                                 }
169                         }
170                 } elsif ( $1 < 99 && $2 < 99 && $3 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
171                         if ($3 < 7) { # probably 2000 or greater, mm.dd.yy
172                                 $y = $3 + 2000;
173                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
174                                         ($m,$d) = ($2,$1);
175                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
176                                         ($m,$d) = ($1,$2);
177                                 }
178                         } else { # probably before 2000, mm.dd.yy
179                                 $y = $3 + 1900;
180                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
181                                         ($m,$d) = ($2,$1);
182                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
183                                         ($m,$d) = ($1,$2);
184                                 }
185                         }
186                 }
187         }
188
189         my $date = $string;
190         if ($y && $m && $d) {
191                 eval {
192                         $date = sprintf('%04d-%02d-%-2d',$y, $m, $d)
193                                 if (new DateTime ( year => $y, month => $m, day => $d ));
194                 }
195         }
196
197         return $date;
198 }
199