1 package Koha::Biblio::Metadata;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Scalar::Util qw( blessed );
23 use C4::Biblio qw( GetMarcFromKohaField );
24 use C4::Charset qw( StripNonXmlChars );
25 use C4::Items qw( GetMarcItem );
27 use Koha::Exceptions::Metadata;
29 use base qw(Koha::Object);
33 Koha::Metadata - Koha Metadata Object class
43 my $record = $metadata->record;
45 Returns an object representing the metadata record. The expected record type
46 corresponds to this table:
48 -------------------------------
49 | format | object type |
50 -------------------------------
51 | marcxml | MARC::Record |
52 -------------------------------
54 $record = $biblio->metadata->record({
57 itemnumbers => $itemnumbers,
62 Koha::Biblio::Metadata::record(
66 biblionumber => $biblionumber,
67 itemnumbers => $itemnumbers,
72 Given a MARC::Record object containing a bib record,
73 modify it to include the items attached to it as 9XX
74 per the bib's MARC framework.
75 if $itemnumbers is defined, only specified itemnumbers are embedded.
77 If $opac is true, then opac-relevant suppressions are included.
79 If opac filtering will be done, patron should be passed to properly
80 override if necessary.
87 =item If an unsupported format is found, it throws a I<Koha::Exceptions::Metadata> exception.
89 =item If it fails to create the record object, it throws a I<Koha::Exceptions::Metadata::Invalid> exception.
97 my ($self, $params) = @_;
99 my $record = $params->{record};
100 my $embed_items = $params->{embed_items};
101 my $format = blessed($self) ? $self->format : $params->{format};
102 $format ||= 'marcxml';
104 if ( !$record && !blessed($self) ) {
105 Koha::Exceptions::Metadata->throw(
106 'Koha::Biblio::Metadata->record must be called on an instantiated object or like a class method with a record passed in parameter'
110 if ( $format eq 'marcxml' ) {
111 $record ||= eval { MARC::Record::new_from_xml( $self->metadata, 'UTF-8', $self->schema ); };
112 my $marcxml_error = $@;
113 chomp $marcxml_error;
116 Koha::Exceptions::Metadata::Invalid->throw(
118 biblionumber => $self->biblionumber,
119 format => $self->format,
120 schema => $self->schema,
121 decoding_error => $marcxml_error,
126 Koha::Exceptions::Metadata->throw(
127 'Koha::Biblio::Metadata->record called on unhandled format: ' . $format );
130 if ( $embed_items ) {
131 $self->_embed_items({ %$params, format => $format, record => $record });
137 =head3 record_strip_nonxml
139 my $record = $metadata->record_strip_nonxml;
141 This subroutine is intended for cases where we encounter a record that cannot be parsed, but want
142 to make a good effort to present the record (for harvesting, deletion, editing) rather than throwing
145 Will return undef if the record cannot be built
149 sub record_strip_nonxml {
151 my ( $self, $params ) = @_;
158 $record = MARC::Record->new_from_xml(
159 StripNonXmlChars( $self->metadata ), 'UTF-8',
165 chomp $marcxml_error;
170 return $self->record( { %$params, record => $record } );
173 =head2 Internal methods
180 my ( $self, $params ) = @_;
182 my $record = $params->{record};
183 my $format = $params->{format};
184 my $biblionumber = $params->{biblionumber} || $self->biblionumber;
185 my $itemnumbers = $params->{itemnumbers} // [];
186 my $patron = $params->{patron};
187 my $opac = $params->{opac};
189 if ( $format eq 'marcxml' ) {
191 # First remove the existing items from the MARC record
192 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
193 foreach my $field ( $record->field($itemtag) ) {
194 $record->delete_field($field);
197 my $biblio = Koha::Biblios->find($biblionumber);
199 my $items = $biblio->items;
200 if ( @$itemnumbers ) {
201 $items = $items->search({ itemnumber => { -in => $itemnumbers } });
204 $items = $items->filter_by_visible_in_opac({ patron => $patron });
206 my @itemnumbers = $items->get_column('itemnumber');
208 for my $itemnumber ( @itemnumbers ) {
209 my $item_marc = C4::Items::GetMarcItem( $biblionumber, $itemnumber );
210 push @item_fields, $item_marc->field($itemtag);
212 $record->insert_fields_ordered( reverse @item_fields );
213 # insert_fields_ordered with the reverse keeps 952s in right order
217 Koha::Exceptions::Metadata->throw(
218 'Koha::Biblio::Metadata->embed_item called on unhandled format: ' . $format );
230 return 'BiblioMetadata';