From f5eb93a55ce47c237bf7afe216302b13b0c276bd Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Tue, 3 Mar 2015 14:02:19 +1300 Subject: [PATCH] Bug 12478: more authorities Signed-off-by: Nick Clemens Signed-off-by: Jesse Weaver Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Brendan Gallagher --- Koha/SearchEngine/Elasticsearch/Search.pm | 54 ++++++++++++++++++----- Koha/SearchEngine/QueryBuilder.pm | 2 + 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/Koha/SearchEngine/Elasticsearch/Search.pm b/Koha/SearchEngine/Elasticsearch/Search.pm index c1d3adce89..33644a475e 100644 --- a/Koha/SearchEngine/Elasticsearch/Search.pm +++ b/Koha/SearchEngine/Elasticsearch/Search.pm @@ -150,26 +150,56 @@ sub search_auth_compat { my $self = shift; my $database = Koha::Database->new(); - my $schema = $database->schema(); - my $res = $self->search(@_); + my $schema = $database->schema(); + my $res = $self->search(@_); my @records; - $res->each(sub { + $res->each( + sub { my %result; - my $record = @_[0]; + my $record = @_[0]; my $marc_json = $record->{record}; + # I wonder if these should be real values defined in the mapping # rather than hard-coded conversions. - $result{authid} = $record{Local-Number}; + $result{authid} = $record->{ Local-Number }; + # TODO put all this info into the record at index time so we # don't have to go and sort it all out now. - my $rs = $schema->resultset('auth_types')->search({ authtypecode => $authtypecode }); - my $authtype = $rs->first; - my $authtypecode = $record{authtype}; - my $marc = $self->json2marc($marc_json); - die Dumper(\@_); + my $authtypecode = $record->{authtype}; + my $rs = $schema->resultset('AuthType') + ->search( { authtypecode => $authtypecode } ); + + # FIXME there's an assumption here that we will get a result. + # the original code also makes an assumption that some provided + # authtypecode may sometimes be used instead of the one stored + # with the record. It's not documented why this is the case, so + # it's not reproduced here. + my $authtype = $rs->single; + my $auth_tag_to_report = $authtype->auth_tag_to_report; + my $marc = $self->json2marc($marc_json); + my $mainentry = $marc->field($auth_tag_to_report); + my $reported_tag; + if ($mainentry) { + foreach ( $mainentry->subfields() ) { + $reported_tag .= '$' . $_->[0] . $_->[1]; + } + } + # Turn the resultset into a hash + my %authtype_cols; + foreach my $col (@{ $authtype->result_source->columns }) { + $authtype_cols{$col} = $authtype->get_column($col); + } + $result{authtype} = $authtype_cols; + $result{reported_tag} = $reported_tag; + + # Reimplementing BuildSummary is out of scope because it'll be hard + $result{summary} = + C4::AuthoritiesMarc::BuildSummary( $marc, $result{authid}, + $authtypecode ); push @records, $marc; - }); - return (\@records, $res->total); + } + ); + return ( \@records, $res->total ); } =head2 json2marc diff --git a/Koha/SearchEngine/QueryBuilder.pm b/Koha/SearchEngine/QueryBuilder.pm index aaaba011af..9a9b712822 100644 --- a/Koha/SearchEngine/QueryBuilder.pm +++ b/Koha/SearchEngine/QueryBuilder.pm @@ -45,11 +45,13 @@ Creates a new C of whatever the relevant type is. use C4::Context; use Modern::Perl; +use Carp; sub new { my $engine = C4::Context->preference("SearchEngine"); my $file = "Koha/SearchEngine/${engine}/QueryBuilder.pm"; my $class = "Koha::SearchEngine::${engine}::QueryBuilder"; + croak "SearchEngine system preference not set" unless $engine; require $file; shift @_; return $class->new(@_); -- 2.39.5