Bug 23682: Fix use Koha::Plugins::Handler statements
[koha.git] / misc / cronjobs / delete_records_via_leader.pl
1 #!/usr/bin/perl
2
3 #-----------------------------------
4 # Copyright 2013 ByWater Solutions
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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 #-----------------------------------
21
22 use Modern::Perl;
23
24 binmode( STDOUT, ":encoding(UTF-8)" );
25
26 BEGIN {
27
28     # find Koha's Perl modules
29     # test carefully before changing this
30     use FindBin;
31     eval { require "$FindBin::Bin/../kohalib.pl" };
32 }
33
34 use Getopt::Long;
35 use Pod::Usage;
36 use Koha::Script -cron;
37 use C4::Biblio;
38 use C4::Items;
39 use Koha::Database;
40 use Koha::Biblios;
41 use Koha::Biblio::Metadatas;
42 use Koha::Items;
43
44 my $delete_items;
45 my $confirm;
46 my $test;
47 my $verbose;
48 my $help;
49
50 GetOptions(
51     'i|di|delete-items' => \$delete_items,
52     'c|confirm'         => \$confirm,
53     't|test'            => \$test,
54     'v|verbose'         => \$verbose,
55     'h|help'            => \$help,
56 );
57
58 pod2usage(q|--test and --confirm cannot be specified together|) if $test and $confirm;
59
60 unless ( $confirm or $test ) {
61     warn "Running in test mode as --confirm is not passed\n";
62     $test = 1;
63 }
64
65 if ( $help ) {
66     say qq{
67 delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd'
68 usage: delete_records_via_leader.pl --confirm --verbose [--test]
69 This script has the following parameters :
70     -h --help: Prints this message
71     -c --confirm: Script will do nothing without this parameter
72     -v --verbose: Be verbose
73     -t --test: Test mode, does not delete records.
74                Test mode cannot determine if a record/item will be deleted successfully,
75                it will only tell you what records and items the script will attempt to delete.
76     -i --delete-items: Try deleting items before deleting record.
77                        Records with items cannot be deleted.
78 };
79     exit();
80 }
81
82 my @metadatas =    # Should be replaced by a call to C4::Search on zebra index
83                    # Record-status when bug 15537 will be pushed
84   Koha::Biblio::Metadatas->search( { format => 'marcxml', schema => C4::Context->preference('marcflavour'), metadata => { LIKE => '%<leader>_____d%' } } );
85
86 my $total_records_count   = @metadatas;
87 my $deleted_records_count = 0;
88 my $total_items_count     = 0;
89 my $deleted_items_count   = 0;
90 foreach my $m (@metadatas) {
91     my $biblionumber = $m->get_column('biblionumber');
92
93     say "RECORD: $biblionumber" if $verbose;
94
95     if ($delete_items) {
96         my $deleted_count = 0;
97         my $biblio = Koha::Biblios->find( $biblionumber );
98         my @items = Koha::Items->search( { biblionumber => $biblionumber } );
99         foreach my $item ( @items ) {
100             my $itemnumber = $item->itemnumber;
101
102             my $error = $test ? "Test mode enabled" : $item->safe_delete;
103             $error = undef if $error eq '1';
104
105             if ($error) {
106                 say "ERROR DELETING ITEM $itemnumber: $error";
107             }
108             else {
109                 say "DELETED ITEM $itemnumber" if $verbose;
110                 $deleted_items_count++;
111             }
112
113             $total_items_count++;
114         }
115
116     }
117
118     my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber);
119     if ( $error ) {
120         say "ERROR DELETING BIBLIO $biblionumber: $error";
121     } else {
122         say "DELETED BIBLIO $biblionumber" if $verbose;
123         $deleted_records_count++;
124     }
125
126     say q{};
127 }
128
129 if ( $verbose ) {
130     say "DELETED $deleted_records_count OF $total_records_count RECORDS";
131     say "DELETED $deleted_items_count OF $total_items_count ITEMS" if $delete_items;
132 }