Bugzilla – Attachment 176937 Details for
Bug 35654
Add option to delete_items.pl to delete record if existing item getting deleted is the only one attached to the bib
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 35654: Add option to delete_items.pl cronjob to delete empty bibs
Bug-35654-Add-option-to-deleteitemspl-cronjob-to-d.patch (text/plain), 6.90 KB, created by
Andrew Fuerste-Henry
on 2025-01-22 21:20:29 UTC
(
hide
)
Description:
Bug 35654: Add option to delete_items.pl cronjob to delete empty bibs
Filename:
MIME Type:
Creator:
Andrew Fuerste-Henry
Created:
2025-01-22 21:20:29 UTC
Size:
6.90 KB
patch
obsolete
>From 74b9ff0afb6c7523db2cb888cc4934d403ea1372 Mon Sep 17 00:00:00 2001 >From: Brendan Lawlor <blawlor@clamsnet.org> >Date: Wed, 13 Nov 2024 21:26:43 +0000 >Subject: [PATCH] Bug 35654: Add option to delete_items.pl cronjob to delete > empty bibs > >This patch adds a new option to the delete_items cronjob > >When --del_bibs is set, bib records will be deleted >if the last item is deleted. > >The following plan assumes default ktd data. > >To test: >1. Apply patch >2. Check the holdings for bib #115 and see 3 items > http://localhost:8081/cgi-bin/koha/catalogue/detail.pl?biblionumber=115 >2. Run the cron with the following flags > perl ./misc/cronjobs/delete_items.pl --where "biblionumber=115 and homebranch ='MPL'" --verbose --del_bibs --commit >3. Notice in the output the count of current items on the bib record: > Where statement: where biblionumber=115 and homebranch='MPL' > Deleting empty bib records! > Deleting item '237' (of 3) > Deleting item '238' (of 2) >4. Confirm the two Midway items were deleted from bib #115, and the bib still exists with one item >4. Run the cron again but change the the --where flag to delete all the items from bib #115 > perl ./misc/cronjobs/delete_items.pl --where "biblionumber=115" --verbose --del_bibs --commit >5. Notice in the output the bib is deleted after the last item is deleted > Where statement: where biblionumber=115 > Deleting empty bib records! > Deleting item '239' (of 1) > Last item deleted - deleting bib '115' >8. Confirm the bib #115 was deleted >9. Add some more items and test with some other --where parameters >10. Test with and without --commit and --del_bibs, test the --help flag > >Sponsored-by: CLAMS >Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com> >--- > misc/cronjobs/delete_items.pl | 105 ++++++++++++++++------------------ > 1 file changed, 49 insertions(+), 56 deletions(-) > >diff --git a/misc/cronjobs/delete_items.pl b/misc/cronjobs/delete_items.pl >index ac86b07648..75ffec6ee1 100755 >--- a/misc/cronjobs/delete_items.pl >+++ b/misc/cronjobs/delete_items.pl >@@ -18,76 +18,68 @@ > use Modern::Perl; > > use Getopt::Long qw( GetOptions ); >-use Pod::Usage qw( pod2usage ); >+use Pod::Usage qw( pod2usage ); > > use Koha::Script -cron; >- >-use C4::Context; >+use C4::Biblio qw( DelBiblio ); > use Koha::Items; > >-my $dbh = C4::Context->dbh(); >- >-my $query = { >- target_items => q|SELECT itemnumber, biblionumber from items| >-}; >- >-my $GLOBAL = { >- query => $query >- , sth => {} >-}; >- >-my $OPTIONS = { >- where => [] >- , flags => { >- verbose => '' >- , commit => '' >- , help => '' >- , manual => '' >- , version => '' >- } >-}; >+my @where = (); >+my $verbose = 0; >+my $help = 0; >+my $manual = 0; >+my $commit = 0; >+my $delete_biblios = 0; > > GetOptions( >- 'where=s' => $OPTIONS->{where} >- , 'v|verbose' => sub { $OPTIONS->{flags}->{verbose} = 1 } >- , 'V|version' => sub { $OPTIONS->{flags}->{version} = 1 } >- , 'h|help' => sub { $OPTIONS->{flags}->{help} = 1 } >- , 'm|manual' => sub { $OPTIONS->{flags}->{manual} = 1 } >- , 'c|commit' => sub { $OPTIONS->{flags}->{commit} = 1 } # aka DO-EET! >+ 'where=s' => \@where, 'v|verbose' => \$verbose, >+ 'h|help' => \$help, >+ 'm|manual' => \$manual, >+ 'c|commit' => \$commit, >+ 'del_bibs' => \$delete_biblios > ); > >-my @where = @{ $OPTIONS->{where} }; >+pod2usage( -verbose => 2 ) if $manual; >+pod2usage( -verbose => 1 ) if $help; >+pod2usage( -verbose => 1, -message => 'You must supply at least one --where option' ) if scalar @where == 0; > >-pod2usage( -verbose => 2 ) if $OPTIONS->{flags}->{manual}; >-pod2usage( -verbose => 1 ) if $OPTIONS->{flags}->{help}; >-pod2usage( -verbose => 1 -msg => 'You must supply at least one --where option' ) if scalar @where == 0; >+my $where_clause = join( " AND ", @where ); > >-sub verbose { >- say @_ if $OPTIONS->{flags}->{verbose}; >+$verbose && say "Where statement: $where_clause"; >+if ($delete_biblios) { >+ $verbose && say "Deleting bibliographic records when all items are deleted!"; > } > >-my $where_clause = ' where ' . join ( " and ", @where ); >+print "Test run only! No data will be deleted.\n" unless $commit; >+my $deleted_string = $commit ? "Deleted" : "Would have deleted"; > >-verbose "Where statement: $where_clause"; >+my $items = Koha::Items->search( \$where_clause ); > >-# FIXME Use Koha::Items instead >-$GLOBAL->{sth}->{target_items} = $dbh->prepare( $query->{target_items} . $where_clause ); >-$GLOBAL->{sth}->{target_items}->execute(); >+DELITEM: while ( my $item = $items->next ){ > >-DELITEM: while ( my $item = $GLOBAL->{sth}->{target_items}->fetchrow_hashref() ) { >+ my $safe_to_delete = $item->safe_to_delete; >+ if ($safe_to_delete) { >+ my $holdings_count = $item->biblio->items->count - 1; >+ $item->safe_delete >+ if $commit; >+ $verbose && say "$deleted_string item " . $item->itemnumber . " ($holdings_count items remain on record)"; > >- my $item_object = Koha::Items->find($item->{itemnumber}); >- my $safe_to_delete = $item_object->safe_to_delete; >- if( $safe_to_delete ) { >- $item_object->safe_delete >- if $OPTIONS->{flags}->{commit}; >- verbose "Deleting '$item->{itemnumber}'"; >+ if ( $delete_biblios && $holdings_count == 0 ) { # aka DO-EET for empty bibs! >+ my $error = &DelBiblio( $item->biblionumber ) if $commit; >+ if ($error) { >+ $verbose && say "Could not delete bib " . $item->biblionumber . ": $error"; >+ } else { >+ $verbose && say "No items remaining. $deleted_string bibliographic record " . $item->biblionumber; >+ } >+ } > } else { >- verbose sprintf "Item '%s' (Barcode: '%s', Title: '%s') not deleted: %s", >- $item->{itemnumber}, >- $item_object->barcode, >- $item_object->biblio->title, >- @{$safe_to_delete->messages}[0]->message; >+ say sprintf( >+ "Item '%s' (Barcode: '%s', Title: '%s') cannot deleted: %s", >+ $item->itemnumber, >+ $item->barcode, >+ $item->biblio->title, >+ @{ $safe_to_delete->messages }[0]->message >+ ) if $verbose; > } > } > >@@ -130,11 +122,14 @@ clause querying the items table. These are joined by C<AND>. > > No items will be deleted unless the C<--commit> flag is present. > >+=item B<--del_bibs> >+ >+Deletes the bibliographic record if the last item is deleted. >+ > =back > > =cut > >- > =head1 EXAMPLES > > The following is an example of this script: >@@ -145,14 +140,12 @@ No items will be deleted unless the C<--commit> flag is present. > > =cut > >- > =head1 DESCRIPTION > > This is a lightweight batch deletion tool for items, suitable for running in a cron job. > > =cut > >- > =head1 AUTHOR > > Barton Chittenden <barton@bywatersolutions.com> >-- >2.39.5
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 35654
:
174480
|
174529
|
175193
|
175194
|
175195
|
175208
|
175209
|
176936
|
176937
|
177868
|
179675