Add a tag cloud.
authorSteinar H. Gunderson <sesse@debian.org>
Wed, 25 Jul 2007 14:41:50 +0000 (16:41 +0200)
committerSteinar H. Gunderson <sesse@debian.org>
Wed, 25 Jul 2007 14:41:50 +0000 (16:41 +0200)
perl/Sesse/pr0n/Common.pm
perl/Sesse/pr0n/Listing.pm
perl/Sesse/pr0n/pr0n.pm
templates/bilder.knatten.com/tag-listing [new file with mode: 0644]
templates/default/tag-listing [new file with mode: 0644]
templates/images.tg05.gathering.org/tag-listing [new file with mode: 0644]
templates/itk-bilder.samfundet.no/tag-listing [new file with mode: 0644]
templates/skoyen.bilder.knatten.com/tag-listing [new file with mode: 0644]
templates/skoyen.bildereks.knatten.com/tag-listing [new file with mode: 0644]

index b9a651cb5261e6b0981c015772387121d8907300..6c3d7e4110545bb2ac4d43c908df2cf0e0b7865b 100644 (file)
@@ -131,11 +131,7 @@ sub get_query_string {
                next unless defined($value);
                next if (defined($defparam->{$key}) && $value == $defparam->{$key});
 
-               $value = URI::Escape::uri_escape($value);
-
-               # Unescape a few for prettiness (we'll need something for a real _, though)
-               $value =~ s/%20/_/g;
-               $value =~ s/%2F/\//g;
+               $value = pretty_escape($value);
        
                $str .= ($first) ? "?" : ';';
                $str .= "$key=$value";
@@ -143,6 +139,18 @@ sub get_query_string {
        }
        return $str;
 }
+               
+sub pretty_escape {
+       my $value = shift;
+
+       $value = URI::Escape::uri_escape($value);
+
+       # Unescape a few for prettiness (we'll need something for a real _, though)
+       $value =~ s/%20/_/g;
+       $value =~ s/%2F/\//g;
+
+       return $value;
+}
 
 sub print_link {
        my ($r, $title, $baseurl, $param, $defparam, $accesskey) = @_;
index 6f47355f4596631f9d20693dc48c48d5ba8c961a..f97a643414677f9ad177a3b5ab0f44ce1586684e 100644 (file)
@@ -3,6 +3,7 @@ use strict;
 use warnings;
 
 use Sesse::pr0n::Common qw(error dberror);
+use HTML::TagCloud;
 
 sub handler {
        my $r = shift;
@@ -27,34 +28,61 @@ sub handler {
        if ((my $rc = $r->meets_conditions) != Apache2::Const::OK) {
                return $rc;
        }
-
-#      my $q = $dbh->prepare('SELECT t1.id,t1.date,t1.name FROM events t1 LEFT JOIN images t2 ON t1.id=t2.event WHERE t1.vhost=? GROUP BY t1.id,t1.date,t1.name ORDER BY COALESCE(MAX(t2.date),\'1970-01-01 00:00:00\'),t1.id') or
-#              dberror($r, "Couldn't list events");
-       my $q = $dbh->prepare('SELECT event,date,name FROM events e JOIN last_picture_cache c USING (vhost,event) WHERE vhost=? ORDER BY last_picture DESC')
-               or dberror($r, "Couldn't list events");
-       $q->execute($r->get_server_name)
-               or dberror($r, "Couldn't get events");
-       
-       Sesse::pr0n::Common::header($r, Sesse::pr0n::Templates::fetch_template($r, 'event-listing'));
-       my $allcaption = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
-       $r->print("    <ul>\n");
-       $r->print("      <li><a href=\"+all/\">$allcaption</a></li>\n");
-       $r->print("    </ul>\n");
        
-       $r->print("    <ul>\n");
+       if ($r->uri =~ /^\/\+tags\/?/) {
+               # Tag cloud
+               my $q = $dbh->prepare('SELECT tag,COUNT(*) AS frequency FROM tags t JOIN images i ON t.image=i.id WHERE vhost=? GROUP BY tag ORDER BY COUNT(*) DESC LIMIT 75')
+                       or dberror($r, "Couldn't list events");
+               $q->execute($r->get_server_name)
+                       or dberror($r, "Couldn't get events");
+               
+               Sesse::pr0n::Common::header($r, Sesse::pr0n::Templates::fetch_template($r, 'tag-listing'));
+
+               my $cloud = HTML::TagCloud->new;
+
+               while (my $ref = $q->fetchrow_hashref()) {
+                       my $tag = Encode::decode_utf8($ref->{'tag'});
+                       my $html = HTML::Entities::encode_entities($tag);    # is this right?
+                       my $uri = Sesse::pr0n::Common::pretty_escape($tag);  # and this?
+
+                       $cloud->add($html, "/+tags/$uri/", $ref->{'frequency'});
+               }
 
-       while (my $ref = $q->fetchrow_hashref()) {
-               my $id = $ref->{'event'};
-               my $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
-               my $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
+               $r->print($cloud->html_and_css());
+               Sesse::pr0n::Common::footer($r);
+
+               $q->finish();
+       } else {
+               # main listing
+#              my $q = $dbh->prepare('SELECT t1.id,t1.date,t1.name FROM events t1 LEFT JOIN images t2 ON t1.id=t2.event WHERE t1.vhost=? GROUP BY t1.id,t1.date,t1.name ORDER BY COALESCE(MAX(t2.date),\'1970-01-01 00:00:00\'),t1.id') or
+#                      dberror($r, "Couldn't list events");
+               my $q = $dbh->prepare('SELECT event,date,name FROM events e JOIN last_picture_cache c USING (vhost,event) WHERE vhost=? ORDER BY last_picture DESC')
+                       or dberror($r, "Couldn't list events");
+               $q->execute($r->get_server_name)
+                       or dberror($r, "Couldn't get events");
                
-               $r->print("      <li><a href=\"$id/\">$name</a> ($date)</li>\n");
-       }
+               Sesse::pr0n::Common::header($r, Sesse::pr0n::Templates::fetch_template($r, 'event-listing'));
+               my $allcaption = Sesse::pr0n::Templates::fetch_template($r, 'all-event-title');
+               $r->print("    <ul>\n");
+               $r->print("      <li><a href=\"+all/\">$allcaption</a></li>\n");
+               $r->print("    </ul>\n");
+               
+               $r->print("    <ul>\n");
 
-       $r->print("    </ul>\n");
-       Sesse::pr0n::Common::footer($r);
+               while (my $ref = $q->fetchrow_hashref()) {
+                       my $id = $ref->{'event'};
+                       my $date = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'date'}));
+                       my $name = HTML::Entities::encode_entities(Encode::decode_utf8($ref->{'name'}));
+                       
+                       $r->print("      <li><a href=\"$id/\">$name</a> ($date)</li>\n");
+               }
+
+               $r->print("    </ul>\n");
+               Sesse::pr0n::Common::footer($r);
+
+               $q->finish();
+       }
 
