X-Git-Url: https://git.octo.it/?a=blobdiff_plain;ds=sidebyside;f=lib%2FOnis%2FPlugins%2FCore.pm;h=627fc5eb88cd6633372120b91387137d124375c9;hb=b7beb463373e3dbf04f28564014c5bf967c589e8;hp=f3ace5abe3efd2d6287412378acef0b006c25401;hpb=2dfc95bddb9ea3e5c85503b1e6f529c4945cfe30;p=onis.git diff --git a/lib/Onis/Plugins/Core.pm b/lib/Onis/Plugins/Core.pm index f3ace5a..627fc5e 100644 --- a/lib/Onis/Plugins/Core.pm +++ b/lib/Onis/Plugins/Core.pm @@ -17,14 +17,14 @@ 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; - -@Onis::Plugins::Core::EXPORT_OK = (qw(get_core_nick_counters get_sorted_nicklist)); +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', @@ -46,52 +46,78 @@ 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 $SortedNicklist = []; -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 $NicksInMainTable = {}; + +our @HorizontalImages = qw#dark-theme/h-red.png dark-theme/h-blue.png dark-theme/h-yellow.png dark-theme/h-green.png#; our $QuoteCacheSize = 10; our $QuoteMin = 30; our $QuoteMax = 80; -our $WORD_LENGTH = 5; -our $SORT_BY = 'LINES'; -our $DISPLAY_LINES = 'BOTH'; -our $DISPLAY_WORDS = 'NONE'; -our $DISPLAY_CHARS = 'NONE'; -our $DISPLAY_TIMES = 0; -our $DISPLAY_IMAGES = 0; -our $DEFAULT_IMAGE = ''; -our $BAR_HEIGHT = 130; -our $BAR_WIDTH = 100; +our $SortBy = 'LINES'; +our $DisplayLines = 'BOTH'; +our $DisplayWords = 'NONE'; +our $DisplayChars = 'NONE'; +our $DisplayTimes = 0; +our $DisplayImages = 0; +our $DefaultImage = ''; 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); } -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'); @@ -99,7 +125,7 @@ if (get_config ('display_lines')) if (($tmp eq 'NONE') or ($tmp eq 'BAR') or ($tmp eq 'NUMBER') or ($tmp eq 'BOTH')) { - $DISPLAY_LINES = $tmp; + $DisplayLines = $tmp; } else { @@ -108,6 +134,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'); @@ -115,7 +148,7 @@ if (get_config ('display_words')) if (($tmp eq 'NONE') or ($tmp eq 'BAR') or ($tmp eq 'NUMBER') or ($tmp eq 'BOTH')) { - $DISPLAY_WORDS = $tmp; + $DisplayWords = $tmp; } else { @@ -124,6 +157,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'); @@ -131,7 +171,7 @@ if (get_config ('display_chars')) if (($tmp eq 'NONE') or ($tmp eq 'BAR') or ($tmp eq 'NUMBER') or ($tmp eq 'BOTH')) { - $DISPLAY_CHARS = $tmp; + $DisplayChars = $tmp; } else { @@ -140,17 +180,25 @@ 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'); if ($tmp =~ m/true|on|yes/i) { - $DISPLAY_TIMES = 1; + $DisplayTimes = 1; } elsif ($tmp =~ m/false|off|no/i) { - $DISPLAY_TIMES = 0; + $DisplayTimes = 0; } else { @@ -158,17 +206,24 @@ 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'); if ($tmp =~ m/true|on|yes/i) { - $DISPLAY_IMAGES = 1; + $DisplayImages = 1; } elsif ($tmp =~ m/false|off|no/i) { - $DISPLAY_IMAGES = 0; + $DisplayImages = 0; } else { @@ -176,10 +231,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'); + $DefaultImage = 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'); @@ -187,7 +259,7 @@ if (get_config ('sort_by')) if (($tmp eq 'LINES') or ($tmp eq 'WORDS') or ($tmp eq 'CHARS')) { - $SORT_BY = $tmp; + $SortBy = $tmp; } else { @@ -196,6 +268,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'); @@ -213,27 +293,32 @@ if (get_config ('horizontal_images')) next; } - $H_IMAGES[$i] = $tmp[$i]; + $HorizontalImages[$i] = $tmp[$i]; } } -if (get_config ('bar_height')) -{ - my $tmp = get_config ('bar_height'); - $tmp =~ s/\D//g; - $BAR_HEIGHT = $tmp if ($tmp >= 10); -} -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'); @@ -300,21 +385,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); } @@ -347,7 +427,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); @@ -359,30 +439,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})) { - my @new = sort (sub { $b->[0] <=> $a->[0] }, @{$QuoteCache->{$nick}}, @{$QuoteData->{$main}}); - splice (@new, $QuoteCacheSize) if (scalar (@new) > $QuoteCacheSize); - $QuoteData->{$main} = \@new; + next; + } + elsif (scalar (@{$QuoteData->{$main}}) < $QuoteCacheSize) + { + 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]; + } } } } @@ -398,7 +510,6 @@ 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 $factor = 0; # used to find a bar's height 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); @@ -434,18 +545,16 @@ sub activetimes $max = 1; } - $factor = (($BAR_HEIGHT - 1) / $max); - 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 (my $i = 0; $i <= 3; $i++) { - for (my $j = 0; $j <= 5; $j++) + for (my $j = 0; $j < 6; $j++) { my $hour = (($i * 6) + $j); if (!defined ($data[$hour])) @@ -453,19 +562,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"; } @@ -486,7 +598,7 @@ sub ranking my $fh = get_filehandle () or die; - my $sort_field = lc ($SORT_BY); + my $sort_field = lc ($SortBy); my $trans; @@ -499,19 +611,19 @@ sub ranking ($tmp) = sort { $NickData->{$b}{'chars_total'} <=> $NickData->{$a}{'chars_total'} } (@nicks); $max_chars = $NickData->{$tmp}{'chars_total'} || 0; - + $trans = translate ('Most active nicks'); print $fh "

