Converted Onis::Plugins::Urls.
[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));
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                 $WordCache->put ($word, $counter, $time, $nick);
53         }
54 }
55
56 sub calculate
57 {
58         my $max = 10;
59         my @data = ();
60         if (get_config ('plugin_max'))
61         {
62                 my $tmp = get_config ('plugin_max');
63                 $tmp =~ s/\D//g;
64
65                 $max = $tmp if ($tmp);
66         }
67
68         for ($WordCache->keys ())
69         {
70                 my $word = $_;
71                 my $ident = nick_to_ident ($word);
72
73                 if ($ident)
74                 {
75                         $WordCache->del ($word);
76                         next;
77                 }
78                 
79                 my ($counter, $lastusedtime, $lastusedby) = $WordCache->get ($word);
80                 die unless (defined ($lastusedby));
81
82                 my $nick = get_main_nick ($lastusedby);
83                 push (@data, [$word, $counter, $nick, $lastusedtime]);
84         }
85
86         @$WordData = sort { $b->[1] <=> $a->[1] } (@data);
87         splice (@$WordData, $max);
88 }
89
90 sub output
91 {
92         calculate ();
93         return (undef) unless (@$WordData);
94
95         my $fh = get_filehandle ();
96         
97         my $word = translate ('Word');
98         my $times = translate ('Times used');
99         my $last = translate ('Last used by');
100         
101         print $fh <<EOF;
102 <table class="plugin">
103   <tr>
104     <td class="invis">&nbsp;</td>
105     <th>$word</th>
106     <th>$times</th>
107     <th>$last</th>
108   </tr>
109 EOF
110
111         my $i = 0;
112         for (@$WordData)
113         {
114                 $i++;
115
116                 my ($word, $count, $nick) = @$_;
117                 my $name = nick_to_name ($nick) || $nick;
118                 
119                 print $fh "  <tr>\n",
120                 qq#    <td class="numeration">$i</td>\n#,
121                 qq#    <td>$word</td>\n#,
122                 qq#    <td>$count</td>\n#,
123                 qq#    <td class="nick">$name</td>\n#,
124                 qq#  </tr>\n#;
125         }
126         print $fh "</table>\n\n";
127
128         return (1);
129 }