Bug 14349: Checkouts and Fines tab missing category description on the left
[koha.git] / misc / migration_tools / remove_unused_authorities.pl
blob25093183fa15ef1d574c36a2360d7de35c12e6aa
1 #!/usr/bin/perl
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>.
23 use strict;
24 use warnings;
26 use C4::Context;
27 use C4::AuthoritiesMarc;
28 use Getopt::Long;
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
39 if ($want_help) {
40 print_usage();
41 exit 0;
44 if ($test) {
45 print "testing only, authorities will not be deleted.\n";
48 my $dbh=C4::Context->dbh;
49 my $thresholdmin=0;
50 my $thresholdmax=0;
51 my @results;
52 # prepare the request to retrieve all authorities of the requested types
53 my $rqsql = "SELECT * from auth_header where 1";
54 $rqsql .= " AND authtypecode IN (".join(",",map{$dbh->quote($_)}@authtypes).")" if @authtypes;
55 my $rqselect = $dbh->prepare($rqsql);
56 $|=1;
58 $rqselect->execute;
59 my $counter=0;
60 my $totdeleted=0;
61 my $totundeleted=0;
62 while (my $data=$rqselect->fetchrow_hashref){
63 my $query;
64 $query= "an=".$data->{'authid'};
65 # search for biblios mapped
66 my ($err,$res,$used) = C4::Search::SimpleSearch($query,0,10);
67 if (defined $err) {
68 warn "error: $err on search $query\n";
69 next;
71 print ".";
72 print "$counter\n" unless $counter++ % 100;
73 # if found, delete, otherwise, just count
74 if ($used>=$thresholdmin and $used<=$thresholdmax){
75 DelAuthority($data->{'authid'}) unless $test;
76 $totdeleted++;
77 } else {
78 $totundeleted++;
82 print "$counter authorities parsed, $totdeleted deleted and $totundeleted unchanged because used\n";
85 sub print_usage {
86 print <<_USAGE_;
87 $0: Remove unused authority records
89 This script removes authority records that do not have any biblio
90 records attached to them.
92 If the --aut option is supplied, only authority records of that
93 particular type will be checked for usage. --aut can be repeated.
95 If --aut is not supplied, all authority records will be checked.
97 Use --test to perform a test run. This script does not ask the
98 operator to confirm the deletion of each authority record.
100 parameters
101 --aut|authtypecode TYPE the list of authtypes to check
102 --test or -t test mode, don't delete really, just count
103 --help or -h show this message.
105 _USAGE_