From 1395fcc411fd4c9859abc95a1aad97dc473fe219 Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Tue, 13 Apr 2010 11:05:29 -0700 Subject: [PATCH] Add RFE #10944, keyword pages. Keyword pages are any page where the title is exactly the same as any keyword for any page in the wiki. Keyword pages work the same as any wiki page, but also may display a list of pages that are tagged with the keyword. --- docs/changelog/7.x.x.txt | 1 + lib/WebGUI/Asset/WikiPage.pm | 54 ++++++++++++++++++-- lib/WebGUI/Help/Asset_WikiPage.pm | 14 ++++++ lib/WebGUI/i18n/English/Asset_WikiPage.pm | 26 +++++++++- t/Asset/WikiPage.t | 61 +++++++++++++++++++++-- 5 files changed, 147 insertions(+), 9 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index c3ce30712..f41265ec2 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -4,6 +4,7 @@ - added Better comment rating icons. - fixed #11520: Wiki Locked - fixed Missing Template variables for the Wiki Page view template. + - added #10944: Wiki Keyword Page 7.9.2 - added: Workflow to extend recurring Calendar events 2 years from the diff --git a/lib/WebGUI/Asset/WikiPage.pm b/lib/WebGUI/Asset/WikiPage.pm index eb99c9de5..7a086fb46 100644 --- a/lib/WebGUI/Asset/WikiPage.pm +++ b/lib/WebGUI/Asset/WikiPage.pm @@ -265,13 +265,17 @@ Get the common template vars for this asset sub getTemplateVars { my ( $self ) = @_; - my $i18n = WebGUI::International->new($self->session, "Asset_WikiPage"); - my $wiki = $self->getWiki; - my $owner = WebGUI::User->new( $self->session, $self->get('ownerUserId') ); - my $keywords = WebGUI::Keyword->new($self->session)->getKeywordsForAsset({ + my $session = $self->session; + my $i18n = WebGUI::International->new($session, "Asset_WikiPage"); + my $wiki = $self->getWiki; + my $owner = WebGUI::User->new( $session, $self->get('ownerUserId') ); + my $keyObj = WebGUI::Keyword->new($session); + + my $keywords = $keyObj->getKeywordsForAsset({ asset => $self, asArrayRef => 1, }); + my @keywordsLoop = (); foreach my $word (@{$keywords}) { push @keywordsLoop, { @@ -305,11 +309,35 @@ sub getTemplateVars { $self->scrubContent, {skipTitles => [$self->get('title')]}, ), + isKeywordPage => $self->isKeywordPage, isSubscribed => $self->isSubscribed, subscribeUrl => $self->getSubscribeUrl, unsubscribeUrl => $self->getUnsubscribeUrl, owner => $owner->get('alias'), }; + my @keyword_pages = (); + if ($var->{isKeywordPage}) { + my $paginator = $keyObj->getMatchingAssets({ + startAsset => $self->getWiki, + keyword => $self->get('title'), + usePaginator => 1, + }); + PAGE: foreach my $assetId (@{ $paginator->getPageData }) { + next PAGE if $assetId->{assetId} eq $self->getId; + my $asset = WebGUI::Asset->newByDynamicClass($session, $assetId->{assetId}); + next PAGE unless $asset; + push @keyword_pages, { + title => $asset->getTitle, + url => $asset->getUrl, + }; + } + $paginator->appendTemplateVariables($var); + @keyword_pages = map { $_->[1] } + sort + map { [ lc $_->{title}, $_ ] } + @keyword_pages; + } + $var->{keyword_page_loop} = \@keyword_pages; return $var; } @@ -359,6 +387,24 @@ sub isProtected { #------------------------------------------------------------------- +=head2 isKeywordPage + +Returns a boolean indicating whether or not the name of this WikiPage matches any keyword in the Wiki that +contains it. + +=cut + +sub isKeywordPage { + my $self = shift; + my $keywords = WebGUI::Keyword->new($self->session)->getMatchingAssets({ + asset => $self->getWiki, + keyword => $self->get('title'), + }); + return scalar @{ $keywords }; +} + +#------------------------------------------------------------------- + =head2 preparePageTemplate This is essentially prepareView, but is smart and will only do the template diff --git a/lib/WebGUI/Help/Asset_WikiPage.pm b/lib/WebGUI/Help/Asset_WikiPage.pm index eda94eba5..dc11d5976 100644 --- a/lib/WebGUI/Help/Asset_WikiPage.pm +++ b/lib/WebGUI/Help/Asset_WikiPage.pm @@ -48,6 +48,9 @@ our $HELP = { { tag => 'wiki page asset template variables', namespace => 'Asset_WikiPage' }, + { tag => 'pagination template variables', + namespace => 'WebGUI' + }, ], variables => [ { name => 'viewLabel', @@ -104,6 +107,17 @@ our $HELP = { name => 'owner', description => 'help owner', }, + { 'name' => 'isKeywordPage', }, + { 'name' => 'keyword_page_loop', + 'variables' => [ + { 'name' => 'title', + 'description' => 'keyword page title', + }, + { 'name' => 'url', + 'description' => 'keyword page url', + }, + ], + }, ], related => [], }, diff --git a/lib/WebGUI/i18n/English/Asset_WikiPage.pm b/lib/WebGUI/i18n/English/Asset_WikiPage.pm index 58c43ac6a..4d0eae3de 100644 --- a/lib/WebGUI/i18n/English/Asset_WikiPage.pm +++ b/lib/WebGUI/i18n/English/Asset_WikiPage.pm @@ -327,12 +327,36 @@ our $I18N = context => 'Body text for help page', }, - 'isFeatured label' => { message => q{Feature this on the front page}, lastUpdated => 0, context => 'Label for asset property', }, + + 'isKeywordPage' => { + message => q{A boolean that is true if this page is a keyword page.}, + lastUpdated => 0, + context => 'template variable help', + }, + + 'keyword_page_loop' => { + message => q{If this page is a keyword page, then this loop will contain a list of all pages tagged with this page's keyword. The pagination variables will apply to the list of pages in this loop. If this page is not a keyword page, the loop will be blank, and the pagination variables will not be present.}, + lastUpdated => 0, + context => 'template variable help', + }, + + 'keyword page title' => { + message => q{The title of a page that has this keyword.}, + lastUpdated => 0, + context => 'template variable help', + }, + + 'keyword page url' => { + message => q{The URL to a page that has this keyword.}, + lastUpdated => 0, + context => 'template variable help', + }, + }; 1; diff --git a/t/Asset/WikiPage.t b/t/Asset/WikiPage.t index 85b923bda..3a92d7e00 100644 --- a/t/Asset/WikiPage.t +++ b/t/Asset/WikiPage.t @@ -16,7 +16,8 @@ use lib "$FindBin::Bin/../lib"; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 17; # increment this value for each test you create +use Test::More tests => 29; # increment this value for each test you create +use Test::Deep; use WebGUI::Asset::Wobject::WikiMaster; use WebGUI::Asset::WikiPage; @@ -27,12 +28,12 @@ my $versionTag = WebGUI::VersionTag->getWorking($session); $versionTag->set({name=>"Wiki Test"}); addToCleanup($versionTag); -my $wiki = $node->addChild({className=>'WebGUI::Asset::Wobject::WikiMaster'}); +my $wiki = $node->addChild({className=>'WebGUI::Asset::Wobject::WikiMaster', title => 'Wiki Test', url => 'wikitest'}); +my @autoCommitCoda = (undef, undef, {skipAutoCommitWorkflows => 1, skipNotification => 1}); $versionTag->commit; my $wikipage = $wiki->addChild( {className=>'WebGUI::Asset::WikiPage'}, - undef, undef, - {skipAutoCommitWorkflows => 1, skipNotification => 1} + @autoCommitCoda, ); # Wikis create and autocommit a version tag when a child is added. Lets get the name so we can roll it back. @@ -90,3 +91,55 @@ $comments = $wikipage->get('comments'); is($comments->[0]{comment}, $secondComment, "you can delete a comment"); is($wikipage->get('averageCommentRating'), 1, 'average rating is adjusted after deleting a comment'); + +################## +# This section tests hierarchical keywords support +################## + +# +## setup some more wiki pages +my $properties = { + className=>'WebGUI::Asset::WikiPage', + content => 'Now is the time for all good men to come to the aid of their country', + title => 'Keyword', + keywords => 'keyword' +}; +my $wikipage2 = $wiki->addChild($properties, @autoCommitCoda); +isa_ok($wikipage2, 'WebGUI::Asset::WikiPage'); + +$properties = { + className=>'WebGUI::Asset::WikiPage', + content => 'The quick brown fox jumps over the lazy dog.', + title => 'Fox', + keywords => 'keyword' +}; +my $wikipage3 = $wiki->addChild($properties, @autoCommitCoda); +isa_ok($wikipage3, 'WebGUI::Asset::WikiPage'); + +# Test keywords support +my $keywords = $wikipage2->get('keywords'); +is($keywords,$properties->{'keywords'}, 'Keywords match'); + +# Test isKeywordPage() +ok $wikipage2->isKeywordPage(), "'".$wikipage2->get('title')."' is a keyword page"; +my $templateVars = $wikipage2->getTemplateVars; +ok $templateVars->{isKeywordPage}, 'isKeywordPage template var, true'; +cmp_deeply + $templateVars->{keyword_page_loop}, + [ + { title => 'Fox', url => '/wikitest/fox', }, + ], + 'populated keyword_page_loop, sorted by title'; +ok ! $wikipage3->isKeywordPage(), "'".$wikipage3->get('title')."' is not a keyword page"; +$templateVars = $wikipage3->getTemplateVars; +ok ! $templateVars->{isKeywordPage}, 'isKeywordPage template var, false'; +cmp_deeply $templateVars->{keyword_page_loop}, [], 'empty keyword_page_loop'; + +$wikipage3->update({keywords => $wikipage3->get('keywords').',Fox'}); +ok $wikipage3->isKeywordPage(), "'".$wikipage3->get('title')."' is now a keyword page"; +$templateVars = $wikipage3->getTemplateVars; +ok $templateVars->{isKeywordPage}, 'isKeywordPage template var, false'; +cmp_deeply + $templateVars->{keyword_page_loop}, + [ ], + 'empty keyword_page_loop, self is not put into the loop';