From a57191f203fc95ea12319c2cb8feba82bd588e8f Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Tue, 16 Apr 2024 14:29:08 +0000 Subject: [PATCH] Bug 30888: Copy info to deleted table Test plan: Delete an authority from the interface. Check presence in the table deletedauth_header, verify that the correct authid and authtype have been inserted. Bonus: Check leader position 05 in deleted table. Signed-off-by: Marcel de Rooy Signed-off-by: Victor Grousset/tuxayo --- C4/AuthoritiesMarc.pm | 45 +++++++++++++++++++++++++++++-------------- Koha/Authority.pm | 24 +++++++++++++++++++++++ 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm index 1656a2ba9b..6fc73cc09a 100644 --- a/C4/AuthoritiesMarc.pm +++ b/C4/AuthoritiesMarc.pm @@ -21,6 +21,8 @@ package C4::AuthoritiesMarc; use strict; use warnings; use MARC::Field; +use Scalar::Util qw(blessed); +use Try::Tiny qw( try catch ); use C4::Context; use C4::Biblio qw( ModBiblio ); @@ -34,6 +36,7 @@ use Koha::Authorities; use Koha::Authority::MergeRequests; use Koha::Authority::Types; use Koha::Authority; +use Koha::Database; use Koha::Libraries; use Koha::RecordProcessor; use Koha::SearchEngine; @@ -734,21 +737,35 @@ sub DelAuthority { my $skip_merge = $params->{skip_merge}; my $skip_record_index = $params->{skip_record_index} || 0; - my $dbh = C4::Context->dbh; - - # Remove older pending merge requests for $authid to itself. (See bug 22437) - my $condition = { authid => $authid, authid_new => [undef, 0, $authid], done => 0 }; - Koha::Authority::MergeRequests->search($condition)->delete; - - merge({ mergefrom => $authid }) if !$skip_merge; - $dbh->do( "DELETE FROM auth_header WHERE authid=?", undef, $authid ); - logaction( "AUTHORITIES", "DELETE", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog"); - unless ( $skip_record_index ) { - my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX }); - $indexer->index_records( $authid, "recordDelete", "authorityserver", undef ); - } + my $schema = Koha::Database->schema; + try { + # TODO Make following lines part of transaction? Does merge take too long? + # Remove older pending merge requests for $authid to itself. (See bug 22437) + my $condition = { authid => $authid, authid_new => [ undef, 0, $authid ], done => 0 }; + Koha::Authority::MergeRequests->search($condition)->delete; + merge( { mergefrom => $authid } ) if !$skip_merge; + + my $authority = Koha::Authorities->find($authid); + $schema->txn_do( + sub { + $authority->move_to_deleted; #FIXME We should define 'move' .. + $authority->delete; + } + ); - _after_authority_action_hooks({ action => 'delete', authority_id => $authid }); + logaction( "AUTHORITIES", "DELETE", $authid, "authority" ) if C4::Context->preference("AuthoritiesLog"); + unless ($skip_record_index) { + my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::AUTHORITIES_INDEX } ); + $indexer->index_records( $authid, "recordDelete", "authorityserver", undef ); + } + _after_authority_action_hooks( { action => 'delete', authority_id => $authid } ); + } catch { + if ( blessed $_ && $_->can('rethrow') ) { + $_->rethrow(); + } else { + die "Deleting authority $authid failed: " . $_; + } + }; } =head2 ModAuthority diff --git a/Koha/Authority.pm b/Koha/Authority.pm index 5cfc806b09..7fa444e89a 100644 --- a/Koha/Authority.pm +++ b/Koha/Authority.pm @@ -308,6 +308,30 @@ sub to_api_mapping { }; } +=head3 move_to_deleted + + $authority->move_to_deleted; + + This sub actually copies the authority (to be deleted) into the + deletedauth_header table. (Just as the other ones.) + +=cut + +sub move_to_deleted { + my ($self) = @_; + my $data = $self->unblessed; + delete $data->{modification_time}; # trigger new timestamp + + # Set leader 05 (deleted) + my $format = C4::Context->preference('marcflavour') eq 'UNIMARC' ? 'UNIMARCAUTH' : 'MARC21'; + my $record = $self->record; + $record->leader( substr( $record->leader, 0, 5 ) . 'd' . substr( $record->leader, 6, 18 ) ); + $data->{marc} = $record->as_usmarc; + $data->{marcxml} = $record->as_xml_record($format); + + return Koha::Database->new->schema->resultset('DeletedauthHeader')->create($data); +} + =head2 Internal methods =head3 _type -- 2.47.0