From 5167a800c28cb8371bb4a8174decf1103a764706 Mon Sep 17 00:00:00 2001
From: Shi Yao Wang <shi-yao.wang@inlibro.com>
Date: Tue, 18 Feb 2025 09:26:32 -0500
Subject: [PATCH] Bug 39156: Add hooks before and after batch on
 authority/biblio/item

Useful to dump marc files after batch operation for example

Signed-off-by: Matthias Meusburger <matthias.meusburger@biblibre.com>
---
 C4/ImportBatch.pm                          |  3 +++
 Koha/BackgroundJob.pm                      | 24 ++++++++++++++++++++++
 Koha/BackgroundJob/BatchDeleteAuthority.pm |  3 +++
 Koha/BackgroundJob/BatchDeleteBiblio.pm    |  3 +++
 Koha/BackgroundJob/BatchDeleteItem.pm      |  3 +++
 Koha/BackgroundJob/BatchUpdateAuthority.pm |  3 +++
 Koha/BackgroundJob/BatchUpdateBiblio.pm    |  3 +++
 Koha/BackgroundJob/BatchUpdateItem.pm      |  3 +++
 authorities/merge.pl                       |  3 +++
 cataloguing/additem.pl                     |  5 +++++
 misc/migration_tools/bulkmarcimport.pl     |  3 +++
 11 files changed, 56 insertions(+)

diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm
index b7b9635af9..58250c32a3 100644
--- a/C4/ImportBatch.pm
+++ b/C4/ImportBatch.pm
@@ -36,6 +36,7 @@ use C4::AuthoritiesMarc
     qw( AddAuthority GuessAuthTypeCode GetAuthorityXML ModAuthority DelAuthority GetAuthorizedHeading );
 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
 use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue;
+use Koha::BackgroundJob;
 use Koha::Items;
 use Koha::SearchEngine;
 use Koha::SearchEngine::Indexer;
@@ -566,6 +567,7 @@ sub BatchCommitRecords {
                              LEFT JOIN import_biblios ON (import_records.import_record_id=import_biblios.import_record_id)
                              WHERE import_batch_id = ?"
     );
+    Koha::BackgroundJob->before_batch_action_hooks();
     $sth->execute($batch_id);
     my $marcflavour = C4::Context->preference('marcflavour');
 
@@ -737,6 +739,7 @@ sub BatchCommitRecords {
     }
 
     $sth->finish();
+    Koha::BackgroundJob->after_batch_action_hooks();
 
     SetImportBatchStatus( $batch_id, 'imported' );
 
diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm
index fb1594a95a..6779a182b9 100644
--- a/Koha/BackgroundJob.pm
+++ b/Koha/BackgroundJob.pm
@@ -29,6 +29,7 @@ use Koha::Exceptions::BackgroundJob;
 use Koha::Plugins;
 
 use base qw( Koha::Object );
+use Koha::Plugins;
 
 =head1 NAME
 
@@ -543,4 +544,27 @@ sub _type {
     return 'BackgroundJob';
 }
 
+
+=head3 after_batch_action_hooks
+
+Helper method that takes care of calling all plugin hooks
+
+=cut
+
+sub after_batch_action_hooks {
+    my ( $self, $args ) = @_;
+    Koha::Plugins->call( 'after_batch_action', {} );
+}
+
+=head3 before_batch_action_hooks
+
+Helper method that takes care of calling all plugin hooks
+
+=cut
+
+sub before_batch_action_hooks {
+    my ( $self, $args ) = @_;
+    Koha::Plugins->call('before_batch_action', {} );
+}
+
 1;
