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