templatized the Syndicated Content wobject

This commit is contained in:
JT Smith 2003-03-13 03:47:05 +00:00
parent f1229e66fb
commit dc9c05129b
4 changed files with 50 additions and 102 deletions

View file

@ -64,6 +64,10 @@ save you many hours of grief.
file. The international messages are now automatically cached file. The international messages are now automatically cached
if caching is available. if caching is available.
* After upgrading please remove the SyndicatedContent.pm file from
sbin/Hourly as it is no longer required, and will cause
problems if it is not removed.
5.1.0 5.1.0
-------------------------------------------------------------------- --------------------------------------------------------------------

View file

@ -3103,8 +3103,12 @@ delete from international where languageId=1 and namespace='WebGUI' and internat
insert into international (internationalId,languageId,namespace,message,lastUpdated) values (896,1,'WebGUI','Page Cache Timeout (Visitors)', 1047342926); insert into international (internationalId,languageId,namespace,message,lastUpdated) values (896,1,'WebGUI','Page Cache Timeout (Visitors)', 1047342926);
delete from international where languageId=1 and namespace='WebGUI' and internationalId=895; delete from international where languageId=1 and namespace='WebGUI' and internationalId=895;
insert into international (internationalId,languageId,namespace,message,lastUpdated) values (895,1,'WebGUI','Page Cache Timeout', 1047342910); insert into international (internationalId,languageId,namespace,message,lastUpdated) values (895,1,'WebGUI','Page Cache Timeout', 1047342910);
delete from international where namespace='SyndicatedContent' and internationalId=5;
delete from international where namespace='SyndicatedContent' and internationalId=6;
alter table SyndicatedContent drop column content;
alter table SyndicatedContent drop column lastFetched;
alter table SyndicatedContent add column templateId int not null default 1;
INSERT INTO template VALUES (1,'Default Syndicated Content','<tmpl_if displayTitle>\r\n <h1><tmpl_var title></h1>\r\n</tmpl_if>\r\n\r\n<tmpl_if description>\r\n <tmpl_var description><p />\r\n</tmpl_if>\r\n\r\n<h1>\r\n<tmpl_if channel.link>\r\n <a href=\"<tmpl_var channel.link>\" target=\"_blank\"><tmpl_var channel.title></a> \r\n<tmpl_else>\r\n <tmpl_var channel.title>\r\n</tmpl_if>\r\n</h1>\r\n\r\n<tmpl_if channel.description>\r\n <tmpl_var channel.description><p />\r\n</tmpl_if>\r\n\r\n\r\n<tmpl_loop item_loop>\r\n<li>\r\n <tmpl_if link>\r\n <a href=\"<tmpl_var link>\" target=\"_blank\"><tmpl_var title></a> \r\n <tmpl_else>\r\n <tmpl_var title>\r\n </tmpl_if>\r\n <tmpl_if description>\r\n - <tmpl_var description>\r\n </tmpl_if>\r\n <br>\r\n\r\n</tmpl_loop>','SyndicatedContent');

View file

