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 --- Build.PL | 6 +++++- 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 +++++++++++++++++++++++++++++++++++++++++++ root/links | 13 +++++++++++++ root/notag | 8 ++++++++ root/tags | 12 ++++++++++++ script/bookmarks_load.pl | 47 +++++++++++++++++++++++++++++++++++++++++++++ script/bookmarks_makedb.pl | 1 + test.yaml | 28 +++++++++++++++++++++++++++ 12 files changed, 209 insertions(+), 14 deletions(-) create mode 100644 lib/Bookmarks/Utils.pm create mode 100644 root/links create mode 100644 root/notag create mode 100644 root/tags create mode 100755 script/bookmarks_load.pl create mode 100644 test.yaml diff --git a/Build.PL b/Build.PL index 20ab39b..eb3376a 100644 --- a/Build.PL +++ b/Build.PL @@ -5,7 +5,11 @@ my $build = Catalyst::Build->new( create_makefile_pl => 'passthrough', license => 'perl', module_name => 'Bookmarks', - requires => { Catalyst => '5.10' }, + requires => { + Catalyst => '5.10', + YAML => 0, + # altra roba + }, create_makefile_pl => 'passthrough', script_files => [ glob('script/*') ], test_files => [ glob('t/*.t'), glob('t/*/*.t') ] 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; diff --git a/root/links b/root/links new file mode 100644 index 0000000..57aed0a --- /dev/null +++ b/root/links @@ -0,0 +1,13 @@ + + +links + + +

Tag: [% tag.name %] ( [% tag.descr %] )

+
    +[% FOR link IN links %] +
  1. »[% link.title %]«
  2. +[% END %] +
+ + diff --git a/root/notag b/root/notag new file mode 100644 index 0000000..005d359 --- /dev/null +++ b/root/notag @@ -0,0 +1,8 @@ + + +no tag + + +

Il tag [% tagname %] non esiste

+ + diff --git a/root/tags b/root/tags new file mode 100644 index 0000000..748631e --- /dev/null +++ b/root/tags @@ -0,0 +1,12 @@ + + +tags + + +
    +[% FOR tag IN tags %] +
  1. [% tag.name %] ([% tag.how_many %])
  2. +[% END %] +
+ + diff --git a/script/bookmarks_load.pl b/script/bookmarks_load.pl new file mode 100755 index 0000000..2687684 --- /dev/null +++ b/script/bookmarks_load.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl +use strict; +use warnings; +use FindBin; +use lib "$FindBin::Bin/../lib"; +use Bookmarks; +use Bookmarks::Utils; +use YAML; + +my $links; +{ + my $yaml; + local $/; + $yaml = <>; + $links=YAML::Load($yaml); +} + +my %fields=( + description => 'title', + extended => 'descr', + created => 'add_date', + modified => 'last_access_date', +); + +for my $link (@$links) { + print "Loading $link->{href}\n"; + my $dblink=Bookmarks::M::DB::Links->find_or_create({ + url => $link->{href}, + }); + if (!Bookmarks::Utils::check_link($link->{href})) { + warn "Link $link->{href} non valido, marco come tale\n"; + $link->{title}.=' [INVALID]'; + } + $link->{created}||=time(); + $link->{modified}||=time(); + while (my ($f1,$f2) = each %fields) { + $dblink->$f2($link->{$f1}); + } + $dblink->icon(Bookmarks::Utils::get_site_icon($link->{href})); + $dblink->update(); + for my $tagname (@{$link->{tags}}) { + my $dbtag=Bookmarks::M::DB::Tags->find_or_create({ + name => $tagname, + }); + $dblink->add_to_tags({ tag => $dbtag}); + } +} diff --git a/script/bookmarks_makedb.pl b/script/bookmarks_makedb.pl index 84e2fd5..93027a7 100644 --- a/script/bookmarks_makedb.pl +++ b/script/bookmarks_makedb.pl @@ -25,6 +25,7 @@ drop table tags; create table links ( pk integer primary key autoincrement, url text unique, + title text, descr text, icon blob, add_date integer, diff --git a/test.yaml b/test.yaml new file mode 100644 index 0000000..c55d106 --- /dev/null +++ b/test.yaml @@ -0,0 +1,28 @@ +--- +- created: ~ + description: 'Italian Agile Movement :: Uncovering better ways of developing software' + href: http://agilemovement.it/ + modified: 1105368948 + tags: + - feeds + - cosi +- created: ~ + description: allofmp3 + href: http://allofmp3.com/ + modified: 1129196905 + tags: + - feeds + - cosi +- created: 1023439288 + description: 'CamelBones - Home' + href: http://camelbones.sourceforge.net/index.html + modified: 1076527236 + tags: + - OS + - Mac OS X +- created: 1096216689 + description: '' + href: http://slashdot.org/ + modified: 1129213810 + tags: + - feeds -- cgit v1.2.3