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 ( &dbGetRowInfo("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");
37 if ($factinfo{'created_by'}) {
39 $factinfo{'created_by'} =~ s/\!/ </;
40 $factinfo{'created_by'} .= ">";
41 $string = "created by $factinfo{'created_by'}";
43 my $time = $factinfo{'created_time'};
45 if (time() - $time > 60*60*24*7) {
46 $string .= " at \037". scalar(localtime $time). "\037";
48 $string .= " ".&Time2String(time() - $time)." ago";
56 # if ($factinfo{'modified_by'}) {
57 # $string = "last modified";
59 # my $time = $factinfo{'modified_time'};
61 # if (time() - $time > 60*60*24*7) {
62 # $string .= " at \037". scalar(localtime $time). "\037";
64 # $string .= " ".&Time2String(time() - $time)." ago";
69 # foreach (split ",", $factinfo{'modified_by'}) {
73 # $string .= "by ".&IJoin(@x);
76 # push(@array,$string);
80 if ($factinfo{'requested_by'}) {
81 my $requested_count = $factinfo{'requested_count'};
83 if ($requested_count) {
84 $string = "it has been requested ";
85 if ($requested_count == 1) {
86 $string .= "\002once\002";
88 $string .= "\002". $requested_count. "\002 ".
89 &fixPlural("time", $requested_count);
93 $string .= ", " if ($string ne "");
95 my $requested_by = $factinfo{'requested_by'};
96 $requested_by =~ /\!/;
97 $string .= "last by $`";
99 my $requested_time = $factinfo{'requested_time'};
100 if ($requested_time) {
101 if (time() - $requested_time > 60*60*24*7) {
102 $string .= " at \037". scalar(localtime $requested_time). "\037";
104 $string .= ", ".&Time2String(time() - $requested_time)." ago";
108 push(@array,$string);
112 if ($factinfo{'locked_by'}) {
113 $factinfo{'locked_by'} =~ /\!/;
114 $string = "it has been locked by $`";
116 push(@array, $string);
119 # factoid was inserted not through the bot.
120 if (!scalar @array) {
121 &performReply("no extra info on \002$faqtoid\002");
125 &performStrictReply("$factinfo{'factoid_key'} -- ". join("; ", @array) .".");
132 if ($type =~ /^author$/i) {
133 my %hash = &dbGetCol("factoids", "factoid_key","created_by");
136 foreach (keys %hash) {
137 my $thisnuh = $hash{$_};
139 $thisnuh =~ /^(\S+)!\S+@\S+$/;
143 if (!scalar keys %author) {
144 return 'sorry, no factoids with created_by field.';
149 foreach (keys %author) {
150 $count{ $author{$_} }{$_} = 1;
156 foreach $count (sort { $b <=> $a } keys %count) {
157 my $author = join(", ", sort keys %{$count{$count}});
158 push(@list, "$count by $author");
161 my $prefix = "factoid statistics by author: ";
162 return &formListReply(0, $prefix, @list);
164 } elsif ($type =~ /^broken$/i) {
165 &status("factstats(broken): starting...");
166 my $start_time = &gettimeofday();
167 my %data = &dbGetCol("factoids", "factoid_key","factoid_value");
170 my $delta_time = &gettimeofday() - $start_time;
171 &status(sprintf("factstats(broken): %.02f sec to retreive all factoids.", $delta_time)) if ($delta_time > 0);
172 $start_time = &gettimeofday();
174 # parse the factoids.
175 foreach (keys %data) {
176 if (&validFactoid($_, $data{$_}) == 0) {
177 s/([\,\;]+)/\037$1\037/g; # highlight chars.
178 push(@list, $_); # push it.
182 $delta_time = &gettimeofday() - $start_time;
183 &status(sprintf("factstats(broken): %.02f sec to complete.", $delta_time)) if ($delta_time > 0);
185 # bail out on no results.
186 if (scalar @list == 0) {
187 return 'no broken factoids... wooohoo.';
191 my $prefix = "broken factoid ";
192 return &formListReply(1, $prefix, @list);
194 } elsif ($type =~ /^deadredir$/i) {
195 my @list = &searchTable("factoids", "factoid_key",
196 "factoid_value", "^<REPLY> see ");
202 my $val = &getFactInfo($factoid, "factoid_value");
203 if ($val =~ /^<REPLY> see( also)? (.*?)\.?$/i) {
205 my $redir = &getFactInfo($redirf, "factoid_value");
206 next if (defined $redir);
207 next if (length $val > 50);
209 $redir{$redirf}{$factoid} = 1;
214 foreach $f (keys %redir) {
215 my @sublist = keys %{$redir{$f}};
217 s/([\,\;]+)/\037$1\037/g;
220 push(@newlist, join(', ', @sublist)." => $f");
224 my $prefix = "Loose link (dead) redirections in factoids ";
225 return &formListReply(1, $prefix, @newlist);
227 } elsif ($type =~ /^dup(licate|e)$/i) {
228 my $start_time = &gettimeofday();
229 &status("factstats(dupe): starting...");
230 my %hash = &dbGetCol("factoids", "factoid_key", "factoid_value", 1);
235 foreach $v (keys %hash) {
236 my $count = scalar(keys %{$hash{$v}});
237 next if ($count == 1);
240 foreach (keys %{$hash{$v}}) {
241 if ($v =~ /^<REPLY> see /i) {
246 s/([\,\;]+)/\037$1\037/g;
248 &WARN("dupe: _ = NULL. should never happen!.");
254 next unless (scalar @sublist);
256 push(@list, join(", ", @sublist));
259 &status("factstats(dupe): (good) dupe refs: $refs.");
260 my $delta_time = &gettimeofday() - $start_time;
261 &status(sprintf("factstats(dupe): %.02f sec to complete", $delta_time)) if ($delta_time > 0);
263 # bail out on no results.
264 if (scalar @list == 0) {
265 return "no duplicate factoids... woohoo.";
269 my $prefix = "dupe factoid ";
270 return &formListReply(1, $prefix, @list);
272 } elsif ($type =~ /^(2|too)short$/i) {
273 # Custom select statement.
274 my $query = "SELECT factoid_key,factoid_value FROM factoids WHERE length(factoid_value) <= 40";
275 my $sth = $dbh->prepare($query);
276 &ERROR("factstats(lame): => '$query'.") unless $sth->execute;
279 while (my @row = $sth->fetchrow_array) {
280 my($key,$val) = ($row[0], $row[1]);
282 $match++ if ($val =~ /\s{3,}/);
283 next unless ($match);
285 $key =~ s/\,/\037\,\037/g;
291 my $prefix = "Lame factoids ";
292 return &formListReply(1, $prefix, @list);
294 } elsif ($type =~ /^listfix$/i) {
295 # Custom select statement.
296 my $query = "SELECT factoid_key,factoid_value FROM factoids";
297 my $sth = $dbh->prepare($query);
298 &ERROR("factstats(listfix): => '$query'.") unless $sth->execute;
301 while (my @row = $sth->fetchrow_array) {
302 my($key,$val) = ($row[0], $row[1]);
304 $match++ if ($val =~ /\S+,? or \S+,? or \S+,? or \S+,?/);
305 next unless ($match);
307 $key =~ s/\,/\037\,\037/g;
309 $val =~ s/,? or /, /g;
310 &DEBUG("fixed: => $val.");
311 &setFactInfo($key,"factoid_value", $val);
316 my $prefix = "Inefficient lists fixed ";
317 return &formListReply(1, $prefix, @list);
319 } elsif ($type =~ /^locked$/i) {
320 my %hash = &dbGetCol("factoids", "factoid_key","locked_by");
321 my @list = keys %hash;
324 s/([\,\;]+)/\037$1\037/g;
327 my $prefix = "factoid statistics on $type ";
328 return &formListReply(0, $prefix, @list);
330 } elsif ($type =~ /^new$/i) {
331 my %hash = &dbGetCol("factoids", "factoid_key","created_time");
334 foreach (keys %hash) {
335 my $created_time = $hash{$_};
336 my $delta_time = time() - $created_time;
337 next if ($delta_time >= 60*60*24);
339 $age{$delta_time}{$_} = 1;
342 if (scalar keys %age == 0) {
343 return "sorry, no new factoids.";
347 foreach (sort {$a <=> $b} keys %age) {
348 push(@list, join(",", keys %{$age{$_}}));
351 my $prefix = "new factoids in the last 24hours ";
352 return &formListReply(0, $prefix, @list);
354 } elsif ($type =~ /^part(ial)?dupe$/i) {
355 ### requires "custom" select statement... oh well...
356 my $start_time = &gettimeofday();
358 # form length|key and key=length hash list.
359 &status("factstats(partdupe): forming length hash list.");
360 my $query = "SELECT factoid_key,factoid_value,length(factoid_value) AS length FROM factoids WHERE length(factoid_value) >= 192 ORDER BY length";
361 my $sth = $dbh->prepare($query);
362 &ERROR("factstats(partdupe): => '$query'.") unless $sth->execute;
366 while (my @row = $sth->fetchrow_array) {
367 $length{$row[2]}{$row[0]} = 1; # length(value)|key.
368 $key{$row[0]} = $row[1]; # key=value.
372 &status("factstats(partdupe): total keys => '". scalar(@key) ."'.");
373 &status("factstats(partdupe): now deciphering data gathered");
375 my @length = sort { $a <=> $b } keys %length;
378 foreach $key (@key) {
379 shift @length if (length $key{$key} == $length[0]);
381 my $val = quotemeta $key{$key};
384 foreach $length (@length) {
385 foreach (keys %{$length{$length}}) {
386 if ($key{$_} =~ /^$val/i) {
387 s/([\,\;]+)/\037$1\037/g;
388 s/( and|and )/\037$1\037/g;
389 push(@sublist,$key." and ".$_);
393 push(@list, join(" ,",@sublist)) if (scalar @sublist);
396 my $delta_time = sprintf("%.02fs", &gettimeofday() - $start_time);
397 &status("factstats(partdupe): $delta_time sec to complete.") if ($delta_time > 0);
399 # bail out on no results.
400 if (scalar @list == 0) {
401 return "no initial partial duplicate factoids... woohoo.";
405 my $prefix = "initial partial dupe factoid ";
406 return &formListReply(1, $prefix, @list);
408 } elsif ($type =~ /^profanity$/i) {
409 my %data = &dbGetCol("factoids", "factoid_key","factoid_value");
412 foreach (keys %data) {
413 push(@list, $_) if (&hasProfanity($_." ".$data{$_}));
417 my $prefix = "Profanity in factoids ";
418 return &formListReply(1, $prefix, @list);
420 } elsif ($type =~ /^redir(ection)?$/i) {
421 my @list = &searchTable("factoids", "factoid_key",
422 "factoid_value", "^<REPLY> see ");
428 my $val = &getFactInfo($factoid, "factoid_value");
429 if ($val =~ /^<REPLY> see( also)? (.*?)\.?$/i) {
431 my $redirval = &getFactInfo($redir, "factoid_value");
432 if (defined $redirval) {
433 $redir{$redir}{$factoid} = 1;
435 &WARN("factstats(redir): '$factoid' has loose link => '$redir'.");
441 foreach $f (keys %redir) {
442 my @sublist = keys %{$redir{$f}};
444 s/([\,\;]+)/\037$1\037/g;
447 push(@newlist, "$f => ". join(', ', @sublist));
451 my $prefix = "Redirections in factoids ";
452 return &formListReply(1, $prefix, @newlist);
454 } elsif ($type =~ /^request(ed)?$/i) {
455 my %hash = &dbGetCol("factoids", "factoid_key", "requested_count",1);
457 if (!scalar keys %hash) {
458 return 'sorry, no factoids have been questioned.';
463 foreach $count (sort {$b <=> $a} keys %hash) {
464 my @faqtoids = sort keys %{$hash{$count}};
467 s/([\,\;]+)/\037$1\037/g;
470 push(@list, "$count - ". join(", ", @faqtoids));
473 my $prefix = "factoid statistics on $type ";
474 return &formListReply(0, $prefix, @list);
476 } elsif ($type =~ /^requesters?$/i) {
477 my %hash = &dbGetCol("factoids", "factoid_key","requested_by");
480 foreach (keys %hash) {
481 my $thisnuh = $hash{$_};
483 $thisnuh =~ /^(\S+)!\S+@\S+$/;
487 if (!scalar keys %requester) {
488 return 'sorry, no factoids with requested_by field.';
493 foreach (keys %requester) {
494 $count{$requester{$_}}{$_} = 1;
500 foreach $count (sort { $b <=> $a } keys %count) {
501 my $requester = join(", ", sort keys %{$count{$count}});
502 push(@list, "$count by $requester");
505 my $prefix = "rank of top factoid requesters: ";
506 return &formListReply(0, $prefix, @list);
508 } elsif ($type =~ /^seefix$/i) {
509 my @list = &searchTable("factoids", "factoid_key",
510 "factoid_value", "^see ");
518 my $val = &getFactInfo($factoid, "factoid_value");
519 if ($val =~ /^see( also)? (.*?)\.?$/i) {
521 my $redir = &getFactInfo($redirf, "factoid_value");
523 if ($redirf =~ /^\Q$factoid\W$/i) {
524 &delFactoid($factoid);
528 if (defined $redir) { # good.
529 &setFactInfo($factoid,"factoid_value","<REPLY> see $redir");
532 push(@newlist, $redirf);
538 &msg($who, "Fixed $fixed factoids.");
539 &msg($who, "Self looped factoids removed: ".
540 sort(keys %loop) ) if (scalar keys %loop);
542 my $prefix = "Loose link (dead) redirections in factoids ";
543 return &formListReply(1, $prefix, @newlist);
545 } elsif ($type =~ /^(2|too)long$/i) {
549 $query = "SELECT factoid_key FROM factoids WHERE length(factoid_key) >= $param{'maxKeySize'}";
550 my $sth = $dbh->prepare($query);
552 while (my @row = $sth->fetchrow_array) {
557 my $query = "SELECT factoid_key,factoid_value FROM factoids WHERE length(factoid_value) >= $param{'maxDataSize'}";
558 $sth = $dbh->prepare($query);
560 while (my @row = $sth->fetchrow_array) {
561 push(@list,sprintf("\002%s\002 - %s", length($row[1]), $row[0]));
564 if (scalar @list == 0) {
565 return "good. no factoids exceed length.";
569 my $prefix = "factoid key||value exceeding length ";
570 return &formListReply(1, $prefix, @list);
572 } elsif ($type =~ /^unrequest(ed)?$/i) {
573 my @list = &dbRawReturn("SELECT factoid_key FROM factoids WHERE requested_count IS NULL");
576 s/([\,\;]+)/\037$1\037/g;
579 my $prefix = "Unrequested factoids ";
580 return &formListReply(0, $prefix, @list);
583 return "error: invalid type => '$type'.";
588 my @list = &searchTable("factoids","factoid_key", "created_by", "^$query!");
590 my $prefix = "factoid author list by '$query' ";
591 &performStrictReply( &formListReply(1, $prefix, @list) );