X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=lib%2FOnis%2FPlugins%2FCore.pm;h=fb689732a3b123a8af8a82e5d9d6a3d63bf94be5;hb=7e3c4b5eacb8bcbfff65fdd4d5def64e079991b7;hp=e24a8a6c5d594b192c468c44205a41a5358f5a1f;hpb=9cb74064dd4d0e6cb03c8021dbf744000f83f851;p=onis.git diff --git a/lib/Onis/Plugins/Core.pm b/lib/Onis/Plugins/Core.pm index e24a8a6..fb68973 100644 --- a/lib/Onis/Plugins/Core.pm +++ b/lib/Onis/Plugins/Core.pm @@ -4,6 +4,7 @@ use strict; use warnings; use Carp (qw(confess)); +use Exporter; =head1 NAME @@ -16,12 +17,15 @@ complicated plugin so far. =cut -use Onis::Config qw/get_config/; -use Onis::Html qw/html_escape get_filehandle/; -use Onis::Language qw/translate/; -use Onis::Users (qw(get_realname get_link get_image ident_to_name)); -use Onis::Data::Core qw#get_all_nicks nick_to_ident ident_to_nick get_main_nick register_plugin#; -use Onis::Data::Persistent; +use Onis::Config (qw(get_config)); +use Onis::Html (qw(html_escape get_filehandle)); +use Onis::Language (qw(translate)); +use Onis::Users (qw(get_realname get_link get_image chatter_to_name)); +use Onis::Data::Core (qw(get_all_nicks nick_to_ident ident_to_nick get_main_nick register_plugin)); +use Onis::Data::Persistent (); + +@Onis::Plugins::Core::EXPORT_OK = (qw(get_core_nick_counters get_sorted_nicklist nick_is_in_main_table)); +@Onis::Plugins::Core::ISA = ('Exporter'); our $NickLinesCounter = Onis::Data::Persistent->new ('NickLinesCounter', 'nick', qw( @@ -42,9 +46,14 @@ our $NickCharsCounter = Onis::Data::Persistent->new ('NickCharsCounter', 'nick', ) ); -our $QuoteCache = {}; # Saves per-nick information without any modification +our $QuoteCache = Onis::Data::Persistent->new ('QuoteCache', 'key', qw(epoch text)); +our $QuotePtr = Onis::Data::Persistent->new ('QuotePtr', 'nick', qw(pointer)); + our $QuoteData = {}; # Is generated before output. Nicks are merged according to Data::Core. -our $NickData = {}: # Same as above, but for nicks rather than quotes. +our $NickData = {}; # Same as above, but for nicks rather than quotes. +our $SortedNicklist = []; + +our $NicksInMainTable = {}; our @H_IMAGES = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#; our $QuoteCacheSize = 10; @@ -63,30 +72,69 @@ our $BAR_WIDTH = 100; our $LongLines = 50; our $ShortLines = 10; +=head1 CONFIGURATION OPTIONS + +=over 4 + +=item B: I<10> + +Sets how many quotes are cached and, at the end, one is chosen at random. + +=cut + if (get_config ('quote_cache_size')) { my $tmp = get_config ('quote_cache_size'); $tmp =~ s/\D//g; $QuoteCacheSize = $tmp if ($tmp); } + +=item B: I<30> + +Minimum number of characters in a line to be included in the quote-cache. + +=cut + if (get_config ('quote_min')) { my $tmp = get_config ('quote_min'); $tmp =~ s/\D//g; $QuoteMin = $tmp if ($tmp); } +=item B: I<80> + +Maximum number of characters in a line to be included in the quote-cache. + +=cut + if (get_config ('quote_max')) { my $tmp = get_config ('quote_max'); $tmp =~ s/\D//g; $QuoteMax = $tmp if ($tmp); } + +=item B: I<5> + +Sets how many word-characters in a row are considered to be a word. Or, in more +normal terms: Sets the minimum length for words.. + +=cut + if (get_config ('min_word_length')) { my $tmp = get_config ('min_word_length'); $tmp =~ s/\D//g; $WORD_LENGTH = $tmp if ($tmp); } + +=item B: I + +Choses wether to display B as I, I, I or not at all +(I). + +=cut + if (get_config ('display_lines')) { my $tmp = get_config ('display_lines'); @@ -103,6 +151,13 @@ if (get_config ('display_lines')) $/, __FILE__, ": Valid values are ``none'', ``bar'', ``number'' and ``both''. Using default value ``both''."; } } + +=item B: I + +See L + +=cut + if (get_config ('display_words')) { my $tmp = get_config ('display_words'); @@ -119,6 +174,13 @@ if (get_config ('display_words')) $/, __FILE__, ": Valid values are ``none'', ``bar'', ``number'' and ``both''. Using default value ``none''."; } } + +=item B: I + +See L + +=cut + if (get_config ('display_chars')) { my $tmp = get_config ('display_chars'); @@ -135,6 +197,14 @@ if (get_config ('display_chars')) $/, __FILE__, ": Valid values are ``none'', ``bar'', ``number'' and ``both''. Using default value ``none''."; } } + +=item B: I + +Wether or not to display a fixed width bar that shows when a user is most +active. + +=cut + if (get_config ('display_times')) { my $tmp = get_config ('display_times'); @@ -153,6 +223,13 @@ if (get_config ('display_times')) $/, __FILE__, ": Valid values are ``true'' and ``false''. Using default value ``false''."; } } + +=item B: I + +Wether or not to display images in the main ranking. + +=cut + if (get_config ('display_images')) { my $tmp = get_config ('display_images'); @@ -171,10 +248,27 @@ if (get_config ('display_images')) $/, __FILE__, ": Valid values are ``true'' and ``false''. Using default value ``false''."; } } + +=item B: I + +Sets the URL to the default image. This is included as-is in the HTML. You have +to take care of (absolute) paths yourself. + +=cut + if (get_config ('default_image')) { $DEFAULT_IMAGE = get_config ('default_image'); } + +=item B: I + +Sets by which field the output has to be sorted. This is completely independent +from B, B and B. Valid options are +I, I and I. + +=cut + if (get_config ('sort_by')) { my $tmp = get_config ('sort_by'); @@ -191,6 +285,14 @@ if (get_config ('sort_by')) $/, __FILE__, ": Valid values are ``lines'' and ``words''. Using default value ``lines''."; } } + +=item B: I, I, I, I + +Sets the B images used for horizontal bars/graphs. As above: You have to +take care of correctness of paths yourself. + +=cut + if (get_config ('horizontal_images')) { my @tmp = get_config ('horizontal_images'); @@ -211,24 +313,55 @@ if (get_config ('horizontal_images')) $H_IMAGES[$i] = $tmp[$i]; } } + +=item B: I<130> + +Sets the height (in pixels) of the highest vertical graph. + +=cut + if (get_config ('bar_height')) { my $tmp = get_config ('bar_height'); $tmp =~ s/\D//g; $BAR_HEIGHT = $tmp if ($tmp >= 10); } + +=item B: I<100> + +Sets the width (in pixels) of the widest horizontal graph. + +=cut + if (get_config ('bar_width')) { my $tmp = get_config ('bar_width'); $tmp =~ s/\D//g; $BAR_WIDTH = $tmp if ($tmp >= 10); } + +=item B: I<50> + +Sets the number of rows of the main ranking table. + +=cut + if (get_config ('longlines')) { my $tmp = get_config ('longlines'); $tmp =~ s/\D//g; $LongLines = $tmp if ($tmp); } + +=item B: I<10> + +Sets the number of rows of the "they didn't write so much" table. There are six +persons per line; you set the number of lines. + +=over + +=cut + if (get_config ('shortlines')) { my $tmp = get_config ('shortlines'); @@ -273,7 +406,7 @@ sub add { @counter = qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0); } - $counter[$hour]++ + $counter[$hour]++; $NickLinesCounter->put ($nick, @counter); @counter = $NickWordsCounter->get ($nick); @@ -295,21 +428,16 @@ sub add if ((length ($text) >= $QuoteMin) and (length ($text) <= $QuoteMax)) { - if (!defined ($QuoteCache->{$nick})) - { - $QuoteCache->{$nick} = []; - } - push (@{$QuoteCache->{$nick}}, [$time, $text]); - } + my ($pointer) = $QuotePtr->get ($nick); + $pointer ||= 0; - if (defined ($QuoteCache->{$nick})) - { - while (scalar (@{$QuoteCache->{$nick}}) > $QuoteCacheSize) - { - shift (@{$QuoteCache->{$nick}}); - } - } + my $key = sprintf ("%s:%02i", $nick, $pointer); + + $QuoteCache->put ($key, $time, $text); + $pointer = ($pointer + 1) % $QuoteCacheSize; + $QuotePtr->put ($nick, $pointer); + } return (1); } @@ -342,7 +470,7 @@ sub calculate $NickData->{$main}{'lines'}[$i] += $counter[$i]; $sum += $counter[$i]; } - $NickData->{$main}{'lines_total'} = $sum; + $NickData->{$main}{'lines_total'} += $sum; } @counter = $NickWordsCounter->get ($nick); @@ -354,30 +482,62 @@ sub calculate $NickData->{$main}{'words'}[$i] += $counter[$i]; $sum += $counter[$i]; } - $NickData->{$main}{'words_total'} = $sum; + $NickData->{$main}{'words_total'} += $sum; } - @counter = $NickWordsCounter->get ($nick); + @counter = $NickCharsCounter->get ($nick); if (@counter) { my $sum = 0; for (my $i = 0; $i < 24; $i++) { - $NickData->{$main}{'words'}[$i] += $counter[$i]; + $NickData->{$main}{'chars'}[$i] += $counter[$i]; $sum += $counter[$i]; } - $NickData->{$main}{'chars_total'} = $sum; + $NickData->{$main}{'chars_total'} += $sum; } if (!defined ($QuoteData->{$main})) { $QuoteData->{$main} = []; } - if (defined ($QuoteCache->{$nick})) + } + + for ($QuoteCache->keys ()) + { + my $key = $_; + my ($nick, $num) = split (m/:/, $key); + my $main = get_main_nick ($nick); + + my ($epoch, $text) = $QuoteCache->get ($key); + die unless (defined ($text)); + + if (!defined ($QuoteData->{$main})) + { + die; + } + elsif (scalar (@{$QuoteData->{$main}}) < $QuoteCacheSize) { - my @new = sort (sub { $b->[0] <=> $a->[0] }, @{$QuoteCache->{$nick}}, @{$QuoteData->{$main}}); - splice (@new, $QuoteCacheSize) if (scalar (@new) > $QuoteCacheSize); - $QuoteData->{$main} = \@new; + push (@{$QuoteData->{$main}}, [$epoch, $text]); + } + else + { + my $insert = -1; + my $min = $epoch; + + for (my $i = 0; $i < $QuoteCacheSize; $i++) + { + if ($QuoteData->{$main}[$i][0] < $min) + { + $insert = $i; + $min = $QuoteData->{$main}[$i][0]; + } + } + + if ($insert != -1) + { + $QuoteData->{$main}[$insert] = [$epoch, $text]; + } } } } @@ -393,11 +553,9 @@ sub activetimes { my $max = 0; # the most lines that were written in one hour.. my $total = 0; # the total amount of lines we wrote.. - my ($i, $j); # used in for-loops my $factor = 0; # used to find a bar's height - my $newline = ''; # buffer variable.. - my @data = @{$DATA->{'byhour'}}; + my @data = qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0); my @img_urls = get_config ('vertical_images'); if (!@img_urls) @@ -409,19 +567,20 @@ sub activetimes # this for loop looks for the most amount of lines in one hour and sets # $most_lines - for ($i = 0; $i < 24; $i++) + for (keys %$NickData) { - if (!defined ($data[$i])) + my $nick = $_; + + for (my $i = 0; $i < 24; $i++) { - next; + $data[$i] += $NickData->{$nick}{'chars'}[$i]; } + } + for (my $i = 0; $i < 24; $i++) + { + $max = $data[$i] if ($max < $data[$i]); $total += $data[$i]; - - if ($data[$i] > $max) - { - $max = $data[$i]; - } } if (!$total) @@ -434,14 +593,14 @@ sub activetimes my $header = translate ('When do we actually talk here?'); print $fh "

