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
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
--------------------------------------------------------------------

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