Bug 17949: Fix batch patron deletion tool
[koha.git] / misc / migration_tools / remove_unused_authorities.pl
1 #!/usr/bin/perl
2
3 #script to administer Authorities without biblio
4
5 # Copyright 2009 BibLibre
6 # written 2009-05-04 by paul dot poulain at biblibre.com
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use strict;
24 use warnings;
25
26 use C4::Context;
27 use C4::AuthoritiesMarc;
28 use Getopt::Long;
29
30 my @authtypes;
31 my $want_help = 0;
32 my $test = 0;
33 GetOptions(
34     'aut|authtypecode:s' => \@authtypes,
35     't|test'             => \$test,
36     'h|help'             => \$want_help
37 );
38
39 if ($want_help) {
40     print_usage();
41     exit 0;
42 }
43
44 if ($test) {
45     print "testing only, authorities will not be deleted.\n";
46 }
47
48 my $errZebraConnection = C4::Context->Zconn("biblioserver",0)->errcode();
49 if ( $errZebraConnection == 10000 ) {
50     die "Zebra server seems not to be available. This script needs Zebra runs."
51 } elsif ( $errZebraConnection ) {
52     die "Error from Zebra: $errZebraConnection";
53 }
54
55 my $dbh=C4::Context->dbh;
56 my $thresholdmin=0;
57 my $thresholdmax=0;
58 my @results;
59 # prepare the request to retrieve all authorities of the requested types
60 my $rqsql = "SELECT * from auth_header where 1";
61 $rqsql .= " AND authtypecode IN (".join(",",map{$dbh->quote($_)}@authtypes).")" if @authtypes;
62 my $rqselect = $dbh->prepare($rqsql);
63 $|=1;
64
65 $rqselect->execute;
66 my $counter=0;
67 my $totdeleted=0;
68 my $totundeleted=0;
69 while (my $data=$rqselect->fetchrow_hashref){
70     my $query;
71     $query= "an=".$data->{'authid'};
72     # search for biblios mapped
73     my ($err,$res,$used) = C4::Search::SimpleSearch($query,0,10);
74     if (defined $err) {
75         warn "error: $err on search $query\n";
76         next;
77     }
78     print ".";
79     print "$counter\n" unless $counter++ % 100;
80     # if found, delete, otherwise, just count
81     if ($used>=$thresholdmin and $used<=$thresholdmax){
82         DelAuthority($data->{'authid'}) unless $test;
83         $totdeleted++;
84     } else {
85         $totundeleted++;
86     }
87 }
88
89 print "$counter authorities parsed, $totdeleted deleted and $totundeleted unchanged because used\n";
90
91
92 sub print_usage {
93     print <<_USAGE_;
94 $0: Remove unused authority records
95
96 This script removes authority records that do not have any biblio
97 records attached to them.
98
99 If the --aut option is supplied, only authority records of that
100 particular type will be checked for usage.  --aut can be repeated.
101
102 If --aut is not supplied, all authority records will be checked.
103
104 Use --test to perform a test run.  This script does not ask the
105 operator to confirm the deletion of each authority record.
106
107 parameters
108     --aut|authtypecode TYPE       the list of authtypes to check
109     --test or -t                  test mode, don't delete really, just count
110     --help or -h                  show this message.
111
112 _USAGE_
113 }