- if ($type =~ /^author$/i) {
- my %hash = &sqlSelectColHash("factoids",
- "factoid_key,created_by", undef,
- "WHERE created_by IS NOT NULL"
- );
- my %author;
-
- foreach my $factoid (keys %hash) {
- my $thisnuh = $hash{$factoid};
-
- $thisnuh =~ /^(\S+)!\S+@\S+$/;
- $author{lc $1}++;
- }
-
- if (!scalar keys %author) {
- return 'sorry, no factoids with created_by field.';
- }
-
- # work-around.
- my %count;
- foreach (keys %author) {
- $count{ $author{$_} }{$_} = 1;
- }
- undef %author;
-
- my $count;
- my @list;
- foreach $count (sort { $b <=> $a } keys %count) {
- my $author = join(", ", sort keys %{ $count{$count} });
- push(@list, "$count by $author");
- }
-
- my $prefix = "factoid statistics by author: ";
- return &formListReply(0, $prefix, @list);
-
- } elsif ($type =~ /^vandalism$/i) {
- &status("factstats(vandalism): starting...");
- my $start_time = &timeget();
- my %data = &sqlSelectColHash("factoids",
- "factoid_key,factoid_value", undef,
- "WHERE factoid_value IS NOT NULL"
- );
- my @list;
-
- my $delta_time = &timedelta($start_time);
- &status(sprintf("factstats(vandalism): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
- $start_time = &timeget();
-
- # parse the factoids.
- foreach (keys %data) {
- if (&validFactoid($_, $data{$_}) == 0) {
- s/([\,\;]+)/\037$1\037/g; # highlight chars.
- push(@list, $_); # push it.
- }
- }
-
- $delta_time = &timedelta($start_time);
- &status(sprintf("factstats(vandalism): %.02f sec to complete.", $delta_time)) if ($delta_time > 0);
-
- # bail out on no results.
- if (scalar @list == 0) {
- return 'no vandalised factoids... wooohoo.';
- }
-
- # parse the results.
- my $prefix = "Vandalised factoid ";
- return &formListReply(1, $prefix, @list);
-
- } elsif ($type =~ /^total$/i) {
- &status("factstats(total): starting...");
- my $start_time = &timeget();
- my @list;
- my $str;
- my($i,$j);
- my %hash;
-
- ### lets do it.
- # total factoids requests.
- $i = &sumKey("factoids", "requested_count");
- push(@list, "total requests - $i");
-
- # total factoids modified.
- $str = &countKeys("factoids", "modified_by");
- push(@list, "total modified - $str");
-
- # total factoids modified.
- $j = &countKeys("factoids", "requested_count");
- $str = &countKeys("factoids", "factoid_key");
- push(@list, "total non-requested - ".($str - $i));
-
- # average request/factoid.
- # i/j == total(requested_count)/count(requested_count)
- $str = sprintf("%.01f", $i/$j);
- push(@list, "average requested per factoid - $str");
-
- # total prepared for deletion.
- $str = scalar( &searchTable("factoids", "factoid_key", "factoid_value", " #DEL") );
- push(@list, "total prepared for deletion - $str");
-
- # total unique authors.
- # TODO: convert to sqlSelectColHash ? (or ColArray?)
- foreach ( &sqlRawReturn("SELECT created_by FROM factoids WHERE created_by IS NOT NULL") ) {
- /^(\S+)!/;
- my $nick = lc $1;
- $hash{$nick}++;
- }
- push(@list, "total unique authors - ".(scalar keys %hash) );
- undef %hash;
-
- # total unique requesters.
- foreach ( &sqlRawReturn("SELECT requested_by FROM factoids WHERE requested_by IS NOT NULL") ) {
- /^(\S+)!/;
- my $nick = lc $1;
- $hash{$nick}++;
- }
- push(@list, "total unique requesters - ".(scalar keys %hash) );
- undef %hash;
-
- ### end of "job".
-
- my $delta_time = &timedelta($start_time);
- &status(sprintf("factstats(broken): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
- $start_time = &timeget();
-
- # bail out on no results.
- if (scalar @list == 0) {
- return 'no broken factoids... wooohoo.';
- }
-
- # parse the results.
- my $prefix = "General factoid statistics ";
- return &formListReply(1, $prefix, @list);
-
- } elsif ($type =~ /^deadredir$/i) {
- my @list = &searchTable("factoids", "factoid_key",
- "factoid_value", "^<REPLY> see ");
- my %redir;
- my $f;
-
- for (@list) {
- my $factoid = $_;
- my $val = &getFactInfo($factoid, "factoid_value");
- if ($val =~ /^<REPLY> ?see( also)? (.*?)\.?$/i) {
- my $redirf = lc $2;
- my $redir = &getFactInfo($redirf, "factoid_value");
- next if (defined $redir);
- next if (length $val > 50);
-
- $redir{$redirf}{$factoid} = 1;
- }
- }
-
- my @newlist;
- foreach $f (keys %redir) {
- my @sublist = keys %{ $redir{$f} };
- for (@sublist) {
- s/([\,\;]+)/\037$1\037/g;
- }
-
- push(@newlist, join(', ', @sublist)." => $f");
- }
-
- # parse the results.
- my $prefix = "Loose link (dead) redirections in factoids ";
- return &formListReply(1, $prefix, @newlist);
-
- } elsif ($type =~ /^dup(licate|e)$/i) {
- &status("factstats(dupe): starting...");
- my $start_time = &timeget();
- my %hash = &sqlSelectColHash("factoids",
- "factoid_key,factoid_value", undef,
- "WHERE factoid_value IS NOT NULL", 1
- );
- my $refs = 0;
- my @list;
- my $v;
-
- foreach $v (keys %hash) {
- my $count = scalar(keys %{ $hash{$v} });
- next if ($count == 1);
-
- my @sublist;
- foreach (keys %{ $hash{$v} }) {
- if ($v =~ /^<REPLY> see /i) {
- $refs++;
- next;
- }
-
- s/([\,\;]+)/\037$1\037/g;
- if ($_ eq "") {
- &WARN("dupe: _ = NULL. should never happen!.");
- next;
- }
- push(@sublist, $_);
- }
-
- next unless (scalar @sublist);
-
- push(@list, join(", ", @sublist));
- }
-
- &status("factstats(dupe): (good) dupe refs: $refs.");
- my $delta_time = &timedelta($start_time);
- &status(sprintf("factstats(dupe): %.02f sec to complete", $delta_time)) if ($delta_time > 0);
-
- # bail out on no results.
- if (scalar @list == 0) {
- return "no duplicate factoids... woohoo.";
- }
-
- # parse the results.
- my $prefix = "dupe factoid ";
- return &formListReply(1, $prefix, @list);
-
- } elsif ($type =~ /^nullfactoids$/i) {
- my $query = "SELECT factoid_key,factoid_value FROM factoids WHERE factoid_value=''";
- my $sth = $dbh->prepare($query);
- &ERROR("factstats(null): => '$query'.") unless $sth->execute;
-
- my @list;
- while (my @row = $sth->fetchrow_array) {
- if ($row[1] ne "") {
- &DEBUG("row[1] != NULL for $row[0].");
- next;
- }
-
- &DEBUG("row[0] => '$row[0]'.");
- push(@list, $row[0]);
- }
- $sth->finish;
-
- # parse the results.
- my $prefix = "NULL factoids (not deleted yet) ";
- return &formListReply(1, $prefix, @list);
-
- } elsif ($type =~ /^(2|too)short$/i) {
- # Custom select statement.
- my $query = "SELECT factoid_key,factoid_value FROM factoids WHERE length(factoid_value) <= 40";
- my $sth = $dbh->prepare($query);
- &ERROR("factstats(lame): => '$query'.") unless $sth->execute;
-
- my @list;
- while (my @row = $sth->fetchrow_array) {
- my($key,$val) = ($row[0], $row[1]);
- my $match = 0;
- $match++ if ($val =~ /\s{3,}/);
- next unless ($match);
-
- my $v = &getFactoid($val);
- if (defined $v) {
- &DEBUG("key $key => $val => $v");
- }
-
- $key =~ s/\,/\037\,\037/g;
- push(@list, $key);
- }
- $sth->finish;
-
- # parse the results.
- my $prefix = "Lame factoids ";
- return &formListReply(1, $prefix, @list);
-
- } elsif ($type =~ /^listfix$/i) {
- # Custom select statement.
- my $query = "SELECT factoid_key,factoid_value FROM factoids";
- my $sth = $dbh->prepare($query);
- &ERROR("factstats(listfix): => '$query'.") unless $sth->execute;
-
- my @list;
- while (my @row = $sth->fetchrow_array) {
- my($key,$val) = ($row[0], $row[1]);
- my $match = 0;
- $match++ if ($val =~ /\S+,? or \S+,? or \S+,? or \S+,?/);
- next unless ($match);
-
- $key =~ s/\,/\037\,\037/g;
- push(@list, $key);
- $val =~ s/,? or /, /g;
- &DEBUG("fixed: => $val.");
- &setFactInfo($key,"factoid_value", $val);
- }
- $sth->finish;
-
- # parse the results.
- my $prefix = "Inefficient lists fixed ";
- return &formListReply(1, $prefix, @list);
-
- } elsif ($type =~ /^locked$/i) {
- my %hash = &sqlSelectColHash("factoids",
- "factoid_key,locked_by", undef,
- "WHERE locked_by IS NOT NULL"
- );
- my @list = keys %hash;