Bug 13591: Pass OPACBaseURL to rss creation process
[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     OPACBaseURL      => C4::Context->preference('OPACBaseURL'),
45     CHANNELTITLE     => $channel{'title'},
46     CHANNELLINK      => $channel{'link'},
47     CHANNELDESC      => $channel{'desc'},
48     CHANNELLANG      => $channel{'lang'},
49     CHANNELLASTBUILD => getDate(),
50
51     IMAGETITLE       => $image{'title'},
52     IMAGEURL         => $image{'url'},
53     IMAGELINK        => $image{'link'},
54     IMAGEDESCRIPTION => $image{'description'},
55     IMAGEWIDTH       => $image{'width'},
56     IMAGEHEIGHT      => $image{'height'},
57
58     ITEMS => getItems( $config{'query'} )
59 };
60
61 my $template_path = $config{"template"};
62 open( my $fh, "<", $template_path ) or die "cannot open $template_path : $!";
63 $feed->process( $fh, $vars, $outFile );
64
65 sub getDate {
66     my $date = strftime( "%a, %d %b %Y %T %Z", localtime );
67     return $date;
68 }
69
70 sub getConf {
71     my $section = shift;
72     my %return;
73     my $inSection = 0;
74
75     open( FILE, $file ) or die "can't open $file";
76     while (<FILE>) {
77         if ($inSection) {
78             my @line = split( /=/, $_, 2 );
79             unless ( $line[1] ) {
80                 $inSection = 0;
81             }
82             else {
83                 my ( $key, $value ) = @line;
84                 chomp $value;
85                 $return{$key} = $value;
86             }
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 }