3f3d5e721e460b46abc5da3cb882b9ab4c15f730
[migration-tools.git] / unicorn / unicorn_patrons_to_tsv.pl
1 #!/usr/bin/perl -w
2
3 # Converts a Unicorn users.data file to a tab-separated file.
4 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
5
6 #use Data::Dumper;
7 #$Data::Dumper::Sortkeys = true;
8
9 my @records;
10 my $serial = -1;
11 my $line = 0;
12 my $section = '';
13 my $field = '';
14 my %unique_fields;
15 my @output_fields = qw(
16 l_user_id l_user_altid l_user_pin l_user_profile l_user_status l_user_library l_user_priv_granted 
17 l_user_priv_expires l_user_mailingaddr l_birthdate l_prefix_name l_last_name l_first_name l_middle_name 
18 l_suffix_name l_note l_note1 l_patron l_comment l_staff l_webcatpref l_user_category1 l_user_category2 
19 l_user_category3 l_user_category4 l_dept l_guardian l_license l_ssn l_misc l_aup l_photo l_notify_via 
20 l_user_claims_ret l_user_environment l_user_department l_ums_id l_user_last_activity l_placcard l_user_email 
21 l_addr1_std_line1 l_addr1_std_line2 l_addr1_std_city l_addr1_std_state l_addr1_std_zip l_addr1_country 
22 l_addr1_township l_addr1_room l_addr1_company l_addr1_office l_addr1_phone l_addr1_dayphone l_addr1_homephone 
23 l_addr1_workphone l_addr1_cellphone l_addr1_fax l_addr1_email l_addr1_location l_addr1_usefor l_addr1_care_of 
24 l_addr1_known_bad l_addr1_ums_addrid l_addr2_std_line1 l_addr2_std_line2 l_addr2_std_city l_addr2_std_state 
25 l_addr2_std_zip l_addr2_country l_addr2_township l_addr2_room l_addr2_company l_addr2_office l_addr2_phone 
26 l_addr2_dayphone l_addr2_homephone l_addr2_workphone l_addr2_cellphone l_addr2_fax l_addr2_email 
27 l_addr2_locationl_addr2_usefor l_addr2_care_of l_addr2_known_bad l_addr2_ums_addrid l_addr3_std_line1 
28 l_addr3_std_line2 l_addr3_std_city l_addr3_std_state l_addr3_std_zip l_addr3_country l_addr3_township 
29 l_addr3_room l_addr3_company l_addr3_office l_addr3_phone l_addr3_dayphone l_addr3_homephone l_addr3_workphone 
30 l_addr3_cellphone l_addr3_fax l_addr3_email l_addr3_location l_addr3_usefor l_addr3_care_of l_addr3_known_bad 
31 l_addr3_ums_addrid l_identific l_noempl l_profession l_program l_represent l_userid_active l_inactive_barcode1 
32 l_inactive_barcode2);
33
34
35 # Load each record
36 while (<>) {
37     s/\r\n/\n/g;
38 # print STDERR "Loaded this line: " . $_;
39
40         # Is this the start of a new record?
41         if ( /^... DOCUMENT BOUNDARY ...$/ ) {
42                 $line = 0;
43                 $serial++;
44                 $section = ''; # just in case this didn't get reset in the previous record
45                 # print STDERR "Processing record $serial.\n";
46                 next;
47         }
48
49         # Is this a FORM= line (which can be ignored)?
50         if ( /^FORM=/ ) {
51                 next;
52         }
53
54         # If this isn't the start of the new record, it's a new line in the present record.
55         $line++;
56
57         # Is this line the beginning of a block of data (typically an address or a note)?
58         if ( /^\.(.*?)_BEGIN.$/ ) {
59                 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
60                 $section = "$1.";
61                 next;
62         }
63
64         # Is this line the end of a block of data (typically an address or a note)?
65         if ( /^\.(.*?)_END.$/ ) {
66                 if ("$1." ne $section) {
67                         print STDERR "Error in record $serial, line $line (input line $.): got an end-of-$1 but I thought I was in a $section block!\n";
68                 }
69                 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
70                 $section = '';
71                 next;
72         }
73
74         # Looks like we've got some actual data!  Let's store it.
75         # FIXME: For large batches of data, we may run out of memory and should store this on disk.
76         if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
77
78                 # Build the name of this field (taking note of whether we're in a named section of data)
79                 $field = '';
80                 if ($section ne '') { 
81                         $field .= $section;
82                 }
83                 $field .= $1;
84
85                 # Store the field as a key of an array.  If it already exists, oh well, now it still exists.
86                 $unique_fields{$field} = 1;
87
88                 # Now we can actually store this line of data!
89                 $records[$serial]{$field} = $3;         
90
91                 # print STDERR "Data extracted: \$records[$serial]{'$field'} = '$3'\n";
92
93                 next;
94         }       
95
96         # This is the continuation of the previous line.
97         else {
98                 chomp($_);
99                 $records[$serial]{$field} .= ' ' . $_;
100                 # print STDERR "Appended data to previous field. \$records[$serial]{'$field'} is now '" . $records[$serial]{$field} . "'.\n";
101         }
102
103 }
104
105 print STDERR "Loaded " . scalar(@records) . " records.\n";
106
107
108 # Process the records:
109 for (my $u = 0; $u < @records; $u++) {
110
111 #print STDERR "Before processing:\n\n";
112 #print STDERR Dumper($records[$u]);
113
114         # Some fields can be mapped straightforwardly:
115         foreach $f (qw( user_id user_alt_id user_pin user_profile user_status user_library user_priv_granted user_priv_expires user_mailingaddr user_claims_ret user_environment user_department user_last_activity user_category1 user_category2 user_category3 user_category4 )) {
116                 $records[$u]{uc($f)} = '' unless defined $records[$u]{uc($f)};
117                 $records[$u]{'l_' . $f} = $records[$u]{uc($f)};
118         }
119
120         # Addresses are a bit different:
121         foreach $a (qw( addr1 addr2 addr3 )) {
122                 foreach $f (qw( std_line1 std_line2 std_city std_state std_zip country township room company office phone dayphone homephone workphone cellphone fax email location usefor care_of known_bad ums_addrid )) {
123                         $records[$u]{uc('USER_' . $a . '.' . $f)} = '' unless defined $records[$u]{uc('USER_' . $a . '.' . $f)};
124                         $records[$u]{'l_' . $a . '_' . $f} = $records[$u]{uc('USER_' . $a . '.' . $f)};
125                 }
126                 $records[$u]{'l_' . $a . '_std_line1'} = $records[$u]{'USER_' . uc($a) . '.STREET'};
127                 if ((defined $records[$u]{'USER_' . uc($a) . '.CITY/STATE'}) && ($records[$u]{'USER_' . uc($a) . '.CITY/STATE'} =~ m/^(.*), (.*)$/)) {
128                         $records[$u]{'l_' . $a . '_std_city'} = $1;
129                         $records[$u]{'l_' . $a . '_std_state'} = $2;
130                 }
131                 $records[$u]{'l_' . $a . '_std_zip'} = $records[$u]{'USER_' . uc($a) . '.ZIP'};
132
133         }
134
135         # Handle fields that don't exactly match (e.g. parse USER_NAME into l_last_name etc.)
136
137         $records[$u]{'l_birthdate'} = $records[$u]{'USER_BIRTH_DATE'};
138         $records[$u]{'l_note'} = $records[$u]{'USER_XINFO.NOTE'};
139         $records[$u]{'l_note1'} = '';
140         $records[$u]{'l_patron'} = '';
141         $records[$u]{'l_comment'} = $records[$u]{'USER_XINFO.COMMENT'};
142         $records[$u]{'l_staff'} = $records[$u]{'USER_XINFO.STAFF'};
143         $records[$u]{'l_webcatpref'} = $records[$u]{'USER_XINFO.WEBCATPREF'};
144         $records[$u]{'l_dept'} = $records[$u]{'USER_DEPARTMENT'};
145         $records[$u]{'l_guardian'} = '';
146         $records[$u]{'l_license'} = $records[$u]{'USER_XINFO.LICENSE'};
147         $records[$u]{'l_ssn'} = $records[$u]{'USER_XINFO.SSN'};
148         $records[$u]{'l_misc'} = '';
149         $records[$u]{'l_aup'} = '';
150         $records[$u]{'l_photo'} = '';
151         $records[$u]{'l_notify_via'} = $records[$u]{'USER_XINFO.NOTIFY_VIA'};
152         $records[$u]{'l_ums_id'} = '';
153         $records[$u]{'l_placcard'} = '';
154         $records[$u]{'l_user_email'} = '';
155         $records[$u]{'l_identific'} = '';
156         $records[$u]{'l_noempl'} = '';
157         $records[$u]{'l_profession'} = '';
158         $records[$u]{'l_program'} = '';
159         $records[$u]{'l_represent'} = '';
160         $records[$u]{'l_userid_active'} = '';
161         $records[$u]{'l_inactive_barcode1'} = $records[$u]{'USER_XINFO.PREV_ID'};
162         $records[$u]{'l_inactive_barcode2'} = $records[$u]{'USER_XINFO.PREV_ID2'};
163         $records[$u]{'l_user_category1'} = $records[$u]{'USER_ROUTING_FLAG'};
164         $records[$u]{'l_user_category2'} = $records[$u]{'USER_WEB_AUTH'};
165
166         # We can parse the name like so:
167
168         # Copy the name to a temp value
169         $temp_name = $records[$u]{'USER_NAME'};
170
171         # If there's no comma, don't try to parse the name
172         unless ($temp_name =~ m/,/) {
173                 $records[$u]{'l_last_name'} = $temp_name;
174                 next;
175         }
176
177         # Strip off a prefix, if there is one
178         foreach $prefix (qw( Ms. Mrs. Mr. Dr. )) {
179                 if ($temp_name =~ /$prefix /i) {
180                         $records[$u]{'l_prefix_name'} = $prefix;
181                         $temp_name =~ s/$prefix //i;
182                 }
183         }
184
185         # Strip off a suffix, if there is one
186         foreach $suffix (qw( Jr. Sr. II III IV )) {
187                 if ($temp_name =~ / $suffix/i) {
188                         $records[$u]{'l_suffix_name'} = $suffix;
189                         $temp_name =~ s/ $suffix//i;
190                 }
191         }
192
193         # Strip off the family name (before the comma)
194         # Of what remains, whatever is before the first space is the first name and the rest is the middle name
195         $temp_name =~ m/^([^,]*)\s*,.*$/;
196         $records[$u]{'l_last_name'} = $1;
197         $temp_name =~ m/^[^,]*\s*,\s*([^,\s]*)\s*(.*)$/;
198         $records[$u]{'l_first_name'} = $1;
199         $records[$u]{'l_middle_name'} = $2;
200
201 #print STDERR "\n\nAfter processing:\n\n";
202 #print STDERR Dumper($records[$u]);
203
204
205 }
206
207
208 # Print the results
209 for (my $u = 0; $u < @records; $u++) {
210         foreach $f (@output_fields) {
211                 if (defined $records[$u]{$f}) {
212                         print $records[$u]{$f};
213                 }
214         print "\t";
215         }
216         print "\n";
217 }
218
219 print STDERR "Wrote " . scalar(@records) . " records.\n";
220 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS