]> git.donarmstrong.com Git - infobot.git/blob - scripts/fixbadchars.pl
* Rebranding from blootbot to infobot
[infobot.git] / scripts / fixbadchars.pl
1 #!/usr/bin/perl -w
2
3 use DBI;
4
5 my $dsn = "DBI:mysql:infobot:localhost";
6 my $dbh = DBI->connect($dsn, "USERNAME", "PASSWORD");
7
8 my @factkey;
9 my %factval;
10 my $query;
11 my $regex = '\\\\([\_\%])';
12
13 $query = "SELECT factoid_key,factoid_value from factoids";
14 my $sth = $dbh->prepare($query);
15 $sth->execute;
16 while (my @row = $sth->fetchrow_array) {
17     if ($row[0] =~ /$regex/) {
18         push(@factkey,$row[0]);
19     } else {
20         $factval{$row[0]} = $row[1] if ($row[1] =~ /$regex/);
21     }
22 }
23 $sth->finish;
24
25 print "scalar factkey => '". scalar(@factkey) ."'\n";
26 foreach (@factkey) {
27     print "factkey => '$_'.\n";
28     my $new = $_;
29     $new =~ s/$regex/$1/g;
30
31     next if ($new eq $_);
32
33     $query = "SELECT factoid_key FROM factoids where factoid_key=".
34                 $dbh->quote($new);
35     my $sth = $dbh->prepare($query);
36     $sth->execute;
37     if (scalar $sth->fetchrow_array) {  # exist.
38         print "please remove $new or $_.\n";
39     } else {                            # ! exist.
40         $sth->finish;
41
42         $query = "UPDATE factoids SET factoid_key=".$dbh->quote($new).
43                 " WHERE factoid_key=".$dbh->quote($_);
44         my $sth = $dbh->prepare($query);
45         $sth->execute;
46         $sth->finish;
47     }
48 }
49
50 print "scalar factval => '". scalar(keys %factval) ."\n";
51 foreach (keys %factval) {
52     print "factval => '$_'.\n";
53     my $fact = $_;
54     my $old = $factval{$_};
55     my $new = $old;
56     $new =~ s/$regex/$1/g;
57
58     next if ($new eq $old);
59
60     $query = "UPDATE factoids SET factoid_value=".$dbh->quote($new).
61                 " WHERE factoid_key=".$dbh->quote($fact);
62     my $sth = $dbh->prepare($query);
63     $sth->execute;
64     $sth->finish;
65 }
66
67 $dbh->disconnect();