From 8150a6d6516b9fe6dd77c1cdeb0a9a6d6fc9a1d9 Mon Sep 17 00:00:00 2001 From: Axel Amghar Date: Fri, 24 May 2019 17:13:50 +0200 Subject: [PATCH] Bug 22690 - Merging records with many items (ElasticSearch) This patch allow us to merged many items without timeout Test plan : Without the patch : > - Create 2 records (one with 1000 items for example) > - Add the records to one list and start the merging process of those records. > - Choose the record with many items as the one to be eliminated. > - Start the merging > - After a while the web server should give you a timeout error (the merging process continues) With the patch : - make the same - this time verify that all items have been merged without time out --- C4/Items.pm | 78 ++++++++++++++++++++++ cataloguing/merge.pl | 8 +-- .../prog/en/modules/cataloguing/merge.tt | 2 +- 3 files changed, 83 insertions(+), 5 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index 15b61f0..9b824e1 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -1694,6 +1694,84 @@ sub MoveItemFromBiblio { return; } +=head2 MoveItemsFromBiblio + + MoveItemsFromBiblio($items_number, $frombiblio, $tobiblio); + +Moves all items from a biblio record to another + +return a table of items not moved + +=cut + +sub MoveItemsFromBiblio { + my ($items_number, $frombiblio, $tobiblio) = @_; + my $dbh = C4::Context->dbh; + + my ( $tobiblioitem ) = $dbh->selectrow_array(q| + SELECT biblioitemnumber + FROM biblioitems + WHERE biblionumber = ? + |, undef, $tobiblio ); + + $dbh->do(q| + UPDATE items + SET biblioitemnumber = ?, + biblionumber = ? + WHERE biblionumber = ? + |, undef, $tobiblioitem, $tobiblio, $frombiblio ); + + my @notmoveditems = map { $_->[0] } @{$dbh->selectall_arrayref(q| + SELECT itemnumber + FROM items + WHERE biblionumber = ? + |, undef, $frombiblio )}; + + my @all_items_to_biblio = map { $_->[0] } @{$dbh->selectall_arrayref(q| + SELECT itemnumber + FROM items + WHERE biblionumber = ? + |, undef, $tobiblio )}; + + my @moved_items; + + #take only items moved + foreach my $item_number (@$items_number){ + foreach my $item_to_biblio (@all_items_to_biblio){ + if ( $item_to_biblio == $item_number){ + push @moved_items , $item_to_biblio; + } + } + } + + #unless all_items are not moved + unless ( scalar(@notmoveditems) == scalar(@$items_number) ){ + ModZebra( $tobiblio, "specialUpdate", "biblioserver" ); + ModZebra( $frombiblio, "specialUpdate", "biblioserver" ); + # Checking if the items we want to move are in an order + require C4::Acquisition; + my $where; + foreach my $moved_item (@moved_items){ + my $order = C4::Acquisition::GetOrderFromItemnumber($moved_item); + if ($order) { + # Replacing the biblionumber within the order if necessary + $order->{'biblionumber'} = $tobiblio; + C4::Acquisition::ModOrder($order); + } + $where .= " itemnumber = " . $moved_item . " OR"; + } + $where .= " itemnumber = " . $moved_items[0]; + # Update reserves, hold_fill_targets, tmp_holdsqueue and linktracker tables + for my $table_name ( qw( reserves hold_fill_targets tmp_holdsqueue linktracker ) ) { + $dbh->do( qq| + UPDATE $table_name + SET biblionumber = $tobiblio + WHERE $where | ); + } + } + return @notmoveditems; +} + =head2 ItemSafeToDelete ItemSafeToDelete( $biblionumber, $itemnumber); diff --git a/cataloguing/merge.pl b/cataloguing/merge.pl index 146e88e..a4ae5b1 100755 --- a/cataloguing/merge.pl +++ b/cataloguing/merge.pl @@ -89,12 +89,12 @@ if ($merge) { # Moving items from the other record to the reference record foreach my $biblionumber (@biblionumbers) { my $items = Koha::Items->search({ biblionumber => $biblionumber }); + my @items_number; + while ( my $item = $items->next) { - my $res = MoveItemFromBiblio( $item->itemnumber, $biblionumber, $ref_biblionumber ); - if ( not defined $res ) { - push @notmoveditems, $item->itemnumber; - } + push @items_number, $item->itemnumber; } + push @notmoveditems, C4::Items::MoveItemsFromBiblio(\@items_number, $biblionumber, $ref_biblionumber); } # If some items could not be moved : if (scalar(@notmoveditems) > 0) { diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/merge.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/merge.tt index c21baca..f770cec 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/merge.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/cataloguing/merge.tt @@ -212,7 +212,7 @@ div#result { margin-top: 1em; } 'subfields': [] }; for (tag in tagslib) { - if (tag == '000' || tag == '001') + if (tag == '000' || tag == '001' || tag == '801') continue; if (tagslib[tag].mandatory == 1) { if ($("#resultul span.field:contains("+ tag +")").length == 0) { -- 2.7.4