]> ruin.nu Git - ndwebbie.git/blobdiff - NDWeb/Pages/Forum.pm
Optimize AllUnread page
[ndwebbie.git] / NDWeb / Pages / Forum.pm
index 55cbe9615481a4ca5ffe35c84ba6fb47e80bd5c7..ca60e3102f10d13f0d3cdd324c00d6e9bef11de5 100644 (file)
 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 #**************************************************************************/
 
-package ND::Web::Pages::Forum;
+package NDWeb::Pages::Forum;
 use strict;
 use warnings;
-use ND::Web::Forum;
+use NDWeb::Forum;
 use CGI qw/:standard/;
-use ND::Web::Include;
+use NDWeb::Include;
 use ND::Include;
 
-use base qw/ND::Web::XMLPage/;
+use base qw/NDWeb::XMLPage/;
 
-$ND::Web::Page::PAGES{forum} = __PACKAGE__;
+$NDWeb::Page::PAGES{forum} = __PACKAGE__;
 
 sub parse {
        my $self = shift;
        if ($self->{URI} =~ m{^/.*/allUnread}){
-               param('allUnread',1);
+               $self->{allUnread} = 1;
+       }elsif ($self->{URI} =~ m{^/.*/search(?:/(.*))?}){
+               bless $self, 'NDWeb::Pages::Forum::Search';
+               $self->{PAGE} = 'forum/search';
        }
 }
 
@@ -155,34 +158,50 @@ sub render_body {
                        ,undef,$thread->{fcid}) or $ND::ERROR .= p($DBH->errstr);
                $BODY->param(Category =>  $category);
 
-       }elsif(defined param('allUnread')){ #List threads in this board
+       }elsif(defined $self->{allUnread}){ #List threads in this board
                $BODY->param(AllUnread => 1);
-               $BODY->param(Id => $board->{id});
+
+               my $threads = $DBH->prepare(q{SELECT fcid,category,fbid,board,ft.ftid AS id,u.username,ft.subject,
+               count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts,
+               date_trunc('seconds',max(fp.time)::timestamp) as last_post,
+               min(fp.time)::date as posting_date, ft.sticky
+               FROM forum_categories fc 
+                       JOIN forum_boards fb USING (fcid) 
+                       JOIN forum_threads ft USING (fbid)
+                       JOIN forum_posts fp USING (ftid) 
+                       JOIN users u ON u.uid = ft.uid
+                       LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv ON ftv.ftid = ft.ftid
+               WHERE fbid > 0 AND
+                       fb.fbid IN (SELECT fbid FROM forum_access WHERE gid IN (SELECT groups($1)))
+               GROUP BY fcid,category,fbid,board,ft.ftid, ft.subject,ft.sticky,u.username
+               HAVING count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) >= 1 
+               ORDER BY fcid,fbid,sticky DESC,last_post DESC});
+
                my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
                $BODY->param(Date => $time);
-               $categories->execute or $ND::ERROR .= p($DBH->errstr);
+               $threads->execute($ND::UID) or $ND::ERROR .= p($DBH->errstr);
                my @categories;
-               while (my $category = $categories->fetchrow_hashref){
-                       $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
-                       my @boards;
-                       while (my $board = $boards->fetchrow_hashref){
-                               next if $board->{id} < 0;
-                               $threads->execute($board->{id},$ND::UID,1) or $ND::ERROR .= p($DBH->errstr);
-                               my $i = 0;
-                               my @threads;
-                               while (my $thread = $threads->fetchrow_hashref){
-                                       $i++;
-                                       $thread->{Odd} = $i % 2;
-                                       push @threads,$thread;
-                               }
-                               $board->{Threads} = \@threads;
-                               delete $board->{post};
-                               push @boards,$board if $i > 0;
+               my $category = {fcid => 0};
+               my $board = {fbid => 0};
+               while (my $thread = $threads->fetchrow_hashref){
+                       if ($category->{fcid} != $thread->{fcid}){
+                               delete $category->{fcid};
+                               $category = {fcid => $thread->{fcid}, category => $thread->{category}};
+                               push @categories,$category;
                        }
-                       $category->{Boards} = \@boards;
-                       delete $category->{id};
-                       push @categories,$category if @boards;
+                       if ($board->{fbid} != $thread->{fbid}){
+                               delete $board->{fbid};
+                               $board = {fbid => $thread->{fbid}, board => $thread->{board}};
+                               push @{$category->{Boards}},$board;
+                       }
+                       delete $thread->{fcid};
+                       delete $thread->{fbid};
+                       delete $thread->{category};
+                       delete $thread->{board};
+                       push @{$board->{Threads}},$thread;
                }
+               delete $category->{fcid};
+               delete $board->{fbid};
                $BODY->param(Categories => \@categories);
 
        }elsif($board){ #List threads in this board
@@ -196,11 +215,8 @@ sub render_body {
                my ($time) = $DBH->selectrow_array('SELECT now()::timestamp',undef);
                $BODY->param(Date => $time);
                $threads->execute($board->{id},$ND::UID,0) or $ND::ERROR .= p($DBH->errstr);
-               my $i = 0;
                my @threads;
                while (my $thread = $threads->fetchrow_hashref){
-                       $i++;
-                       $thread->{Odd} = $i % 2;
                        push @threads,$thread;
                }
                $BODY->param(Threads => \@threads);
@@ -226,8 +242,6 @@ sub render_body {
                my ($category) = $DBH->selectrow_array(q{SELECT category FROM forum_categories WHERE fcid = $1}
                        ,undef,$board->{fcid}) or $ND::ERROR .= p($DBH->errstr);
                $BODY->param(Category =>  $category);
-
-       }elsif($self->{URI} =~ m{^/.*/search/(.*)}){ #List threads in this board
        }else{ #List boards
                $BODY->param(Overview => 1);
                $categories->execute or $ND::ERROR .= p($DBH->errstr);
@@ -241,15 +255,12 @@ sub render_body {
                while (my $category = $categories->fetchrow_hashref){
                        $boards->execute($category->{id},$ND::UID) or $ND::ERROR .= p($DBH->errstr);
                        my @boards;
-                       my $i = 0;
                        while (my $board = $boards->fetchrow_hashref){
-                               $i++;
-                               $board->{Odd} = $i % 2;
                                push @boards,$board;
                        }
                        $category->{Boards} = \@boards;
                        delete $category->{id};
-                       push @categories,$category if $i > 0;
+                       push @categories,$category if $boards->rows > 0;
                }
                $BODY->param(Categories => \@categories);