-       $q->finish();
        return Apache2::Const::OK;
 }
 
index a0c682bcfafac2e5882e8ccff5d62256810acf6f..c2d9dbc93ddefb1dc155b9f2ad76bd023c39cd57 100644 (file)
@@ -17,7 +17,7 @@ sub handler {
        my $r = shift;
 
        my $uri = $r->uri;
-       if ($uri eq '/') {
+       if ($uri eq '/' || $uri eq '/+tags' || $uri eq '/+tags/') {
                return Sesse::pr0n::Listing::handler($r);
        } elsif ($uri eq '/robots.txt' ||
                 $uri eq '/pr0n.css' ||
diff --git a/templates/bilder.knatten.com/tag-listing b/templates/bilder.knatten.com/tag-listing
new file mode 100644 (file)
index 0000000..d8fd95d
--- /dev/null
@@ -0,0 +1 @@
+Tagsky
diff --git a/templates/default/tag-listing b/templates/default/tag-listing
new file mode 100644 (file)
index 0000000..c89240a
--- /dev/null
@@ -0,0 +1 @@
+Tag cloud
diff --git a/templates/images.tg05.gathering.org/tag-listing b/templates/images.tg05.gathering.org/tag-listing
new file mode 100644 (file)
index 0000000..c89240a
--- /dev/null
@@ -0,0 +1 @@
+Tag cloud
diff --git a/templates/itk-bilder.samfundet.no/tag-listing b/templates/itk-bilder.samfundet.no/tag-listing
new file mode 100644 (file)
index 0000000..d8fd95d
--- /dev/null
@@ -0,0 +1 @@
+Tagsky
diff --git a/templates/skoyen.bilder.knatten.com/tag-listing b/templates/skoyen.bilder.knatten.com/tag-listing
new file mode 100644 (file)
index 0000000..d8fd95d
--- /dev/null
@@ -0,0 +1 @@
+Tagsky
diff --git a/templates/skoyen.bildereks.knatten.com/tag-listing b/templates/skoyen.bildereks.knatten.com/tag-listing
new file mode 100644 (file)
index 0000000..d8fd95d
--- /dev/null
@@ -0,0 +1 @@
+Tagsky