Fixes this bug:
[onis.git] / lib / Onis / Plugins / Words.pm
1 package Onis::Plugins::Words;
2
3 use strict;
4 use warnings;
5
6 use Onis::Config (qw(get_config));
7 use Onis::Html (qw(get_filehandle));
8 use Onis::Language (qw(translate));
9 use Onis::Data::Core (qw(register_plugin get_main_nick nick_to_ident nick_to_name));
10 use Onis::Data::Persistent ();
11
12 register_plugin ('TEXT', \&add);
13 register_plugin ('ACTION', \&add);
14 register_plugin ('OUTPUT', \&output);
15
16 our $WordCache = Onis::Data::Persistent->new ('WordCache', 'word', qw(counter lastusedtime lastusedby));
17 our $WordData = [];
18
19 our $MIN_LENGTH = 5;
20
21 if (get_config ('ignore_words'))
22 {
23         my $tmp = get_config ('ignore_words');
24         $tmp =~ s/\D//g;
25
26         $MIN_LENGTH = $tmp if ($tmp);
27 }
28
29 my $VERSION = '$Id$';
30 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
31
32 return (1);
33
34 sub add
35 {
36         my $data = shift;
37         my $text = $data->{'text'};
38         my $nick = $data->{'nick'};
39         my $words = $data->{'words'};
40         my $time = $data->{'epoch'};
41         
42         for (@$words)
43         {
44                 my $word = lc ($_);
45                 
46                 next if (length ($word) < $MIN_LENGTH);
47
48                 my ($counter) = $WordCache->get ($word);
49                 $counter ||= 0;
50                 $counter++;
51                 $WordCache->put ($word, $counter, $time, $nick);
52         }
53 }
54
55 sub calculate
56 {
57         my $max = 10;
58         my @data = ();
59         if (get_config ('plugin_max'))
60         {
61                 my $tmp = get_config ('plugin_max');
62                 $tmp =~ s/\D//g;
63
64                 $max = $tmp if ($tmp);
65         }
66
67         for ($WordCache->keys ())
68         {
69                 my $word = $_;
70                 my $ident = nick_to_ident ($word);
71
72                 if ($ident)
73                 {
74                         $WordCache->del ($word);
75                         next;
76                 }
77                 
78                 my ($counter, $lastusedtime, $lastusedby) = $WordCache->get ($word);
79                 die unless (defined ($lastusedby));
80
81                 my $nick = get_main_nick ($lastusedby);
82                 push (@data, [$word, $counter, $nick, $lastusedtime]);
83         }
84
85         @$WordData = sort { $b->[1] <=> $a->[1] } (@data);
86         splice (@$WordData, $max) if (scalar (@$WordData) > $max);
87 }
88
89 sub output
90 {
91         calculate ();
92         return (undef) unless (@$WordData);
93
94         my $fh = get_filehandle ();
95         
96         my $word = translate ('Word');
97         my $times = translate ('Times used');
98         my $last = translate ('Last used by');
99         
100         print $fh <<EOF;
101 <table class="plugin">
102   <tr>
103     <td class="invis">&nbsp;</td>
104     <th>$word</th>
105     <th>$times</th>
106     <th>$last</th>
107   </tr>
108 EOF
109
110         my $i = 0;
111         for (@$WordData)
112         {
113                 $i++;
114
115                 my ($word, $count, $nick) = @$_;
116                 my $name = nick_to_name ($nick) || $nick;
117                 
118                 print $fh "  <tr>\n",
119                 qq#    <td class="numeration">$i</td>\n#,
120                 qq#    <td>$word</td>\n#,
121                 qq#    <td>$count</td>\n#,
122                 qq#    <td class="nick">$name</td>\n#,
123                 qq#  </tr>\n#;
124         }
125         print $fh "</table>\n\n";
126
127         return (1);
128 }