Bug 26922: Regression tests
[koha.git] / Koha / SuggestionEngine / Plugin / LibrisSpellcheck.pm
blob5356276922dbe0f4b9a4f348b3ec676d9adfdb11
1 package Koha::SuggestionEngine::Plugin::LibrisSpellcheck;
2 # Copyright (C) 2015 Eivin Giske Skaaren
4 # This file is part of Koha.
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19 use Modern::Perl;
20 use LWP::UserAgent;
21 use XML::Simple qw(XMLin);
22 use C4::Context;
23 use base qw(Koha::SuggestionEngine::Base);
25 sub NAME {
26 return 'LibrisSpellcheck';
29 sub get_suggestions {
30 my ($self, $query) = @_;
31 my $key = C4::Context->preference('LibrisKey');
32 my $base = C4::Context->preference('LibrisURL');
33 my $search = $query->{'search'};
34 my $response = LWP::UserAgent->new->get($base."spell?query={$search}&key=$key");
35 my $xml = XMLin($response->content, NoAttr => 1, ForceArray => qr/term/);
37 my @terms;
38 my $label;
40 if ($xml->{suggestion}->{term}) {
41 for (@{$xml->{suggestion}->{term}}) {
42 push @terms, $_;
44 $label = join(' ', @terms);
45 } else {
46 return; # No result from LIBRIS
49 my @results;
50 push @results,
52 'search' => $label, #$thissearch,
53 relevance => 100,
54 # FIXME: it'd be nice to have some empirical measure of
55 # "relevance" in this case, but we don't.
56 label => $label
58 return \@results;
62 __END__
64 =head1 NAME
66 Koha::SuggestionEngine::Plugin::LibrisSpellcheck
68 =head2 FUNCTIONS
70 This module provides facilities for using the LIBRIS spell checker API
72 =over
74 =item NAME
76 my $name = $plugin->NAME;
78 =back
80 =over
82 =item get_suggestions(query)
84 Sends in the search query and gets an XML with a suggestion
86 my $suggestions = $plugin->get_suggestions(\%query);
88 =back
90 =cut
92 =head1 NOTES
94 =cut
96 =head1 AUTHOR
98 Eivin Giske Skaaren <eskaaren@yahoo.no>
100 =cut