NEW feature : RSS feeds. See POD & koha-devel for details
This commit is contained in:
parent
cb27afeb6d
commit
847b82ff23
|
|
@ -183,10 +183,6 @@ sub SimpleSearch {
|
|||
my $query = shift;
|
||||
if (C4::Context->preference('NoZebra')) {
|
||||
my $result = NZorder(NZanalyse($query))->{'biblioserver'}->{'RECORDS'};
|
||||
use Data::Dumper;
|
||||
foreach (@$result) {
|
||||
warn "$query :".@_;
|
||||
}
|
||||
return (undef,$result);
|
||||
} else {
|
||||
my @servers = @_;
|
||||
|
|
@ -272,7 +268,6 @@ sub getRecords {
|
|||
$query_to_use = $federated_query;
|
||||
}
|
||||
|
||||
# warn "HERE : $query_type => $query_to_use";
|
||||
# check if we've got a query_type defined
|
||||
eval {
|
||||
if ($query_type)
|
||||
|
|
@ -500,8 +495,6 @@ sub getRecords {
|
|||
}
|
||||
}
|
||||
}
|
||||
use Data::Dumper;
|
||||
warn Dumper($results_hashref);
|
||||
return ( undef, $results_hashref, \@facets_loop );
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
</title>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<!-- TMPL_IF name="RSS" -->
|
||||
<link rel="alternate" type="application/rss+xml" title="VNUNet.fr - Actualités" href="<!-- TMPL_VAR name="site_url"-->/opac-rss.pl?q=<!-- TMPL_VAR name="searchdesc"-->">
|
||||
<!-- /TMPL_IF -->
|
||||
<!-- TMPL_IF name="opacstylesheet" -->
|
||||
<style type="text/css">
|
||||
@import url(<!-- TMPL_VAR NAME="opacstylesheet" -->);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,128 @@
|
|||
#!/usr/bin/perl
|
||||
|
||||
use strict; # always use
|
||||
|
||||
use XML::RSS;
|
||||
use Digest::MD5 qw(md5_base64);
|
||||
use POSIX qw(ceil floor);
|
||||
use Date::Calc qw(Today_and_Now Delta_YMDHMS);
|
||||
use C4::Context;
|
||||
use C4::Search;
|
||||
use C4::Koha;
|
||||
use C4::Biblio;
|
||||
|
||||
=head1 NAME
|
||||
|
||||
opac-search.pl : script to have RSS feeds automatically on each OPAC search
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
on each query (on OPAC), a link to this script is automatically added. The user can save it's queries as RSS feeds.
|
||||
This script :
|
||||
|
||||
=over 4
|
||||
|
||||
- build the RDF file from the query
|
||||
- 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)
|
||||
- let the user specify it's query (q parameter : opac-rss.pl?q=ti:hugo)
|
||||
- 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)
|
||||
|
||||
This script auto calculates the website URL
|
||||
|
||||
the RDF contains :
|
||||
|
||||
=over 4
|
||||
|
||||
- Koha: $query as RSS title
|
||||
- Koha as subject
|
||||
- LibraryName systempreference as RDF description and creator
|
||||
- copyright currentyear
|
||||
- biblio title as RSS "title" and biblio author as RSS description
|
||||
|
||||
=cut
|
||||
|
||||
# create a new CGI object
|
||||
# not sure undef_params option is working, need to test
|
||||
use CGI qw('-no_undef_params');
|
||||
my $cgi = new CGI;
|
||||
|
||||
# the query to use
|
||||
my $query = $cgi->param('q');
|
||||
$query =~ s/:/=/g;
|
||||
|
||||
# the number of lines to retrieve
|
||||
my $size=$cgi->param('size') || 20;
|
||||
|
||||
# the filename of the cached rdf file.
|
||||
my $filename = md5_base64($query);
|
||||
my $rss = new XML::RSS (version => '1.0');
|
||||
|
||||
# the site URL
|
||||
my $url = $cgi->url();
|
||||
$url =~ s/opac-rss\.pl.*//;
|
||||
$url =~ /(http:\/\/.*?)\//;
|
||||
my $short_url=$1;
|
||||
|
||||
my $RDF_update_needed=1;
|
||||
my ($year,$month,$day, $hour,$min,$sec) = Today_and_Now();
|
||||
|
||||
if (-e "rss/$filename") {
|
||||
$rss->parsefile("rss/$filename");
|
||||
# check if we have to rebuild the RSS feed (once every 30mn), or just return the actual rdf
|
||||
my $rdf_stamp = $rss->{'channel'}->{'dc'}->{'date'};
|
||||
$rdf_stamp =~ /(.*)-(.*)-(.*):(.*):(.*):(.*)/;
|
||||
my ($stamp_year,$stamp_month,$stamp_day,$stamp_hour,$stamp_min,$stamp_sec) = ($1,$2,$3,$4,$5,$6);
|
||||
# if less than 30 mn since the last RDF update, rebuild the RDF. Otherwise, just return it
|
||||
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)) {
|
||||
$RDF_update_needed =0;
|
||||
}
|
||||
}
|
||||
|
||||
if ($RDF_update_needed) {
|
||||
# warn "RDF update in progress";
|
||||
$rss->channel(
|
||||
title => "Koha : $query",
|
||||
description => C4::Context->preference("LibraryName"),
|
||||
link => $short_url,
|
||||
dc => {
|
||||
date => "$year-$month-$day:$hour:$min:$sec",
|
||||
subject => "Koha",
|
||||
creator => C4::Context->preference("LibraryName"),
|
||||
rights => "Copyright $year" ,
|
||||
language => C4::Context->preference("opaclanguages"),
|
||||
},
|
||||
);
|
||||
|
||||
my $total; # the total results for the whole set
|
||||
my ($error, $marcresults) = SimpleSearch($query);
|
||||
|
||||
my $hits = scalar @$marcresults;
|
||||
$hits=$size if $hits > $size;
|
||||
my @results;
|
||||
for(my $i=0;$i<$hits;$i++) {
|
||||
my %resultsloop;
|
||||
my $marcrecord = MARC::File::USMARC::decode($marcresults->[$i]);
|
||||
my $biblio = TransformMarcToKoha(C4::Context->dbh,$marcrecord,'');
|
||||
# check if the entry is already in the feed. Otherwise, pop the $line th line and add this new one.
|
||||
my $already_in_feed=0;
|
||||
foreach (@{$rss->{'items'}}) {
|
||||
if ($_->{'link'} =~ /biblionumber=$biblio->{'biblionumber'}/) {
|
||||
$already_in_feed=1;
|
||||
}
|
||||
}
|
||||
unless ($already_in_feed) {
|
||||
pop(@{$rss->{'items'}}) if (@{$rss->{'items'}} >= $size);
|
||||
$rss->add_item(title => $biblio->{'title'},
|
||||
description => $biblio->{'author'},
|
||||
link => "$url/opac-detail.pl?biblionumber=".$biblio->{'biblionumber'},
|
||||
mode =>'insert',
|
||||
);
|
||||
}
|
||||
}
|
||||
# save the rss feed.
|
||||
$rss->save("rss/$filename");
|
||||
} else {
|
||||
# warn "RDF CACHE used"
|
||||
}
|
||||
print $cgi->header(-type => "application/rss+xml");
|
||||
print $rss->as_string;
|
||||
|
|
@ -440,16 +440,15 @@ my $facets
|
|||
my @results_array;
|
||||
my $results_hashref;
|
||||
|
||||
eval {
|
||||
|
||||
( $error, $results_hashref, $facets ) = getRecords(
|
||||
$koha_query, $federated_query, \@sort_by,
|
||||
\@servers, $results_per_page, $offset,
|
||||
$expanded_facet, $branches, $query_type,
|
||||
$scan
|
||||
);
|
||||
|
||||
};
|
||||
if (C4::Context->preference('NoZebra')) {
|
||||
eval {
|
||||
($error, $results_hashref, $facets) = NZgetRecords($koha_query,$federated_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
|
||||
};
|
||||
} else {
|
||||
eval {
|
||||
($error, $results_hashref, $facets) = getRecords($koha_query,$federated_query,\@sort_by,\@servers,$results_per_page,$offset,$expanded_facet,$branches,$query_type,$scan);
|
||||
};
|
||||
}
|
||||
if ( $@ || $error ) {
|
||||
$template->param( query_error => $error . $@ );
|
||||
|
||||
|
|
@ -550,6 +549,9 @@ my $RequestOnOpac;
|
|||
if (C4::Context->preference("RequestOnOpac")) {
|
||||
$RequestOnOpac = 1;
|
||||
}
|
||||
# get site URL (for RSS link)
|
||||
$cgi->url() =~ /(.*)\/(.*)/;
|
||||
my $site_url = $1;
|
||||
|
||||
$template->param(
|
||||
|
||||
|
|
@ -563,6 +565,8 @@ $template->param(
|
|||
scan_use => $scan,
|
||||
search_error => $error,
|
||||
RequestOnOpac => $RequestOnOpac,
|
||||
RSS=> 1,
|
||||
site_url => $site_url,
|
||||
);
|
||||
## Now let's find out if we have any supplemental data to show the user
|
||||
# and in the meantime, save the current query for statistical purposes, etc.
|
||||
|
|
|
|||
Loading…
Reference in New Issue