test suite - various changes
[koha.git] / opac / opac-rss.pl
1 #!/usr/bin/perl
2
3 # Copyright 2007 Paul POULAIN
4 #
5 # This file is part of Koha
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;    # always use
21
22 use XML::RSS;
23 use Digest::MD5 qw(md5_base64);
24 use POSIX qw(ceil floor);
25 use Date::Calc qw(Today_and_Now Delta_YMDHMS);
26 use C4::Context;
27 use C4::Search;
28 use C4::Koha;
29 use C4::Biblio;
30
31 =head1 NAME
32
33 opac-rss.pl : script to have RSS feeds automatically on each OPAC search
34
35 =head1 SYNOPSIS
36
37 on each query (on OPAC), a link to this script is automatically added. The user can save it's queries as RSS feeds.
38 This script :
39
40 =over 4
41
42   - build the RDF file from the query
43   - save the RDF file in a opac/rss directory for caching : the RDF is calculated only once every 30mn, and the cache file name is calculated by a md5_base64 of the query (each user registering the same query will use the same cache : speed improvement)
44   - let the user specify it's query (q parameter : opac-rss.pl?q=ti:hugo)
45   - let the user specify the number of results returned (by default 20, but there are no limits : opac-rss.pl?q=ti:hugo&size=9999)
46
47 This script auto calculates the website URL
48
49 the RDF contains : 
50
51 =over 4
52
53   - Koha: $query as RSS title
54   - Koha as subject
55   - LibraryName systempreference as RDF description and creator
56   - copyright currentyear
57   - biblio title as RSS "title" and biblio author as RSS description
58
59 =cut
60
61 # create a new CGI object
62 # not sure undef_params option is working, need to test
63 use CGI qw('-no_undef_params');
64 my $cgi = new CGI;
65
66 # the query to use
67 my $query = $cgi->param('q');
68 $query =~ s/:/=/g;
69
70 # the number of lines to retrieve
71 my $size = $cgi->param('size') || 50;
72
73 # the filename of the cached rdf file.
74 my $filename = md5_base64($query);
75 $filename =~ s/\///g;
76 my $rss = new XML::RSS( version => '1.0', encoding=>C4::Context->preference("TemplateEncoding"), output=>C4::Context->preference("TemplateEncoding"),language=>C4::Context->preference('opaclanguages'));
77
78 # the site URL
79 my $url = $cgi->url();
80 $url =~ s/opac-rss\.pl.*//;
81 $url =~ /(http:\/\/.*?)\//;
82 my $short_url = $1;
83
84 my $RDF_update_needed = 1;
85 my ( $year, $month, $day, $hour, $min, $sec ) = Today_and_Now();
86
87 if ( -e "rss/$filename" ) {
88     $rss->parsefile("rss/$filename");
89
90 # check if we have to rebuild the RSS feed (once every 30mn), or just return the actual rdf
91     my $rdf_stamp = $rss->{'channel'}->{'dc'}->{'date'};
92     my ( $stamp_year, $stamp_month, $stamp_day, $stamp_hour, $stamp_min, $stamp_sec ) =
93        ( $rdf_stamp =~ /(.*)-(.*)-(.*):(.*):(.*):(.*)/ );
94
95 # if more than 30 mn since the last RDF update, rebuild the RDF. Otherwise, just return it
96     unless ( ( $year - $stamp_year > 0 )
97         or ( $month - $stamp_month > 0 )
98         or ( $day - $stamp_day > 0 )
99         or ( $hour - $stamp_hour > 0 )
100         or ( $min - $stamp_min > 30 ) )
101     {
102         $RDF_update_needed = 0;
103     }
104 }
105
106 if ($RDF_update_needed) {
107
108     #     warn "RDF update in progress";
109     utf8::decode($query);
110     my $libname = C4::Context->preference("LibraryName");
111     $rss->channel(
112         title       => "Koha: $query",
113         description => $libname,
114         link        => $short_url,
115         dc          => {
116             date     => "$year-$month-$day:$hour:$min:$sec",
117             subject  => "Koha",
118             creator  => $libname,
119             rights   => "Copyright $year",
120             language => C4::Context->preference("opaclanguages"),
121         },
122     );
123
124     warn "fetching $size results for $query";
125     my ( $error, $marcresults, $total_hits ) = SimpleSearch( $query, 0, $size );
126
127     my $hits = scalar @$marcresults;
128     my @results;
129     for ( my $i = 0 ; $i < $hits ; $i++ ) {
130         my %resultsloop;
131         my $marcrecord = MARC::File::USMARC::decode( $marcresults->[$i] );
132         my $biblio = TransformMarcToKoha( C4::Context->dbh, $marcrecord, '' );
133
134 # check if the entry is already in the feed. Otherwise, pop the $line th line and add this new one.
135         my $already_in_feed = 0;
136         foreach ( @{ $rss->{'items'} } ) {
137             if ( $_->{'link'} =~ /biblionumber=$biblio->{'biblionumber'}/ ) {
138                 $already_in_feed = 1;
139             }
140         }
141         unless ($already_in_feed) {
142             pop( @{ $rss->{'items'} } ) if ( @{ $rss->{'items'} } >= $size );
143             utf8::decode($biblio->{'title'});
144             utf8::decode($biblio->{'author'});
145             $rss->add_item(
146                 title       => $biblio->{'title'},
147                 description => $biblio->{'author'},
148                 link        => "$url/opac-detail.pl?biblionumber="
149                   . $biblio->{'biblionumber'},
150                 mode => 'insert',
151             );
152         }
153     }
154
155     # save the rss feed.
156     $rss->save("rss/$filename");
157 }
158 print $cgi->header( -type => "application/rss+xml" );
159 print $rss->as_string;