File: fixbadchars.pl

package info (click to toggle)
blootbot 1.1.0-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,420 kB
  • ctags: 586
  • sloc: perl: 15,941; sh: 154; makefile: 56; sql: 45
file content (67 lines) | stat: -rw-r--r-- 1,571 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/perl -w

use DBI;

my $dsn = "DBI:mysql:blootbot:localhost";
my $dbh = DBI->connect($dsn, "USERNAME", "PASSWORD");

my @factkey;
my %factval;
my $query;
my $regex = '\\\\([\_\%])';

$query = "SELECT factoid_key,factoid_value from factoids";
my $sth = $dbh->prepare($query);
$sth->execute;
while (my @row = $sth->fetchrow_array) {
    if ($row[0] =~ /$regex/) {
	push(@factkey,$row[0]);
    } else {
	$factval{$row[0]} = $row[1] if ($row[1] =~ /$regex/);
    }
}
$sth->finish;

print "scalar factkey => '". scalar(@factkey) ."'\n";
foreach (@factkey) {
    print "factkey => '$_'.\n";
    my $new = $_;
    $new =~ s/$regex/$1/g;

    next if ($new eq $_);

    $query = "SELECT factoid_key FROM factoids where factoid_key=".
		$dbh->quote($new);
    my $sth = $dbh->prepare($query);
    $sth->execute;
    if (scalar $sth->fetchrow_array) {	# exist.
	print "please remove $new or $_.\n";
    } else {				# ! exist.
	$sth->finish;

	$query = "UPDATE factoids SET factoid_key=".$dbh->quote($new).
		" WHERE factoid_key=".$dbh->quote($_);
	my $sth = $dbh->prepare($query);
	$sth->execute;
	$sth->finish;
    }
}

print "scalar factval => '". scalar(keys %factval) ."\n";
foreach (keys %factval) {
    print "factval => '$_'.\n";
    my $fact = $_;
    my $old = $factval{$_};
    my $new = $old;
    $new =~ s/$regex/$1/g;

    next if ($new eq $old);

    $query = "UPDATE factoids SET factoid_value=".$dbh->quote($new).
		" WHERE factoid_key=".$dbh->quote($fact);
    my $sth = $dbh->prepare($query);
    $sth->execute;
    $sth->finish;
}

$dbh->disconnect();