Unicorn TSV-maker can now handle bill.data and charge.data too.
[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 my @users;
7 my $serial = -1;
8 my $line = 0;
9 my $section = '';
10 my $field = '';
11 my %unique_fields;
12
13
14 # Load each record
15 while (<>) {
16     s/\r\n/\n/g;
17 # print STDERR "Loaded this line: " . $_;
18
19         # Is this the start of a new record?
20         if ( /^... DOCUMENT BOUNDARY ...$/ ) {
21                 $line = 0;
22                 $serial++;
23                 $section = ''; # just in case this didn't get reset in the previous record
24                 # print STDERR "Processing record $serial.\n";
25                 next;
26         }
27
28         # Is this a FORM= line (which can be ignored)?
29         if ( /^FORM=/ ) {
30                 next;
31         }
32
33         # If this isn't the start of the new record, it's a new line in the present record.
34         $line++;
35
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";
39                 $section = "$1.";
40                 next;
41         }
42
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";
47                 }
48                 # print STDERR "It's been fun, guys, but... this is the end of the " . $1 . ".\n";
49                 $section = '';
50                 next;
51         }
52
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)?(.*)$/ ) {
56
57                 # Build the name of this field (taking note of whether we're in a named section of data)
58                 $field = '';
59                 if ($section ne '') { 
60                         $field .= $section;
61                 }
62                 $field .= $1;
63
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;
66
67                 # Now we can actually store this line of data!
68                 $users[$serial]{$field} = $3;           
69
70                 # print STDERR "Data extracted: \$users[$serial]{'$field'} = '$3'\n";
71
72                 next;
73         }       
74
75         # This is the continuation of the previous line.
76         else {
77                 chomp($_);
78                 $users[$serial]{$field} .= ' ' . $_;
79                 # print STDERR "Appended data to previous field. \$users[$serial]{'$field'} is now '" . $users[$serial]{$field} . "'.\n";
80         }
81
82 }
83
84 print STDERR "Loaded " . scalar(@users) . " users.\n";
85
86
87 # Print a header line
88 print "SERIAL\t";
89 @sorted_fields = sort keys %unique_fields;
90 foreach $i (@sorted_fields) {
91         print "$i\t";
92 }
93 print "\n";
94
95
96 # Print the results
97 for (my $u = 0; $u < @users; $u++) {
98         print "$u\t";   
99         foreach $f (@sorted_fields) {
100                 if (defined $users[$u]{$f}) {
101                         print $users[$u]{$f};
102                 }
103         print "\t";
104         }
105         print "\n";
106 }
107
108 print STDERR "Wrote " . scalar(@users) . " users.\n";
109 # uh-bdee-uh-bdee-uh-bdee-uh- THAT'S ALL, FOLKS