From 86a26a896db925d5d3ee8e0d6ed226a565214912 Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 20 Oct 2005 11:27:13 +0000 Subject: =?UTF-8?q?comincia=20a=20far=20finta=20di=20andare.=20le=20icone?= =?UTF-8?q?=20non=20vengono=20caricate,=20non=20so=20perch=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Bookmarks.pm | 2 +- lib/Bookmarks/C/Main.pm | 36 +++++++++++++++++++++++++++++++----- lib/Bookmarks/M/DB/Links.pm | 8 +++++--- lib/Bookmarks/M/DB/Tags.pm | 17 +++++++++++++---- lib/Bookmarks/Utils.pm | 45 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 lib/Bookmarks/Utils.pm (limited to 'lib') diff --git a/lib/Bookmarks.pm b/lib/Bookmarks.pm index fb270bd..56e098c 100644 --- a/lib/Bookmarks.pm +++ b/lib/Bookmarks.pm @@ -31,7 +31,7 @@ Catalyst based application. sub default : Private { my ( $self, $c ) = @_; - $c->res->output('Congratulations, Bookmarks is on Catalyst!'); + $c->res->redirect($c->req->base.'tags'); } =back diff --git a/lib/Bookmarks/C/Main.pm b/lib/Bookmarks/C/Main.pm index 7182a9e..89c985d 100644 --- a/lib/Bookmarks/C/Main.pm +++ b/lib/Bookmarks/C/Main.pm @@ -23,17 +23,43 @@ Catalyst component. =cut -sub default : Private { - my ( $self, $c ) = @_; - $c->res->redirect($c->req->base,'/tags'); -} - sub tags : Global { my ( $self, $c ) = @_; $c->stash->{template}='tags'; $c->stash->{tags}=[Bookmarks::M::DB::Tags->get_all_tags_by_popularity()]; } +sub tag : Regex('^tag/(.*)$') { + my ( $self, $c ) = @_; + + my $tagname=$c->req->snippets->[0]; + my ($tag)=Bookmarks::M::DB::Tags->search({name=>$tagname}); + if (!defined $tag) { + $c->stash->{tagname}=$tagname; + $c->stash->{template}='notag'; + } + else { + my @links=$tag->ordered_links(); + $c->stash->{tag}=$tag; + $c->stash->{links}=[@links]; + $c->stash->{template}='links'; + } +} + +sub icon : Global { + my ( $self, $c ) = @_; + + my $link=Bookmarks::M::DB::Links->retrieve($c->req->param('link')); + if ($link->icon()) { + $c->res->content_type('image/x-icon'); + $c->res->body($link->icon()); + } + else { + $c->res->status(404); + $c->res->body('nonce'); + } +} + =back diff --git a/lib/Bookmarks/M/DB/Links.pm b/lib/Bookmarks/M/DB/Links.pm index 851d9a3..3ae761d 100644 --- a/lib/Bookmarks/M/DB/Links.pm +++ b/lib/Bookmarks/M/DB/Links.pm @@ -1,15 +1,17 @@ package Bookmarks::M::DB::Links; - use strict; +use DBI; for my $col_name (qw(add_date last_access_date)) { __PACKAGE__->has_a( $col_name => 'DateTime', - inflate => sub { DateTime->from_epoch( epoch => $_ ) }, + inflate => sub { DateTime->from_epoch( epoch => $_[0] ) }, deflate => 'epoch', ); } -__PACKAGE__->has_many( tags => [Bookmarks::M::DB::LinkTags => 'tag'] ); +__PACKAGE__->has_many( tags => ['Bookmarks::M::DB::LinksTags' => 'tag'] ); + +__PACKAGE__->data_type( icon => DBI::SQL_BINARY ); =head1 NAME diff --git a/lib/Bookmarks/M/DB/Tags.pm b/lib/Bookmarks/M/DB/Tags.pm index e9057a8..c1e1f0b 100644 --- a/lib/Bookmarks/M/DB/Tags.pm +++ b/lib/Bookmarks/M/DB/Tags.pm @@ -2,9 +2,9 @@ package Bookmarks::M::DB::Tags; use strict; -__PACKAGE__->has_many( links => [Bookmarks::M::DB::LinkTags => 'link'] ); +__PACKAGE__->has_many( links => ['Bookmarks::M::DB::LinksTags' => 'link'] ); -__PACKAGE__->set_sql('related_tags' <<'END_SQL'); +__PACKAGE__->set_sql('related_tags', <<'END_SQL'); SELECT tags.pk FROM tags, links_tags lt1, links_tags lt2 WHERE tags.pk = lt1.tag @@ -12,13 +12,16 @@ SELECT tags.pk AND lt2.tag = ? END_SQL -__PACKAGE__->set_sql('popularity' <<'END_SQL'); +__PACKAGE__->set_sql('popularity', <<'END_SQL'); SELECT tags.pk, COUNT(links_tags.link) AS how_many FROM tags, links_tags WHERE tags.pk = links_tags.tag -ORDER BY how_many +GROUP BY links_tags.tag +ORDER BY how_many DESC END_SQL +__PACKAGE__->columns(TEMP=> 'how_wany'); + sub count_links { my ($self)=@_; return scalar $self->links(); @@ -34,6 +37,12 @@ sub get_all_tags_by_popularity { return $_[0]->search_popularity(); } +sub ordered_links { + my ($self)=@_; + my @links=$self->links(); + return sort {$a->access_count <=> $b->access_count} @links; +} + =head1 NAME Bookmarks::M::DB::Tags - CDBI Model Component Table Class diff --git a/lib/Bookmarks/Utils.pm b/lib/Bookmarks/Utils.pm new file mode 100644 index 0000000..fd0b68d --- /dev/null +++ b/lib/Bookmarks/Utils.pm @@ -0,0 +1,45 @@ +package Bookmarks::Utils; +use strict; +use warnings; +use LWP::Simple; +use URI::URL; + +sub check_link { + my ($url)=@_; + return 1 if $url!~/^http:/; + #my ($type,$length,$update,$expires,$server)=head($url); + #return defined $type; + return scalar head($url); +} + +sub get_site_icon { + my ($url)=@_; + + return if $url!~/^http:/; + + print "Provo nella pagina\n"; + my $page=get($url); + my ($favicon)= grep {defined $_ and $_ ne ''} + ($page =~ + m{new($favicon,$url)->abs->canonical->as_string; + print ", ovvero '$favicon'\n"; + my $icon=get($favicon); + return $icon; +} + +1; -- cgit v1.2.3