Bug 11401: QA followup
[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 # Copyright 2014 ByWater Solutions
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 3 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
25 # with Koha; if not, write to the Free Software Foundation, Inc.,
26 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27
28 use Modern::Perl;
29
30 use 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    = Template->new();
40
41 my %channel = getConf("channel");
42 my %image   = getConf("image");
43 my $vars    = {
44     CHANNELTITLE     => $channel{'title'},
45     CHANNELLINK      => $channel{'link'},
46     CHANNELDESC      => $channel{'desc'},
47     CHANNELLANG      => $channel{'lang'},
48     CHANNELLASTBUILD => getDate(),
49
50     IMAGETITLE       => $image{'title'},
51     IMAGEURL         => $image{'url'},
52     IMAGELINK        => $image{'link'},
53     IMAGEDESCRIPTION => $image{'description'},
54     IMAGEWIDTH       => $image{'width'},
55     IMAGEHEIGHT      => $image{'height'},
56
57     ITEMS => getItems( $config{'query'} )
58 };
59
60 my $template_path = $config{"template"};
61 open( my $fh, "<", $template_path ) or die "cannot open $template_path : $!";
62 $feed->process( $fh, $vars, $outFile );
63
64 sub getDate {
65     my $date = strftime( "%a, %d %b %Y %T %Z", localtime );
66     return $date;
67 }
68
69 sub getConf {
70     my $section = shift;
71     my %return;
72     my $inSection = 0;
73
74     open( FILE, $file ) or die "can't open $file";
75     while (<FILE>) {
76         if ($inSection) {
77             my @line = split( /=/, $_, 2 );
78             unless ( $line[1] ) {
79                 $inSection = 0;
80             }
81             else {
82                 my ( $key, $value ) = @line;
83                 chomp $value;
84                 $return{$key} = $value;
85             }
86         }
87         else {
88             if ( $_ eq "$section\n" ) { $inSection = 1 }
89         }
90     }
91     close FILE;
92     return %return;
93 }
94
95 sub getItems {
96     my $query = shift;
97     $query .= " limit 15";
98     my $sth = $dbh->prepare($query);
99     $sth->execute;
100     my @return;
101     while ( my $data = $sth->fetchrow_hashref ) {
102         foreach my $key ( keys %$data ) {
103             my $value = $data->{$key};
104             $value = '' unless defined $value;
105             $value =~ s/\&/\&amp;/g and $data->{$key} = $value;
106         }
107         push @return, $data;
108     }
109     $sth->finish;
110     return \@return;
111 }