Adding more variables for RSS 2.0 template, altering date format for RSS 2.0 compatib...
[koha.git] / rss / rss.pl
1 #!/usr/bin/perl
2
3 # This script can be used to generate rss 0.91 files for syndication.
4
5
6 # it should be run from cron like:
7 #
8 #    rss.pl config.conf
9 #
10
11 # Copyright 2003 Katipo Communications
12 #
13 # This file is part of Koha.
14 #
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
18 # version.
19 #
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA  02111-1307 USA
27
28
29 use strict;
30 use HTML::Template;
31 use C4::Context;
32 use Time::Local;
33 use POSIX;
34
35 my $dbh = C4::Context->dbh;
36 my $file = $ARGV[0];
37 my %config = getConf("config");
38 my $outFile = $config{"output"};
39 my $feed = HTML::Template->new(filename => $config{"tmpl"});
40
41 my %channel = getConf("channel");
42 $feed->param(CHANNELTITLE => $channel{'title'});
43 $feed->param(CHANNELLINK => $channel{'link'});
44 $feed->param(CHANNELDESC => $channel{'desc'});
45 $feed->param(CHANNELLANG => $channel{'lang'});
46 $feed->param(CHANNELLASTBUILD => getDate());
47
48 my %image = getConf("image");
49 $feed->param(IMAGETITLE => $image{'title'});
50 $feed->param(IMAGEURL => $image{'url'});
51 $feed->param(IMAGELINK => $image{'link'});
52 $feed->param(IMAGEDESCRIPTION => $image{'description'});
53 $feed->param(IMAGEWIDTH => $image{'width'});
54 $feed->param(IMAGEHEIGHT => $image{'height'});
55
56 #
57 # handle the items
58 #
59 $feed->param(ITEMS => getItems($config{'query'}));
60
61 open(FILE, ">$outFile") or die "can't open $outFile";
62 print FILE $feed->output();
63 close FILE;
64
65 sub getDate {
66 #    my $date = localtime(timelocal(localtime));
67     my $date = strftime("%a, %d %b %Y %T %Z", localtime);
68     return $date;
69 }
70
71 sub getConf {
72     my $section = shift;
73     my %return;
74     my $inSection = 0;
75
76     open(FILE,$file) or die "can't open $file";
77     while (<FILE>) {
78         if ($inSection) {
79             my @line = split(/=/,$_,2);
80             unless ($line[1]) {
81                 $inSection=0;
82             } else {
83                 my ($key, $value) = @line;
84                 chomp $value;
85                 $return{$key} = $value;
86             }
87         } else {
88             if ($_ eq "$section\n") { $inSection = 1 }
89         }
90     }
91     return %return;
92 }
93 sub getItems {
94     my $query = shift;
95     $query .= " limit 15";
96     my $sth=$dbh->prepare($query);
97     $sth->execute;
98     my @return;
99     while (my $data = $sth->fetchrow_hashref) {
100         foreach my $key (keys %$data) {
101             my $value=$data->{$key};
102             $value=~s/\&/\&amp;/g and $data->{$key}=$value;
103         }
104         push @return, $data;
105     }
106     $sth->finish;
107     return \@return;
108 }