$trans

\n"; - if ($SORT_BY eq 'LINES') + if ($SortBy eq 'LINES') { $trans = translate ('Nicks sorted by numbers of lines written'); } - elsif ($SORT_BY eq 'WORDS') + elsif ($SortBy eq 'WORDS') { $trans = translate ('Nicks sorted by numbers of words written'); } - else # ($SORT_BY eq 'CHARS') + else # ($SortBy eq 'CHARS') { $trans = translate ('Nicks sorted by numbers of characters written'); } @@ -523,7 +635,7 @@ sub ranking   EOF - if ($DISPLAY_IMAGES) + if ($DisplayImages) { $trans = translate ('Image'); print $fh " $trans\n"; @@ -533,22 +645,25 @@ EOF $trans = translate ('Nick'); print $fh " $trans\n"; } - if ($DISPLAY_LINES ne 'NONE') + if ($DisplayLines ne 'NONE') { + my $span = $DisplayLines eq 'BOTH' ? ' colspan="2"' : ''; $trans = translate ('Number of Lines'); - print $fh " $trans\n"; + print $fh " $trans\n"; } - if ($DISPLAY_WORDS ne 'NONE') + if ($DisplayWords ne 'NONE') { + my $span = $DisplayWords eq 'BOTH' ? ' colspan="2"' : ''; $trans = translate ('Number of Words'); - print $fh " $trans\n"; + print $fh " $trans\n"; } - if ($DISPLAY_CHARS ne 'NONE') + if ($DisplayChars ne 'NONE') { + my $span = $DisplayChars eq 'BOTH' ? ' colspan="2"' : ''; $trans = translate ('Number of Characters'); - print $fh " $trans\n"; + print $fh " $trans\n"; } - if ($DISPLAY_TIMES) + if ($DisplayTimes) { $trans = translate ('When?'); print $fh " $trans\n"; @@ -569,7 +684,8 @@ EOF { 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++; @@ -578,13 +694,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 = ''; @@ -600,11 +719,11 @@ EOF print $fh " \n", qq# # . $linescount . "\n"; - if ($DISPLAY_IMAGES) + if ($DisplayImages) { - if ($DEFAULT_IMAGE and !$image) + if ($DefaultImage and !$image) { - $image = $DEFAULT_IMAGE; + $image = $DefaultImage; } print $fh qq# #; @@ -637,65 +756,59 @@ 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') + if ($DisplayLines ne 'NONE') { - print $fh qq# #; - if (($DISPLAY_LINES eq 'BOTH') or ($DISPLAY_LINES eq 'BAR')) + if (($DisplayLines eq 'BOTH') or ($DisplayLines eq 'NUMBER')) { - my $code = bar ($max_lines, $NickData->{$nick}{'lines'}); - print $fh $code; + my $num = $NickData->{$nick}{'lines_total'}; + print $fh qq( $num\n); } - print $fh ' ' if ($DISPLAY_LINES eq 'BOTH'); - if (($DISPLAY_LINES eq 'BOTH') or ($DISPLAY_LINES eq 'NUMBER')) + if (($DisplayLines eq 'BOTH') or ($DisplayLines eq 'BAR')) { - print $fh $NickData->{$nick}{'lines_total'}; + my $code = bar ($max_lines, $NickData->{$nick}{'lines'}); + print $fh qq( $code\n); } - print $fh "\n"; } - if ($DISPLAY_WORDS ne 'NONE') + if ($DisplayWords ne 'NONE') { - print $fh qq# #; - if (($DISPLAY_WORDS eq 'BOTH') or ($DISPLAY_WORDS eq 'BAR')) + if (($DisplayWords eq 'BOTH') or ($DisplayWords eq 'NUMBER')) { - my $code = bar ($max_words, $NickData->{$nick}{'words'}); - print $fh $code; + my $num = $NickData->{$nick}{'words_total'}; + print $fh qq( $num\n); } - print $fh ' ' if ($DISPLAY_WORDS eq 'BOTH'); - if (($DISPLAY_WORDS eq 'BOTH') or ($DISPLAY_WORDS eq 'NUMBER')) + if (($DisplayWords eq 'BOTH') or ($DisplayWords eq 'BAR')) { - print $fh $NickData->{$nick}{'words_total'}; + my $code = bar ($max_words, $NickData->{$nick}{'words'}); + print $fh qq( $code\n); } - print $fh "\n"; } - if ($DISPLAY_CHARS ne 'NONE') + if ($DisplayChars ne 'NONE') { - print $fh qq# #; - if (($DISPLAY_CHARS eq 'BOTH') or ($DISPLAY_CHARS eq 'BAR')) + if (($DisplayChars eq 'BOTH') or ($DisplayChars eq 'NUMBER')) { - my $code = bar ($max_chars, $NickData->{$nick}{'chars'}); - print $fh $code; + my $num = $NickData->{$nick}{'chars_total'}; + print $fh qq( $num\n); } - print $fh ' ' if ($DISPLAY_CHARS eq 'BOTH'); - if (($DISPLAY_CHARS eq 'BOTH') or ($DISPLAY_CHARS eq 'NUMBER')) + if (($DisplayChars eq 'BOTH') or ($DisplayChars eq 'BAR')) { - print $fh $NickData->{$nick}{'chars_total'}; + my $code = bar ($max_chars, $NickData->{$nick}{'chars'}); + print $fh qq( $code\n); } - print $fh "\n"; } - if ($DISPLAY_TIMES) + if ($DisplayTimes) { my $code = bar ($NickData->{$nick}{'chars_total'}, $NickData->{$nick}{'chars'}); - print $fh qq# $code\n#; + print $fh qq# $code\n#; } print $fh qq# $quote\n#, @@ -717,15 +830,15 @@ EOF my $col_in_this_table = ($linescount - $LongLines - 1) % 6; my $total = 0; - if ($SORT_BY eq 'LINES') + if ($SortBy eq 'LINES') { $total = $NickData->{$nick}{'lines_total'}; } - elsif ($SORT_BY eq 'WORDS') + elsif ($SortBy eq 'WORDS') { $total = $NickData->{$nick}{'words_total'}; } - else # ($SORT_BY eq 'CHARS') + else # ($SortBy eq 'CHARS') { $total = $NickData->{$nick}{'chars_total'}; } @@ -751,7 +864,7 @@ EOF qq# \n#; } - print $fh qq# $name ($total)\n#; + print $fh qq# $print ($total)\n#; if ($row_in_this_table == $ShortLines and $col_in_this_table == 5) { @@ -795,22 +908,18 @@ sub bar confess () unless (ref ($source) eq 'ARRAY'); - # BAR_WIDTH is a least 10 - my $max_width = $BAR_WIDTH - 4; - my $factor = 1; my $retval = ''; my $i; my $j; - if (!$max_num) { return ($retval); } - $factor = $max_width / $max_num; + $max_num ||= 1; for ($i = 0; $i < 4; $i++) { my $sum = 0; - my $width = 1; - my $img = $H_IMAGES[$i]; + my $img = $HorizontalImages[$i]; + my $width; for ($j = 0; $j < 6; $j++) { @@ -818,12 +927,12 @@ sub bar $sum += $source->[$hour]; } - $width += int (0.5 + ($sum * $factor)); + $width = sprintf ("%.2f", 95 * $sum / $max_num); - $retval .= qq#$sum); } return ($retval); @@ -873,10 +982,24 @@ 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