Bug 20920: Add missing dependency on CGI::Compile
[koha.git] / misc / cronjobs / delete_records_via_leader.pl
blobadd109345e8a0b8e9a8c6ae0adb1c9451f8a53ca
1 #!/usr/bin/perl
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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 3 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #-----------------------------------
22 use Modern::Perl;
24 binmode( STDOUT, ":encoding(UTF-8)" );
26 BEGIN {
28 # find Koha's Perl modules
29 # test carefully before changing this
30 use FindBin;
31 eval { require "$FindBin::Bin/../kohalib.pl" };
34 use Getopt::Long;
35 use Pod::Usage;
36 use C4::Biblio;
37 use C4::Items;
38 use Koha::Database;
39 use Koha::Biblios;
40 use Koha::Biblio::Metadatas;
42 my $delete_items;
43 my $confirm;
44 my $test;
45 my $verbose;
46 my $help;
48 GetOptions(
49 'i|di|delete-items' => \$delete_items,
50 'c|confirm' => \$confirm,
51 't|test' => \$test,
52 'v|verbose' => \$verbose,
53 'h|help' => \$help,
56 pod2usage(q|--test and --confirm cannot be specified together|) if $test and $confirm;
58 unless ( $confirm or $test ) {
59 warn "Running in test mode as --confirm is not passed\n";
60 $test = 1;
63 if ( $help ) {
64 say qq{
65 delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd'
66 usage: delete_records_via_leader.pl --confirm --verbose [--test]
67 This script has the following parameters :
68 -h --help: Prints this message
69 -c --confirm: Script will do nothing without this parameter
70 -v --verbose: Be verbose
71 -t --test: Test mode, does not delete records.
72 Test mode cannot determine if a record/item will be deleted successfully,
73 it will only tell you what records and items the script will attempt to delete.
74 -i --delete-items: Try deleting items before deleting record.
75 Records with items cannot be deleted.
77 exit();
80 my @metadatas = # Should be replaced by a call to C4::Search on zebra index
81 # Record-status when bug 15537 will be pushed
82 Koha::Biblio::Metadatas->search( { format => 'marcxml', marcflavour => C4::Context->preference('marcflavour'), metadata => { LIKE => '%<leader>_____d%' } } );
84 my $total_records_count = @metadatas;
85 my $deleted_records_count = 0;
86 my $total_items_count = 0;
87 my $deleted_items_count = 0;
89 foreach my $m (@metadatas) {
90 my $biblionumber = $m->get_column('biblionumber');
92 say "RECORD: $biblionumber" if $verbose;
94 if ($delete_items) {
95 my $deleted_count = 0;
96 my $biblio = Koha::Biblios->find( $biblionumber );
97 my @items = $biblio ? $biblio->items : ();
98 foreach my $item ( @items ) {
99 my $itemnumber = $item->itemnumber();
101 my $error = $test ? "Test mode enabled" : DelItemCheck( $biblionumber, $itemnumber );
102 $error = undef if $error eq '1';
104 if ($error) {
105 say "ERROR DELETING ITEM $itemnumber: $error";
107 else {
108 say "DELETED ITEM $itemnumber" if $verbose;
109 $deleted_items_count++;
112 $total_items_count++;
117 my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber);
118 if ( $error ) {
119 say "ERROR DELETING BIBLIO $biblionumber: $error";
120 } else {
121 say "DELETED BIBLIO $biblionumber" if $verbose;
122 $deleted_records_count++;
125 say q{};
128 if ( $verbose ) {
129 say "DELETED $deleted_records_count OF $total_records_count RECORDS";
130 say "DELETED $deleted_items_count OF $total_items_count ITEMS" if $delete_items;