From cc735a7ca4cfc90f636d8cd0f3a24b09312268bf Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Sun, 20 Oct 2013 14:02:52 -0400 Subject: [PATCH] Bug 11084 - Delete biblios on Leader 05 == d Position 05 of the leader in MARC 21 indicates the record's status. A lower case 'd' indicates that the record is to be deleted (http://www.loc.gov/marc/authority/adleader.html). This patch adds a cronjob script that may be run nightly to delete records where position 05 of the leader is set to 'd'. Test Plan: 1) Edit one or more records, and set the leader position 05 to 'd'. 2) Run misc/cronjobs/delete_records_via_leader.pl -c -v 3) Those records should either be deleted, or an error message should state why they were not. Signed-off-by: Magnus Enger Works as advertised. Edited a record with an item to have leader05 = d, then ran delete_records_via_leader.pl with -c and got a message that the record could not be deleted because of the item. Removed the item and the script deleted the record. Tried with another record with an item, this time with the -i option and the record was deleted. Signed-off-by: Katrin Fischer Signed-off-by: Brendan A Gallagher --- C4/Items.pm | 3 + misc/cronjobs/delete_records_via_leader.pl | 118 +++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100755 misc/cronjobs/delete_records_via_leader.pl diff --git a/C4/Items.pm b/C4/Items.pm index a7db6eff0f..5b5b5d4811 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -2315,6 +2315,9 @@ Exported function (core API) for deleting an item record in Koha if there no cur sub DelItemCheck { my ( $dbh, $biblionumber, $itemnumber ) = @_; + + $dbh ||= C4::Context->dbh; + my $error; my $countanalytics=GetAnalyticsCount($itemnumber); diff --git a/misc/cronjobs/delete_records_via_leader.pl b/misc/cronjobs/delete_records_via_leader.pl new file mode 100755 index 0000000000..f8e10f45bf --- /dev/null +++ b/misc/cronjobs/delete_records_via_leader.pl @@ -0,0 +1,118 @@ +#!/usr/bin/perl + +#----------------------------------- +# Copyright 2013 ByWater Solutions +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 3 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +#----------------------------------- + +use Modern::Perl; + +binmode( STDOUT, ":utf8" ); + +BEGIN { + + # find Koha's Perl modules + # test carefully before changing this + use FindBin; + eval { require "$FindBin::Bin/../kohalib.pl" }; +} + +use Getopt::Long; + +use C4::Biblio; +use C4::Items; +use Koha::Database; + +my $delete_items; +my $confirm; +my $test; +my $verbose; +my $help; + +GetOptions( + 'i|di|delete-items' => \$delete_items, + 'c|confirm' => \$confirm, + 't|test' => \$test, + 'v|verbose' => \$verbose, + 'h|help' => \$help, +); + +if ( $help || !$confirm ) { + say qq{ +delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd' +usage: delete_records_via_leader.pl --confirm --verbose [--test] +This script has the following parameters : + -h --help: Prints this message + -c --confirm: Script will do nothing without this parameter + -v --verbose: Be verbose + -t --test: Test mode, does not delete records + -i --delete-items: Try deleting items before deleting record. + Records with items cannot be deleted. +}; + exit(); +} + +my $schema = Koha::Database->new()->schema(); +my @biblioitems = + $schema->resultset('Biblioitem')->search( { marc => { LIKE => '_____d%' } } ); + +my $total_records_count = @biblioitems; +my $deleted_records_count = 0; +my $total_items_count = 0; +my $deleted_items_count = 0; + +foreach my $biblioitem (@biblioitems) { + my $biblionumber = $biblioitem->get_column('biblionumber'); + + say "RECORD: $biblionumber" if $verbose; + + if ($delete_items) { + my $deleted_count = 0; + foreach my $item ( $biblioitem->items() ) { + my $itemnumber = $item->itemnumber(); + + my $error = $test ? "Test mode enabled" : DelItemCheck( undef, $biblionumber, $itemnumber ); + $error = undef if $error eq '1'; + + if ($error) { + say "ERROR DELETING ITEM $itemnumber: $error"; + } + else { + say "DELETED ITEM $itemnumber" if $verbose; + $deleted_items_count++; + } + + $total_items_count++; + } + + } + + my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber); + if ( $error ) { + say "ERROR DELETING BIBLIO $biblionumber: $error"; + } else { + say "DELETED BIBLIO $biblionumber" if $verbose; + $deleted_records_count++; + } + + say q{}; +} + +if ( $verbose ) { + say "DELETED $deleted_records_count OF $total_records_count RECORDS"; + say "DELETED $deleted_items_count OF $total_items_count ITEMS" if $delete_items; +} -- 2.39.5