3 # Converts a Unicorn users.data, bill.data, or charge.data file to a tab-separated file.
4 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
17 # print STDERR "Loaded this line: " . $_;
19 # Is this the start of a new record?
20 if ( /^... DOCUMENT BOUNDARY ...$/ ) {
23 $section = ''; # just in case this didn't get reset in the previous record
24 # print STDERR "Processing record $serial.\n";
28 # Is this a FORM= line (which can be ignored)?
33 # If this isn't the start of the new record, it's a new line in the present record.
36 # Is this line the beginning of a block of data (typically an address or a note)?
37 if ( /^\.(.*?)_BEGIN.$/ ) {
38 # print STDERR "I think this might be the beginning of a beautiful " . $1 . ".\n";
43 # Is this line the beginning of a block of data (typically an address or a note)?
44 if ( /^\.(.*?)_END.$/ ) {
45 if ("$1." ne $section) {
46 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";
48 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
53 # Looks like we've got some actual data! Let's store it.
54 # FIXME: For large batches of data, we may run out of memory and should store this on disk.
55 if ( /^\.(.*?).\s+(\|a)?(.*)$/ ) {
57 # Build the name of this field (taking note of whether we're in a named section of data)
64 # Store the field as a key of an array. If it already exists, oh well, now it still exists.
65 $unique_fields{$field} = 1;
67 # Now we can actually store this line of data!
68 $records[$serial]{$field} = $3;
70 # print STDERR "Data extracted: \$records[$serial]{'$field'} = '$3'\n";
75 # This is the continuation of the previous line.
78 $records[$serial]{$field} .= ' ' . $_;
79 # print STDERR "Appended data to previous field. \$records[$serial]{'$field'} is now '" . $records[$serial]{$field} . "'.\n";
84 print STDERR "Loaded " . scalar(@records) . " records.\n";
87 # We're aiming to produce output that can be slurped in by this SQL code: \COPY m_anderson.actor_usr_unicorn ( l_user_id, l_user_altid, l_user_pin, l_user_profile, l_user_status, l_user_library,
88 # l_user_priv_granted, l_user_priv_expires, l_user_mailingaddr, l_birthdate, l_prefix_name, l_last_name, l_first_name, l_middle_name, l_suffix_name, l_note, l_note1, l_patron, l_comment, l_staff,
89 # l_webcatpref, l_user_category1, l_user_category2, l_user_category3, l_user_category4, l_dept, l_guardian, l_license, l_ssn, l_misc, l_aup, l_photo, l_notify_via, l_user_claims_ret, l_user_environment,
90 # l_user_department, l_ums_id, l_user_last_activity, l_placcard, l_user_email, 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_township,
91 # l_addr1_room, l_addr1_company, l_addr1_office, l_addr1_phone, l_addr1_dayphone, l_addr1_homephone, l_addr1_workphone, l_addr1_cellphone, l_addr1_fax, l_addr1_email, l_addr1_location, l_addr1_usefor,
92 # l_addr1_care_of, l_addr1_known_bad, l_addr1_ums_addrid, l_addr2_std_line1, l_addr2_std_line2, l_addr2_std_city, l_addr2_std_state, l_addr2_std_zip, l_addr2_country, l_addr2_township, l_addr2_room,
93 # l_addr2_company, l_addr2_office, l_addr2_phone, l_addr2_dayphone, l_addr2_homephone, l_addr2_workphone, l_addr2_cellphone, l_addr2_fax, l_addr2_email, l_addr2_location,l_addr2_usefor, l_addr2_care_of,
94 # l_addr2_known_bad, l_addr2_ums_addrid, l_addr3_std_line1, l_addr3_std_line2, l_addr3_std_city, l_addr3_std_state, l_addr3_std_zip, l_addr3_country, l_addr3_township, l_addr3_room, l_addr3_company,
95 # l_addr3_office, l_addr3_phone, l_addr3_dayphone, l_addr3_homephone, l_addr3_workphone, l_addr3_cellphone, l_addr3_fax, l_addr3_email, l_addr3_location, l_addr3_usefor, l_addr3_care_of, l_addr3_known_bad,
96 # l_addr3_ums_addrid, l_identific, l_noempl, l_profession, l_program, l_represent, l_userid_active, l_inactive_barcode1, l_inactive_barcode2 ) FROM './users.data.tsv'
98 # Process the records:
99 for (my $u = 0; $u < @records; $u++) {
101 # Some fields can be mapped straightforwardly:
102 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 )) {
103 $records[$u]{uc($f)} '' unless defined $records[$u]{uc($f)};
104 $records[$u]{'l_' + $f} = $records[$u]{uc($f)};
107 # Addresses are a bit different:
108 foreach $a (qw( addr1 addr2 addr3 )) {
109 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 )) {
110 $records[$u]{uc('USER_' + $a + '.' + $f)} = '' unless defined $records[$u]{uc('USER_' + $a + '.' + $f)};
111 $records[$u]{'l_' + $a + '_' + '$f'} = $records[$u]{uc('USER_' + $a + '.' + $f)};
115 # Handle fields that don't exactly match (e.g. parse USER_NAME into l_last_name etc.)
117 $records[$u]{'l_birthdate'} = $records[$u]{'USER.BIRTH_DATE'}';
118 $records[$u]{'l_note'} = $records[$u]{'USER_XINFO.NOTE'}';
119 $records[$u]{'l_note1'} = '';
120 $records[$u]{'l_patron'} = '';
121 $records[$u]{'l_comment'} = $records[$u]{'USER_XINFO.COMMENT'}';
122 $records[$u]{'l_staff'} = $records[$u]{'USER_XINFO.STAFF'}';
123 $records[$u]{'l_webcatpref'} = $records[$u]{'USER_XINFO.WEBCATPREF'}';
124 $records[$u]{'l_dept'} = $records[$u]{'USER_DEPARTMENT'}';
125 $records[$u]{'l_guardian'} = '';
126 $records[$u]{'l_license'} = $records[$u]{'USER_XINFO.LICENSE'}';
127 $records[$u]{'l_ssn'} = $records[$u]{'USER_XINFO.SSN'}';
128 $records[$u]{'l_misc'} = '';
129 $records[$u]{'l_aup'} = '';
130 $records[$u]{'l_photo'} = '';
131 $records[$u]{'l_notify_via'} = $records[$u]{'USER_XINFO.NOTIFY_VIA'}';
132 $records[$u]{'l_ums_id'} = '';
133 $records[$u]{'l_placcard'} = '';
134 $records[$u]{'l_user_email'} = '';
135 $records[$u]{'l_identific'} = '';
136 $records[$u]{'l_noempl'} = '';
137 $records[$u]{'l_profession'} = '';
138 $records[$u]{'l_program'} = '';
139 $records[$u]{'l_represent'} = '';
140 $records[$u]{'l_userid_active'} = '';
141 $records[$u]{'l_inactive_barcode1'} = $records[$u]{'USER_XINFO.PREV_ID'}';
142 $records[$u]{'l_inactive_barcode2'} = $records[$u]{'USER_XINFO.PREV_ID2'}';
144 # We can parse the name like so:
146 # Copy the name to a temp value
147 $temp_name = $records[$u]{'USER_NAME'};
149 # Strip off a prefix, if there is one
150 foreach $prefix (qw( Ms. Mrs. Mr. Dr. )) {
151 if ($temp_name =~ /$prefix /i) {
152 $records[$u]{'l_prefix_name'} = $prefix;
153 $temp_name =~ s/$prefix //i;
157 # Strip off a suffix, if there is one
158 foreach $suffix (qw( Jr. Sr. II III IV )) {
159 if ($temp_name =~ /$suffix /i) {
160 $records[$u]{'l_suffix_name'} = $suffix;
161 $temp_name =~ s/$suffix //i;
165 # Strip off the family name (before the comma)
166 # Of what remains, whatever is before the first space is the first name and the rest is the middle name
167 $records[$u]{'l_last_name'} = $temp_name =~ /^([^,])*\s*,.*$/;
168 $records[$u]{'l_first_given_name', 'l_second_given_name'} = $temp_name =~ /^[^,]*\s*,\s*([^,\s]*)\s*(.*)$/;
175 # Print a header line
176 # FIXME: don't print a serial, and do print the fields in the order above
178 @sorted_fields = sort keys %unique_fields;
179 foreach $i (@sorted_fields) {
186 for (my $u = 0; $u < @records; $u++) {
188 foreach $f (@sorted_fields) {
189 if (defined $records[$u]{$f}) {
190 print $records[$u]{$f};
197 print STDERR "Wrote " . scalar(@records) . " records.\n";
198 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS