3 #-----------------------------------
4 # Copyright 2013 ByWater Solutions
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 #-----------------------------------
24 binmode( STDOUT, ":encoding(UTF-8)" );
26 use Getopt::Long qw( GetOptions );
27 use Pod::Usage qw( pod2usage );
28 use Koha::Script -cron;
29 use C4::Biblio qw( DelBiblio );
32 use Koha::Biblio::Metadatas;
42 'i|di|delete-items' => \$delete_items,
43 'c|confirm' => \$confirm,
45 'v|verbose' => \$verbose,
49 pod2usage(q|--test and --confirm cannot be specified together|) if $test and $confirm;
51 unless ( $confirm or $test ) {
52 warn "Running in test mode as --confirm is not passed\n";
58 delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd'
59 usage: delete_records_via_leader.pl --confirm --verbose [--test]
60 This script has the following parameters :
61 -h --help: Prints this message
62 -c --confirm: Script will do nothing without this parameter
63 -v --verbose: Be verbose
64 -t --test: Test mode, does not delete records.
65 Test mode cannot determine if a record/item will be deleted successfully,
66 it will only tell you what records and items the script will attempt to delete.
67 -i --delete-items: Try deleting items before deleting record.
68 Records with items cannot be deleted.
73 my $metadatas = # Should be replaced by a call to C4::Search on zebra index
74 # Record-status when bug 15537 will be pushed
75 Koha::Biblio::Metadatas->search( { format => 'marcxml', schema => C4::Context->preference('marcflavour'), metadata => { LIKE => '%<leader>_____d%' } } );
77 my $total_records_count = $metadatas->count;
78 my $deleted_records_count = 0;
79 my $total_items_count = 0;
80 my $deleted_items_count = 0;
81 while ( my $m = $metadatas->next ) {
82 my $biblionumber = $m->biblionumber;
84 say "RECORD: $biblionumber" if $verbose;
87 my $deleted_count = 0;
88 my $biblio = Koha::Biblios->find( $biblionumber );
89 my @items = Koha::Items->search( { biblionumber => $biblionumber } )->as_list;
90 foreach my $item ( @items ) {
91 my $itemnumber = $item->itemnumber;
94 my $deleted = $item->safe_to_delete;
96 say "TEST MODE: Item $itemnumber would have been deleted";
98 my $error = @{$deleted->messages}[0]->message;
99 say "TEST MODE: ERROR DELETING ITEM $itemnumber: $error";
102 my $deleted = $item->safe_delete;
104 say "DELETED ITEM $itemnumber" if $verbose;
105 $deleted_items_count++;
107 my $error = @{$deleted->messages}[0]->message;
108 say "ERROR DELETING ITEM $itemnumber: $error";
111 $total_items_count++;
115 my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber);
117 say "ERROR DELETING BIBLIO $biblionumber: $error";
119 say "DELETED BIBLIO $biblionumber" if $verbose;
120 $deleted_records_count++;
127 say "DELETED $deleted_records_count OF $total_records_count RECORDS";
128 say "DELETED $deleted_items_count OF $total_items_count ITEMS" if $delete_items;