Each election should only have one set of results; use UNIQUE to enforce.
[conservancy:voting.git] / bin / create-tmp-tokens.pl
1 #!/usr/bin/perl
2 use DBI;
3
4 # Script to create temporary tokens for voters.
5 #
6 # How to use this script
7 # ======================
8 #
9 # Look for the elections/referendum id in the database. Like
10 # "SELECT * FROM elections"
11 # Look for the current one and remember its id.
12 #
13 #
14 # If don't don't have a row for the current election yet, consider using
15 # BEGIN; SET NAMES 'utf8';
16 # INSERT INTO elections (name, voting_start, voting_end, choices_nb, question)
17 # VALUES ('2010 Spring Board of Directors Election',
18 #   TIMESTAMP('2009-06-08 00:00:00'), 
19 #   TIMESTAMP('2009-06-22 23:59:59'),
20 #   7,
21 #   'Which candidates would you like to see in the GNOME Foundation Board?');
22 #
23 # INSERT INTO election_choices (election_id, choice)
24 # VALUES ((SELECT LAST_INSERT_ID()), 'Firstname Lastname1'),
25 # ((SELECT LAST_INSERT_ID()), 'Firstname Lastname2'),
26 # ((SELECT LAST_INSERT_ID()), 'Youget Theidea');
27 # And "COMMIT;" if there were no errors. Or "ROLLBACK;" if there were errors.
28 #
29 # You should then use this script like this:
30 # $ ./create-tmp-tokens.pl 1 tokens.txt maildata.txt
31 #
32 # where 1 is the elections/referendum id in the database.
33 #
34 # tokens.txt now contains SQL statements you can use to create the temporary
35 # tokens in the database. You can do that with, e.g.
36 # mysql -h button-back -u username -p foundation < tokens.txt
37 #
38 # maildata.txt now contains the data that will be used by mail-instructions.pl
39 #
40 # This script assumes, that there is a "electorate" Table which can be a 
41 # simple VIEW created like this:
42 # CREATE OR REPLACE VIEW `foundation`.`electorate` AS SELECT  id, firstname, lastname, email FROM `foundation`.`foundationmembers` WHERE DATE_SUB(CURDATE(), INTERVAL 2 YEAR) <= foundationmembers.last_renewed_on;
43
44 #    This program is free software: you can redistribute it and/or modify
45 #    it under the terms of the GNU General Public License as published by
46 #    the Free Software Foundation, either version 3 of the License, or
47 #    (at your option) any later version.
48 #
49 #    This program is distributed in the hope that it will be useful,
50 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
51 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52 #    GNU General Public License for more details.
53 #
54 #    You should have received a copy of the GNU General Public License
55 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
56
57
58 die "Usage: create-tmp-tokens.pl <election id> <output file for tokens> <output file for mail data>\n" unless $#ARGV == 2;
59
60 $election_id = $ARGV[0];
61
62 open TOKENS, ">$ARGV[1]" || die "Cannot open file $ARGV[1]: $!";
63 open MAILDATA, ">$ARGV[2]" || die "Cannot open file $ARGV[2]: $!";
64
65 my $datasource = "dbi:mysql:foundation:button-back:3306";
66 my $dbi = DBI->connect ($datasource, 'username', 'password') or die "Unable to connect mysql server: $DBI:errstr\n";
67
68 my $query = "SET NAMES 'utf8'";
69 my $dbh = $dbi->prepare($query);
70 $dbh->execute();
71
72 my $query = "SELECT id,firstname,lastname,email FROM electorate";
73
74 my $dbh = $dbi->prepare($query);
75 $dbh->execute();
76 $dbh->bind_columns(\$id, \$firstname, \$lastname, \$email);
77
78 print TOKENS "SET NAMES 'utf8';\n";
79 while ($dbh->fetch()) { 
80     @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9 );
81     $token = join("", @chars[ map { rand @chars } ( 1 .. 10 ) ]);
82
83     print TOKENS "INSERT INTO election_tmp_tokens (election_id, member_id, tmp_token) VALUES ($election_id,$id,'$token');\n";
84     print MAILDATA "$firstname $lastname;$email;$token\n";
85 }
86
87 close MEMBERS;
88 close TOKENS;
89 close MAILDATA;