silence un-init string warning
[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     "user_category4",
38     "dept",
39     "guardian",
40     "aup",
41     "photo",
42     "notify_via",
43     "user_claims_ret",
44     #"user_environment",
45     #"user_library",
46     "user_department"
47 );
48
49 my @addr_elements = (
50     "std_line1",
51     "std_line2",
52     "std_city",
53     "std_state",
54     "std_zip",
55     "phone",
56     "dayphone",
57     "homephone",
58     "workphone",
59     "cellphone",
60     "email",
61     "location",
62     "usefor",
63     "care_of",
64     "known_bad"
65 );
66
67 print STDOUT join("\t", @base_elements);
68 foreach my $addr ( 1..3 ) {
69     print STDOUT "\t" . join("\t", @addr_elements);
70 }
71 print STDOUT "\tuserid_active\tinactive_barcode1\tinactive_barcode2";
72 print STDOUT "\n";
73
74 for my $patron ( $doc->documentElement->childNodes ) {
75         next if ($patron->nodeType == 3);
76
77         my $bc = $patron->findvalue( 'user_id' ); $bc =~ s/^\s+//; $bc =~ s/\s+$//;
78         if (exists($s_map{$bc})) {
79                 $count++;
80                 warn "\n!!! already saw barcode $bc, skipping\n";
81                 next;
82         } else {
83                 $s_map{$bc} = 1;
84         }
85
86         unless (defined($bc)) {
87                 my $xml = $patron->toString;
88                 warn "\n!!! no barcode found in UMS data, user number $count, xml => $xml \n";
89                 $count++;
90                 next;
91         }
92
93     foreach my $e ( @base_elements ) {
94         my $v = $patron->findvalue( $e ); $v =~ s/^\s+//; $v =~ s/\s+$//;
95         if ( $v && ( $e eq 'birthdate' || $e eq 'user_priv_granted' || $e eq 'user_priv_expires' ) ) { $v = parse_date($v); }
96         print STDOUT ( $v ? $v : '' ) . "\t";
97     }
98
99         my %addresses;
100
101         for my $addr ( $patron->findnodes( "Address" ) ) {
102                 my $addr_type = $addr->getAttribute('addr_type');
103                 $addresses{$addr_type} = $addr;
104         }
105
106     foreach my $t ( 1..3 ) {
107         if ($addresses{$t}) {
108             foreach my $e ( @addr_elements ) {
109                 my $v;
110                 if ($e eq "known_bad") {
111                     $v = $addresses{$t}->getAttribute( $e ); if ($v) { $v =~ s/^\s+//; $v =~ s/\s+$//; }
112                 } else {
113                     $v = $addresses{$t}->findvalue( $e ); $v =~ s/^\s+//; $v =~ s/\s+$//;
114                 }
115                 print STDOUT ( $v ? $v : '' ) . "\t";
116             }
117         } else {
118             foreach ( @addr_elements ) { print STDOUT "\t"; }
119         }
120     }
121
122     my $inactive_barcode1 = '';
123     my $inactive_barcode2 = '';
124     my $userid_active = 't';
125     my @barcodes = $patron->findnodes( "barcodes" );
126     for my $i_bc ( $barcodes[0]->findnodes( "barcode" ) ) {
127         my $active = $i_bc->getAttribute('active');
128         if ($active eq "0" && $i_bc->textContent eq $bc) {
129             $userid_active = 'f';
130         }
131         if ($active eq "0" && $i_bc->textContent ne $bc) {
132             if (! $inactive_barcode1 ) {
133                 $inactive_barcode1 = $i_bc->textContent;
134                 $inactive_barcode1 =~ s/^\s+//;
135                 $inactive_barcode1 =~ s/\s+$//;
136             } else {
137                 if (! $inactive_barcode2 ) {
138                     $inactive_barcode2 = $i_bc->textContent;
139                     $inactive_barcode2 =~ s/^\s+//;
140                     $inactive_barcode2 =~ s/\s+$//;
141                 } else {
142                     warn "Extra barcode (" . $i_bc->textContent . ") for user with id = " . $bc . "\n";
143                 }
144             }
145         }
146     }
147     print STDOUT "$userid_active\t$inactive_barcode1\t$inactive_barcode2";
148
149     print STDOUT "\n";
150         $count++;
151 }
152
153 sub parse_date {
154         my $string = shift;
155         my $group = shift;
156
157         my ($y,$m,$d);
158
159         if ($string eq 'NEVER') {
160                 my (undef,undef,undef,$d,$m,$y) = localtime();
161                 return sprintf('%04d-%02d-%02d', $y + 1920, $m + 1, $d);
162         } elsif (length($string) == 8 && $string =~ /^(\d{4})(\d{2})(\d{2})$/o) {
163                 ($y,$m,$d) = ($1,$2,$3);
164         } elsif ($string =~ /(\d+)\D(\d+)\D(\d+)/o) { #looks like it's parsable
165                 if ( length($3) > 2 )  { # looks like mm.dd.yyyy
166                         if ( $1 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
167                                 if ($1 > 12 && $1 < 31 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
168                                         ($y,$m,$d) = ($3,$2,$1);
169                                 } elsif ($2 > 12 && $2 < 31 && $1 < 13) {
170                                         ($y,$m,$d) = ($3,$1,$2);
171                                 }
172                         }
173                 } elsif ( length($1) > 3 ) { # format probably yyyy.mm.dd
174                         if ( $3 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
175                                 if ($2 > 12 && $2 < 32 && $3 < 13) { # well, actually it looks like yyyy.dd.mm -- why, I don't konw
176                                         ($y,$m,$d) = ($1,$3,$2);
177                                 } elsif ($3 > 12 && $3 < 31 && $2 < 13) {
178                                         ($y,$m,$d) = ($1,$2,$3);
179                                 }
180                         }
181                 } elsif ( $1 < 99 && $2 < 99 && $3 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
182                         if ($3 < 7) { # probably 2000 or greater, mm.dd.yy
183                                 $y = $3 + 2000;
184                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
185                                         ($m,$d) = ($2,$1);
186                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
187                                         ($m,$d) = ($1,$2);
188                                 }
189                         } else { # probably before 2000, mm.dd.yy
190                                 $y = $3 + 1900;
191                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
192                                         ($m,$d) = ($2,$1);
193                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
194                                         ($m,$d) = ($1,$2);
195                                 }
196                         }
197                 }
198         }
199
200         my $date = $string;
201         if ($y && $m && $d) {
202                 eval {
203                         $date = sprintf('%04d-%02d-%-2d',$y, $m, $d)
204                                 if (new DateTime ( year => $y, month => $m, day => $d ));
205                 }
206         }
207
208         return $date;
209 }
210