From 9fa7abc25577be176189ac322545def7d771ad06 Mon Sep 17 00:00:00 2001 From: Joe Atzberger Date: Mon, 23 Mar 2009 16:25:21 -0500 Subject: [PATCH] Improve performance and fault tolerance for sync_items... Pulled the GetMarcFromKohaField outside the loop, since we only need to check it once. Same for $item_sth. Added safety checks for success of delete_field and GetMarcItem, with warnings on failure. Signed-off-by: Galen Charlton --- misc/maintenance/sync_items_in_marc_bib.pl | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/misc/maintenance/sync_items_in_marc_bib.pl b/misc/maintenance/sync_items_in_marc_bib.pl index d2f62e1a27..159c48f4ea 100755 --- a/misc/maintenance/sync_items_in_marc_bib.pl +++ b/misc/maintenance/sync_items_in_marc_bib.pl @@ -29,13 +29,17 @@ if (not $result or $want_help or not $do_update) { exit 0; } -my $num_bibs_processed = 0; -my $num_bibs_modified = 0; +my $num_bibs_processed = 0; +my $num_bibs_modified = 0; my $num_marc_items_deleted = 0; -my $num_marc_items_added = 0; -my $num_bad_bibs = 0; +my $num_marc_items_added = 0; +my $num_bad_bibs = 0; my $dbh = C4::Context->dbh; $dbh->{AutoCommit} = 0; + +our ($itemtag, $itemsubfield) = GetMarcFromKohaField("items.itemnumber", ''); +our ($item_sth) = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?"); + process_bibs(); $dbh->commit(); @@ -81,18 +85,23 @@ sub process_bib { my $bib_modified = 0; # delete any item tags - my ($itemtag, $itemsubfield) = GetMarcFromKohaField("items.itemnumber", ''); foreach my $field ($bib->field($itemtag)) { - $bib->delete_field($field); + unless ($bib->delete_field($field)) { + warn "Could not delete item in $itemtag for biblionumber $biblionumber"; + next; + } $num_marc_items_deleted++; $bib_modified = 1; } # add back items from items table - my $item_sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber = ?"); $item_sth->execute($biblionumber); while (my $itemnumber = $item_sth->fetchrow_array) { my $marc_item = C4::Items::GetMarcItem($biblionumber, $itemnumber); + unless ($marc_item) { + warn "FAILED C4::Items::GetMarcItem for biblionumber=$biblionumber, itemnumber=$itemnumber"; + next; + } foreach my $item_field ($marc_item->field($itemtag)) { $bib->insert_fields_ordered($item_field); $num_marc_items_added++; -- 2.39.5