findnodes only looks at immediate children
[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 "\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' );
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 );
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 );
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 @barcodes = $patron->findnodes( "barcodes" );
114     for my $i_bc ( $barcodes[0]->findnodes( "barcode" ) ) {
115         my $active = $i_bc->getAttribute('active');
116         if ($active eq "0") {
117             if (! $inactive_barcode1 ) {
118                 $inactive_barcode1 = $i_bc->textContent;
119             } else {
120                 if (! $inactive_barcode2 ) {
121                     $inactive_barcode2 = $i_bc->textContent;
122                 } else {
123                     warn "Extra barcode (" . $i_bc->textContent . ") for user with id = " . $bc . "\n";
124                 }
125             }
126         }
127     }
128     print STDOUT "$inactive_barcode1\t$inactive_barcode2";
129
130     print STDOUT "\n";
131         $count++;
132 }
133
134 sub parse_date {
135         my $string = shift;
136         my $group = shift;
137
138         my ($y,$m,$d);
139
140         if ($string eq 'NEVER') {
141                 my (undef,undef,undef,$d,$m,$y) = localtime();
142                 return sprintf('%04d-%02d-%02d', $y + 1920, $m + 1, $d);
143         } elsif (length($string) == 8 && $string =~ /^(\d{4})(\d{2})(\d{2})$/o) {
144                 ($y,$m,$d) = ($1,$2,$3);
145         } elsif ($string =~ /(\d+)\D(\d+)\D(\d+)/o) { #looks like it's parsable
146                 if ( length($3) > 2 )  { # looks like mm.dd.yyyy
147                         if ( $1 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
148                                 if ($1 > 12 && $1 < 31 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
149                                         ($y,$m,$d) = ($3,$2,$1);
150                                 } elsif ($2 > 12 && $2 < 31 && $1 < 13) {
151                                         ($y,$m,$d) = ($3,$1,$2);
152                                 }
153                         }
154                 } elsif ( length($1) > 3 ) { # format probably yyyy.mm.dd
155                         if ( $3 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
156                                 if ($2 > 12 && $2 < 32 && $3 < 13) { # well, actually it looks like yyyy.dd.mm -- why, I don't konw
157                                         ($y,$m,$d) = ($1,$3,$2);
158                                 } elsif ($3 > 12 && $3 < 31 && $2 < 13) {
159                                         ($y,$m,$d) = ($1,$2,$3);
160                                 }
161                         }
162                 } elsif ( $1 < 99 && $2 < 99 && $3 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
163                         if ($3 < 7) { # probably 2000 or greater, mm.dd.yy
164                                 $y = $3 + 2000;
165                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
166                                         ($m,$d) = ($2,$1);
167                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
168                                         ($m,$d) = ($1,$2);
169                                 }
170                         } else { # probably before 2000, mm.dd.yy
171                                 $y = $3 + 1900;
172                                 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
173                                         ($m,$d) = ($2,$1);
174                                 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
175                                         ($m,$d) = ($1,$2);
176                                 }
177                         }
178                 }
179         }
180
181         my $date = $string;
182         if ($y && $m && $d) {
183                 eval {
184                         $date = sprintf('%04d-%02d-%-2d',$y, $m, $d)
185                                 if (new DateTime ( year => $y, month => $m, day => $d ));
186                 }
187         }
188
189         return $date;
190 }
191