more sorting options for SyndicatedContent

This commit is contained in:
Paul Driver 2010-05-06 14:55:32 -07:00
parent 2fe34a2a06
commit 40d11e8693
5 changed files with 207 additions and 47 deletions

View file

@ -31,10 +31,10 @@ my $quiet; # this line required
my $session = start(); # this line required
# upgrade functions go here
modifySortItems();
finish($session); # this line required
#----------------------------------------------------------------------------
# Describe what our function does
#sub exampleFunction {
@ -44,6 +44,33 @@ finish($session); # this line required
# print "DONE!\n" unless $quiet;
#}
#----------------------------------------------------------------------------
# Changes sortItems to a SelectBox
sub modifySortItems {
my $session = shift;
print "\tUpdating SyndicatedContent...\n" unless $quiet;
require WebGUI::Form::SelectBox;
print "\t\tModifying table...\n" unless $quiet;
my $type = WebGUI::Form::SelectBox->getDatabaseFieldType;
$session->db->write("ALTER TABLE SyndicatedContent MODIFY sortItems $type");
print "\t\tConverting old values...\n" unless $quiet;
$session->db->write(q{
UPDATE SyndicatedContent
SET sortItems = 'none'
WHERE sortItems <> '1'
});
$session->db->write(q{
UPDATE SyndicatedContent
SET sortItems = 'pubDate_des'
WHERE sortItems = '1'
});
# and here's our code
print "DONE!\n" unless $quiet;
}
# -------------- DO NOT EDIT BELOW THIS LINE --------------------------------

View file

