Bug 30477: Add new UNIMARC installer translation files
[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 use Getopt::Long qw( GetOptions );
27 use Pod::Usage qw( pod2usage );
28 use Koha::Script -cron;
29 use C4::Biblio qw( DelBiblio );
30 use Koha::Database;
31 use Koha::Biblios;
32 use Koha::Biblio::Metadatas;
33 use Koha::Items;
34
35 my $delete_items;
36 my $confirm;
37 my $test;
38 my $verbose;
39 my $help;
40
41 GetOptions(
42     'i|di|delete-items' => \$delete_items,
43     'c|confirm'         => \$confirm,
44     't|test'            => \$test,
45     'v|verbose'         => \$verbose,
46     'h|help'            => \$help,
47 );
48
49 pod2usage(q|--test and --confirm cannot be specified together|) if $test and $confirm;
50
51 unless ( $confirm or $test ) {
52     warn "Running in test mode as --confirm is not passed\n";
53     $test = 1;
54 }
55
56 if ( $help ) {
57     say qq{
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.
69 };
70     exit();
71 }
72
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%' } } );
76
77 my $total_records_count   = @metadatas;
78 my $deleted_records_count = 0;
79 my $total_items_count     = 0;
80 my $deleted_items_count   = 0;
81 foreach my $m (@metadatas) {
82     my $biblionumber = $m->get_column('biblionumber');
83
84     say "RECORD: $biblionumber" if $verbose;
85
86     if ($delete_items) {
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;
92
93             if( $test ){
94                 my $deleted = $item->safe_to_delete;
95                 if ( $deleted ) {
96                     say "TEST MODE: Item $itemnumber would have been deleted";
97                 } else {
98                     my $error = @{$deleted->messages}[0]->message;
99                     say "TEST MODE: ERROR DELETING ITEM $itemnumber: $error";
100                 }
101             } else {
102                 my $deleted = $item->safe_to_delete;
103                 if ( $deleted ) {
104                     say "DELETED ITEM $itemnumber" if $verbose;
105                     $deleted_items_count++;
106                 } else {
107                     my $error = @{$deleted->messages}[0]->message;
108                     say "ERROR DELETING ITEM $itemnumber: $error";
109                 }
110             }
111             $total_items_count++;
112         }
113     }
114
115     my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber);
116     if ( $error ) {
117         say "ERROR DELETING BIBLIO $biblionumber: $error";
118     } else {
119         say "DELETED BIBLIO $biblionumber" if $verbose;
120         $deleted_records_count++;
121     }
122
123     say q{};
124 }
125
126 if ( $verbose ) {
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;
129 }