diff --git a/Koha/BackgroundJob/BatchDeleteAuthority.pm b/Koha/BackgroundJob/BatchDeleteAuthority.pm
index bab15e1645..8b37655519 100644
--- a/Koha/BackgroundJob/BatchDeleteAuthority.pm
+++ b/Koha/BackgroundJob/BatchDeleteAuthority.pm
@@ -28,6 +28,8 @@ sub process {
     my $mmtid      = $args->{mmtid};
     my @record_ids = @{ $args->{record_ids} };
 
+    $self->before_batch_action_hooks();
+
     my $report = {
         total_records => scalar @record_ids,
         total_success => 0,
@@ -79,6 +81,7 @@ RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
     $data->{report}   = $report;
 
     $self->finish($data);
+    $self->after_batch_action_hooks();
 }
 
 sub enqueue {
diff --git a/Koha/BackgroundJob/BatchDeleteBiblio.pm b/Koha/BackgroundJob/BatchDeleteBiblio.pm
index 3bc7f7336e..9ff0a023a8 100644
--- a/Koha/BackgroundJob/BatchDeleteBiblio.pm
+++ b/Koha/BackgroundJob/BatchDeleteBiblio.pm
@@ -52,6 +52,8 @@ sub process {
     my $mmtid      = $args->{mmtid};
     my @record_ids = @{ $args->{record_ids} };
 
+    $self->before_batch_action_hooks();
+
     my $report = {
         total_records => scalar @record_ids,
         total_success => 0,
@@ -172,6 +174,7 @@ RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
     $data->{report}   = $report;
 
     $self->finish($data);
+    $self->after_batch_action_hooks();
 }
 
 =head3 enqueue
diff --git a/Koha/BackgroundJob/BatchDeleteItem.pm b/Koha/BackgroundJob/BatchDeleteItem.pm
index 6533d9dc1a..24c9d058c7 100644
--- a/Koha/BackgroundJob/BatchDeleteItem.pm
+++ b/Koha/BackgroundJob/BatchDeleteItem.pm
@@ -84,6 +84,8 @@ sub process {
     my @record_ids     = @{ $args->{record_ids} };
     my $delete_biblios = $args->{delete_biblios};
 
+    $self->before_batch_action_hooks();
+
     my $report = {
         total_records => scalar @record_ids,
         total_success => 0,
@@ -200,6 +202,7 @@ sub process {
     $data->{report}   = $report;
 
     $self->finish($data);
+    $self->after_batch_action_hooks();
 }
 
 =head3 enqueue
diff --git a/Koha/BackgroundJob/BatchUpdateAuthority.pm b/Koha/BackgroundJob/BatchUpdateAuthority.pm
index ef8d78d42e..0cbe817b08 100644
--- a/Koha/BackgroundJob/BatchUpdateAuthority.pm
+++ b/Koha/BackgroundJob/BatchUpdateAuthority.pm
@@ -64,6 +64,8 @@ sub process {
     my $mmtid      = $args->{mmtid};
     my @record_ids = @{ $args->{record_ids} };
 
+    $self->before_batch_action_hooks();
+
     my $report = {
         total_records => scalar @record_ids,
         total_success => 0,
@@ -106,6 +108,7 @@ RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
     $data->{report}   = $report;
 
     $self->finish($data);
+    $self->after_batch_action_hooks();
 
 }
 
diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm
index df6d53e7ad..b22ae8a46f 100644
--- a/Koha/BackgroundJob/BatchUpdateBiblio.pm
+++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm
@@ -70,6 +70,8 @@ sub process {
     my $mmtid      = $args->{mmtid};
     my @record_ids = @{ $args->{record_ids} };
 
+    $self->before_batch_action_hooks();
+
     my $report = {
         total_records => scalar @record_ids,
         total_success => 0,
@@ -133,6 +135,7 @@ RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
     $data->{report}   = $report;
 
     $self->finish($data);
+    $self->after_batch_action_hooks();
 }
 
 =head3 enqueue
diff --git a/Koha/BackgroundJob/BatchUpdateItem.pm b/Koha/BackgroundJob/BatchUpdateItem.pm
index 1158efd568..0993191a40 100644
--- a/Koha/BackgroundJob/BatchUpdateItem.pm
+++ b/Koha/BackgroundJob/BatchUpdateItem.pm
@@ -98,6 +98,8 @@ sub process {
     my $exclude_from_local_holds_priority = $args->{exclude_from_local_holds_priority};
     my $mark_items_returned               = $args->{mark_items_returned};
 
+    $self->before_batch_action_hooks();
+
     my $report = {
         total_records   => scalar @record_ids,
         modified_fields => 0,
@@ -125,6 +127,7 @@ sub process {
     $data->{report} = $report;
 
     $self->finish($data);
+    $self->after_batch_action_hooks();
 }
 
 =head3 enqueue
diff --git a/authorities/merge.pl b/authorities/merge.pl
index 3204b5c93a..7e1c0a7299 100755
--- a/authorities/merge.pl
+++ b/authorities/merge.pl
@@ -27,6 +27,7 @@ use C4::Biblio          qw( TransformHtmlToMarc );
 use Koha::Authority::MergeRequests;
 use Koha::Authority::Types;
 use Koha::MetadataRecord::Authority;
+use Koha::BackgroundJob;
 
 my $input  = CGI->new;
 my @authid = $input->multi_param('authid');
@@ -47,6 +48,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 # Merging
 #------------------------
 if ( $op eq 'cud-merge' ) {
+    Koha::BackgroundJob->before_batch_action_hooks();
 
     # Creating a new record from the html code
     my $record    = TransformHtmlToMarc( $input, 0 );
@@ -83,6 +85,7 @@ if ( $op eq 'cud-merge' ) {
 
     # Delete the other record. No need to merge.
     DelAuthority( { authid => $recordid2, skip_merge => 1 } );
+    Koha::BackgroundJob->after_batch_action_hooks();
 
     # Parameters
     $template->param(
diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl
index fecd4fa6a8..07db11b604 100755
--- a/cataloguing/additem.pl
+++ b/cataloguing/additem.pl
@@ -42,6 +42,7 @@ use Koha::Patrons;
 use Koha::SearchEngine::Indexer;
 use Koha::UI::Form::Builder::Item;
 use Koha::Result::Boolean;
+use Koha::BackgroundJob;
 
 use Encode          qw( encode_utf8 );
 use List::MoreUtils qw( any uniq );
@@ -407,6 +408,7 @@ if ( $op eq "cud-additem" ) {
         my $testbarcode;
         my $barcodeobj = C4::Barcodes->new;
         $testbarcode = $barcodeobj->next_value($oldbarcode) if $barcodeobj;
+        Koha::BackgroundJob->before_batch_action_hooks();
         if ( $oldbarcode && !$testbarcode ) {
 
             push @errors, "no_next_barcode";
@@ -484,6 +486,7 @@ if ( $op eq "cud-additem" ) {
 
             undef($current_item);
         }
+        Koha::BackgroundJob->after_batch_action_hooks();
     }
     if ( $frameworkcode eq 'FA' && $fa_circborrowernumber ) {
         print $input->redirect( '/cgi-bin/koha/circ/circulation.pl?'
@@ -546,10 +549,12 @@ if ( $op eq "cud-additem" ) {
 
     #-------------------------------------------------------------------------------
     my $items = Koha::Items->search( { biblionumber => $biblionumber } );
+    Koha::BackgroundJob->before_batch_action_hooks();
     while ( my $item = $items->next ) {
         my $deleted = $item->safe_delete( { skip_record_index => 1 } );
         push @errors, @{ $deleted->messages }[0]->message unless $deleted;
     }
+    Koha::BackgroundJob->after_batch_action_hooks();
     my $indexer = Koha::SearchEngine::Indexer->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } );
     $indexer->index_records( $biblionumber, "specialUpdate", "biblioserver" );
     if (@errors) {
diff --git a/misc/migration_tools/bulkmarcimport.pl b/misc/migration_tools/bulkmarcimport.pl
index cd5fa255a6..73e8c859a9 100755
--- a/misc/migration_tools/bulkmarcimport.pl
+++ b/misc/migration_tools/bulkmarcimport.pl
@@ -42,6 +42,7 @@ use Koha::Logger;
 use Koha::Biblios;
 use Koha::SearchEngine;
 use Koha::SearchEngine::Search;
+use Koha::BackgroundJob;
 
 use open qw( :std :encoding(UTF-8) );
 binmode( STDOUT, ":encoding(UTF-8)" );
@@ -325,6 +326,7 @@ my $logger            = Koha::Logger->get;
 my $schema            = Koha::Database->schema;
 my $lint              = MARC::Lint->new;
 
+Koha::BackgroundJob->before_batch_action_hooks();
 $schema->txn_begin;
 RECORD: while () {
 
@@ -739,6 +741,7 @@ RECORD: while () {
 if ( !$test_parameter ) {
     $schema->txn_commit;
 }
+Koha::BackgroundJob->after_batch_action_hooks();
 
 if ($fk_off) {
     $dbh->do("SET FOREIGN_KEY_CHECKS = 1");
-- 
2.43.0