3 #script to administer Authorities without biblio
5 # Copyright 2009 BibLibre
6 # written 2009-05-04 by paul dot poulain at biblibre.com
8 # This file is part of Koha.
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.
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.
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>.
28 use C4::AuthoritiesMarc;
31 use Koha::SearchEngine::Search;
37 'aut|authtypecode:s' => \@authtypes,
39 'h|help' => \$want_help
48 print "testing only, authorities will not be deleted.\n";
51 my $errZebraConnection = C4::Context->Zconn("biblioserver",0)->errcode();
52 if ( $errZebraConnection == 10000 ) {
53 die "Zebra server seems not to be available. This script needs Zebra runs."
54 } elsif ( $errZebraConnection ) {
55 die "Error from Zebra: $errZebraConnection";
58 my $dbh=C4::Context->dbh;
62 # prepare the request to retrieve all authorities of the requested types
63 my $rqsql = "SELECT * from auth_header where 1";
64 $rqsql .= " AND authtypecode IN (".join(",",map{$dbh->quote($_)}@authtypes).")" if @authtypes;
65 my $rqselect = $dbh->prepare($rqsql);
72 my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
73 while (my $data=$rqselect->fetchrow_hashref){
75 $query= "an=".$data->{'authid'};
76 # search for biblios mapped
77 my ($err,$res,$used) = $searcher->simple_search_compat($query,0,10);
79 warn "error: $err on search $query\n";
83 print "$counter\n" unless $counter++ % 100;
84 # if found, delete, otherwise, just count
85 if ($used>=$thresholdmin and $used<=$thresholdmax){
86 DelAuthority({ authid => $data->{'authid'} }) unless $test;
93 print "$counter authorities parsed, $totdeleted deleted and $totundeleted unchanged because used\n";
98 $0: Remove unused authority records
100 This script removes authority records that do not have any biblio
101 records attached to them.
103 If the --aut option is supplied, only authority records of that
104 particular type will be checked for usage. --aut can be repeated.
106 If --aut is not supplied, all authority records will be checked.
108 Use --test to perform a test run. This script does not ask the
109 operator to confirm the deletion of each authority record.
112 --aut|authtypecode TYPE the list of authtypes to check
113 --test or -t test mode, don't delete really, just count
114 --help or -h show this message.