$header

\n", - qq#\n#, - qq# \n#; + qq#
\n#, + qq# \n#; # this for circles through the four colors. Each color represents six hours. # (4 * 6 hours = 24 hours) - for ($i = 0; $i <= 3; $i++) + for (my $i = 0; $i <= 3; $i++) { - for ($j = 0; $j <= 5; $j++) + for (my $j = 0; $j < 6; $j++) { my $hour = (($i * 6) + $j); if (!defined ($data[$hour])) @@ -449,19 +608,22 @@ sub activetimes $data[$hour] = 0; } - my $percent = 100 * ($data[$hour] / $total); - my $height = int ($data[$hour] * $factor) + 1; - my $img_url = $img_urls[$i]; + my $height = sprintf ("%.2f", 95 * $data[$hour] / $max); + my $img = $img_urls[$i]; - print $fh ' \n#; + print $fh qq# \n#; } } + print $fh qq# \n \n#; + for (my $i = 0; $i < 24; $i++) + { + my $percent = sprintf ("%.1f", 100 * $data[$i] / $total); + print $fh qq# \n#; + } print $fh " \n", - qq# \n#; - print $fh map { " \n" } (0 .. 23); + qq# \n#; + print $fh map { qq# \n# } (0 .. 23); print $fh " \n", "
', sprintf ("%2.1f", $percent), - qq#%
$percent\%
$_
$_
\n\n"; } @@ -554,14 +716,19 @@ EOF print $fh " $trans\n", " \n"; - for (sort + @$SortedNicklist = sort { $NickData->{$b}{"${sort_field}_total"} <=> $NickData->{$a}{"${sort_field}_total"} - } (@nicks)) + } (@nicks); + + @nicks = (); + + for (@$SortedNicklist) { my $nick = $_; my $ident = nick_to_ident ($nick); - my $name = ident_to_name ($ident); + my $name = chatter_to_name ("$nick!$ident"); + my $print = $name || $nick; $linescount++; @@ -570,13 +737,16 @@ EOF # our table.. if ($linescount <= $LongLines) { + $NicksInMainTable->{$nick} = $linescount; + my $quote = translate ('-- no quote available --'); - if (defined ($QuoteData->{$nick})) + if (@{$QuoteData->{$nick}}) { my $num = scalar (@{$QuoteData->{$nick}}); my $rand = int (rand ($num)); - $quote = html_escape ($QuoteData->{$nick}[$rand]); + + $quote = html_escape ($QuoteData->{$nick}[$rand][1]); } my $link = ''; @@ -629,11 +799,11 @@ EOF if ($link) { - print $fh qq#$name\n# + print $fh qq#$print\n# } else { - print $fh qq#$name\n#; + print $fh qq#$print\n#; } if ($DISPLAY_LINES ne 'NONE') @@ -711,15 +881,22 @@ EOF my $total = 0; if ($SORT_BY eq 'LINES') { - $total = $DATA->{'byname'}{$name}{'lines'}; + $total = $NickData->{$nick}{'lines_total'}; } elsif ($SORT_BY eq 'WORDS') { - $total = $DATA->{'byname'}{$name}{'words'}; + $total = $NickData->{$nick}{'words_total'}; } else # ($SORT_BY eq 'CHARS') { - $total = $DATA->{'byname'}{$name}{'chars'}; + $total = $NickData->{$nick}{'chars_total'}; + } + + my $title = $name ? get_realname ($name) : ''; + if (!$title) + { + $title = "User: $name; " if ($name); + $title .= "Ident: $ident"; } if ($row_in_this_table == 0 and $col_in_this_table == 0) @@ -736,7 +913,7 @@ EOF qq# \n#; } - print $fh " $name ($total)\n"; + print $fh qq# $print ($total)\n#; if ($row_in_this_table == $ShortLines and $col_in_this_table == 5) { @@ -776,10 +953,9 @@ EOF sub bar { my $max_num = shift; - my $source = shift; - confess unless (ref ($source eq 'ARRAY')); + confess () unless (ref ($source) eq 'ARRAY'); # BAR_WIDTH is a least 10 my $max_width = $BAR_WIDTH - 4; @@ -809,14 +985,74 @@ sub bar $retval .= qq#$sum); } return ($retval); } +=head1 EXPORTED FUNCTIONS + +=over 4 + +=item B (I<$nick>) + +Returns a hash-ref that containes all the nick-counters available. It looks +like this: + + { + lines => [qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)], + words => [qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)], + chars => [qw(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)], + lines_total => 0, + words_total => 0, + chars_total => 0 + } + +=cut + +sub get_core_nick_counters +{ + my $nick = shift; + + if (!defined ($NickData->{$nick})) + { + return ({}); + } + + return ($NickData->{$nick}); +} + +=item B () + +Returns an array-ref that containes all nicks, sorted by the field given in the +config-file. + +=cut + +sub get_sorted_nicklist +{ + return ($SortedNicklist); +} + +=item B (I<$nick>) + +Returns the position of the nick in the main table or zero if it is not in the +main table. + +=cut + +sub nick_is_in_main_table +{ + my $nick = shift; + + return (defined ($NicksInMainTable->{$nick}) ? $NicksInMainTable->{$nick} : 0); +} + +=back + =head1 AUTHOR -Florian octo Forster, Eocto at verplant.orgE +Florian octo Forster Eocto at verplant.orgE =cut