summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@luxion>2005-10-20 11:27:13 +0000
committerdakkar <dakkar@luxion>2005-10-20 11:27:13 +0000
commit86a26a896db925d5d3ee8e0d6ed226a565214912 (patch)
tree376c14bfa44a20e7bb128e4ccee5bf54eebfdfd2
parentrelazioni, e inizio lavori (diff)
downloadBookmarks-86a26a896db925d5d3ee8e0d6ed226a565214912.tar.gz
Bookmarks-86a26a896db925d5d3ee8e0d6ed226a565214912.tar.bz2
Bookmarks-86a26a896db925d5d3ee8e0d6ed226a565214912.zip
comincia a far finta di andare. le icone non vengono caricate, non so perché
-rw-r--r--Build.PL6
-rw-r--r--lib/Bookmarks.pm2
-rw-r--r--lib/Bookmarks/C/Main.pm36
-rw-r--r--lib/Bookmarks/M/DB/Links.pm8
-rw-r--r--lib/Bookmarks/M/DB/Tags.pm17
-rw-r--r--lib/Bookmarks/Utils.pm45
-rw-r--r--root/links13
-rw-r--r--root/notag8
-rw-r--r--root/tags12
-rwxr-xr-xscript/bookmarks_load.pl47
-rw-r--r--script/bookmarks_makedb.pl1
-rw-r--r--test.yaml28
12 files changed, 209 insertions, 14 deletions
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{<link
+ \s+
+ (?:
+ rel="shortcut\ icon"
+ \s+
+ href="(.*?)"
+ )|(?:
+ href="(.*?)"
+ \s+
+ rel="shortcut\ icon"
+ )}smx);
+ if (!$favicon) {
+ $favicon='/favicon.ico';
+ }
+ print "Trovato: '$favicon'";
+ $favicon=URI::URL->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 @@
+<html>
+<head>
+<title>links</title>
+</head>
+<body>
+<p>Tag: [% tag.name %] ( [% tag.descr %] )</p>
+<ol>
+[% FOR link IN links %]
+<li><img src="[% base _ 'icon?link=' _ link.pk %]" /> <a href="[% link.url %]">»[% link.title %]«</a></li>
+[% END %]
+</ol>
+</body>
+</html>
diff --git a/root/notag b/root/notag
new file mode 100644
index 0000000..005d359
--- /dev/null
+++ b/root/notag
@@ -0,0 +1,8 @@
+<html>
+<head>
+<title>no tag</title>
+</head>
+<body>
+<p>Il tag [% tagname %] non esiste</p>
+</body>
+</html>
diff --git a/root/tags b/root/tags
new file mode 100644
index 0000000..748631e
--- /dev/null
+++ b/root/tags
@@ -0,0 +1,12 @@
+<html>
+<head>
+<title>tags</title>
+</head>
+<body>
+<ol>
+[% FOR tag IN tags %]
+<li><a href="[% base _ 'tag/' _ tag.name %]">[% tag.name %]</a> ([% tag.how_many %])</li>
+[% END %]
+</ol>
+</body>
+</html>
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