@ -12,6 +12,7 @@ package WebGUI::Wobject::SyndicatedContent;
use strict; use strict;
use Tie::CPHash; use Tie::CPHash;
use WebGUI::Cache;
use WebGUI::DateTime; use WebGUI::DateTime;
use WebGUI::HTMLForm; use WebGUI::HTMLForm;
use WebGUI::Icon; use WebGUI::Icon;
@ -20,6 +21,7 @@ use WebGUI::Privilege;
use WebGUI::Session; use WebGUI::Session;
use WebGUI::SQL; use WebGUI::SQL;
use WebGUI::Wobject; use WebGUI::Wobject;
use XML::RSSLite;
our @ISA = qw(WebGUI::Wobject); our @ISA = qw(WebGUI::Wobject);
@ -37,12 +39,9 @@ sub new {
-properties=>$property, -properties=>$property,
-extendedProperties=>{ -extendedProperties=>{
rssUrl=>{}, rssUrl=>{},
content=>{ templateId=>{
defaultValue=>'Not yet fetched!' defaultValue=>1
}, }
lastFetched=>{
defaultValue=>time()
}
} }
); );
bless $self, $class; bless $self, $class;
@ -56,27 +55,22 @@ sub uiLevel {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub www_edit { sub www_edit {
my $f = WebGUI::HTMLForm->new; my $properties = WebGUI::HTMLForm->new;
$f->url( my $layout = WebGUI::HTMLForm->new;
$properties->url(
-name=>"rssUrl", -name=>"rssUrl",
-label=>WebGUI::International::get(1,$_[0]->get("namespace")), -label=>WebGUI::International::get(1,$_[0]->get("namespace")),
-value=>$_[0]->getValue("rssUrl") -value=>$_[0]->getValue("rssUrl")
); );
if ($_[0]->get("wobjectId") ne "new") { $layout->template(
$f->readOnly( -name=>"templateId",
-value=>WebGUI::DateTime::epochToHuman($_[0]->getValue("lastFetched"),"%z %Z"), -value=>$_[0]->getValue("templateId"),
-label=>WebGUI::International::get(5,$_[0]->get("namespace")) -namespace=>$_[0]->get("namespace"),
); -afterEdit=>'func=edit&wid='.$_[0]->get("wobjectId")
$f->readOnly( );
-value=>$_[0]->getValue("content"),
-label=>WebGUI::International::get(6,$_[0]->get("namespace"))
);
} else {
$f->hidden("content",$_[0]->getValue("content"));
$f->hidden("lastFetched",$_[0]->getValue("lastFetched"));
}
return $_[0]->SUPER::www_edit( return $_[0]->SUPER::www_edit(
-properties=>$f->printRowsOnly, -properties=>$properties->printRowsOnly,
-layout=>$layout->printRowsOnly,
-headingId=>4, -headingId=>4,
-helpId=>1 -helpId=>1
); );
@ -85,11 +79,30 @@ sub www_edit {
#------------------------------------------------------------------- #-------------------------------------------------------------------
sub www_view { sub www_view {
my ($output); my %rss;
$output = $_[0]->displayTitle; my $cache = WebGUI::Cache->new($_[0]->get("rssUrl"),"URL");
$output .= $_[0]->description; my $rssFile = $cache->get;
$output .= $_[0]->get("content"); unless (defined $rssFile) {
return $output; $rssFile = $cache->setByHTTP($_[0]->get("rssUrl"),3600);
}
eval{parseXML(\%rss, \$rssFile)};
if ($@) {
WebGUI::ErrorHandler::warn($_[0]->get("rssUrl")." ".$@);
}
my %var;
$var{"channel.title"} = $rss{title};
$var{"channel.link"} = $rss{link};
$var{"channel.description"} = $rss{description};
my @items;
foreach my $item (@{$rss{items}}) {
push (@items,{
link=>$item->{link},
title=>$item->{title},
description=>$item->{description}
});
}
$var{item_loop} = \@items;
return $_[0]->processTemplate($_[0]->get("templateId"),\%var);
} }

View file

@ -1,73 +0,0 @@
package Hourly::SyndicatedContent;
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2002 Plain Black LLC.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#-------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#-------------------------------------------------------------------
use HTTP::Request;
use LWP::UserAgent;
use strict;
use WebGUI::Session;
use WebGUI::SQL;
use XML::RSSLite;
#-------------------------------------------------------------------
sub getRSS {
my ($userAgent, $request, $response, $content, %result);
$userAgent = new LWP::UserAgent;
$request = new HTTP::Request (GET => $_[0]);
$response = $userAgent->request($request);
$content = $response->content;
eval{parseXML(\%result, \$content)} or print $@;
return %result;
}
#-------------------------------------------------------------------
sub generateHTML {
my (%rss, $html, $item);
%rss = @_;
$html = $rss{title};
$html = '<a href="'.$rss{link}.'" target="_blank">'.$html.'</a>' if ($rss{link});
$html = '<h1>'.$html.'</h1>';
$html .= $rss{description}.'<p>' if ($rss{description});
foreach $item (@{$rss{items}}) {
$html .= '<li>';
if ($item->{link}) {
$html .= '<a href="'.$item->{link}.'" target="_blank">'.$item->{title}.'</a>';
} else {
$html .= $item->{title};
}
$html .= ' - '.$item->{description} if ($item->{description});
$html .= '<br>';
}
return ($html);
}
#-------------------------------------------------------------------
sub process {
my ($sth, @data, %rss, $html);
$sth = WebGUI::SQL->read("select wobject.wobjectId, SyndicatedContent.rssURL, SyndicatedContent.content
from wobject,SyndicatedContent where wobject.wobjectId=SyndicatedContent.wobjectId and wobject.pageId<>3");
while (@data = $sth->array) {
%rss = getRSS($data[1]);
$html = generateHTML(%rss);
if ($html ne "") {
WebGUI::SQL->write("update SyndicatedContent set content=".quote($html).", lastFetched=".time()." where wobjectId=$data[0]");
} elsif (substr($data[2],6) ne "Unable" && substr($data[2],7) ne "Not yet") {
# then just leave the existing content in place
} else {
WebGUI::SQL->write("update SyndicatedContent set content='Unable to fetch content. Perhaps the RSS is improperly formated.',
lastFetched=".time()." where wobjectId=$data[0]");
}
}
$sth->finish;
}
1;