Bug 8209 follow-up: fix failing test in AuthorityFile suggestion plugin
[koha.git] / Koha / SuggestionEngine / Plugin / AuthorityFile.pm
1 package Koha::SuggestionEngine::Plugin::AuthorityFile;
2
3 # Copyright 2012 C & P Bibliography Services
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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 =head1 NAME
21
22 Koha::SuggestionEngine::Plugin::AuthorityFile - get suggestions from the authority file
23
24 =head1 SYNOPSIS
25
26
27 =head1 DESCRIPTION
28
29 Plugin to get suggestions from Koha's authority file
30
31 =cut
32
33 use strict;
34 use warnings;
35 use Carp;
36
37 use base qw(Koha::SuggestionEngine::Base);
38 our $NAME    = 'AuthorityFile';
39 our $VERSION = '1.0';
40
41 =head2 get_suggestions
42
43     my $suggestions = $plugin->get_suggestions(\%param);
44
45 Return suggestions for the specified search by searching for the
46 search terms in the authority file and returning the results.
47
48 =cut
49
50 sub get_suggestions {
51     my $self  = shift;
52     my $param = shift;
53
54     my $search = $param->{'search'};
55
56     # Remove any CCL. This does not handle CQL or PQF, which is unfortunate,
57     # but what can you do? At some point the search will have to be passed
58     # not as a string but as some sort of data structure, at which point it
59     # will be possible to support multiple search syntaxes.
60     $search =~ s/ccl=//;
61     $search =~ s/\w*[:=](\w*)/$1/g;
62
63     my @marclist  = ['mainentry'];
64     my @and_or    = ['and'];
65     my @excluding = [];
66     my @operator  = ['any'];
67     my @value     = ["$search"];
68
69     # FIXME: calling into C4
70     require C4::AuthoritiesMarc;
71     my ( $searchresults, $count ) = C4::AuthoritiesMarc::SearchAuthorities(
72         \@marclist,  \@and_or, \@excluding,       \@operator,
73         @value,      0,        $param->{'count'}, '',
74         'Relevance', 0
75     );
76
77     my @results;
78     foreach my $auth (@$searchresults) {
79         push @results,
80           {
81             'search'  => "an=$auth->{'authid'}",
82             relevance => $count--,
83             label     => $auth->{summary}->{authorized}->[0]
84           };
85     }
86     return \@results;
87 }