From 7d0152772b93fc6742fc47bd2ecd69a21ce6b78c Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 6 Dec 2023 11:49:43 +0000 Subject: [PATCH] Bug 35104: [Alternative] Throw exception on store of invalid marcxml This patch adds an exception Koha::Biblio::Metadata->store to prevent erroneous characters in marcxml being stored into the database. We also remove the cleanup of such characters from TransformHtmlToMarc as introduced by bug 34549. This should result in there being no way, outside of direct database manipulation, of getting forbidden characters into our marcxml store. We'll need to work through the codebase to catch the exception thrown to give the end user the oportunities to act on the error and fix the data at source. --- C4/Biblio.pm | 3 --- Koha/Biblio/Metadata.pm | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index ab895b25753..4b3c5582fdf 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -93,7 +93,6 @@ use C4::Charset qw( nsb_clean SetMarcUnicodeFlag SetUTF8Flag - StripNonXmlChars ); use C4::Languages; use C4::Linker; @@ -2248,7 +2247,6 @@ sub TransformHtmlToMarc { ; # between 001 and 009 (included) } elsif ( $fval ne '' ) { - $fval = StripNonXmlChars($fval); #Strip out any non-XML characters like control characters $newfield = MARC::Field->new( $tag, $fval, ); } @@ -2270,7 +2268,6 @@ sub TransformHtmlToMarc { $newfield->add_subfields( $fkey => $fval); } elsif($fval ne '') { - $fval = StripNonXmlChars($fval); #Strip out any non-XML characters like control characters $newfield = MARC::Field->new( $tag, $ind1, $ind2, $fkey => $fval ); } $j += 2; diff --git a/Koha/Biblio/Metadata.pm b/Koha/Biblio/Metadata.pm index d5bfea5cd00..f9617c40bdc 100644 --- a/Koha/Biblio/Metadata.pm +++ b/Koha/Biblio/Metadata.pm @@ -38,6 +38,42 @@ Koha::Metadata - Koha Metadata Object class =cut +=head3 store + +Metadata specific store method to catch errant characters prior +to commiting to the database. + +=cut + +sub store { + my $self = shift; + + # Check marcxml will roundtrip + if ( $self->format eq 'marcxml' ) { + + my $marcxml = eval { + MARC::Record::new_from_xml( + $self->metadata, 'UTF-8', + $self->schema + ); + }; + my $marcxml_error = $@; + chomp $marcxml_error; + unless ($marcxml) { + warn $marcxml_error; + Koha::Exceptions::Metadata::Invalid->throw( + id => $self->id, + biblionumber => $self->biblionumber, + format => $self->format, + schema => $self->schema, + decoding_error => $marcxml_error, + ); + } + } + + return $self->SUPER::store; +} + =head3 record my $record = $metadata->record; -- 2.43.0