From a3fe595738a04a31f99f540e5efabea27043b596 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 13 Jun 2022 20:30:36 +0200 Subject: [PATCH] Bug 27421: Enqueue only one job for indexing Might need to be backported. --- C4/Biblio.pm | 10 ++++------ C4/ImportBatch.pm | 40 ++++++++++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index ef094ab3759..0da9de4a7b3 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -202,15 +202,13 @@ unless you can guarantee that C will be called. sub AddBiblio { my $record = shift; my $frameworkcode = shift; - my $options = @_ ? shift : undef; - my $defer_marc_save = 0; + my $options = @_ ? shift : {}; if (!$record) { carp('AddBiblio called with undefined record'); return; } - if ( defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'} ) { - $defer_marc_save = 1; - } + my $defer_marc_save = $options->{defer_marc_save} || 0; + my $skip_record_index = $options->{skip_record_index} || 0; my $schema = Koha::Database->schema; my ( $biblionumber, $biblioitemnumber ); @@ -291,7 +289,7 @@ sub AddBiblio { } # now add the record - ModBiblioMarc( $record, $biblionumber ) unless $defer_marc_save; + ModBiblioMarc( $record, $biblionumber, { skip_record_index => $skip_record_index } ) unless $defer_marc_save; # update OAI-PMH sets if(C4::Context->preference("OAI-PMH:AutoUpdateSets")) { diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index a427be5c9fb..3645357cb8e 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -35,6 +35,8 @@ use C4::Charset qw( MarcToUTF8Record SetUTF8Flag StripNonXmlChars ); use C4::AuthoritiesMarc qw( AddAuthority GuessAuthTypeCode GetAuthorityXML ModAuthority DelAuthority ); use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate ); use Koha::Items; +use Koha::SearchEngine; +use Koha::SearchEngine::Indexer; use Koha::Plugins::Handler; use Koha::Logger; @@ -582,6 +584,7 @@ sub BatchCommitRecords { my $logged_in_patron = Koha::Patrons->find( $userenv->{number} ); my $rec_num = 0; + my @biblionumbers; while (my $rowref = $sth->fetchrow_hashref) { $record_type = $rowref->{'record_type'}; $rec_num++; @@ -621,7 +624,7 @@ sub BatchCommitRecords { $num_added++; if ($record_type eq 'biblio') { my $biblioitemnumber; - ($recordid, $biblioitemnumber) = AddBiblio($marc_record, $framework); + ( $recordid, $biblioitemnumber ) = AddBiblio( $marc_record, $framework, { skip_record_index => 1 } ); $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead if ($item_result eq 'create_new' || $item_result eq 'replace') { my ($bib_items_added, $bib_items_replaced, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid, $item_result, $biblioitemnumber); @@ -661,7 +664,8 @@ sub BatchCommitRecords { } ModBiblio($marc_record, $recordid, $oldbiblio->frameworkcode, { - overlay_context => $context + overlay_context => $context, + skip_record_index => 1, }); $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; # FIXME call SetMatchedBiblionumber instead @@ -701,9 +705,16 @@ sub BatchCommitRecords { } SetImportRecordStatus($rowref->{'import_record_id'}, 'ignored'); } + push @biblionumbers, $recordid if $record_type eq 'biblio'; } $sth->finish(); SetImportBatchStatus($batch_id, 'imported'); + + if ( @biblionumbers ) { + my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); + $indexer->index_records( \@biblionumbers, "specialUpdate", "biblioserver" ); + } + return ($num_added, $num_updated, $num_items_added, $num_items_replaced, $num_items_errored, $num_ignored); } @@ -748,7 +759,7 @@ sub BatchCommitItems { my $updsth = $dbh->prepare("UPDATE import_items SET status = ?, itemnumber = ?, import_error = ? WHERE import_items_id = ?"); if ( $action eq "replace" && $duplicate_itemnumber ) { # Duplicate itemnumbers have precedence, that way we can update barcodes by overlaying - ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber} ); + ModItemFromMarc( $item_marc, $biblionumber, $item->{itemnumber}, { skip_record_index => 1 } ); $updsth->bind_param( 1, 'imported' ); $updsth->bind_param( 2, $item->{itemnumber} ); $updsth->bind_param( 3, undef ); @@ -758,7 +769,7 @@ sub BatchCommitItems { $num_items_replaced++; } elsif ( $action eq "replace" && $duplicate_barcode ) { my $itemnumber = $duplicate_barcode->itemnumber; - ModItemFromMarc( $item_marc, $biblionumber, $itemnumber ); + ModItemFromMarc( $item_marc, $biblionumber, $itemnumber, { skip_record_index => 1 } ); $updsth->bind_param( 1, 'imported' ); $updsth->bind_param( 2, $item->{itemnumber} ); $updsth->bind_param( 3, undef ); @@ -778,7 +789,7 @@ sub BatchCommitItems { my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField( "items.itemnumber" ); $item_marc->field($itemtag)->delete_subfield( code => $itemsubfield ); - my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber, {biblioitemnumber => $biblioitemnumber} ); + my ( $item_biblionumber, $biblioitemnumber, $itemnumber ) = AddItemFromMarc( $item_marc, $biblionumber, { biblioitemnumber => $biblioitemnumber, skip_record_index => 1 } ); if( $itemnumber ) { $updsth->bind_param( 1, 'imported' ); $updsth->bind_param( 2, $itemnumber ); @@ -827,6 +838,7 @@ sub BatchRevertRecords { $sth->execute($batch_id); my $marc_type; my $marcflavour = C4::Context->preference('marcflavour'); + my ( @deleted_biblionumbers, @updated_biblionumbers ); while (my $rowref = $sth->fetchrow_hashref) { $record_type = $rowref->{'record_type'}; if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') { @@ -847,7 +859,8 @@ sub BatchRevertRecords { my $error = undef; if ($record_type eq 'biblio') { $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); - $error = DelBiblio($rowref->{'matched_biblionumber'}); + $error = DelBiblio($rowref->{'matched_biblionumber'}, { skip_record_index => 1 } ); + push @deleted_biblionumbers, $rowref->{'matched_biblionumber'}; } else { DelAuthority({ authid => $rowref->{'matched_authid'} }); } @@ -868,7 +881,8 @@ sub BatchRevertRecords { next unless $oldbiblio; # Record has since been deleted. Deleted records should stay deleted. $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); - ModBiblio($old_record, $biblionumber, $oldbiblio->frameworkcode); + ModBiblio($old_record, $biblionumber, $oldbiblio->frameworkcode, { skip_record_index => 1 }); + push @updated_biblionumbers, $biblionumber; } else { my $authid = $rowref->{'matched_authid'}; ModAuthority($authid, $old_record, GuessAuthTypeCode($old_record)); @@ -877,6 +891,7 @@ sub BatchRevertRecords { } elsif ($record_result eq 'ignore') { if ($record_type eq 'biblio') { $num_items_deleted += BatchRevertItems($rowref->{'import_record_id'}, $rowref->{'matched_biblionumber'}); + push @updated_biblionumbers, $rowref->{'matched_biblionumber'}; } SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted'); } @@ -892,6 +907,15 @@ sub BatchRevertRecords { $sth2->execute($rowref->{'import_record_id'}); } + if ( $record_type eq 'biblio' && ( @updated_biblionumbers || @deleted_biblionumbers ) ) { + my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); + if ( @deleted_biblionumbers ) { + $indexer->index_records( \@deleted_biblionumbers, "recordDelete", "biblioserver" ); + } else { + $indexer->index_records( \@updated_biblionumbers, "specialUpdate", "biblioserver" ); + } + } + $sth->finish(); SetImportBatchStatus($batch_id, 'reverted'); return ($num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored); @@ -917,7 +941,7 @@ sub BatchRevertItems { $sth->execute(); while (my $row = $sth->fetchrow_hashref()) { my $item = Koha::Items->find($row->{itemnumber}); - if ($item->safe_delete){ + if ($item->safe_delete({ skip_record_index => 1 })){ my $updsth = $dbh->prepare("UPDATE import_items SET status = ? WHERE import_items_id = ?"); $updsth->bind_param(1, 'reverted'); $updsth->bind_param(2, $row->{'import_items_id'}); -- 2.25.1