From 127cf1cf92d62c6fbd0ec275c226400083da25ce Mon Sep 17 00:00:00 2001 From: Shi Yao Wang Date: Wed, 2 Apr 2025 12:25:20 -0400 Subject: [PATCH] Bug 39156: Pass batch action to the hooks + refactoring --- C4/ImportBatch.pm | 6 ++++-- Koha/BackgroundJob.pm | 14 ++++++++++++-- Koha/BackgroundJob/BatchDeleteAuthority.pm | 4 ++-- Koha/BackgroundJob/BatchDeleteBiblio.pm | 4 ++-- Koha/BackgroundJob/BatchDeleteItem.pm | 4 ++-- Koha/BackgroundJob/BatchUpdateAuthority.pm | 4 ++-- Koha/BackgroundJob/BatchUpdateBiblio.pm | 4 ++-- Koha/BackgroundJob/BatchUpdateItem.pm | 4 ++-- Koha/BackgroundJob/MARCImportRevertBatch.pm | 3 --- authorities/merge.pl | 4 ++-- cataloguing/additem.pl | 8 ++++---- misc/migration_tools/bulkmarcimport.pl | 4 ++-- t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t | 6 +++--- t/lib/plugins/Koha/Plugin/Test.pm | 13 ++++++++----- 14 files changed, 47 insertions(+), 35 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index a9c4395250..be3d2e85f8 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -567,7 +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(); + Koha::BackgroundJob->before_batch_action_hooks({ action => 'batch_commit_records' }); $sth->execute($batch_id); my $marcflavour = C4::Context->preference('marcflavour'); @@ -739,7 +739,7 @@ sub BatchCommitRecords { } $sth->finish(); - Koha::BackgroundJob->after_batch_action_hooks(); + Koha::BackgroundJob->after_batch_action_hooks({ action => 'batch_commit_records' }); SetImportBatchStatus( $batch_id, 'imported' ); @@ -898,6 +898,7 @@ sub BatchRevertRecords { 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({ action => 'batch_revert_records' }); $sth->execute($batch_id); my $marc_type; my $marcflavour = C4::Context->preference('marcflavour'); @@ -979,6 +980,7 @@ sub BatchRevertRecords { } $sth->finish(); + Koha::BackgroundJob->after_batch_action_hooks({ action => 'batch_revert_records' }); SetImportBatchStatus( $batch_id, 'reverted' ); return ( $num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored ); } diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index a655f7c93b..5d95061863 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -550,7 +550,12 @@ Helper method that takes care of calling all plugin hooks sub after_batch_action_hooks { my ( $self, $args ) = @_; - Koha::Plugins->call( 'after_batch_action', {} ); + + my $action = $args->{action}; + + Koha::Plugins->call( 'after_batch_action', { + action => $action + } ); } =head3 before_batch_action_hooks @@ -561,7 +566,12 @@ Helper method that takes care of calling all plugin hooks sub before_batch_action_hooks { my ( $self, $args ) = @_; - Koha::Plugins->call('before_batch_action', {} ); + + my $action = $args->{action}; + + Koha::Plugins->call( 'before_batch_action', { + action => $action + } ); } 1; diff --git a/Koha/BackgroundJob/BatchDeleteAuthority.pm b/Koha/BackgroundJob/BatchDeleteAuthority.pm index 8b37655519..e7e9b490d2 100644 --- a/Koha/BackgroundJob/BatchDeleteAuthority.pm +++ b/Koha/BackgroundJob/BatchDeleteAuthority.pm @@ -28,7 +28,7 @@ sub process { my $mmtid = $args->{mmtid}; my @record_ids = @{ $args->{record_ids} }; - $self->before_batch_action_hooks(); + $self->before_batch_action_hooks({ action => 'batch_delete_authority' }); my $report = { total_records => scalar @record_ids, @@ -81,7 +81,7 @@ RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) { $data->{report} = $report; $self->finish($data); - $self->after_batch_action_hooks(); + $self->after_batch_action_hooks({ action => 'batch_delete_authority' }); } sub enqueue { diff --git a/Koha/BackgroundJob/BatchDeleteBiblio.pm b/Koha/BackgroundJob/BatchDeleteBiblio.pm index 9ff0a023a8..761ccadf7d 100644 --- a/Koha/BackgroundJob/BatchDeleteBiblio.pm +++ b/Koha/BackgroundJob/BatchDeleteBiblio.pm @@ -52,7 +52,7 @@ sub process { my $mmtid = $args->{mmtid}; my @record_ids = @{ $args->{record_ids} }; - $self->before_batch_action_hooks(); + $self->before_batch_action_hooks({ action => 'batch_delete_biblio' }); my $report = { total_records => scalar @record_ids, @@ -174,7 +174,7 @@ RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) { $data->{report} = $report; $self->finish($data); - $self->after_batch_action_hooks(); + $self->after_batch_action_hooks({ action => 'batch_delete_biblio' }); } =head3 enqueue diff --git a/Koha/BackgroundJob/BatchDeleteItem.pm b/Koha/BackgroundJob/BatchDeleteItem.pm index 24c9d058c7..454171d3cb 100644 --- a/Koha/BackgroundJob/BatchDeleteItem.pm +++ b/Koha/BackgroundJob/BatchDeleteItem.pm @@ -84,7 +84,7 @@ sub process { my @record_ids = @{ $args->{record_ids} }; my $delete_biblios = $args->{delete_biblios}; - $self->before_batch_action_hooks(); + $self->before_batch_action_hooks({ action => 'batch_delete_item' }); my $report = { total_records => scalar @record_ids, @@ -202,7 +202,7 @@ sub process { $data->{report} = $report; $self->finish($data); - $self->after_batch_action_hooks(); + $self->after_batch_action_hooks({ action => 'batch_delete_item' }); } =head3 enqueue diff --git a/Koha/BackgroundJob/BatchUpdateAuthority.pm b/Koha/BackgroundJob/BatchUpdateAuthority.pm index 0cbe817b08..ac21fa6110 100644 --- a/Koha/BackgroundJob/BatchUpdateAuthority.pm +++ b/Koha/BackgroundJob/BatchUpdateAuthority.pm @@ -64,7 +64,7 @@ sub process { my $mmtid = $args->{mmtid}; my @record_ids = @{ $args->{record_ids} }; - $self->before_batch_action_hooks(); + $self->before_batch_action_hooks({ action => 'batch_update_authority' }); my $report = { total_records => scalar @record_ids, @@ -108,7 +108,7 @@ RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) { $data->{report} = $report; $self->finish($data); - $self->after_batch_action_hooks(); + $self->after_batch_action_hooks({ action => 'batch_update_authority' }); } diff --git a/Koha/BackgroundJob/BatchUpdateBiblio.pm b/Koha/BackgroundJob/BatchUpdateBiblio.pm index b22ae8a46f..7f4fc4eb76 100644 --- a/Koha/BackgroundJob/BatchUpdateBiblio.pm +++ b/Koha/BackgroundJob/BatchUpdateBiblio.pm @@ -70,7 +70,7 @@ sub process { my $mmtid = $args->{mmtid}; my @record_ids = @{ $args->{record_ids} }; - $self->before_batch_action_hooks(); + $self->before_batch_action_hooks({ action => 'batch_update_biblio' }); my $report = { total_records => scalar @record_ids, @@ -135,7 +135,7 @@ RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) { $data->{report} = $report; $self->finish($data); - $self->after_batch_action_hooks(); + $self->after_batch_action_hooks({ action => 'batch_update_biblio' }); } =head3 enqueue diff --git a/Koha/BackgroundJob/BatchUpdateItem.pm b/Koha/BackgroundJob/BatchUpdateItem.pm index 0993191a40..ffc3d668de 100644 --- a/Koha/BackgroundJob/BatchUpdateItem.pm +++ b/Koha/BackgroundJob/BatchUpdateItem.pm @@ -98,7 +98,7 @@ 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(); + $self->before_batch_action_hooks({ action => 'batch_update_item' }); my $report = { total_records => scalar @record_ids, @@ -127,7 +127,7 @@ sub process { $data->{report} = $report; $self->finish($data); - $self->after_batch_action_hooks(); + $self->after_batch_action_hooks({ action => 'batch_update_item' }); } =head3 enqueue diff --git a/Koha/BackgroundJob/MARCImportRevertBatch.pm b/Koha/BackgroundJob/MARCImportRevertBatch.pm index 4d11555119..a9115492c2 100644 --- a/Koha/BackgroundJob/MARCImportRevertBatch.pm +++ b/Koha/BackgroundJob/MARCImportRevertBatch.pm @@ -59,8 +59,6 @@ sub process { my $import_batch_id = $args->{import_batch_id}; - $self->before_batch_action_hooks(); - my @messages; my $job_progress = 0; my ( @@ -101,7 +99,6 @@ 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 7e1c0a7299..d8d993a446 100755 --- a/authorities/merge.pl +++ b/authorities/merge.pl @@ -48,7 +48,7 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( # Merging #------------------------ if ( $op eq 'cud-merge' ) { - Koha::BackgroundJob->before_batch_action_hooks(); + Koha::BackgroundJob->before_batch_action_hooks({ action => 'merge_authorities' }); # Creating a new record from the html code my $record = TransformHtmlToMarc( $input, 0 ); @@ -85,7 +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(); + Koha::BackgroundJob->after_batch_action_hooks({ action => 'merge_authorities' }); # Parameters $template->param( diff --git a/cataloguing/additem.pl b/cataloguing/additem.pl index fb7f15531d..816376256f 100755 --- a/cataloguing/additem.pl +++ b/cataloguing/additem.pl @@ -411,7 +411,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(); + Koha::BackgroundJob->before_batch_action_hooks({ action => 'add_item' }); if ( $oldbarcode && !$testbarcode ) { push @errors, "no_next_barcode"; @@ -489,7 +489,7 @@ if ( $op eq "cud-additem" ) { undef($current_item); } - Koha::BackgroundJob->after_batch_action_hooks(); + Koha::BackgroundJob->after_batch_action_hooks({ action => 'add_item' }); } if ( $frameworkcode eq 'FA' && $fa_circborrowernumber ) { print $input->redirect( '/cgi-bin/koha/circ/circulation.pl?' @@ -552,12 +552,12 @@ if ( $op eq "cud-additem" ) { #------------------------------------------------------------------------------- my $items = Koha::Items->search( { biblionumber => $biblionumber } ); - Koha::BackgroundJob->before_batch_action_hooks(); + Koha::BackgroundJob->before_batch_action_hooks({ action => 'del_all_item' }); 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(); + Koha::BackgroundJob->after_batch_action_hooks({ action => 'del_all_item' }); 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 d67791d95d..2314985ea1 100755 --- a/misc/migration_tools/bulkmarcimport.pl +++ b/misc/migration_tools/bulkmarcimport.pl @@ -326,7 +326,7 @@ my $schema = Koha::Database->schema; my $marc_records = []; my $lint = MARC::Lint->new; -Koha::BackgroundJob->before_batch_action_hooks(); +Koha::BackgroundJob->before_batch_action_hooks({ action => 'bulk_marc_import' }); RECORD: while () { my $record; $record_number++; @@ -734,7 +734,7 @@ RECORD: foreach my $record ( @{$marc_records} ) { last if $record_number == $number; } $schema->txn_commit; -Koha::BackgroundJob->after_batch_action_hooks(); +Koha::BackgroundJob->after_batch_action_hooks({ action => 'bulk_marc_import' }); if ($fk_off) { $dbh->do("SET FOREIGN_KEY_CHECKS = 1"); diff --git a/t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t b/t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t index 3cb8b0d66c..95c9055d1b 100644 --- a/t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t +++ b/t/db_dependent/Koha/Plugins/Batch_plugin_hooks.t @@ -104,7 +104,7 @@ subtest 'before_batch_action and after_batch_action hooks with after_biblio_acti frameworkcode => q{}, } ); - } qr/after_batch_action called with addBiblio count: 178/, + } qr/after_batch_action called with action: batch_commit_records, addBiblio count: 178/, 'MARCImportCommitBatch calls the after_batch_action hook'; Koha::Plugins->RemovePlugins; @@ -170,7 +170,7 @@ subtest 'before_batch_action and after_batch_action hooks with after_item_action frameworkcode => q{}, } ); - } qr/after_batch_action called with addItem count: 138/, + } qr/after_batch_action called with action: batch_commit_records, addItem count: 138/, 'MARCImportCommitBatch calls the after_batch_action hook'; Koha::Plugins->RemovePlugins; @@ -234,7 +234,7 @@ subtest 'before_batch_action and after_batch_action hooks with after_authority_a frameworkcode => q{}, } ); - } qr/after_batch_action called with addAuthority count: 2/, + } qr/after_batch_action called with action: batch_commit_records, addAuthority count: 2/, 'MARCImportCommitBatch calls the after_batch_action hook'; Koha::Plugins->RemovePlugins; diff --git a/t/lib/plugins/Koha/Plugin/Test.pm b/t/lib/plugins/Koha/Plugin/Test.pm index ccce37fb77..ac1746b640 100644 --- a/t/lib/plugins/Koha/Plugin/Test.pm +++ b/t/lib/plugins/Koha/Plugin/Test.pm @@ -407,30 +407,33 @@ sub after_recall_action { } sub before_batch_action { - my ( $self ) = @_; + my ( $self, $params ) = @_; $is_batch = 1; } sub after_batch_action { - my ( $self ) = @_; + my ( $self, $params ) = @_; + + my $action = $params->{action} // ''; + $is_batch = 0; if (@addBiblio) { my $count = scalar(@addBiblio); @addBiblio = (); - Koha::Exception->throw("after_batch_action called with addBiblio count: " . $count); + Koha::Exception->throw("after_batch_action called with action: $action, addBiblio count: " . $count); } if (@addItem) { my $count = scalar(@addItem); @addItem = (); - Koha::Exception->throw("after_batch_action called with addItem count: " . $count); + Koha::Exception->throw("after_batch_action called with action: $action, addItem count: " . $count); } if (@addAuthority) { my $count = scalar(@addAuthority); @addAuthority = (); - Koha::Exception->throw("after_batch_action called with addAuthority count: " . $count); + Koha::Exception->throw("after_batch_action called with action: $action, addAuthority count: " . $count); } } -- 2.43.0