Bug 21865: improve remove_unused_authorities.pl script
[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 Modern::Perl;
24
25 use Koha::Script;
26 use C4::Context;
27 use C4::AuthoritiesMarc;
28 use Getopt::Long;
29
30 use Koha::SearchEngine::Search;
31
32 my @authtypes;
33 my $want_help = 0;
34 my $test = 0;
35 GetOptions(
36     'aut|authtypecode:s' => \@authtypes,
37     't|test'             => \$test,
38     'h|help'             => \$want_help
39 );
40
41 if ($want_help) {
42     print_usage();
43     exit 0;
44 }
45 if ($test) {
46     print "*** Testing only, authorities will not be deleted. ***\n";
47 }
48 if (@authtypes) {
49     print "Restricted to authority type(s) : ".join(',', @authtypes).".\n";
50 }
51
52 my $errZebraConnection = C4::Context->Zconn("biblioserver",0)->errcode();
53 if ( $errZebraConnection == 10000 ) {
54     die "Zebra server seems not to be available. This script needs Zebra runs.";
55 } elsif ( $errZebraConnection ) {
56     die "Error from Zebra: $errZebraConnection";
57 }
58
59 my $dbh=C4::Context->dbh;
60 my @results;
61 # prepare the request to retrieve all authorities of the requested types
62 my $rqsql = q{ SELECT authid,authtypecode FROM auth_header };
63 $rqsql .= q{ WHERE authtypecode IN (}.join(',',map{ '?' }@authtypes).')' if @authtypes;
64 my $rqselect = $dbh->prepare($rqsql);
65 $|=1;
66
67 $rqselect->execute(@authtypes);
68 my $counter=0;
69 my $totdeleted=0;
70 my $totundeleted=0;
71 my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
72 while (my $data=$rqselect->fetchrow_hashref){
73     $counter++;
74     print 'authid='.$data->{'authid'};
75     print ' type='.$data->{'authtypecode'};
76     my $bibliosearch = 'an:'.$data->{'authid'};
77     # search for biblios mapped
78     my ($err,$res,$used) = $searcher->simple_search_compat($bibliosearch,0,10);
79     if (defined $err) {
80         print "\n";
81         warn "Error: $err on search for biblios $bibliosearch\n";
82         next;
83     }
84     unless ($used > 0){
85         unless ($test) {
86             DelAuthority({ authid => $data->{'authid'} });
87             print " : deleted";
88         } else {
89             print " : can be deleted";
90         }
91         $totdeleted++;
92     } else {
93         $totundeleted++;
94         print " : used $used time(s)";
95     }
96     print "\n";
97 }
98
99 print "$counter authorities parsed\n";
100 unless ($test) {
101     print "$totdeleted deleted because unused\n";
102 } else {
103     print "$totdeleted can be deleted because unused\n";
104 }
105 print "$totundeleted unchanged because used\n";
106
107 sub print_usage {
108     print <<_USAGE_;
109 $0: Remove unused authority records
110
111 This script removes authority records that do not have any biblio
112 records attached to them.
113
114 If the --aut option is supplied, only authority records of that
115 particular type will be checked for usage.  --aut can be repeated.
116
117 If --aut is not supplied, all authority records will be checked.
118
119 Use --test to perform a test run.  This script does not ask the
120 operator to confirm the deletion of each authority record.
121
122 parameters
123     --aut|authtypecode TYPE       the list of authtypes to check
124     --test or -t                  test mode, don't delete really, just count
125     --help or -h                  show this message.
126
127 _USAGE_
128 }