bug 2864 [2/2]: move rss/* to misc/cronjobs/rss/*
[koha.git] / misc / cronjobs / rss / rss.pl
1 #!/usr/bin/perl
2
3 # This script can be used to generate rss 0.91 files for syndication.
4
5 # it should be run from cron like:
6 #
7 #    rss.pl config.conf
8 #
9
10 # Copyright 2003 Katipo Communications
11 #
12 # This file is part of Koha.
13 #
14 # Koha is free software; you can redistribute it and/or modify it under the
15 # terms of the GNU General Public License as published by the Free Software
16 # Foundation; either version 2 of the License, or (at your option) any later
17 # version.
18 #
19 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
20 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License along with
24 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25 # Suite 330, Boston, MA  02111-1307 USA
26
27 use strict;
28 use warnings;
29
30 use HTML::Template::Pro;
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::Pro->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
67     #    my $date = localtime(timelocal(localtime));
68     my $date = strftime( "%a, %d %b %Y %T %Z", localtime );
69     return $date;
70 }
71
72 sub getConf {
73     my $section = shift;
74     my %return;
75     my $inSection = 0;
76
77     open( FILE, $file ) or die "can't open $file";
78     while (<FILE>) {
79         if ($inSection) {
80             my @line = split( /=/, $_, 2 );
81             unless ( $line[1] ) {
82                 $inSection = 0;
83             } else {
84                 my ( $key, $value ) = @line;
85                 chomp $value;
86                 $return{$key} = $value;
87             }
88         } else {
89             if ( $_ eq "$section\n" ) { $inSection = 1 }
90         }
91     }
92     close FILE;
93     return %return;
94 }
95
96 sub getItems {
97     my $query = shift;
98     $query .= " limit 15";
99     my $sth = $dbh->prepare($query);
100     $sth->execute;
101     my @return;
102     while ( my $data = $sth->fetchrow_hashref ) {
103         foreach my $key ( keys %$data ) {
104             my $value = $data->{$key};
105             $value = '' unless defined $value;
106             $value =~ s/\&/\&amp;/g and $data->{$key} = $value;
107         }
108         push @return, $data;
109     }
110     $sth->finish;
111     return \@return;
112 }