--- trunk/bin/irc-logger.pl 2008/04/01 19:04:32 132 +++ trunk/bin/irc-logger.pl 2008/04/05 21:08:27 133 @@ -7,7 +7,6 @@ use DBI; use Regexp::Common qw /URI/; use CGI::Simple; -use HTML::TagCloud; use POSIX qw/strftime/; use HTML::CalendarMonthSimple; use Getopt::Long; @@ -128,7 +127,7 @@ my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); my $escape_re = join '|' => keys %escape; -my $tag_regex = '\b([\w-_]+)//'; +my $tag_regex = '\b([\w\-_]+)//'; my %nick_enumerator; my $max_color = 0; @@ -526,62 +525,8 @@ # tags support -my $cloud = HTML::TagCloud->new; - -=head2 add_tag - - add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] ); - -=cut - -my @last_tags; - -sub add_tag { - my $arg = {@_}; - - return unless ($arg->{id} && $arg->{message}); - - my $m = $arg->{message}; - - my @tags; - - while ($m =~ s#$tag_regex##s) { - my $tag = $1; - next if (! $tag || $tag =~ m/https?:/i); - push @{ $tags->{$tag} }, $arg->{id}; - #warn "+tag $tag: $arg->{id}\n"; - $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1); - push @tags, $tag; - - } - - if ( @tags ) { - pop @last_tags if $#last_tags == $last_x_tags; - unshift @last_tags, { tags => [ @tags ], %$arg }; - } - -} - -=head2 seed_tags - -Read all tags from database and create in-memory cache for tags - -=cut - -sub seed_tags { - my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc }); - $sth->execute; - while (my $row = $sth->fetchrow_hashref) { - add_tag( %$row ); - } - - foreach my $tag (keys %$tags) { - $cloud->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}} + 1); - } -} - -seed_tags; - +my $cloud = TagCloud->new; +$cloud->seed_tags; =head2 save_message @@ -612,7 +557,7 @@ eval { $sth_insert_log->execute($a->{channel}, $a->{me}, $a->{nick}, $a->{message}, $a->{time}); }; _log "ERROR: can't archive ", $a->{message} if $@; - add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); + $cloud->add_tag( id => $dbh->last_insert_id(undef,undef,"log",undef), %$a ); } @@ -1241,7 +1186,7 @@ $response->content_type( 'application/' . lc($type) . '+xml' ); my $html = ''; - #warn "create $type feed from ",dump( @last_tags ); + #warn "create $type feed from ",dump( $cloud->last_tags ); my $feed = XML::Feed->new( $type ); $feed->link( $url ); @@ -1275,7 +1220,7 @@ $feed->title( "last $nr tagged messages from $CHANNEL" ); $feed->description( "collects messages which have tags// in them" ); - foreach my $m ( @last_tags ) { + foreach my $m ( $cloud->last_tags ) { # warn dump( $m ); #my $tags = join(' ', @{$m->{tags}} ); my $feed_entry = XML::Feed::Entry->new($type); @@ -1436,3 +1381,110 @@ } POE::Kernel->run; + +=head1 TagCloud + +Extended L + +=cut + +package TagCloud; +use warnings; +use strict; +use HTML::TagCloud; +use base 'HTML::TagCloud'; +use Data::Dump qw/dump/; + +=head2 html + +Generate html with number of tags in title of link + +=cut + +sub html { + my($self, $limit) = @_; + my @tags=$self->tags($limit); + + my $ntags = scalar(@tags); + if ($ntags == 0) { + return ""; +# } elsif ($ntags == 1) { +# my $tag = $tags[0]; +# return qq{
}.$tag->{name}.qq{
\n}; + } + + my $html = qq{
}; + foreach my $tag (@tags) { + $html .= sprintf(qq{%s\n}, + $tag->{level}, $tag->{url}, $tag->{count}, $tag->{name} + ); + } + $html .= qq{
}; + return $html; +} + +=head2 last_tags + + my @tags = $cloud->last_tags; + +=cut + +my @last_tags; +sub last_tags { + return @last_tags; +} + +=head2 add_tag + + $cloud->add_tag( id => 42, message => 'irc message', nick => 'foobar' [, me => 1 ] ); + +=cut + + +sub add_tag { + my $self = shift; + my $arg = {@_}; + + return unless ($arg->{id} && $arg->{message}); + + my $m = $arg->{message}; + + my @tags; + + while ($m =~ s#$tag_regex##s) { + my $tag = $1; + next if (! $tag || $tag =~ m/https?:/i); + push @{ $tags->{$tag} }, $arg->{id}; + #warn "+tag $tag: $arg->{id}\n"; + $self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}}); + push @tags, $tag; + + } + + if ( @tags ) { + pop @last_tags if $#last_tags == $last_x_tags; + unshift @last_tags, { tags => [ @tags ], %$arg }; + } + +} + +=head2 seed_tags + +Read all tags from database and create in-memory cache for tags + +=cut + +sub seed_tags { + my $self = shift; + my $sth = $dbh->prepare(qq{ select id,message,nick,me,time from log where message like '%//%' order by time asc }); + $sth->execute; + while (my $row = $sth->fetchrow_hashref) { + $self->add_tag( %$row ); + } + + foreach my $tag (keys %$tags) { + $self->add($tag, "$url?tag=$tag", scalar @{$tags->{$tag}}); + } +} +