[LON-CAPA-cvs] cvs: loncom /interface lonrss.pm
www
lon-capa-cvs@mail.lon-capa.org
Thu, 17 Nov 2005 21:40:54 -0000
www Thu Nov 17 16:40:54 2005 EDT
Added files:
/loncom/interface lonrss.pm
Log:
RSS Handler
Index: loncom/interface/lonrss.pm
+++ loncom/interface/lonrss.pm
# The LearningOnline Network
# RSS Feeder
#
# $Id: lonrss.pm,v 1.1 2005/11/17 21:40:52 www Exp $
#
# Copyright Michigan State University Board of Trustees
#
# This file is part of the LearningOnline Network with CAPA (LON-CAPA).
#
# LON-CAPA is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LON-CAPA is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LON-CAPA; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# /home/httpd/html/adm/gpl.txt
#
# http://www.lon-capa.org/
#
package Apache::lonrss;
use strict;
use Apache::Constants qw(:common);
use Apache::loncommon;
use Apache::lonnet;
use Apache::lontexconvert;
use Apache::lonfeedback;
use Apache::lonannounce;
use Apache::lonlocal;
use Apache::lonhtmlcommon;
use Apache::lonspeller();
my $feedcounter=0;
sub filterfeedname {
my $filename=shift;
$filename=~s/\.rss$//;
$filename=~s/\W//g;
return $filename;
}
sub feedname {
return 'nohist_'.&filterfeedname(shift).'_rssfeed';
}
sub displayfeedname {
my $filename=&filterfeedname(shift);
$filename=~s/\_/ /g;
return $filename;
}
sub addentry {
my ($uname,$udom,$filename,$title,$description,$url,$status,$encurl,$enclength,$enctype)=@_;
my $feedname=&feedname($filename);
$feedcounter++;
my $id=time.'00000'.$$.'00000'.$feedcounter;
return &Apache::lonnet::put($feedname,{
$id.'_title' => $title,
$id.'_description' => $description,
$id.'_link' => $url,
$id.'_enclosureurl' => $encurl,
$id.'_enclosurelength' => $enclength,
$id.'_enclosuretype' => $enctype,
$id.'_status' => $status},$udom,$uname);
}
sub handler {
my $r = shift;
&Apache::loncommon::content_type($r,'text/xml');
$r->send_http_header;
return OK if $r->header_only;
my (undef,undef,$udom,$uname,$filename)=split(/\//,$r->uri);
my $filterfeedname=&filterfeedname($filename);
my $feedname=&feedname($filename);
my $displayfeedname=&displayfeedname($filename);
$r->print("<rss version='2.0' xmlns:dc='http://purl.org/dc/elements/1.1'>\n<channel>".
"\n<link>http://".$ENV{'HTTP_HOST'}.'/</link>'.
"\n<description>".&mt('An RSS Feed provided by the LON-CAPA Learning Content Management System').'</description>');
# Is this user for real?
my $homeserver=&Apache::lonnet::homeserver($uname,$udom);
if ($homeserver eq 'no_host') {
$r->print('<title>'.&mt('No feed available').'</title>');
} else {
# Course or user?
my $name='';
if ($uname=~/^\d/) {
my %cenv=&Apache::lonnet::dump('environment',$udom,$uname);
$name=$cenv{'description'};
} else {
$name=&Apache::loncommon::nickname($uname,$udom);
}
$r->print("\n<title>".&mt('LON-CAPA RSS Feed "[_1]" for [_2]',$displayfeedname,$name).'</title>');
# Render private items?
my $viewpubliconly=1;
if (($env{'user.name'} eq $uname) && ($env{'user.domain'} eq $udom)) {
$viewpubliconly=0;
}
# Get feed items
my %newsfeed=&Apache::lonnet::dump($feedname,$udom,$uname);
foreach (sort keys %newsfeed) {
if ($_=~/^(\d+)\_status$/) {
my $id=$1;
if (($newsfeed{$id.'_status'} ne 'public') && ($viewpubliconly)) { next; }
$r->print("\n<item>\n<title>".$newsfeed{$id.'_title'}."</title>\n<description>".
$newsfeed{$id.'_description'}."</description>\n<link>".
$newsfeed{$id.'_link'}."</link>\n");
if ($newsfeed{$id.'_enclosureurl'}) {
$r->print("\n<enclosure url='".$newsfeed{$id.'_enclosureurl'}."' length='".$newsfeed{$id.'_enclosurelength'}.
"' type='".$newsfeed{$id.'_enclosuretype'}."' />");
}
$r->print("\n<guid>".$id.$filterfeedname.'_'.$udom.'_'.$uname."</guid></item>\n");
}
}
}
$r->print("\n</channel></rss>\n");
return OK;
}
1;
__END__