--- trunk/irc-logger.pl 2006/05/22 16:19:00 27 +++ trunk/irc-logger.pl 2006/06/16 20:51:32 28 @@ -18,7 +18,7 @@ ## CONFIG -my $NICK = 'irc-logger'; +my $NICK = 'irc-logger-dev'; my $CONNECT = {Server => 'irc.freenode.net', Nick => $NICK, @@ -47,7 +47,7 @@ use Encode qw/from_to/; use Regexp::Common qw /URI/; use CGI::Simple; - +use HTML::TagCloud; my $dbh = DBI->connect($DSN,"","", { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; @@ -82,6 +82,8 @@ values (?,?,?,?) }); +my $tags; + =head2 get_from_log my @messages = get_from_log( @@ -146,6 +148,7 @@ my $sql = $context ? $sql_context : $sql_message; $sql .= " where message ilike ? or nick ilike ? " if ($args->{search}); + $sql .= " where id in (" . join(",", @{ $tags->{ $args->{tag} } }) . ") " if ($args->{tag} && $tags->{ $args->{tag} }); $sql .= " order by log.time desc"; $sql .= " limit " . $args->{limit}; @@ -155,6 +158,9 @@ $search =~ s/\s+$//; $sth->execute( ( '%' . $search . '%' ) x 2 ); warn "search for '$search' returned ", $sth->rows, " results ", $context || '', "\n"; + } elsif (my $tag = $args->{tag}) { + $sth->execute(); + warn "tag '$tag' returned ", $sth->rows, " results ", $context || '', "\n"; } else { $sth->execute(); } @@ -473,6 +479,48 @@ }, ); +# tags support + +my $cloud = HTML::TagCloud->new; + +=head2 add_tag + + add_tag( id => 42, message => 'irc message' ); + +=cut + +sub add_tag { + my $arg = {@_}; + + return unless ($arg->{id} && $arg->{message}); + + while ($arg->{message} =~ s#\b(\S+)//##s) { + my $tag = $1; + next if (! $tag || $tag =~ m/https?:/i); + push @{ $tags->{$tag} }, $arg->{id}; + } +} + +=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 from log where message like '%//%' }); + $sth->execute; + while (my $row = $sth->fetchrow_hashref) { + add_tag( %$row ); + } + + foreach my $tag (keys %$tags) { + $cloud->add($tag, "?tag=$tag", scalar @{$tags->{$tag}} + 1); + } +} + +seed_tags; + # http server my $httpd = POE::Component::Server::HTTP->new( @@ -487,7 +535,7 @@ my $style = <<'_END_OF_STYLE_'; p { margin: 0; padding: 0.1em; } .time, .channel { color: #808080; font-size: 60%; } -.date { float: right; clear: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; } +.date { float: right; background: #e0e0e0; color: #404040; font-size: 120%; padding: 0.25em; border: 1px dashed #808080; } .nick { color: #000000; font-size: 80%; padding: 2px; font-family: courier, courier new, monospace ; } .message { color: #000000; font-size: 100%; } .search { float: right; } @@ -520,17 +568,22 @@ my $search = $q->param('search') || $q->param('grep') || ''; $response->content( - qq{$NICK + qq{$NICK} . + qq{ -

} . + qq{

} . $cloud->html(500) . qq{
} . + qq{

} . join("

", get_from_log( limit => $q->param('last') || 100, - search => $q->param('search') || $q->param('grep') || undef, + search => $search || undef, + tag => $q->param('tag'), fmt => { date => '


%s
', time => '%s ',