Subversion Repositories PEEPS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 - 1
#!/usr/bin/perl
2
 
3
use strict;
4
use PEEPS;
5
use WebDB;
6
 
7
my $dbh = WebDB::connect ();
8
my $DEBUG = 1;
9
 
10
my $inputfile = shift // "";
11
 
12
if ($inputfile) {
13
	process_file ($inputfile);
14
} else {
15
	error ("No input file specified.");
16
}
17
 
18
$dbh->disconnect ();
19
 
20
 
21
 
22
sub process_file  {
23
	my $uploadedfile = shift or error ("No file passed to 'process_file()'");
24
	my @errors = ();
25
 
26
  logit (0, "Bulk Uploaded PEEPS Users from file $uploadedfile");
27
 
28
  print "Processing file [$uploadedfile]...\n" if $DEBUG;
29
  open(INPUT, $uploadedfile) or error ("Could not open file [$uploadedfile]!");
30
 
31
  my @columnlabels = split /,/, <INPUT>;
32
  @columnlabels = map { s/[^\x00-\x7f]//g; WebDB::trim ($_) } @columnlabels;
33
  error ("File not read!") unless scalar @columnlabels;
34
#  pop @columnlabels;
35
 
36
#  my @AcceptableColumns = qw(event_name order_date ticket_type id first_name last_name email derby_name);
37
#  @columnlabels = @AcceptableColumns;
38
 
39
  my $fields = join ", ", @columnlabels;
40
  my $values = join ", ", map { '?' } 0..$#columnlabels;
41
  my $sth = $dbh->prepare ("replace into person ($fields) values ($values)");
42
 
43
  while (<INPUT>) {
44
    chomp;
45
 
46
    my @R = map { s/[^\x00-\x7f]//g; WebDB::trim ($_) } split /,/;
47
 
48
    if (scalar @R != scalar @columnlabels) {
49
      # there's an extra field, likely from commas in the derby_name...
50
      my $extra = scalar @R - scalar @columnlabels;
51
#      print "fixing a derby name with $extra comma(s)...: @R\n";
52
      $R[5] = join ", ", @R[5..5+$extra];
53
      splice @R, 6, $extra;
54
      $R[5] =~ s/"//g;
55
#      print "now it's @R\n";
56
#      exit;
57
    }
58
 
59
    # The last value (birthdate) should be a valid date format.
60
    $R[$#R] = ($R[$#R] =~ /^\d{4}-\d{2}-\d{2}$/) ? $R[$#R] : undef;
61
 
62
    for my $i (0..$#R) {
63
      $R[$i] = undef unless $R[$i];
64
    }
65
 
66
    print "inserting: ", join (" ", @R), "\n" if $DEBUG;
67
    $sth->execute ( @R ) or error($sth->errstr);
68
  }
69
  print "DONE!\n\n" if $DEBUG;
70
}
71
 
72
 
73
sub error {
74
	my $msg = shift;
75
	logit (0, "Problem with Bulk MVP Pass upload: ".$msg);
76
	print "Error: $msg\n";
77
	exit (1);
78
}
79