|
Line 0
Link Here
|
|
|
1 |
package Koha::Filter::MARC::EmbedRecordSource; |
| 2 |
|
| 3 |
# Copyright 2024 BibLibre |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
=head1 NAME |
| 21 |
|
| 22 |
Koha::Filter::MARC::EmbedRecordSource - embeds record source into MARC for indexing |
| 23 |
|
| 24 |
=head1 SYNOPSIS |
| 25 |
|
| 26 |
|
| 27 |
=head1 DESCRIPTION |
| 28 |
|
| 29 |
Filter to embed record source into MARC records. |
| 30 |
|
| 31 |
=cut |
| 32 |
|
| 33 |
use strict; |
| 34 |
use warnings; |
| 35 |
use Koha::Biblios; |
| 36 |
|
| 37 |
use base qw(Koha::RecordProcessor::Base); |
| 38 |
our $NAME = 'EmbedRecordSource'; |
| 39 |
|
| 40 |
=head2 filter |
| 41 |
|
| 42 |
my $newrecord = $filter->filter($record); |
| 43 |
my $newrecords = $filter->filter(\@records); |
| 44 |
|
| 45 |
Embed see from headings into the specified record(s) and return the result. |
| 46 |
In order to differentiate added headings from actual headings, a 'z' is |
| 47 |
put in the first indicator. |
| 48 |
|
| 49 |
=cut |
| 50 |
sub filter { |
| 51 |
my $self = shift; |
| 52 |
my $record = shift; |
| 53 |
my $newrecord; |
| 54 |
|
| 55 |
return unless defined $record; |
| 56 |
|
| 57 |
if (ref $record eq 'ARRAY') { |
| 58 |
my @recarray; |
| 59 |
foreach my $thisrec (@$record) { |
| 60 |
push @recarray, $self->_processrecord($thisrec); |
| 61 |
} |
| 62 |
$newrecord = \@recarray; |
| 63 |
} elsif (ref $record eq 'MARC::Record') { |
| 64 |
$newrecord = $self->_processrecord($record); |
| 65 |
} |
| 66 |
|
| 67 |
return $newrecord; |
| 68 |
} |
| 69 |
|
| 70 |
sub _processrecord { |
| 71 |
my ($self, $record) = @_; |
| 72 |
|
| 73 |
$record->append_fields($self->fields($record)); |
| 74 |
|
| 75 |
return $record; |
| 76 |
} |
| 77 |
|
| 78 |
=head2 fields |
| 79 |
|
| 80 |
my @fields = $filter->fields($record); |
| 81 |
|
| 82 |
Retrieve the fields that would be embedded if the record were processed by the filter. |
| 83 |
Used during Elasticsearch indexing to give special treatment to these field (i.e. don't |
| 84 |
include in facets, sorting, or suggestion fields) |
| 85 |
|
| 86 |
=cut |
| 87 |
sub fields { |
| 88 |
my ($self, $record) = @_; |
| 89 |
my @newfield; |
| 90 |
|
| 91 |
my $biblionumber = $record->field('999')->subfield('c'); |
| 92 |
return @newfield unless $biblionumber; |
| 93 |
|
| 94 |
my $biblio = Koha::Biblios->find($biblionumber); |
| 95 |
return @newfield unless $biblio; |
| 96 |
|
| 97 |
my $source = $biblio->metadata->record_source; |
| 98 |
return @newfield unless $source; |
| 99 |
|
| 100 |
my $marc_source = MARC::Field->new('942', 'z', ' ', 'z' => $source->name ); |
| 101 |
push @newfield, $marc_source if (scalar($marc_source->subfields()) > 0); |
| 102 |
return @newfield; |
| 103 |
} |
| 104 |
|
| 105 |
1; |