Bug 18631: Use - instead of _ for options
[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 qw( GetOptions );
35 use Pod::Usage qw( pod2usage );
36 use Koha::Script -cron;
37 use C4::Biblio qw( DelBiblio );
38 use Koha::Database;
39 use Koha::Biblios;
40 use Koha::Biblio::Metadatas;
41 use Koha::Items;
42
43 my $delete_items;
44 my $confirm;
45 my $test;
46 my $verbose;
47 my $help;
48
49 GetOptions(
50     'i|di|delete-items' => \$delete_items,
51     'c|confirm'         => \$confirm,
52     't|test'            => \$test,
53     'v|verbose'         => \$verbose,
54     'h|help'            => \$help,
55 );
56
57 pod2usage(q|--test and --confirm cannot be specified together|) if $test and $confirm;
58
59 unless ( $confirm or $test ) {
60     warn "Running in test mode as --confirm is not passed\n";
61     $test = 1;
62 }
63
64 if ( $help ) {
65     say qq{
66 delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd'
67 usage: delete_records_via_leader.pl --confirm --verbose [--test]
68 This script has the following parameters :
69     -h --help: Prints this message
70     -c --confirm: Script will do nothing without this parameter
71     -v --verbose: Be verbose
72     -t --test: Test mode, does not delete records.
73                Test mode cannot determine if a record/item will be deleted successfully,
74                it will only tell you what records and items the script will attempt to delete.
75     -i --delete-items: Try deleting items before deleting record.
76                        Records with items cannot be deleted.
77 };
78     exit();
79 }
80
81 my @metadatas =    # Should be replaced by a call to C4::Search on zebra index
82                    # Record-status when bug 15537 will be pushed
83   Koha::Biblio::Metadatas->search( { format => 'marcxml', schema => C4::Context->preference('marcflavour'), metadata => { LIKE => '%<leader>_____d%' } } );
84
85 my $total_records_count   = @metadatas;
86 my $deleted_records_count = 0;
87 my $total_items_count     = 0;
88 my $deleted_items_count   = 0;
89 foreach my $m (@metadatas) {
90     my $biblionumber = $m->get_column('biblionumber');
91
92     say "RECORD: $biblionumber" if $verbose;
93
94     if ($delete_items) {
95         my $deleted_count = 0;
96         my $biblio = Koha::Biblios->find( $biblionumber );
97         my @items = Koha::Items->search( { biblionumber => $biblionumber } );
98         foreach my $item ( @items ) {
99             my $itemnumber = $item->itemnumber;
100
101             if( $test ){
102                 my $result = $item->safe_to_delete;
103                 if ( $result eq "1") {
104                     say "TEST MODE: Item $itemnumber would have been deleted";
105                 } else {
106                     say "TEST MODE: ERROR DELETING ITEM $itemnumber: $result";
107                 }
108             } else {
109                 my $result = $item->safe_delete;
110                 if ( ref $result eq "Koha::Item" ){
111                     say "DELETED ITEM $itemnumber" if $verbose;
112                     $deleted_items_count++;
113                 } else {
114                     say "ERROR DELETING ITEM $itemnumber: $result";
115                 }
116             }
117             $total_items_count++;
118         }
119     }
120
121     my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber);
122     if ( $error ) {
123         say "ERROR DELETING BIBLIO $biblionumber: $error";
124     } else {
125         say "DELETED BIBLIO $biblionumber" if $verbose;
126         $deleted_records_count++;
127     }
128
129     say q{};
130 }
131
132 if ( $verbose ) {
133     say "DELETED $deleted_records_count OF $total_records_count RECORDS";
134     say "DELETED $deleted_items_count OF $total_items_count ITEMS" if $delete_items;
135 }