additional string cleanup prevention added to checking table names
[migration-tools.git] / unicorn / unicorn_patrons_to_tsv.pl
index 454486a..636c21a 100755 (executable)
@@ -1,8 +1,27 @@
 #!/usr/bin/perl -w
 
+# Copyright 2009-2012, Equinox Software, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
 # Converts a Unicorn users.data file to a tab-separated file.
 # 2009-08-10 Ben Ostrowsky <ben@esilibrary.com>
 
+#use Data::Dumper;
+#$Data::Dumper::Sortkeys = true;
+
 my @records;
 my $serial = -1;
 my $line = 0;
@@ -15,14 +34,14 @@ l_user_priv_expires l_user_mailingaddr l_birthdate l_prefix_name l_last_name l_f
 l_suffix_name l_note l_note1 l_patron l_comment l_staff 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 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_std_line1 l_addr1_std_line2 l_addr1_std_city l_addr1_std_state l_addr1_std_zip l_addr1_country l_addr1_county
 l_addr1_township 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 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 l_addr2_company l_addr2_office l_addr2_phone 
+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 
 l_addr2_dayphone l_addr2_homephone l_addr2_workphone l_addr2_cellphone l_addr2_fax l_addr2_email 
-l_addr2_locationl_addr2_usefor l_addr2_care_of 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_addr2_location l_addr2_usefor l_addr2_care_of 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_county l_addr3_township 
 l_addr3_room l_addr3_company 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 
 l_addr3_ums_addrid l_identific l_noempl l_profession l_program l_represent l_userid_active l_inactive_barcode1 
@@ -58,7 +77,7 @@ while (<>) {
                next;
        }
 
-       # Is this line the beginning of a block of data (typically an address or a note)?
+       # Is this line the end of a block of data (typically an address or a note)?
        if ( /^\.(.*?)_END.$/ ) {
                if ("$1." ne $section) {
                        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";
@@ -105,6 +124,9 @@ print STDERR "Loaded " . scalar(@records) . " records.\n";
 # Process the records:
 for (my $u = 0; $u < @records; $u++) {
 
+#print STDERR "Before processing:\n\n";
+#print STDERR Dumper($records[$u]);
+
        # Some fields can be mapped straightforwardly:
        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 )) {
                $records[$u]{uc($f)} = '' unless defined $records[$u]{uc($f)};
@@ -113,15 +135,23 @@ for (my $u = 0; $u < @records; $u++) {
 
        # Addresses are a bit different:
        foreach $a (qw( addr1 addr2 addr3 )) {
-               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 )) {
+               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 )) {
                        $records[$u]{uc('USER_' . $a . '.' . $f)} = '' unless defined $records[$u]{uc('USER_' . $a . '.' . $f)};
-                       $records[$u]{'l_' . $a . '_' . '$f'} = $records[$u]{uc('USER_' . $a . '.' . $f)};
+                       $records[$u]{'l_' . $a . '_' . $f} = $records[$u]{uc('USER_' . $a . '.' . $f)};
                }
+               $records[$u]{'l_' . $a . '_std_line1'} = $records[$u]{'USER_' . uc($a) . '.STREET'};
+               $records[$u]{'l_' . $a . '_std_line2'} = $records[$u]{'USER_' . uc($a) . '.LINE2'};
+               if ((defined $records[$u]{'USER_' . uc($a) . '.CITY/STATE'}) && ($records[$u]{'USER_' . uc($a) . '.CITY/STATE'} =~ m/^(.*),?(\s+)(.*)$/)) {
+                       $records[$u]{'l_' . $a . '_std_city'} = $1;
+                       $records[$u]{'l_' . $a . '_std_state'} = $3;
+               }
+               $records[$u]{'l_' . $a . '_std_zip'} = $records[$u]{'USER_' . uc($a) . '.ZIP'};
+
        }
 
        # Handle fields that don't exactly match (e.g. parse USER_NAME into l_last_name etc.)
 
-       $records[$u]{'l_birthdate'} = $records[$u]{'USER.BIRTH_DATE'};
+       $records[$u]{'l_birthdate'} = $records[$u]{'USER_BIRTH_DATE'};
        $records[$u]{'l_note'} = $records[$u]{'USER_XINFO.NOTE'};
        $records[$u]{'l_note1'} = '';
        $records[$u]{'l_patron'} = '';
@@ -147,14 +177,22 @@ for (my $u = 0; $u < @records; $u++) {
        $records[$u]{'l_userid_active'} = '';
        $records[$u]{'l_inactive_barcode1'} = $records[$u]{'USER_XINFO.PREV_ID'};
        $records[$u]{'l_inactive_barcode2'} = $records[$u]{'USER_XINFO.PREV_ID2'};
+#      $records[$u]{'l_user_category1'} = $records[$u]{'USER_ROUTING_FLAG'};
+#      $records[$u]{'l_user_category2'} = $records[$u]{'USER_WEB_AUTH'};
 
        # We can parse the name like so:
 
        # Copy the name to a temp value
        $temp_name = $records[$u]{'USER_NAME'};
 
+       # If there's no comma, don't try to parse the name
+       unless ($temp_name =~ m/,/) {
+               $records[$u]{'l_last_name'} = $temp_name;
+               next;
+       }
+
        # Strip off a prefix, if there is one
-       foreach $prefix (qw( Ms. Mrs. Mr. Dr. )) {
+       foreach $prefix (qw( Ms\. Mrs\. Mr\. Dr\. )) {
                if ($temp_name =~ /$prefix /i) {
                        $records[$u]{'l_prefix_name'} = $prefix;
                        $temp_name =~ s/$prefix //i;
@@ -162,22 +200,30 @@ for (my $u = 0; $u < @records; $u++) {
        }
 
        # Strip off a suffix, if there is one
-       foreach $suffix (qw( Jr. Sr. II III IV )) {
-               if ($temp_name =~ /$suffix /i) {
+       foreach $suffix (qw( Jr\. Jr Sr\. Sr III II IV )) {
+               if ($temp_name =~ / $suffix/i) {
                        $records[$u]{'l_suffix_name'} = $suffix;
-                       $temp_name =~ s/$suffix //i;
+                       $temp_name =~ s/ $suffix//i;
                }
        }
 
        # Strip off the family name (before the comma)
        # Of what remains, whatever is before the first space is the first name and the rest is the middle name
-       $records[$u]{'l_last_name'} = $temp_name =~ /^([^,])*\s*,.*$/;
-       $records[$u]{'l_first_given_name', 'l_second_given_name'} = $temp_name =~ /^[^,]*\s*,\s*([^,\s]*)\s*(.*)$/;
+       $temp_name =~ m/^([^,]*)\s*,.*$/;
+       $records[$u]{'l_last_name'} = $1;
+       $temp_name =~ m/^[^,]*\s*,\s*([^,\s]*)\s*(.*)$/;
+       $records[$u]{'l_first_name'} = $1;
+       $records[$u]{'l_middle_name'} = $2;
 
-}
+#print STDERR "\n\nAfter processing:\n\n";
+#print STDERR Dumper($records[$u]);
 
 
+}
+
 # Print the results
+print join("\t", @output_fields) . "\n";
+
 for (my $u = 0; $u < @records; $u++) {
        foreach $f (@output_fields) {
                if (defined $records[$u]{$f}) {