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