From 7339c54ab806aa23e4a788af6f828a8f5646c3f8 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 4 Dec 2024 14:44:55 +0000 Subject: [PATCH] Bug 35654: Use Koha::Items instead of direct SQL --- misc/cronjobs/delete_items.pl | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/misc/cronjobs/delete_items.pl b/misc/cronjobs/delete_items.pl index 0c49076cdf0..b79341c7d72 100755 --- a/misc/cronjobs/delete_items.pl +++ b/misc/cronjobs/delete_items.pl @@ -46,7 +46,7 @@ pod2usage( -verbose => 2 ) pod2usage( -verbose => 1 ) if $help; pod2usage( -verbose => 1, -message => 'You must supply at least one --where option' ) if scalar @where == 0; -my $where_clause = ' where ' . join( " and ", @where ); +my $where_clause = join( " AND ", @where ); $verbose && say "Where statement: $where_clause"; if ($delete_biblios) { @@ -56,37 +56,33 @@ if ($delete_biblios) { print "Test run only! No data will be deleted.\n" unless $commit; my $deleted_string = $commit ? "Deleted" : "Would have deleted"; -# FIXME Use Koha::Items instead -my $query = "SELECT itemnumber, biblionumber from items "; -my $sth = $dbh->prepare( $query . $where_clause ); -$sth->execute(); +my $items = Koha::Items->search( \$where_clause ); -DELITEM: while ( my $item = $sth->fetchrow_hashref() ) { +DELITEM: while ( my $item = $items->next ){ #$sth->fetchrow_hashref() ) { - my $item_object = Koha::Items->find( $item->{itemnumber} ); - my $holdings_count = $item_object->biblio->items->count; + my $holdings_count = $item->biblio->items->count; my $error; - my $safe_to_delete = $item_object->safe_to_delete; + my $safe_to_delete = $item->safe_to_delete; if ($safe_to_delete) { - $item_object->safe_delete + $item->safe_delete if $commit; - $verbose && say "$deleted_string item '$item->{itemnumber}' (of $holdings_count)"; + $verbose && say "$deleted_string item " . $item->itemnumber . " ($holdings_count items remain on record)"; if ( $delete_biblios && $holdings_count == 1 ) { # aka DO-EET for empty bibs! - $error = &DelBiblio( $item->{biblionumber} ) if $commit; + $error = &DelBiblio( $item->biblionumber ) if $commit; if ($error) { - $verbose && say "Could not delete bib '$item->{biblionumber}': $error"; + $verbose && say "Could not delete bib " . $item->biblionumber . ": $error"; } else { - $verbose && say "No items remaining. $deleted_string bibliographic record '$item->{biblionumber}'"; + $verbose && say "No items remaining. $deleted_string bibliographic record " . $item->biblionumber; } } } else { say sprintf( "Item '%s' (Barcode: '%s', Title: '%s') cannot deleted: %s", - $item_object->itemnumber, - $item_object->barcode, - $item_object->biblio->title, + $item->itemnumber, + $item->barcode, + $item->biblio->title, @{ $safe_to_delete->messages }[0]->message ) if $verbose; } -- 2.39.5