]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Modules/Factoids.pl
use count(*) for unrequested
[infobot.git] / src / Modules / Factoids.pl
index 7b1ac1446010d76c9d3aebb3593cf1004e4a7419..c163edb888ba4f899bccc418e10f0e232518feac 100644 (file)
@@ -22,7 +22,9 @@ sub CmdFactInfo {
        return;
     }
 
-    my %factinfo = &dbGetColNiceHash("factoids", "*", "factoid_key=".&dbQuote($faqtoid));
+    my %factinfo = &sqlSelectRowHash("factoids", "*",
+       { factoid_key => $faqtoid }
+    );
 
     # factoid does not exist.
     if (scalar (keys %factinfo) <= 1) {
@@ -62,7 +64,7 @@ sub CmdFactInfo {
        push(@array,$string);
     }
 
-    # modified:
+    # modified: (TimRiker asks "why do you keep turning this off?)
     if ($factinfo{'modified_by'}) {
        $string = "last modified";
 
@@ -75,14 +77,8 @@ sub CmdFactInfo {
            }
        }
 
-       my @x;
-       foreach (split ",", $factinfo{'modified_by'}) {
-           /\!/;
-           push(@x, $`);
-       }
-       $string .= "by ".&IJoin(@x);
+       $string .= " by ".(split ",", $factinfo{'modified_by'})[0];
 
-       $i++;
        push(@array,$string);
     }
 
@@ -98,24 +94,24 @@ sub CmdFactInfo {
                $string .= "\002". $requested_count. "\002 ".
                        &fixPlural("time", $requested_count);
            }
-       }
 
-       $string .= ", " if ($string ne "");
+           my $requested_by = $factinfo{'requested_by'};
+           $requested_by =~ /\!/;
+           $string .= ", last by $`";
 
-       my $requested_by = $factinfo{'requested_by'};
-       $requested_by =~ /\!/;
-       $string .= "last by $`";
-
-       my $requested_time = $factinfo{'requested_time'};
-       if ($requested_time) {
-           if (time() - $requested_time > 60*60*24*7) {
-               $string .= " at \037". scalar(gmtime $requested_time). "\037";
-           } else {
-               $string .= ", ".&Time2String(time() - $requested_time)." ago";
+           my $requested_time = $factinfo{'requested_time'};
+           if ($requested_time) {
+               if (time() - $requested_time > 60*60*24*7) {
+                   $string .= " at \037". scalar(localtime $requested_time). "\037";
+               } else {
+                   $string .= ", ".&Time2String(time() - $requested_time)." ago";
+               }
            }
+       } else {
+           $string  = "has not been requested yet";
        }
 
-       push(@array,$string);
+       push(@array, $string);
     }
 
     # locked:
@@ -132,7 +128,7 @@ sub CmdFactInfo {
        return;
     }
 
-    &performStrictReply("$factinfo{'factoid_key'} -- ". join("; ", @array) .".");
+    &pSReply("$factinfo{'factoid_key'} -- ". join("; ", @array) .".");
     return;
 }
 
@@ -140,11 +136,14 @@ sub CmdFactStats {
     my ($type) = @_;
 
     if ($type =~ /^author$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key,created_by", "created_by IS NOT NULL");
+       my %hash = &sqlSelectColHash("factoids",
+               "factoid_key,created_by", { },
+               "created_by IS NOT NULL"
+       );
        my %author;
 
-       foreach (keys %hash) {
-           my $thisnuh = $hash{$_};
+       foreach my $factoid (keys %hash) {
+           my $thisnuh = $hash{$factoid};
 
            $thisnuh =~ /^(\S+)!\S+@\S+$/;
            $author{lc $1}++;
@@ -174,11 +173,14 @@ sub CmdFactStats {
     } elsif ($type =~ /^vandalism$/i) {
         &status("factstats(vandalism): starting...");
        my $start_time  = &timeget();
-       my %data        = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL");
+       my %data        = &sqlSelectColHash("factoids",
+               "factoid_key,factoid_value", { },
+               "factoid_value IS NOT NULL"
+       );
        my @list;
 
        my $delta_time  = &timedelta($start_time);
-        &status(sprintf("factstats(vandalismbroken): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
+        &status(sprintf("factstats(vandalism): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
        $start_time     = &timeget();
 
        # parse the factoids.
@@ -233,7 +235,8 @@ sub CmdFactStats {
        push(@list, "total prepared for deletion - $str");
 
        # total unique authors.
-       foreach ( &dbRawReturn("SELECT created_by FROM factoids WHERE created_by IS NOT NULL") ) {
+       # 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}++;
@@ -242,7 +245,7 @@ sub CmdFactStats {
        undef %hash;
 
        # total unique requesters.
-       foreach ( &dbRawReturn("SELECT requested_by FROM factoids WHERE requested_by IS NOT NULL") ) {
+       foreach ( &sqlRawReturn("SELECT requested_by FROM factoids WHERE requested_by IS NOT NULL") ) {
            /^(\S+)!/;
            my $nick = lc $1;
            $hash{$nick}++;
@@ -301,7 +304,10 @@ sub CmdFactStats {
     } elsif ($type =~ /^dup(licate|e)$/i) {
         &status("factstats(dupe): starting...");
        my $start_time  = &timeget();
-       my %hash        = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL", 1);
+       my %hash        = &sqlSelectColHash("factoids", 
+               "factoid_key,factoid_value", { },
+               "factoid_value IS NOT NULL", 1
+       );
        my $refs        = 0;
        my @list;
        my $v;
@@ -417,7 +423,10 @@ sub CmdFactStats {
        return &formListReply(1, $prefix, @list);
 
     } elsif ($type =~ /^locked$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key,locked_by", "locked_by IS NOT NULL");
+       my %hash = &sqlSelectColhash("factoids", 
+               "factoid_key,locked_by", { },
+               "locked_by IS NOT NULL"
+       );
        my @list = keys %hash;
 
        for (@list) {
@@ -428,7 +437,10 @@ sub CmdFactStats {
        return &formListReply(0, $prefix, @list);
 
     } elsif ($type =~ /^new$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key,created_time", "created_time IS NOT NULL");
+       my %hash = &sqlSelectColHash("factoids",
+               "factoid_key,created_time", { },
+               "created_time IS NOT NULL"
+       );
        my %age;
 
        foreach (keys %hash) {
@@ -506,7 +518,10 @@ sub CmdFactStats {
        return &formListReply(1, $prefix, @list);
 
     } elsif ($type =~ /^profanity$/i) {
-       my %data = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL");
+       my %data = &sqlSelectColHash("factoids",
+               "factoid_key,factoid_value", { },
+               "factoid_value IS NOT NULL"
+       );
        my @list;
 
        foreach (keys %data) {
@@ -522,6 +537,7 @@ sub CmdFactStats {
                        "factoid_value", "^<REPLY> see ");
        my %redir;
        my $f;
+       my $dangling = 0;
 
        for (@list) {
            my $factoid = $_;
@@ -532,7 +548,8 @@ sub CmdFactStats {
                if (defined $redirval) {
                    $redir{$redir}{$factoid} = 1;
                } else {
-                   &WARN("factstats(redir): '$factoid' has loose link => '$redir'.");
+                   &DEBUG("factstats(redir): '$factoid' has loose link => '$redir'.");
+                   $dangling++;
                }
            }
        }
@@ -548,11 +565,14 @@ sub CmdFactStats {
        }
 
        # parse the results.
-       my $prefix = "Redirections in factoids ";
+       my $prefix = "Redirections in factoids, $dangling dangling ";
        return &formListReply(1, $prefix, @newlist);
 
     } elsif ($type =~ /^request(ed)?$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key,requested_count", "requested_count IS NOT NULL", 1);
+       my %hash = &sqlSelectColHash("factoids",
+               "factoid_key,requested_count", { },
+               "requested_count IS NOT NULL", 1
+       );
 
        if (!scalar keys %hash) {
            return 'sorry, no factoids have been questioned.';
@@ -577,9 +597,10 @@ sub CmdFactStats {
        return &formListReply(0, $prefix, @list);
 
     } elsif ($type =~ /^reqrate$/i) {
-       my %hash = &dbGetCol("factoids",
-               "factoid_key,(unix_timestamp() - created_time)/requested_count as rate",
-               "requested_by IS NOT NULL and created_time IS NOT NULL ORDER BY rate LIMIT 15", 1);
+       my %hash = &sqlSelectColHash("factoids",
+               "factoid_key,(unix_timestamp() - created_time)/requested_count as rate", { },
+               "requested_by IS NOT NULL and created_time IS NOT NULL ORDER BY rate LIMIT 15", 1
+       );
 
        my $rate;
        my @list;
@@ -596,7 +617,10 @@ sub CmdFactStats {
        return &formListReply(0, $prefix, @list);
 
     } elsif ($type =~ /^requesters?$/i) {
-       my %hash = &dbGetCol("factoids", "factoid_key,requested_by", "requested_by IS NOT NULL");
+       my %hash = &sqlSelectColHash("factoids",
+               "factoid_key,requested_by", { },
+               "requested_by IS NOT NULL"
+       );
        my %requester;
 
        foreach (keys %hash) {
@@ -645,7 +669,7 @@ sub CmdFactStats {
        for (@list) {
            my $factoid = $_;
            my $val = &getFactInfo($factoid, "factoid_value");
-       
+
            next unless ($val =~ /^see( also)? (.*?)\.?$/i);
 
            my $redirf  = lc $2;
@@ -703,14 +727,10 @@ sub CmdFactStats {
        return &formListReply(1, $prefix, @list);
 
     } elsif ($type =~ /^unrequest(ed)?$/i) {
-       my @list = &dbRawReturn("SELECT factoid_key FROM factoids WHERE requested_count IS NULL");
-
-       for (@list) {
-           s/([\,\;]+)/\037$1\037/g;
-       }
+       # todo: use sqlSelect()
+       my $count = &sqlRawReturn("SELECT COUNT(*) FROM factoids WHERE requested_count = '0'");
 
-       my $prefix = "Unrequested factoids ";
-       return &formListReply(0, $prefix, @list);
+       return "Unrequested factoids: $count";
     }
 
     return "error: invalid type => '$type'.";