From 9ecbc043a9daf80792c3f4b3aab0099581e7c8ac Mon Sep 17 00:00:00 2001 From: Julian Maurice Date: Tue, 2 Nov 2021 14:36:49 +0100 Subject: [PATCH] Bug 29392: Add plugin hooks before merging biblios or authorities Hooks are called 'before_biblio_action' and 'before_authority_action' and they both take an 'action' parameter whose value is 'merge'. Other parameters include the record id (biblionumber or authid), the object itself (Koha::Biblio or Koha::Authority), the MARC::Record about to be saved (so plugins can modify it), and the list of record ids that will be deleted after the merge. Test plan: 1. Create a plugin with these two methods. For instance: sub before_biblio_action { my ($self, $args) = @_; my $action = $args->{action}; return unless $action eq 'merge'; my $record = $args->{record}; my $merged_biblio_ids = $args->{merged_biblio_ids}; if ($record && $merged_biblio_ids) { my @fields = map { MARC::Field->new('035', ' ', ' ', 'z' => $_) } @$merged_biblio_ids; $record->insert_fields_ordered(@fields); } } sub before_authority_action { my ($self, $args) = @_; my $action = $args->{action}; return unless $action eq 'merge'; my $record = $args->{record}; my $merged_authority_ids = $args->{merged_authority_ids}; if ($record && $merged_authority_ids) { my @fields = map { MARC::Field->new('035', ' ', ' ', 'z' => $_) } @$merged_authority_ids; $record->insert_fields_ordered(@fields); } } This will copy the identifier of deleted records inside 035$z of the final record. (035$z is for "Canceled/invalid system control number") 2. Add the plugin path to your $KOHA_CONF 3. Run misc/devel/install_plugins.pl. It should say: Installed version 4. Apply patch and restart starman 5. Create two or more biblios with the same title. Make sure they are indexed in your search engine. 6. Search for them. In the results check their corresponding boxes and click on "Edit -> Merge records" 7. Merge them and verify that your plugin code was called. If you used the example above, verify that it added a 035$z for each deleted record. 8. Do the same for authorities --- authorities/merge.pl | 14 ++++++++++++++ cataloguing/merge.pl | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/authorities/merge.pl b/authorities/merge.pl index 0799c26b04..60888d390a 100755 --- a/authorities/merge.pl +++ b/authorities/merge.pl @@ -24,9 +24,11 @@ use C4::Auth qw( get_template_and_user ); use C4::AuthoritiesMarc qw( GetAuthority ModAuthority DelAuthority GetTagsLabels merge ); use C4::Biblio qw( TransformHtmlToMarc ); +use Koha::Authorities; use Koha::Authority::MergeRequests; use Koha::Authority::Types; use Koha::MetadataRecord::Authority; +use Koha::Plugins; my $input = CGI->new; my @authid = $input->multi_param('authid'); @@ -73,6 +75,18 @@ if ($merge) { $record->leader( $authrec->leader() ); } + my $authority = Koha::Authorities->find($recordid1); + Koha::Plugins->call( + 'before_authority_action', + { + action => 'merge', + authority => $authority, + authority_id => $recordid1, + record => $record, + merged_authority_ids => [$recordid2], + } + ); + # Modifying the reference record # This triggers a merge for the biblios attached to $recordid1 ModAuthority( $recordid1, $record, $typecode ); diff --git a/cataloguing/merge.pl b/cataloguing/merge.pl index fca94ca4d5..c57773c83f 100755 --- a/cataloguing/merge.pl +++ b/cataloguing/merge.pl @@ -41,6 +41,7 @@ use Koha::BiblioFrameworks; use Koha::Biblios; use Koha::Items; use Koha::MetadataRecord; +use Koha::Plugins; my $input = CGI->new; my @biblionumbers = $input->multi_param('biblionumber'); @@ -69,6 +70,18 @@ if ($merge) { my $ref_biblionumber = $input->param('ref_biblionumber'); @biblionumbers = grep { $_ != $ref_biblionumber } @biblionumbers; + my $biblio = Koha::Biblios->find($ref_biblionumber); + Koha::Plugins->call( + 'before_biblio_action', + { + action => 'merge', + biblio => $biblio, + biblio_id => $ref_biblionumber, + record => $record, + merged_biblio_ids => \@biblionumbers + } + ); + # prepare report my @report_records; my $report_fields_str = $input->param('report_fields'); @@ -93,7 +106,7 @@ if ($merge) { ModBiblio($record, $ref_biblionumber, $frameworkcode); # Moving items and article requests from the other record to the reference record - my $biblio = Koha::Biblios->find($ref_biblionumber); + $biblio = Koha::Biblios->find($ref_biblionumber); foreach my $biblionumber (@biblionumbers) { my $from_biblio = Koha::Biblios->find($biblionumber); $from_biblio->items->move_to_biblio($biblio); -- 2.20.1