From a7b4fbc0ed4735d48b9b756ff35d6871379616ac Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 16 Mar 2010 18:29:09 -0700 Subject: [PATCH] Fix thread search URLs when threads are archived, or trashed. Location in the pagination is now found by interrupted linear search. --- lib/WebGUI/Asset/Post/Thread.pm | 42 ++++++++-- lib/WebGUI/Asset/Wobject/Collaboration.pm | 96 ++++++++++++----------- t/Asset/Post/Thread.t | 20 ++++- 3 files changed, 99 insertions(+), 59 deletions(-) diff --git a/lib/WebGUI/Asset/Post/Thread.pm b/lib/WebGUI/Asset/Post/Thread.pm index 1f8d55387..9181df243 100644 --- a/lib/WebGUI/Asset/Post/Thread.pm +++ b/lib/WebGUI/Asset/Post/Thread.pm @@ -349,20 +349,46 @@ for the site. =cut sub getCSLinkUrl { - my $self = shift; + my $self = shift; + if ($self->get('status') eq 'archived') { + return $self->get('url'); + } + my $session = $self->session; my $url; my $cs = $self->getParent; - my $paginator = WebGUI::Paginator->new($self->session, '', $self->getParent->get('threadsPerPage')); - my $page_size = $paginator->{_rpp}; ##To make sure defaults are handled correctly. - my $place = $self->getRank+1; - my $last_place = $cs->getLastChild->getRank+1; - my $page = int(($last_place - $place)/$page_size) + 1; + my $page_size = $cs->get('threadsPerPage'); + my $sql =<db->read($sql, [$cs->getId]); + my $place = 1; ##1 based indexing + my $found = 0; + my $lineage = $self->get('lineage'); + THREAD: while (my $arrayRef = $sth->arrayRef) { + if ($arrayRef->[0] eq $lineage) { + $found = 1; + last THREAD; + } + ++$place; + } + $sth->finish; + return $self->get('url') if !$found; + my $page = int($place/$page_size) + 1; my $page_frag = 'pn='.$page.';sortBy=lineage;sortOrder=desc'; - $url = $self->session->url->append($cs->get('url'), $page_frag); + $url = $session->url->append($cs->get('url'), $page_frag); return $url; } - #------------------------------------------------------------------- =head2 getThreadLinkUrl ( ) diff --git a/lib/WebGUI/Asset/Wobject/Collaboration.pm b/lib/WebGUI/Asset/Wobject/Collaboration.pm index 56569c70e..da3606598 100644 --- a/lib/WebGUI/Asset/Wobject/Collaboration.pm +++ b/lib/WebGUI/Asset/Wobject/Collaboration.pm @@ -1159,66 +1159,68 @@ Collaboration System =cut sub getThreadsPaginator { - my $self = shift; - + my $self = shift; + my $scratchSortBy = $self->getId."_sortBy"; - my $scratchSortOrder = $self->getId."_sortDir"; - my $sortBy = $self->session->form->process("sortBy") + my $scratchSortOrder = $self->getId."_sortDir"; + my $sortBy = $self->session->form->process("sortBy") || $self->session->scratch->get($scratchSortBy) || $self->get("sortBy"); my $sortOrder = $self->session->form->process("sortOrder") || $self->session->scratch->get($scratchSortOrder) || $self->get("sortOrder"); - if ($sortBy ne $self->session->scratch->get($scratchSortBy) && $self->session->form->process("func") ne "editSave") { - $self->session->scratch->set($scratchSortBy,$self->session->form->process("sortBy")); + if ($sortBy ne $self->session->scratch->get($scratchSortBy) && $self->session->form->process("func") ne "editSave") { + $self->session->scratch->set($scratchSortBy,$self->session->form->process("sortBy")); $self->session->scratch->set($scratchSortOrder, $sortOrder); - } elsif ($self->session->form->process("sortBy") && $self->session->form->process("func") ne "editSave") { - if ($sortOrder eq "asc") { - $sortOrder = "desc"; - } else { - $sortOrder = "asc"; - } - $self->session->scratch->set($scratchSortOrder, $sortOrder); - } - $sortBy ||= "assetData.revisionDate"; - $sortOrder ||= "desc"; + } + elsif ($self->session->form->process("sortBy") && $self->session->form->process("func") ne "editSave") { + if ($sortOrder eq "asc") { + $sortOrder = "desc"; + } + else { + $sortOrder = "asc"; + } + $self->session->scratch->set($scratchSortOrder, $sortOrder); + } + $sortBy ||= "assetData.revisionDate"; + $sortOrder ||= "desc"; # Sort by the thread rating instead of the post rating. other places don't care about threads. if ($sortBy eq 'rating') { $sortBy = 'threadRating'; } $sortBy = join('.', map { $self->session->db->dbh->quote_identifier($_) } split(/\./, $sortBy)); - my $sql = " - select - asset.assetId, - asset.className, - assetData.revisionDate as revisionDate - from Thread - left join asset on Thread.assetId=asset.assetId - left join Post on Post.assetId=Thread.assetId and Thread.revisionDate = Post.revisionDate - left join assetData on assetData.assetId=Thread.assetId and Thread.revisionDate = assetData.revisionDate - where - asset.parentId=".$self->session->db->quote($self->getId)." - and asset.state='published' - and asset.className='WebGUI::Asset::Post::Thread' - and assetData.revisionDate=( - select - max(revisionDate) - from - assetData - where - assetData.assetId=asset.assetId - and (status='approved' or status='archived') - ) - and status='approved' - group by - assetData.assetId - order by - Thread.isSticky desc, - ".$sortBy." - ".$sortOrder; - my $p = WebGUI::Paginator->new($self->session,$self->getUrl,$self->get("threadsPerPage")); - $p->setDataByQuery($sql); + my $sql = " + select + asset.assetId, + asset.className, + assetData.revisionDate as revisionDate + from Thread + left join asset on Thread.assetId=asset.assetId + left join Post on Post.assetId=Thread.assetId and Thread.revisionDate = Post.revisionDate + left join assetData on assetData.assetId=Thread.assetId and Thread.revisionDate = assetData.revisionDate + where + asset.parentId=".$self->session->db->quote($self->getId)." + and asset.state='published' + and asset.className='WebGUI::Asset::Post::Thread' + and assetData.revisionDate=( + select + max(revisionDate) + from + assetData + where + assetData.assetId=asset.assetId + and (status='approved' or status='archived') + ) + and status='approved' + group by + assetData.assetId + order by + Thread.isSticky desc, + ".$sortBy." + ".$sortOrder; + my $p = WebGUI::Paginator->new($self->session,$self->getUrl,$self->get("threadsPerPage")); + $p->setDataByQuery($sql); return $p; } diff --git a/t/Asset/Post/Thread.t b/t/Asset/Post/Thread.t index 3a6705bf2..00459839e 100644 --- a/t/Asset/Post/Thread.t +++ b/t/Asset/Post/Thread.t @@ -13,7 +13,7 @@ use strict; use lib "$FindBin::Bin/../../lib"; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 11; # increment this value for each test you create +use Test::More tests => 12; # increment this value for each test you create use Test::MockObject::Extends; use Test::Exception; use WebGUI::Asset::Wobject::Collaboration; @@ -79,13 +79,25 @@ $collab->update({threadsPerPage => 3, postsPerPage => 10,}); note 'getCSLinkUrl'; my @newThreads; -push @newThreads, $collab->addChild($props, @addArgs); -push @newThreads, $collab->addChild($props, @addArgs); +my $threadCount = 15; +while ($threadCount--) { + push @newThreads, $collab->addChild($props, @addArgs); +} +my $versionTag2 = WebGUI::VersionTag->getWorking($session); +$versionTag2->commit; + my $csUrl = $collab->get('url'); like $newThreads[-1]->getCSLinkUrl, qr/^$csUrl/, 'getCsLinkUrl returns URL of the parent CS with no gateway'; like $newThreads[-1]->getCSLinkUrl, qr/\?pn=1/, 'and has the right page number'; like $newThreads[-1]->getCSLinkUrl, qr/\?pn=1;sortBy=lineage;sortOrder=desc/, 'and has the right sort parameters'; -like $thread->getCSLinkUrl, qr/\?pn=2/, 'checking 2nd thread on another page'; +$thread->restore(); +like $thread->getCSLinkUrl, qr/\?pn=6/, 'checking 2nd thread on another page'; + +$newThreads[0]->archive; +$newThreads[1]->archive; +$newThreads[2]->archive; +like $thread->getCSLinkUrl, qr/\?pn=5/, 'checking thread on another page, with archived threads'; + $collab->update({threadsPerPage => 0, postsPerPage => 0,}); lives_ok { $uncommittedThread->getCSLinkUrl } '... works when pagination set to 0';