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