From aff265a76fa9b6bcd4b832a26eab845b80b8d9f8 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 12 Mar 2026 10:48:13 +0000 Subject: [PATCH] Bug 40135: (follow-up) Strip undef and empty fields from biblio log data Add _unblessed_for_log to Koha::Biblio, mirroring the same method in Koha::Patron (bug 40136). It excludes undef/empty-string columns and the auto-updated timestamp field so that biblio diffs only contain meaningful values. Update AddBiblio, ModBiblio and DelBiblio in C4/Biblio.pm to use _unblessed_for_log instead of raw unblessed when building the hashrefs passed to logaction, eliminating noise from null columns in ADD/MODIFY/DELETE diffs. Signed-off-by: Lisette Scheer --- C4/Biblio.pm | 8 ++++---- Koha/Biblio.pm | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index d52ac44ab3c..d1578d9a62b 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -315,7 +315,7 @@ sub AddBiblio { my $marc_for_log = eval { $new_biblio->metadata->record_strip_nonxml }; logaction( "CATALOGUING", "ADD", $biblionumber, "biblio", undef, - { %{ $new_biblio->unblessed }, _marc => _marc_record_to_diffable($marc_for_log) } + { %{ $new_biblio->_unblessed_for_log }, _marc => _marc_record_to_diffable($marc_for_log) } ); } @@ -405,7 +405,7 @@ sub ModBiblio { my ( $original, $original_marc ); if ( C4::Context->preference("CataloguingLog") ) { my $pre_biblio = Koha::Biblios->find($biblionumber); - $original = $pre_biblio->unblessed; + $original = $pre_biblio->_unblessed_for_log; $original_marc = eval { $pre_biblio->metadata->record_strip_nonxml }; } @@ -474,7 +474,7 @@ sub ModBiblio { my $updated_marc_log = eval { $updated_biblio->metadata->record_strip_nonxml }; logaction( "CATALOGUING", "MODIFY", $biblionumber, - { %{ $updated_biblio->unblessed }, _marc => _marc_record_to_diffable($updated_marc_log) }, + { %{ $updated_biblio->_unblessed_for_log }, _marc => _marc_record_to_diffable($updated_marc_log) }, undef, { %{$original}, _marc => _marc_record_to_diffable($original_marc) } ); @@ -627,7 +627,7 @@ sub DelBiblio { logaction( "CATALOGUING", "DELETE", $biblionumber, "biblio", undef, - { %{ $biblio->unblessed }, _marc => _marc_record_to_diffable($biblio_marc) } + { %{ $biblio->_unblessed_for_log }, _marc => _marc_record_to_diffable($biblio_marc) } ) if C4::Context->preference("CataloguingLog"); Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( { biblio_ids => [$biblionumber] } ) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 1f3f0b45e13..7e8dfeb706d 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -20,6 +20,7 @@ package Koha::Biblio; use Modern::Perl; use List::MoreUtils qw( any ); +use Scalar::Util qw( blessed ); use URI; use URI::Escape qw( uri_escape_utf8 ); use Try::Tiny; @@ -2402,6 +2403,29 @@ sub merge_with { =head2 Internal methods +=head3 _unblessed_for_log + +Returns a plain hashref of biblio column values safe for JSON serialisation +and action log diffing. Timestamp is excluded as it changes on every write, +and undef/empty-string fields are removed so that genuinely unset columns +do not produce noise in diffs. + +=cut + +sub _unblessed_for_log { + my ($self) = @_; + my $data = $self->unblessed; + delete $data->{timestamp}; + for my $key ( keys %$data ) { + if ( defined $data->{$key} && $data->{$key} ne '' ) { + $data->{$key} = "$data->{$key}" if blessed( $data->{$key} ); + } else { + delete $data->{$key}; + } + } + return $data; +} + =head3 type =cut -- 2.39.5