Bug 22600: Add 'cron' to interface types and set appropriately
[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
32 use Koha::Cron;
33 use C4::Context;
34 use Time::Local;
35 use POSIX;
36
37 my $dbh     = C4::Context->dbh;
38 my $file    = $ARGV[0];
39 my %config  = getConf("config");
40 my $outFile = $config{"output"};
41 my $feed    = Template->new();
42
43 my %channel = getConf("channel");
44 my %image   = getConf("image");
45 my $vars    = {
46     OPACBaseURL      => C4::Context->preference('OPACBaseURL'),
47     CHANNELTITLE     => $channel{'title'},
48     CHANNELLINK      => $channel{'link'},
49     CHANNELDESC      => $channel{'desc'},
50     CHANNELLANG      => $channel{'lang'},
51     CHANNELLASTBUILD => getDate(),
52
53     IMAGETITLE       => $image{'title'},
54     IMAGEURL         => $image{'url'},
55     IMAGELINK        => $image{'link'},
56     IMAGEDESCRIPTION => $image{'description'},
57     IMAGEWIDTH       => $image{'width'},
58     IMAGEHEIGHT      => $image{'height'},
59
60     ITEMS => getItems( $config{'query'} )
61 };
62
63 my $template_path = $config{"template"};
64 open( my $fh, "<", $template_path ) or die "cannot open $template_path : $!";
65 $feed->process( $fh, $vars, $outFile );
66
67 sub getDate {
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             }
84             else {
85                 my ( $key, $value ) = @line;
86                 chomp $value;
87                 $return{$key} = $value;
88             }
89         }
90         else {
91             if ( $_ eq "$section\n" ) { $inSection = 1 }
92         }
93     }
94     close FILE;
95     return %return;
96 }
97
98 sub getItems {
99     my $query = shift;
100     $query .= " limit 15";
101     my $sth = $dbh->prepare($query);
102     $sth->execute;
103     my @return;
104     while ( my $data = $sth->fetchrow_hashref ) {
105         foreach my $key ( keys %$data ) {
106             my $value = $data->{$key};
107             $value = '' unless defined $value;
108             $value =~ s/\&/\&amp;/g and $data->{$key} = $value;
109         }
110         push @return, $data;
111     }
112     $sth->finish;
113     return \@return;
114 }