Bug 17600: Standardize our EXPORT_OK
[srvgit] / 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
16 # under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 3 of the License, or
18 # (at your option) any later version.
19 #
20 # Koha is distributed in the hope that it will be useful, but
21 # WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License
26 # along with Koha; if not, see <http://www.gnu.org/licenses>.
27
28 use Modern::Perl;
29
30 use Template;
31
32 use Koha::Script -cron;
33 use C4::Context;
34 use POSIX qw( close localtime open strftime );
35
36 my $dbh     = C4::Context->dbh;
37 my $file    = $ARGV[0];
38 my %config  = getConf("config");
39 my $outFile = $config{"output"};
40 my $feed    = Template->new();
41
42 my %channel = getConf("channel");
43 my %image   = getConf("image");
44 my $vars    = {
45     OPACBaseURL      => C4::Context->preference('OPACBaseURL'),
46     CHANNELTITLE     => $channel{'title'},
47     CHANNELLINK      => $channel{'link'},
48     CHANNELDESC      => $channel{'desc'},
49     CHANNELLANG      => $channel{'lang'},
50     CHANNELLASTBUILD => getDate(),
51
52     IMAGETITLE       => $image{'title'},
53     IMAGEURL         => $image{'url'},
54     IMAGELINK        => $image{'link'},
55     IMAGEDESCRIPTION => $image{'description'},
56     IMAGEWIDTH       => $image{'width'},
57     IMAGEHEIGHT      => $image{'height'},
58
59     ITEMS => getItems( $config{'query'} )
60 };
61
62 my $template_path = $config{"template"};
63 open( my $fh, "<", $template_path ) or die "cannot open $template_path : $!";
64 $feed->process( $fh, $vars, $outFile );
65
66 sub getDate {
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( my $fh, '<', $file ) or die "can't open $file";
77     while (<$fh>) {
78         if ($inSection) {
79             my @line = split( /=/, $_, 2 );
80             unless ( $line[1] ) {
81                 $inSection = 0;
82             }
83             else {
84                 my ( $key, $value ) = @line;
85                 chomp $value;
86                 $return{$key} = $value;
87             }
88         }
89         else {
90             if ( $_ eq "$section\n" ) { $inSection = 1 }
91         }
92     }
93     close $fh;
94     return %return;
95 }
96
97 sub getItems {
98     my $query = shift;
99     $query .= " limit 15";
100     my $sth = $dbh->prepare($query);
101     $sth->execute;
102     my @return;
103     while ( my $data = $sth->fetchrow_hashref ) {
104         foreach my $key ( keys %$data ) {
105             my $value = $data->{$key};
106             $value = '' unless defined $value;
107             $value =~ s/\&/\&amp;/g and $data->{$key} = $value;
108         }
109         push @return, $data;
110     }
111     $sth->finish;
112     return \@return;
113 }