2 # Factoids.pl: Helpers for generating factoids statistics.
4 # Version: v0.1 (20000514)
5 # Splitted: SQLExtras.pl
8 if (&IsParam("useStrict")) { use strict; }
11 # Usage: &CmdFactInfo($faqtoid, $query);
13 my ($faqtoid, $query) = (lc $_[0], $_[1]);
24 my @factinfo = &getFactInfo($faqtoid,"*");
25 foreach ( &dbGetColInfo("factoids") ) {
26 $factinfo{$_} = $factinfo[$i] || '';
30 # factoid does not exist.
31 if (scalar @factinfo <= 1) {
32 &performReply("there's no such factoid as \002$faqtoid\002");
36 # fix for problem observed by asuffield.
37 # why did it happen though?
38 if (!$factinfo{'factoid_value'}) {
39 &performReply("there's no such factoid as \002$faqtoid\002; deleted because we don't have factoid_value!");
40 foreach (keys %factinfo) {
41 &DEBUG("factinfo{$_} => '$factinfo{$_}'.");
43 ### &delFactoid($faqtoid);
48 if ($factinfo{'created_by'}) {
50 $factinfo{'created_by'} =~ s/\!/ </;
51 $factinfo{'created_by'} .= ">";
52 $string = "created by $factinfo{'created_by'}";
54 my $time = $factinfo{'created_time'};
56 if (time() - $time > 60*60*24*7) {
57 my $days = int( (time() - $time)/60/60/24 );
58 $string .= " at \037". scalar(localtime $time). "\037" .
61 $string .= " ".&Time2String(time() - $time)." ago";
69 # if ($factinfo{'modified_by'}) {
70 # $string = "last modified";
72 # my $time = $factinfo{'modified_time'};
74 # if (time() - $time > 60*60*24*7) {
75 # $string .= " at \037". scalar(localtime $time). "\037";
77 # $string .= " ".&Time2String(time() - $time)." ago";
82 # foreach (split ",", $factinfo{'modified_by'}) {
86 # $string .= "by ".&IJoin(@x);
89 # push(@array,$string);
93 if ($factinfo{'requested_by'}) {
94 my $requested_count = $factinfo{'requested_count'};
96 if ($requested_count) {
97 $string = "it has been requested ";
98 if ($requested_count == 1) {
99 $string .= "\002once\002";
101 $string .= "\002". $requested_count. "\002 ".
102 &fixPlural("time", $requested_count);
106 $string .= ", " if ($string ne "");
108 my $requested_by = $factinfo{'requested_by'};
109 $requested_by =~ /\!/;
110 $string .= "last by $`";
112 my $requested_time = $factinfo{'requested_time'};
113 if ($requested_time) {
114 if (time() - $requested_time > 60*60*24*7) {
115 $string .= " at \037". scalar(localtime $requested_time). "\037";
117 $string .= ", ".&Time2String(time() - $requested_time)." ago";
121 push(@array,$string);
125 if ($factinfo{'locked_by'}) {
126 $factinfo{'locked_by'} =~ /\!/;
127 $string = "it has been locked by $`";
129 push(@array, $string);
132 # factoid was inserted not through the bot.
133 if (!scalar @array) {
134 &performReply("no extra info on \002$faqtoid\002");
138 &performStrictReply("$factinfo{'factoid_key'} -- ". join("; ", @array) .".");
145 if ($type =~ /^author$/i) {
146 my %hash = &dbGetCol("factoids", "factoid_key,created_by", "created_by IS NOT NULL");
149 foreach (keys %hash) {
150 my $thisnuh = $hash{$_};
152 $thisnuh =~ /^(\S+)!\S+@\S+$/;
156 if (!scalar keys %author) {
157 return 'sorry, no factoids with created_by field.';
162 foreach (keys %author) {
163 $count{ $author{$_} }{$_} = 1;
169 foreach $count (sort { $b <=> $a } keys %count) {
170 my $author = join(", ", sort keys %{ $count{$count} });
171 push(@list, "$count by $author");
174 my $prefix = "factoid statistics by author: ";
175 return &formListReply(0, $prefix, @list);
177 } elsif ($type =~ /^vandalism$/i) {
178 &status("factstats(vandalism): starting...");
179 my $start_time = &timeget();
180 my %data = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL");
183 my $delta_time = &timedelta($start_time);
184 &status(sprintf("factstats(vandalismbroken): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
185 $start_time = &timeget();
187 # parse the factoids.
188 foreach (keys %data) {
189 if (&validFactoid($_, $data{$_}) == 0) {
190 s/([\,\;]+)/\037$1\037/g; # highlight chars.
191 push(@list, $_); # push it.
195 $delta_time = &timedelta($start_time);
196 &status(sprintf("factstats(vandalism): %.02f sec to complete.", $delta_time)) if ($delta_time > 0);
198 # bail out on no results.
199 if (scalar @list == 0) {
200 return 'no vandalised factoids... wooohoo.';
204 my $prefix = "Vandalised factoid ";
205 return &formListReply(1, $prefix, @list);
207 } elsif ($type =~ /^total$/i) {
208 &status("factstats(total): starting...");
209 my $start_time = &timeget();
216 # total factoids requests.
217 $i = &sumKey("factoids", "requested_count");
218 push(@list, "total requests - $i");
220 # total factoids modified.
221 $str = &countKeys("factoids", "modified_by");
222 push(@list, "total modified - $str");
224 # total factoids modified.
225 $j = &countKeys("factoids", "requested_count");
226 $str = &countKeys("factoids", "factoid_key");
227 push(@list, "total non-requested - ".($str - $i));
229 # average request/factoid.
230 # i/j == total(requested_count)/count(requested_count)
231 $str = sprintf("%.01f", $i/$j);
232 push(@list, "average requested per factoid - $str");
234 # total prepared for deletion.
235 $str = scalar( &searchTable("factoids", "factoid_key", "factoid_value", " #DEL") );
236 push(@list, "total prepared for deletion - $str");
238 # total unique authors.
239 foreach ( &dbRawReturn("SELECT created_by FROM factoids WHERE created_by IS NOT NULL") ) {
244 push(@list, "total unique authors - ".(scalar keys %hash) );
247 # total unique requesters.
248 foreach ( &dbRawReturn("SELECT requested_by FROM factoids WHERE requested_by IS NOT NULL") ) {
253 push(@list, "total unique requesters - ".(scalar keys %hash) );
258 my $delta_time = &timedelta($start_time);
259 &status(sprintf("factstats(broken): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
260 $start_time = &timeget();
262 # bail out on no results.
263 if (scalar @list == 0) {
264 return 'no broken factoids... wooohoo.';
268 my $prefix = "General factoid stiatistics ";
269 return &formListReply(1, $prefix, @list);
271 } elsif ($type =~ /^deadredir$/i) {
272 my @list = &searchTable("factoids", "factoid_key",
273 "factoid_value", "^<REPLY> see ");
279 my $val = &getFactInfo($factoid, "factoid_value");
280 if ($val =~ /^<REPLY> see( also)? (.*?)\.?$/i) {
282 my $redir = &getFactInfo($redirf, "factoid_value");
283 next if (defined $redir);
284 next if (length $val > 50);
286 $redir{$redirf}{$factoid} = 1;
291 foreach $f (keys %redir) {
292 my @sublist = keys %{ $redir{$f} };
294 s/([\,\;]+)/\037$1\037/g;
297 push(@newlist, join(', ', @sublist)." => $f");
301 my $prefix = "Loose link (dead) redirections in factoids ";
302 return &formListReply(1, $prefix, @newlist);
304 } elsif ($type =~ /^dup(licate|e)$/i) {
305 &status("factstats(dupe): starting...");
306 my $start_time = &timeget();
307 my %hash = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL", 1);
312 foreach $v (keys %hash) {
313 my $count = scalar(keys %{ $hash{$v} });
314 next if ($count == 1);
317 foreach (keys %{ $hash{$v} }) {
318 if ($v =~ /^<REPLY> see /i) {
323 s/([\,\;]+)/\037$1\037/g;
325 &WARN("dupe: _ = NULL. should never happen!.");
331 next unless (scalar @sublist);
333 push(@list, join(", ", @sublist));
336 &status("factstats(dupe): (good) dupe refs: $refs.");
337 my $delta_time = &timedelta($start_time);
338 &status(sprintf("factstats(dupe): %.02f sec to complete", $delta_time)) if ($delta_time > 0);
340 # bail out on no results.
341 if (scalar @list == 0) {
342 return "no duplicate factoids... woohoo.";
346 my $prefix = "dupe factoid ";
347 return &formListReply(1, $prefix, @list);
349 } elsif ($type =~ /^nullfactoids$/i) {
350 my $query = "SELECT factoid_key,factoid_value FROM factoids WHERE factoid_value=''";
351 my $sth = $dbh->prepare($query);
352 &ERROR("factstats(null): => '$query'.") unless $sth->execute;
355 while (my @row = $sth->fetchrow_array) {
357 &DEBUG("row[1] != NULL for $row[0].");
361 &DEBUG("row[0] => '$row[0]'.");
362 push(@list, $row[0]);
367 my $prefix = "NULL factoids (not deleted yet) ";
368 return &formListReply(1, $prefix, @list);
370 } elsif ($type =~ /^(2|too)short$/i) {
371 # Custom select statement.
372 my $query = "SELECT factoid_key,factoid_value FROM factoids WHERE length(factoid_value) <= 40";
373 my $sth = $dbh->prepare($query);
374 &ERROR("factstats(lame): => '$query'.") unless $sth->execute;
377 while (my @row = $sth->fetchrow_array) {
378 my($key,$val) = ($row[0], $row[1]);
380 $match++ if ($val =~ /\s{3,}/);
381 next unless ($match);
383 my $v = &getFactoid($val);
385 &DEBUG("key $key => $val => $v");
388 $key =~ s/\,/\037\,\037/g;
394 my $prefix = "Lame factoids ";
395 return &formListReply(1, $prefix, @list);
397 } elsif ($type =~ /^listfix$/i) {
398 # Custom select statement.
399 my $query = "SELECT factoid_key,factoid_value FROM factoids";
400 my $sth = $dbh->prepare($query);
401 &ERROR("factstats(listfix): => '$query'.") unless $sth->execute;
404 while (my @row = $sth->fetchrow_array) {
405 my($key,$val) = ($row[0], $row[1]);
407 $match++ if ($val =~ /\S+,? or \S+,? or \S+,? or \S+,?/);
408 next unless ($match);
410 $key =~ s/\,/\037\,\037/g;
412 $val =~ s/,? or /, /g;
413 &DEBUG("fixed: => $val.");
414 &setFactInfo($key,"factoid_value", $val);
419 my $prefix = "Inefficient lists fixed ";
420 return &formListReply(1, $prefix, @list);
422 } elsif ($type =~ /^locked$/i) {
423 my %hash = &dbGetCol("factoids", "factoid_key,locked_by", "locked_by IS NOT NULL");
424 my @list = keys %hash;
427 s/([\,\;]+)/\037$1\037/g;
430 my $prefix = "factoid statistics on $type ";
431 return &formListReply(0, $prefix, @list);
433 } elsif ($type =~ /^new$/i) {
434 my %hash = &dbGetCol("factoids", "factoid_key,created_time", "created_time IS NOT NULL");
437 foreach (keys %hash) {
438 my $created_time = $hash{$_};
439 my $delta_time = time() - $created_time;
440 next if ($delta_time >= 60*60*24);
442 $age{$delta_time}{$_} = 1;
445 if (scalar keys %age == 0) {
446 return "sorry, no new factoids.";
450 foreach (sort {$a <=> $b} keys %age) {
451 push(@list, join(",", keys %{ $age{$_} }));
454 my $prefix = "new factoids in the last 24hours ";
455 return &formListReply(0, $prefix, @list);
457 } elsif ($type =~ /^part(ial)?dupe$/i) {
458 ### requires "custom" select statement... oh well...
459 my $start_time = &timeget();
461 # form length|key and key=length hash list.
462 &status("factstats(partdupe): forming length hash list.");
463 my $query = "SELECT factoid_key,factoid_value,length(factoid_value) AS length FROM factoids WHERE length(factoid_value) >= 192 ORDER BY length";
464 my $sth = $dbh->prepare($query);
465 &ERROR("factstats(partdupe): => '$query'.") unless $sth->execute;
469 while (my @row = $sth->fetchrow_array) {
470 $length{$row[2]}{$row[0]} = 1; # length(value)|key.
471 $key{$row[0]} = $row[1]; # key=value.
475 &status("factstats(partdupe): total keys => '". scalar(@key) ."'.");
476 &status("factstats(partdupe): now deciphering data gathered");
478 my @length = sort { $a <=> $b } keys %length;
481 foreach $key (@key) {
482 shift @length if (length $key{$key} == $length[0]);
484 my $val = quotemeta $key{$key};
487 foreach $length (@length) {
488 foreach (keys %{ $length{$length} }) {
489 if ($key{$_} =~ /^$val/i) {
490 s/([\,\;]+)/\037$1\037/g;
491 s/( and|and )/\037$1\037/g;
492 push(@sublist,$key." and ".$_);
496 push(@list, join(" ,",@sublist)) if (scalar @sublist);
499 my $delta_time = sprintf("%.02fs", &timedelta($start_time) );
500 &status("factstats(partdupe): $delta_time sec to complete.") if ($delta_time > 0);
502 # bail out on no results.
503 if (scalar @list == 0) {
504 return "no initial partial duplicate factoids... woohoo.";
508 my $prefix = "initial partial dupe factoid ";
509 return &formListReply(1, $prefix, @list);
511 } elsif ($type =~ /^profanity$/i) {
512 my %data = &dbGetCol("factoids", "factoid_key,factoid_value", "factoid_value IS NOT NULL");
515 foreach (keys %data) {
516 push(@list, $_) if (&hasProfanity($_." ".$data{$_}));
520 my $prefix = "Profanity in factoids ";
521 return &formListReply(1, $prefix, @list);
523 } elsif ($type =~ /^redir(ection)?$/i) {
524 my @list = &searchTable("factoids", "factoid_key",
525 "factoid_value", "^<REPLY> see ");
531 my $val = &getFactInfo($factoid, "factoid_value");
532 if ($val =~ /^<REPLY> see( also)? (.*?)\.?$/i) {
534 my $redirval = &getFactInfo($redir, "factoid_value");
535 if (defined $redirval) {
536 $redir{$redir}{$factoid} = 1;
538 &WARN("factstats(redir): '$factoid' has loose link => '$redir'.");
544 foreach $f (keys %redir) {
545 my @sublist = keys %{ $redir{$f} };
547 s/([\,\;]+)/\037$1\037/g;
550 push(@newlist, "$f => ". join(', ', @sublist));
554 my $prefix = "Redirections in factoids ";
555 return &formListReply(1, $prefix, @newlist);
557 } elsif ($type =~ /^request(ed)?$/i) {
558 my %hash = &dbGetCol("factoids", "factoid_key,requested_count", "requested_count IS NOT NULL", 1);
560 if (!scalar keys %hash) {
561 return 'sorry, no factoids have been questioned.';
567 foreach $count (sort {$b <=> $a} keys %hash) {
568 my @faqtoids = sort keys %{ $hash{$count} };
571 s/([\,\;]+)/\037$1\037/g;
573 $total += $count * scalar(@faqtoids);
575 push(@list, "$count - ". join(", ", @faqtoids));
577 unshift(@list, "\037$total - TOTAL\037");
579 my $prefix = "factoid statistics on $type ";
580 return &formListReply(0, $prefix, @list);
582 } elsif ($type =~ /^reqrate$/i) {
583 my %hash = &dbGetCol("factoids",
584 "factoid_key,(unix_timestamp() - created_time)/requested_count as rate",
585 "requested_by IS NOT NULL and created_time IS NOT NULL ORDER BY rate LIMIT 15", 1);
591 foreach $rate (sort { $b <=> $a } keys %hash) {
592 my $f = join(", ", sort keys %{ $hash{$rate} });
593 my $str = "$f - ".&Time2String($rate);
598 my $prefix = "Rank of top factoid rate (time/req): ";
599 return &formListReply(0, $prefix, @list);
601 } elsif ($type =~ /^requesters?$/i) {
602 my %hash = &dbGetCol("factoids", "factoid_key,requested_by", "requested_by IS NOT NULL");
605 foreach (keys %hash) {
606 my $thisnuh = $hash{$_};
608 $thisnuh =~ /^(\S+)!\S+@\S+$/;
612 if (!scalar keys %requester) {
613 return 'sorry, no factoids with requested_by field.';
618 foreach (keys %requester) {
619 $count{ $requester{$_} }{$_} = 1;
627 foreach $count (sort { $b <=> $a } keys %count) {
628 my $requester = join(", ", sort keys %{ $count{$count} });
629 $total += $count * scalar(keys %{ $count{$count} });
630 $users += scalar(keys %{ $count{$count} });
631 push(@list, "$count by $requester");
633 unshift(@list, "\037$total TOTAL REQUESTS; $users UNIQUE REQUESTERS\037");
634 # should not the above value be the same as collected by
635 # 'requested'? soemthing weird is going on!
637 my $prefix = "rank of top factoid requesters: ";
638 return &formListReply(0, $prefix, @list);
640 } elsif ($type =~ /^seefix$/i) {
641 my @list = &searchTable("factoids", "factoid_key",
642 "factoid_value", "^see ");
650 my $val = &getFactInfo($factoid, "factoid_value");
652 next unless ($val =~ /^see( also)? (.*?)\.?$/i);
655 my $redir = &getFactInfo($redirf, "factoid_value");
657 if ($redirf =~ /^\Q$factoid\W$/i) {
658 &delFactoid($factoid);
662 if (defined $redir) { # good.
663 &setFactInfo($factoid,"factoid_value","<REPLY> see $redir");
666 push(@newlist, $redirf);
671 &msg($who, "Fixed $fixed factoids.");
672 &msg($who, "Self looped factoids removed: ".
673 sort(keys %loop) ) if (scalar keys %loop);
675 my $prefix = "Loose link (dead) redirections in factoids ";
676 return &formListReply(1, $prefix, @newlist);
678 } elsif ($type =~ /^(2|too)long$/i) {
682 $query = "SELECT factoid_key FROM factoids WHERE length(factoid_key) >= $param{'maxKeySize'}";
683 my $sth = $dbh->prepare($query);
685 while (my @row = $sth->fetchrow_array) {
690 my $query = "SELECT factoid_key,factoid_value FROM factoids WHERE length(factoid_value) >= $param{'maxDataSize'}";
691 $sth = $dbh->prepare($query);
693 while (my @row = $sth->fetchrow_array) {
694 push(@list,sprintf("\002%s\002 - %s", length($row[1]), $row[0]));
697 if (scalar @list == 0) {
698 return "good. no factoids exceed length.";
702 my $prefix = "factoid key||value exceeding length ";
703 return &formListReply(1, $prefix, @list);
705 } elsif ($type =~ /^unrequest(ed)?$/i) {
706 my @list = &dbRawReturn("SELECT factoid_key FROM factoids WHERE requested_count IS NULL");
709 s/([\,\;]+)/\037$1\037/g;
712 my $prefix = "Unrequested factoids ";
713 return &formListReply(0, $prefix, @list);
716 return "error: invalid type => '$type'.";
721 my @list = &searchTable("factoids","factoid_key", "created_by", "^$query!");
723 my $prefix = "factoid author list by '$query' ";
724 &performStrictReply( &formListReply(1, $prefix, @list) );