Line 0
Link Here
|
0 |
- |
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 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. |
12 |
# |
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. |
16 |
# |
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 |
#----------------------------------- |
21 |
|
22 |
use Modern::Perl; |
23 |
|
24 |
binmode( STDOUT, ":utf8" ); |
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 |
|
36 |
use C4::Biblio; |
37 |
use C4::Items; |
38 |
use Koha::Database; |
39 |
|
40 |
my $delete_items; |
41 |
my $confirm; |
42 |
my $test; |
43 |
my $verbose; |
44 |
my $help; |
45 |
|
46 |
GetOptions( |
47 |
'i|di|delete-items' => \$delete_items, |
48 |
'c|confirm' => \$confirm, |
49 |
't|test' => \$test, |
50 |
'v|verbose' => \$verbose, |
51 |
'h|help' => \$help, |
52 |
); |
53 |
|
54 |
if ( $help || !$confirm ) { |
55 |
say qq{ |
56 |
delete_records_via_leader.pl - Attempt to delete any MARC records where the leader character 5 equals 'd' |
57 |
usage: delete_records_via_leader.pl --confirm --verbose [--test] |
58 |
This script has the following parameters : |
59 |
-h --help: Prints this message |
60 |
-c --confirm: Script will do nothing without this parameter |
61 |
-v --verbose: Be verbose |
62 |
-t --test: Test mode, does not delete records |
63 |
-i --delete-items: Try deleting items before deleting record. |
64 |
Records with items cannot be deleted. |
65 |
}; |
66 |
exit(); |
67 |
} |
68 |
|
69 |
my $schema = Koha::Database->new()->schema(); |
70 |
my @biblioitems = |
71 |
$schema->resultset('Biblioitem')->search( { marc => { LIKE => '_____d%' } } ); |
72 |
|
73 |
my $total_records_count = @biblioitems; |
74 |
my $deleted_records_count = 0; |
75 |
my $total_items_count = 0; |
76 |
my $deleted_items_count = 0; |
77 |
|
78 |
foreach my $biblioitem (@biblioitems) { |
79 |
my $biblionumber = $biblioitem->get_column('biblionumber'); |
80 |
|
81 |
say "RECORD: $biblionumber" if $verbose; |
82 |
|
83 |
if ($delete_items) { |
84 |
my $deleted_count = 0; |
85 |
foreach my $item ( $biblioitem->items() ) { |
86 |
my $itemnumber = $item->itemnumber(); |
87 |
|
88 |
my $error = $test ? "Test mode enabled" : DelItemCheck( undef, $biblionumber, $itemnumber ); |
89 |
$error = undef if $error eq '1'; |
90 |
|
91 |
if ($error) { |
92 |
say "ERROR DELETING ITEM $itemnumber: $error"; |
93 |
} |
94 |
else { |
95 |
say "DELETED ITEM $itemnumber" if $verbose; |
96 |
$deleted_items_count++; |
97 |
} |
98 |
|
99 |
$total_items_count++; |
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
my $error = $test ? q{Test mode enabled} : DelBiblio($biblionumber); |
105 |
if ( $error ) { |
106 |
say "ERROR DELETING BIBLIO $biblionumber: $error"; |
107 |
} else { |
108 |
say "DELETED BIBLIO $biblionumber" if $verbose; |
109 |
$deleted_records_count++; |
110 |
} |
111 |
|
112 |
say q{}; |
113 |
} |
114 |
|
115 |
if ( $verbose ) { |
116 |
say "DELETED $deleted_records_count OF $total_records_count RECORDS"; |
117 |
say "DELETED $deleted_items_count OF $total_items_count ITEMS" if $delete_items; |
118 |
} |