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