@ -109,8 +109,17 @@ sub definition {
},
sortItems => {
tab => 'properties',
fieldType => 'yesNo',
defaultValue => 1,
fieldType => 'selectBox',
options => do {
tie my %o, 'Tie::IxHash', (
none => $i18n->get('no order'),
feed => $i18n->get('feed order'),
pubDate_asc => $i18n->get('publication date ascending'),
pubDate_des =>
$i18n->get('publication date descending'),
); \%o;
},
defaultValue => 'none',
label => $i18n->get('sortItemsLabel'),
hoverHelp => $i18n->get('sortItemsLabel description'),
},
@ -136,10 +145,13 @@ Combines all feeds into a single XML::FeedPP object.
=cut
sub generateFeed {
my $self = shift;
my $self = shift;
my $limit = shift || $self->get('maxHeadlines');
my $feed = XML::FeedPP::Atom->new();
my $log = $self->session->log;
my $log = $self->session->log;
my $sort = $self->get('sortItems');
my @opt = (use_ixhash => 1) if $sort eq 'feed';
my $feed = XML::FeedPP::Atom->new(@opt);
# build one feed out of many
my $newlyCached = 0;
@ -160,7 +172,7 @@ sub generateFeed {
# care of any encoding specified in the XML prolog
utf8::downgrade($value, 1);
eval {
my $singleFeed = XML::FeedPP->new($value, utf8_flag => 1, -type => 'string');
my $singleFeed = XML::FeedPP->new($value, utf8_flag => 1, -type => 'string', @opt);
$feed->merge_channel($singleFeed);
$feed->merge_item($singleFeed);
};
@ -192,9 +204,14 @@ sub generateFeed {
}
# sort them by date and remove any duplicate from the OR based term matching above
if ($self->get('sortItems')) {
if ($sort =~ /^pubDate/) {
$feed->sort_item();
}
if ($sort =~ /_asc$/) {
my @items = $feed->get_item;
$feed->clear_item;
$feed->add_item($_) for (reverse @items);
}
# limit the feed to the maximum number of headlines (or the feed generator limit).
$feed->limit_item($limit);

View file

@ -245,11 +245,33 @@ our $I18N = {
},
'sortItemsLabel' => {
message => q{Sort feed items by date?},
message => q{Sort items by},
},
'sortItemsLabel description' => {
message => q{If enabled, items will be sorted by date. If disabled, items will be left in the order they appear in the original feed.},
message => q{No order: items will be in semi-random order<br />
Publication Date: sort by item pubDate<br />
Feed Order: Items will be in the order they appeared in the feed}
},
'no order' => {
message => 'No Order',
context => 'name for the sortItems value that indicates that no sorting should be done '
},
'feed order' => {
message => 'Feed Order',
context => 'name for the sortItems value that indicates items should be in the order they appeared in the feed'
},
'publication date ascending' => {
message => 'Publication Date (oldest first)',
context => 'name for the sortItems value that indicates items should be sorted by publication date from oldest to newest'
},
'publication date descending' => {
message => 'Publication Date (newest first)',
context => 'name for the sortItems value that indicates items should be sorted by publication date from newest to oldest'
},
'syndicated content asset template variables title' => {

View file

@ -20,7 +20,7 @@ use Data::Dumper;
use WebGUI::Test;
use WebGUI::Session;
use Test::More tests => 22; # increment this value for each test you create
use Test::More tests => 25; # increment this value for each test you create
use Test::Deep;
use WebGUI::Asset::Wobject::SyndicatedContent;
use XML::FeedPP;
@ -147,32 +147,40 @@ ok( defined $vars->{item_loop}->[0]->{description}, 'getTemplateVariables: descr
#
####################################################################
my $tbbUrl = 'http://www.plainblack.com/tbb.rss';
$syndicated_content->update({
rssUrl => $tbbUrl,
hasTerms => 'WebGUI',
});
sub withCachedFeed {
my ($url, $path, $block) = @_;
$syndicated_content->update({ rssUrl => $url });
my $cache = WebGUI::Cache->new($session, $tbbUrl, 'RSS');
open my $rssFile, '<', WebGUI::Test->getTestCollateralPath('tbb.rss')
or die "Unable to get RSS file";
my $rssContent = do { local $/; <$rssFile>; };
close $rssFile;
$cache->set($rssContent, 60);
open my $file, '<', WebGUI::Test->getTestCollateralPath($path)
or die "Unable to get RSS file: $path";
my $content = do { local $/; <$file> };
close $file;
my $filteredFeed = $syndicated_content->generateFeed();
my $cache = WebGUI::Cache->new($session, $url, 'RSS');
$cache->set($content, 60);
$block->();
$cache->delete;
}
cmp_deeply(
[ map { $_->title } $filteredFeed->get_item() ],
[
'Google Picasa Plugin for WebGUI Gallery',
'WebGUI Roadmap',
'WebGUI 8 Performance',
],
'generateFeed: filters items based on the terms being in title, or description'
);
sub titles_are {
my ($expected, $message) = @_;
my $feed = $syndicated_content->generateFeed;
my @got = map { $_->title } $feed->get_item;
cmp_deeply \@got, $expected, $message;
}
$cache->delete;
$syndicated_content->update({ hasTerms => 'WebGUI' });
withCachedFeed 'http://www.plainblack.com/tbb.rss', 'tbb.rss', sub {
titles_are(
[
'Google Picasa Plugin for WebGUI Gallery',
'WebGUI Roadmap',
'WebGUI 8 Performance',
],
'generateFeed: filters items based on the terms being in title, or description'
);
};
####################################################################
#
@ -180,27 +188,47 @@ $cache->delete;
#
####################################################################
##Feed with no links or pubDates.
my $oncpUrl = 'http://www.oncp.gob.ve/oncp.xml';
$syndicated_content->update({
rssUrl => $oncpUrl,
hasTerms => '',
maxHeadlines => 50,
});
my $cache = WebGUI::Cache->new($session, $oncpUrl, 'RSS');
open my $rssFile, '<', WebGUI::Test->getTestCollateralPath('oncp.xml')
or die "Unable to get RSS file: oncp.xml";
my $rssContent = do { local $/; <$rssFile>; };
close $rssFile;
$cache->set($rssContent, 60);
withCachedFeed 'http://www.oncp.gob.ve/oncp.xml', 'oncp.xml', sub {
my $oddFeed1 = $syndicated_content->generateFeed();
my $oddFeed1 = $syndicated_content->generateFeed();
my @oddItems = $oddFeed1->get_item();
is (@oddItems, 13, 'feed has items even without pubDates or links');
};
my @oddItems = $oddFeed1->get_item();
is (@oddItems, 13, 'feed has items even without pubDates or links');
$cache->delete;
####################################################################
#
# sorting
#
####################################################################
withCachedFeed 'http://www.plainblack.com/tbb.rss', 'tbb_odd.rss', sub {
my @ascending = (
'I have arrived in Lisboa!',
'WebGUI 8 Performance',
'WebGUI Roadmap',
'Google Picasa Plugin for WebGUI Gallery',
);
my @descending = reverse @ascending;
my @feed = (
'WebGUI Roadmap',
'Google Picasa Plugin for WebGUI Gallery',
'I have arrived in Lisboa!',
'WebGUI 8 Performance',
);
$syndicated_content->update({ sortItems => 'pubDate_asc' });
titles_are \@ascending, 'ascending sort';
$syndicated_content->update({ sortItems => 'pubDate_des' });
titles_are \@descending, 'descending sort';
$syndicated_content->update({ sortItems => 'feed' });
titles_are \@feed, 'feed order';
};

View file

@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>The Black Blog</title>
<link>/tbb</link>
<copyright/>
<pubDate>Mon, 12 Oct 2009 11:54:28 -0500</pubDate>
<description/>
<item>
<title>WebGUI Roadmap</title>
<link>http://www.plainblack.com/tbb/webgui-roadmap</link>
<author>JT</author>
<epochDate>1254325377</epochDate>
<guid isPermaLink="true">http://www.plainblack.com/tbb/webgui-roadmap</guid>
<pubDate>Wed, 30 Sep 2009 10:42:57 -0500</pubDate>
<userDefined1/>
<userDefined2/>
<userDefined3/>
<userDefined4/>
<userDefined5/>
<description>The new roadmap is online.</description>
</item>
<item>
<title>Google Picasa Plugin for WebGUI Gallery</title>
<link>http://www.plainblack.com/tbb/google-picasa-plugin-for-webgui-gallery</link>
<author>JT</author>
<epochDate>1254854387</epochDate>
<guid isPermaLink="true">http://www.plainblack.com/tbb/google-picasa-plugin-for-webgui-gallery</guid>
<pubDate>Tue, 06 Oct 2009 13:39:47 -0500</pubDate>
<userDefined1/>
<userDefined2/>
<userDefined3/>
<userDefined4/>
<userDefined5/>
<description>Today we unveil the Google Picasa plugin for WebGUI Gallery.</description>
</item>
<item>
<title>I have arrived in Lisboa!</title>
<link>http://www.plainblack.com/tbb/i-have-arrived-in-lisboa</link>
<author>JT</author>
<epochDate>1249140064</epochDate>
<guid isPermaLink="true">http://www.plainblack.com/tbb/i-have-arrived-in-lisboa</guid>
<pubDate>Sat, 01 Aug 2009 10:21:04 -0500</pubDate>
<userDefined1/>
<userDefined2/>
<userDefined3/>
<userDefined4/>
<userDefined5/>
<description>I&apos;m in Lisbon, Portugal for YAPC::EU.</description>
</item>
<item>
<title>WebGUI 8 Performance</title>
<link>http://www.plainblack.com/tbb/webgui-8-performance</link>
<author>JT</author>
<epochDate>1254236976</epochDate>
<guid isPermaLink="true">http://www.plainblack.com/tbb/webgui-8-performance</guid>
<pubDate>Tue, 29 Sep 2009 10:09:36 -0500</pubDate>
<userDefined1/>
<userDefined2/>
<userDefined3/>
<userDefined4/>
<userDefined5/>
<description>WebGUI 8 is going to be the fastest version of WebGUI ever released.</description>
</item>
</channel>
</rss>