From e13a37ca21a73794e4e451944db6f23c088da090 Mon Sep 17 00:00:00 2001 From: octo Date: Sun, 10 Apr 2005 16:06:00 +0000 Subject: [PATCH] Converted Onis::Plugins::Words --- lib/Onis/Plugins/Words.pm | 128 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 lib/Onis/Plugins/Words.pm diff --git a/lib/Onis/Plugins/Words.pm b/lib/Onis/Plugins/Words.pm new file mode 100644 index 0000000..7707848 --- /dev/null +++ b/lib/Onis/Plugins/Words.pm @@ -0,0 +1,128 @@ +package Onis::Plugins::Words; + +use strict; +use warnings; + +use Onis::Config (qw(get_config)); +use Onis::Html (qw(get_filehandle)); +use Onis::Language (qw(translate)); +use Onis::Data::Core (qw(register_plugin)); +use Onis::Data::Persistent (); + +register_plugin ('TEXT', \&add); +register_plugin ('ACTION', \&add); +register_plugin ('OUTPUT', \&output); + +our $WordCache = Onis::Data::Persistent->new ('WordCache', 'word', qw(counter lastusedtime lastusedby)); +our $WordData = []; + +our $MIN_LENGTH = 5; + +if (get_config ('ignore_words')) +{ + my $tmp = get_config ('ignore_words'); + $tmp =~ s/\D//g; + + $MIN_LENGTH = $tmp if ($tmp); +} + +my $VERSION = '$Id$'; +print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG); + +return (1); + +sub add +{ + my $data = shift; + my $text = $data->{'text'}; + my $nick = $data->{'nick'}; + my $words = $data->{'words'}; + my $time = $data->{'epoch'}; + + for (@$words) + { + my $word = lc ($_); + + next if (length ($word) < $MIN_LENGTH); + + my ($counter) = $WordCache->get ($word); + $counter ||= 0; + $counter++; + + $WordCache->put ($word, $counter, $time, $nick); + } +} + +sub calculate +{ + my $max = 10; + my @data = (); + if (get_config ('plugin_max')) + { + my $tmp = get_config ('plugin_max'); + $tmp =~ s/\D//g; + + $max = $tmp if ($tmp); + } + + for ($WordCache->keys ()) + { + my $word = $_; + my $ident = nick_to_ident ($word); + + if ($ident) + { + $WordCache->del ($word); + next; + } + + my ($counter, $lastusedtime, $lastusedby) = $WordCache->get ($word); + die unless (defined ($lastusedby)); + + my $nick = get_main_nick ($lastusedby); + push (@data, [$word, $counter, $nick, $lastusedtime]); + } + + @$WordData = sort { $b->[0] <=> $a->[0] } (@data); + splice (@$WordData, $max); +} + +sub output +{ + calculate (); + return (undef) unless (@$WordData); + + my $fh = get_filehandle (); + + my $word = translate ('Word'); + my $times = translate ('Times used'); + my $last = translate ('Last used by'); + + print $fh < + +   + $word + $times + $last + +EOF + + my $i = 0; + for (@$WordData) + { + $i++; + + my ($word, $count, $nick) = @$_; + + print $fh " \n", + qq# $i\n#, + qq# $word\n#, + qq# $count\n#, + qq# $nick\n#, + qq# \n#; + } + print $fh "\n\n"; + + return (1); +} -- 2.11.0