3 use strict; # always use
6 use Digest::MD5 qw(md5_base64);
7 use POSIX qw(ceil floor);
8 use Date::Calc qw(Today_and_Now Delta_YMDHMS);
16 opac-search.pl : script to have RSS feeds automatically on each OPAC search
20 on each query (on OPAC), a link to this script is automatically added. The user can save it's queries as RSS feeds.
25 - build the RDF file from the query
26 - 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)
27 - let the user specify it's query (q parameter : opac-rss.pl?q=ti:hugo)
28 - 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)
30 This script auto calculates the website URL
36 - Koha: $query as RSS title
38 - LibraryName systempreference as RDF description and creator
39 - copyright currentyear
40 - biblio title as RSS "title" and biblio author as RSS description
44 # create a new CGI object
45 # not sure undef_params option is working, need to test
46 use CGI qw('-no_undef_params');
50 my $query = $cgi->param('q');
53 # the number of lines to retrieve
54 my $size=$cgi->param('size') || 20;
56 # the filename of the cached rdf file.
57 my $filename = md5_base64($query);
58 my $rss = new XML::RSS (version => '1.0');
61 my $url = $cgi->url();
62 $url =~ s/opac-rss\.pl.*//;
63 $url =~ /(http:\/\/.*?)\//;
66 my $RDF_update_needed=1;
67 my ($year,$month,$day, $hour,$min,$sec) = Today_and_Now();
69 if (-e "rss/$filename") {
70 $rss->parsefile("rss/$filename");
71 # check if we have to rebuild the RSS feed (once every 30mn), or just return the actual rdf
72 my $rdf_stamp = $rss->{'channel'}->{'dc'}->{'date'};
73 $rdf_stamp =~ /(.*)-(.*)-(.*):(.*):(.*):(.*)/;
74 my ($stamp_year,$stamp_month,$stamp_day,$stamp_hour,$stamp_min,$stamp_sec) = ($1,$2,$3,$4,$5,$6);
75 # if less than 30 mn since the last RDF update, rebuild the RDF. Otherwise, just return it
76 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)) {
77 $RDF_update_needed =0;
81 if ($RDF_update_needed) {
82 # warn "RDF update in progress";
84 title => "Koha : $query",
85 description => C4::Context->preference("LibraryName"),
88 date => "$year-$month-$day:$hour:$min:$sec",
90 creator => C4::Context->preference("LibraryName"),
91 rights => "Copyright $year" ,
92 language => C4::Context->preference("opaclanguages"),
96 my $total; # the total results for the whole set
97 my ($error, $marcresults) = SimpleSearch($query);
99 my $hits = scalar @$marcresults;
100 $hits=$size if $hits > $size;
102 for(my $i=0;$i<$hits;$i++) {
104 my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
105 my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
106 # check if the entry is already in the feed. Otherwise, pop the $line th line and add this new one.
107 my $already_in_feed=0;
108 foreach (@{$rss->{'items'}}) {
109 if ($_->{'link'} =~ /biblionumber=$biblio->{'biblionumber'}/) {
113 unless ($already_in_feed) {
114 pop(@{$rss->{'items'}}) if (@{$rss->{'items'}} >= $size);
115 $rss->add_item(title => $biblio->{'title'},
116 description => $biblio->{'author'},
117 link => "$url/opac-detail.pl?biblionumber=".$biblio->{'biblionumber'},
123 $rss->save("rss/$filename");
125 # warn "RDF CACHE used"
127 print $cgi->header(-type => "application/rss+xml");
128 print $rss->as_string;