merging opac details only for items with the same branch+location+itemcallnumber...
[koha.git] / opac / opac-rss.pl
1 #!/usr/bin/perl
2
3 use strict;    # always use
4
5 use XML::RSS;
6 use Digest::MD5 qw(md5_base64);
7 use POSIX qw(ceil floor);
8 use Date::Calc qw(Today_and_Now Delta_YMDHMS);
9 use C4::Context;
10 use C4::Search;
11 use C4::Koha;
12 use C4::Biblio;
13
14 # Copyright 2007 Paul POULAIN
15 #
16 # This file is part of Koha
17 #
18 # Koha is free software; you can redistribute it and/or modify it under the
19 # terms of the GNU General Public License as published by the Free Software
20 # Foundation; either version 2 of the License, or (at your option) any later
21 # version.
22 #
23 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
24 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
25 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
26 #
27 # You should have received a copy of the GNU General Public License along with
28 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
29 # Suite 330, Boston, MA  02111-1307 USA
30
31 =head1 NAME
32
33 opac-search.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 my $rss = new XML::RSS (version => '1.0');
76
77 # the site URL
78 my $url = $cgi->url();
79 $url =~ s/opac-rss\.pl.*//;
80 $url =~ /(http:\/\/.*?)\//;
81 my $short_url=$1;
82
83 my $RDF_update_needed=1;
84     my ($year,$month,$day, $hour,$min,$sec) = Today_and_Now();
85
86 if (-e "rss/$filename") {
87     $rss->parsefile("rss/$filename");
88     # check if we have to rebuild the RSS feed (once every 30mn), or just return the actual rdf
89     my $rdf_stamp = $rss->{'channel'}->{'dc'}->{'date'};
90     $rdf_stamp =~ /(.*)-(.*)-(.*):(.*):(.*):(.*)/;
91     my ($stamp_year,$stamp_month,$stamp_day,$stamp_hour,$stamp_min,$stamp_sec) = ($1,$2,$3,$4,$5,$6);
92     # if more than 30 mn since the last RDF update, rebuild the RDF. Otherwise, just return it
93     unless (($year-$stamp_year >0) or ($month-$stamp_month >0) or ($day-$stamp_day >0) or ($hour-$stamp_hour >0) or ($min-$stamp_min >30)) {
94         $RDF_update_needed =0;
95     }
96 }
97
98 if ($RDF_update_needed) {
99 #     warn "RDF update in progress";
100     $rss->channel(
101                 title        => "Koha : $query",
102                 description  => C4::Context->preference("LibraryName"),
103                 link => $short_url,
104                 dc => {
105                     date       => "$year-$month-$day:$hour:$min:$sec",
106                     subject    => "Koha",
107                     creator    => C4::Context->preference("LibraryName"),
108                     rights     => "Copyright $year" ,
109                     language   => C4::Context->preference("opaclanguages"),
110                 },
111     );
112     
113     my $total;    # the total results for the whole set
114     my ($error, $marcresults) = SimpleSearch($query);
115     
116     my $hits = scalar @$marcresults;
117     $hits=$size if $hits > $size;
118     my @results;
119     for(my $i=0;$i<$hits;$i++) {
120         my %resultsloop;
121         my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
122         my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
123         # check if the entry is already in the feed. Otherwise, pop the $line th line and add this new one.
124         my $already_in_feed=0;
125         foreach (@{$rss->{'items'}}) {
126             if ($_->{'link'} =~ /biblionumber=$biblio->{'biblionumber'}/) {
127                 $already_in_feed=1;
128             }
129         }
130         unless ($already_in_feed) {
131             pop(@{$rss->{'items'}}) if (@{$rss->{'items'}} >= $size);
132             $rss->add_item(title => $biblio->{'title'},
133                             description => $biblio->{'author'},
134                             link        => "$url/opac-detail.pl?biblionumber=".$biblio->{'biblionumber'},
135                             mode =>'insert',
136             );
137         }
138     }
139     # save the rss feed.
140     $rss->save("rss/$filename");
141 } else {
142 #     warn "RDF CACHE used"
143 }
144 print $cgi->header(-type => "application/rss+xml");
145 print $rss->as_string;