2 # Factoids.pl: Helpers for generating factoids statistics.
4 # Version: v0.1 (20000514)
5 # Splitted: SQLExtras.pl
10 use vars qw($dbh $who);
14 # Usage: &CmdFactInfo($faqtoid, $query);
16 my ( $faqtoid, $query ) = ( lc $_[0], $_[1] );
20 if ( $faqtoid eq '' ) {
26 &sqlSelectRowHash( 'factoids', '*', { factoid_key => $faqtoid } );
28 # factoid does not exist.
29 if ( scalar( keys %factinfo ) <= 1 ) {
30 &performReply("there's no such factoid as \002$faqtoid\002");
34 # fix for problem observed by asuffield.
35 # why did it happen though?
36 if ( !$factinfo{'factoid_value'} ) {
38 "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 );
59 . scalar( gmtime $time ) . "\037"
63 $string .= ' ' . &Time2String( time() - $time ) . ' ago';
67 push( @array, $string );
70 # modified: (TimRiker asks: why do you keep turning this off?)
71 if ( $factinfo{'modified_by'} ) {
72 $string = 'last modified';
74 my $time = $factinfo{'modified_time'};
76 if ( time() - $time > 60 * 60 * 24 * 7 ) {
77 $string .= " at \037" . scalar( gmtime $time ) . "\037";
80 $string .= ' ' . &Time2String( time() - $time ) . ' ago ';
84 $string .= ' by ' . ( split ',', $factinfo{'modified_by'} )[0];
86 push( @array, $string );
90 if ( $factinfo{'requested_by'} ) {
91 my $requested_count = $factinfo{'requested_count'};
93 if ($requested_count) {
94 $string = 'it has been requested ';
95 if ( $requested_count == 1 ) {
96 $string .= "\002once\002";
100 . $requested_count . "\002 "
101 . &fixPlural( 'time', $requested_count );
104 my $requested_by = $factinfo{'requested_by'};
105 $requested_by =~ /\!/;
106 $string .= ", last by $`";
108 my $requested_time = $factinfo{'requested_time'};
109 if ($requested_time) {
110 if ( time() - $requested_time > 60 * 60 * 24 * 7 ) {
112 " at \037" . scalar( localtime $requested_time ) . "\037";
116 ', ' . &Time2String( time() - $requested_time ) . ' ago';
121 $string = 'has not been requested yet';
124 push( @array, $string );
128 if ( $factinfo{'locked_by'} ) {
129 $factinfo{'locked_by'} =~ /\!/;
130 $string = "it has been locked by $`";
132 push( @array, $string );
135 # factoid was inserted not through the bot.
136 if ( !scalar @array ) {
137 &performReply("no extra info on \002$faqtoid\002");
142 "$factinfo{'factoid_key'} -- " . join( '; ', @array ) . '.' );
149 if ( $type =~ /^author$/i ) {
150 my %hash = &sqlSelectColHash(
151 'factoids', 'factoid_key,created_by',
152 undef, 'WHERE created_by IS NOT NULL'
156 foreach my $factoid ( keys %hash ) {
157 my $thisnuh = $hash{$factoid};
159 $thisnuh =~ /^(\S+)!\S+@\S+$/;
163 if ( !scalar keys %author ) {
164 return 'sorry, no factoids with created_by field.';
169 foreach ( keys %author ) {
170 $count{ $author{$_} }{$_} = 1;
176 foreach $count ( sort { $b <=> $a } keys %count ) {
177 my $author = join( ', ', sort keys %{ $count{$count} } );
178 push( @list, "$count by $author" );
181 my $prefix = 'factoid statistics by author: ';
182 return &formListReply( 0, $prefix, @list );
185 elsif ( $type =~ /^vandalism$/i ) {
186 &status('factstats(vandalism): starting...');
187 my $start_time = &timeget();
188 my %data = &sqlSelectColHash(
189 'factoids', 'factoid_key,factoid_value',
190 undef, 'WHERE factoid_value IS NOT NULL'
194 my $delta_time = &timedelta($start_time);
197 'factstats(vandalism): %.02f sec to retreive all factoids.',
199 ) if ( $delta_time > 0 );
200 $start_time = &timeget();
202 # parse the factoids.
203 foreach ( keys %data ) {
204 if ( &validFactoid( $_, $data{$_} ) == 0 ) {
205 s/([\,\;]+)/\037$1\037/g; # highlight chars.
206 push( @list, $_ ); # push it.
210 $delta_time = &timedelta($start_time);
212 sprintf( 'factstats(vandalism): %.02f sec to complete.',
214 ) if ( $delta_time > 0 );
216 # bail out on no results.
217 if ( scalar @list == 0 ) {
218 return 'no vandalised factoids... wooohoo.';
222 my $prefix = 'Vandalised factoid ';
223 return &formListReply( 1, $prefix, @list );
226 elsif ( $type =~ /^total$/i ) {
227 &status('factstats(total): starting...');
228 my $start_time = &timeget();
235 # total factoids requests.
236 $i = &sumKey( 'factoids', 'requested_count' );
237 push( @list, "total requests - $i" );
239 # total factoids modified.
240 $str = &countKeys( 'factoids', 'modified_by' );
241 push( @list, "total modified - $str" );
243 # total factoids modified.
244 $j = &countKeys( 'factoids', 'requested_count' );
245 $str = &countKeys( 'factoids', 'factoid_key' );
246 push( @list, 'total non-requested - ' . ( $str - $i ) );
248 # average request/factoid.
249 # i/j == total(requested_count)/count(requested_count)
250 $str = sprintf( '%.01f', $i / $j );
251 push( @list, "average requested per factoid - $str" );
253 # total prepared for deletion.
256 &searchTable( 'factoids', 'factoid_key', 'factoid_value', ' #DEL' )
258 push( @list, "total prepared for deletion - $str" );
260 # total unique authors.
261 # TODO: convert to sqlSelectColHash ? (or ColArray?)
264 'SELECT created_by FROM factoids WHERE created_by IS NOT NULL')
271 push( @list, 'total unique authors - ' . ( scalar keys %hash ) );
274 # total unique requesters.
277 'SELECT requested_by FROM factoids WHERE requested_by IS NOT NULL'
285 push( @list, 'total unique requesters - ' . ( scalar keys %hash ) );
290 my $delta_time = &timedelta($start_time);
292 sprintf( 'factstats(broken): %.02f sec to retreive all factoids.',
294 ) if ( $delta_time > 0 );
295 $start_time = &timeget();
297 # bail out on no results.
298 if ( scalar @list == 0 ) {
299 return 'no broken factoids... wooohoo.';
303 my $prefix = 'General factoid statistics ';
304 return &formListReply( 1, $prefix, @list );
307 elsif ( $type =~ /^deadredir$/i ) {
309 &searchTable( 'factoids', 'factoid_key', 'factoid_value',
316 my $val = &getFactInfo( $factoid, 'factoid_value' );
317 if ( $val =~ /^<REPLY> ?see( also)? (.*?)\.?$/i ) {
319 my $redir = &getFactInfo( $redirf, 'factoid_value' );
320 next if ( defined $redir );
321 next if ( length $val > 50 );
323 $redir{$redirf}{$factoid} = 1;
328 foreach $f ( keys %redir ) {
329 my @sublist = keys %{ $redir{$f} };
331 s/([\,\;]+)/\037$1\037/g;
334 push( @newlist, join( ', ', @sublist ) . " => $f" );
338 my $prefix = 'Loose link (dead) redirections in factoids ';
339 return &formListReply( 1, $prefix, @newlist );
342 elsif ( $type =~ /^dup(licate|e)$/i ) {
343 &status('factstats(dupe): starting...');
344 my $start_time = &timeget();
346 &sqlSelectColHash( 'factoids', 'factoid_key,factoid_value', undef,
347 'WHERE factoid_value IS NOT NULL', 1 );
352 foreach $v ( keys %hash ) {
353 my $count = scalar( keys %{ $hash{$v} } );
354 next if ( $count == 1 );
357 foreach ( keys %{ $hash{$v} } ) {
358 if ( $v =~ /^<REPLY> see /i ) {
363 s/([\,\;]+)/\037$1\037/g;
365 &WARN('dupe: _ = NULL. should never happen!.');
368 push( @sublist, $_ );
371 next unless ( scalar @sublist );
373 push( @list, join( ', ', @sublist ) );
376 &status("factstats(dupe): (good) dupe refs: $refs.");
377 my $delta_time = &timedelta($start_time);
379 sprintf( 'factstats(dupe): %.02f sec to complete', $delta_time ) )
380 if ( $delta_time > 0 );
382 # bail out on no results.
383 if ( scalar @list == 0 ) {
384 return 'no duplicate factoids... woohoo.';
388 my $prefix = 'dupe factoid ';
389 return &formListReply( 1, $prefix, @list );
392 elsif ( $type =~ /^nullfactoids$/i ) {
394 "SELECT factoid_key,factoid_value FROM factoids WHERE factoid_value=''";
395 my $sth = $dbh->prepare($query);
396 &ERROR("factstats(null): => '$query'.") unless $sth->execute;
399 while ( my @row = $sth->fetchrow_array ) {
400 if ( $row[1] ne '' ) {
401 &DEBUG("row[1] != NULL for $row[0].");
405 &DEBUG("row[0] => '$row[0]'.");
406 push( @list, $row[0] );
411 my $prefix = 'NULL factoids (not deleted yet) ';
412 return &formListReply( 1, $prefix, @list );
415 elsif ( $type =~ /^(2|too)short$/i ) {
417 # Custom select statement.
419 'SELECT factoid_key,factoid_value FROM factoids WHERE length(factoid_value) <= 40';
420 my $sth = $dbh->prepare($query);
421 &ERROR("factstats(lame): => '$query'.") unless $sth->execute;
424 while ( my @row = $sth->fetchrow_array ) {
425 my ( $key, $val ) = ( $row[0], $row[1] );
427 $match++ if ( $val =~ /\s{3,}/ );
428 next unless ($match);
430 my $v = &getFactoid($val);
432 &DEBUG("key $key => $val => $v");
435 $key =~ s/\,/\037\,\037/g;
441 my $prefix = 'Lame factoids ';
442 return &formListReply( 1, $prefix, @list );
445 elsif ( $type =~ /^listfix$/i ) {
447 # Custom select statement.
448 my $query = 'SELECT factoid_key,factoid_value FROM factoids';
449 my $sth = $dbh->prepare($query);
450 &ERROR("factstats(listfix): => '$query'.") unless $sth->execute;
453 while ( my @row = $sth->fetchrow_array ) {
454 my ( $key, $val ) = ( $row[0], $row[1] );
456 $match++ if ( $val =~ /\S+,? or \S+,? or \S+,? or \S+,?/ );
457 next unless ($match);
459 $key =~ s/\,/\037\,\037/g;
461 $val =~ s/,? or /, /g;
462 &DEBUG("fixed: => $val.");
463 &setFactInfo( $key, 'factoid_value', $val );
468 my $prefix = 'Inefficient lists fixed ';
469 return &formListReply( 1, $prefix, @list );
472 elsif ( $type =~ /^locked$/i ) {
473 my %hash = &sqlSelectColHash(
474 'factoids', 'factoid_key,locked_by',
475 undef, 'WHERE locked_by IS NOT NULL'
477 my @list = keys %hash;
480 s/([\,\;]+)/\037$1\037/g;
483 my $prefix = "factoid statistics on $type ";
484 return &formListReply( 0, $prefix, @list );
487 elsif ( $type =~ /^new$/i ) {
488 my %hash = &sqlSelectColHash(
489 'factoids', 'factoid_key,created_time',
490 undef, 'WHERE created_time IS NOT NULL'
494 foreach ( keys %hash ) {
495 my $created_time = $hash{$_};
496 my $delta_time = time() - $created_time;
497 next if ( $delta_time >= 60 * 60 * 24 );
499 $age{$delta_time}{$_} = 1;
502 if ( scalar keys %age == 0 ) {
503 return 'sorry, no new factoids.';
507 foreach ( sort { $a <=> $b } keys %age ) {
508 push( @list, join( ',', keys %{ $age{$_} } ) );
511 my $prefix = 'new factoids in the last 24hours ';
512 return &formListReply( 0, $prefix, @list );
515 elsif ( $type =~ /^part(ial)?dupe$/i ) {
516 ### requires 'custom' select statement... oh well...
517 my $start_time = &timeget();
519 # form length|key and key=length hash list.
520 &status('factstats(partdupe): forming length hash list.');
522 'SELECT factoid_key,factoid_value,length(factoid_value) AS length FROM factoids WHERE length(factoid_value) >= 192 ORDER BY length';
523 my $sth = $dbh->prepare($query);
524 &ERROR("factstats(partdupe): => '$query'.") unless $sth->execute;
527 my ( %key, %length );
528 while ( my @row = $sth->fetchrow_array ) {
529 $length{ $row[2] }{ $row[0] } = 1; # length(value)|key.
530 $key{ $row[0] } = $row[1]; # key=value.
531 push( @key, $row[0] );
534 &status( "factstats(partdupe): total keys => '" . scalar(@key) . "'." );
535 &status('factstats(partdupe): now deciphering data gathered');
537 my @length = sort { $a <=> $b } keys %length;
540 foreach $key (@key) {
541 shift @length if ( length $key{$key} == $length[0] );
543 my $val = quotemeta $key{$key};
546 foreach $length (@length) {
547 foreach ( keys %{ $length{$length} } ) {
548 if ( $key{$_} =~ /^$val/i ) {
549 s/([\,\;]+)/\037$1\037/g;
550 s/( and|and )/\037$1\037/g;
551 push( @sublist, $key . ' and ' . $_ );
555 push( @list, join( ' ,', @sublist ) ) if ( scalar @sublist );
558 my $delta_time = sprintf( '%.02fs', &timedelta($start_time) );
559 &status("factstats(partdupe): $delta_time sec to complete.")
560 if ( $delta_time > 0 );
562 # bail out on no results.
563 if ( scalar @list == 0 ) {
564 return 'no initial partial duplicate factoids... woohoo.';
568 my $prefix = 'initial partial dupe factoid ';
569 return &formListReply( 1, $prefix, @list );
572 elsif ( $type =~ /^profanity$/i ) {
573 my %data = &sqlSelectColHash(
574 'factoids', 'factoid_key,factoid_value',
575 undef, 'WHERE factoid_value IS NOT NULL'
579 foreach ( keys %data ) {
580 push( @list, $_ ) if ( &hasProfanity( $_ . ' ' . $data{$_} ) );
584 my $prefix = 'Profanity in factoids ';
585 return &formListReply( 1, $prefix, @list );
588 elsif ( $type =~ /^redir(ection)?$/i ) {
590 &searchTable( 'factoids', 'factoid_key', 'factoid_value',
598 my $val = &getFactInfo( $factoid, 'factoid_value' );
599 if ( $val =~ /^<REPLY> see( also)? (.*?)\.?$/i ) {
601 my $redirval = &getFactInfo( $redir, 'factoid_value' );
602 if ( defined $redirval ) {
603 $redir{$redir}{$factoid} = 1;
607 "factstats(redir): '$factoid' has loose link => '$redir'."
615 foreach $f ( keys %redir ) {
616 my @sublist = keys %{ $redir{$f} };
618 s/([\,\;]+)/\037$1\037/g;
621 push( @newlist, "$f => " . join( ', ', @sublist ) );
625 my $prefix = "Redirections in factoids, $dangling dangling ";
626 return &formListReply( 1, $prefix, @newlist );
629 elsif ( $type =~ /^request(ed)?$/i ) {
631 &sqlSelectColHash( 'factoids', 'factoid_key,requested_count', undef,
632 'WHERE requested_count IS NOT NULL', 1 );
634 if ( !scalar keys %hash ) {
635 return 'sorry, no factoids have been questioned.';
641 foreach $count ( sort { $b <=> $a } keys %hash ) {
642 my @faqtoids = sort keys %{ $hash{$count} };
645 s/([\,\;]+)/\037$1\037/g;
647 $total += $count * scalar(@faqtoids);
649 push( @list, "$count - " . join( ', ', @faqtoids ) );
651 unshift( @list, "\037$total - TOTAL\037" );
653 my $prefix = "factoid statistics on $type ";
654 return &formListReply( 0, $prefix, @list );
657 elsif ( $type =~ /^reqrate$/i ) {
658 my %hash = &sqlSelectColHash(
660 "factoid_key,(unix_timestamp() - created_time)/requested_count as rate",
662 'WHERE requested_by IS NOT NULL and created_time IS NOT NULL ORDER BY rate LIMIT 15',
670 foreach $rate ( sort { $b <=> $a } keys %hash ) {
671 my $f = join( ', ', sort keys %{ $hash{$rate} } );
672 my $str = "$f - " . &Time2String($rate);
677 my $prefix = "Rank of top factoid rate (time/req): ";
678 return &formListReply( 0, $prefix, @list );
681 elsif ( $type =~ /^requesters?$/i ) {
682 my %hash = &sqlSelectColHash(
683 'factoids', 'factoid_key,requested_by',
684 undef, 'WHERE requested_by IS NOT NULL'
688 foreach ( keys %hash ) {
689 my $thisnuh = $hash{$_};
691 $thisnuh =~ /^(\S+)!\S+@\S+$/;
692 $requester{ lc $1 }++;
695 if ( !scalar keys %requester ) {
696 return 'sorry, no factoids with requested_by field.';
701 foreach ( keys %requester ) {
702 $count{ $requester{$_} }{$_} = 1;
710 foreach $count ( sort { $b <=> $a } keys %count ) {
711 my $requester = join( ', ', sort keys %{ $count{$count} } );
712 $total += $count * scalar( keys %{ $count{$count} } );
713 $users += scalar( keys %{ $count{$count} } );
714 push( @list, "$count by $requester" );
717 "\037$total TOTAL REQUESTS; $users UNIQUE REQUESTERS\037" );
719 # should not the above value be the same as collected by
720 # 'requested'? soemthing weird is going on!
722 my $prefix = 'rank of top factoid requesters: ';
723 return &formListReply( 0, $prefix, @list );
726 elsif ( $type =~ /^seefix$/i ) {
728 &searchTable( 'factoids', 'factoid_key', 'factoid_value', '^see ' );
736 my $val = &getFactInfo( $factoid, 'factoid_value' );
738 next unless ( $val =~ /^see( also)? (.*?)\.?$/i );
741 my $redir = &getFactInfo( $redirf, 'factoid_value' );
743 if ( $redirf =~ /^\Q$factoid\W$/i ) {
744 &delFactoid($factoid);
748 if ( defined $redir ) { # good.
749 &setFactInfo( $factoid, 'factoid_value', "<REPLY> see $redir" );
753 push( @newlist, $redirf );
758 &msg( $who, "Fixed $fixed factoids." );
759 &msg( $who, 'Self looped factoids removed: ' . keys %loop )
760 if ( scalar keys %loop );
762 my $prefix = "Loose link (dead) redirections in factoids ";
763 return &formListReply( 1, $prefix, @newlist );
766 elsif ( $type =~ /^(2|too)long$/i ) {
772 "SELECT factoid_key FROM factoids WHERE length(factoid_key) >= $param{'maxKeySize'}";
773 my $sth = $dbh->prepare($query);
775 while ( my @row = $sth->fetchrow_array ) {
776 push( @list, $row[0] );
782 "SELECT factoid_key,factoid_value FROM factoids WHERE length(factoid_value) >= $param{'maxDataSize'}";
783 $sth = $dbh->prepare($query);
785 while ( my @row = $sth->fetchrow_array ) {
787 sprintf( "\002%s\002 - %s", length( $row[1] ), $row[0] ) );
791 if ( scalar @list == 0 ) {
792 return 'good. no factoids exceed length.';
796 my $prefix = 'factoid key||value exceeding length ';
797 return &formListReply( 1, $prefix, @list );
800 elsif ( $type =~ /^unrequest(ed)?$/i ) {
802 # TODO: use sqlSelect()
805 "SELECT COUNT(*) FROM factoids WHERE requested_count = '0'");
807 return "Unrequested factoids: $count";
810 return "error: invalid type => '$type'.";
815 my $maxshow = &::getChanConfDefault( 'maxListReplyCount', 15, $chan );
817 &searchTable( 'factoids', 'factoid_key', 'created_by', "^$query!" );
818 @list = grep( !/\#DEL\#$/, @list ) if ( scalar(@list) > $maxshow );
820 my $prefix = "factoid author list by '$query' ";
821 &performStrictReply( &formListReply( 1, $prefix, @list ) );
826 # vim:ts=4:sw=4:expandtab:tw=80