Bug 11441: (follow-up) improve utility help text
[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 under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
14 #
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 $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;
57
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;
70     }
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++;
79     }
80 }
81
82 print "$counter authorities parsed, $totdeleted deleted and $totundeleted unchanged because used\n";
83
84
85 sub print_usage {
86     print <<_USAGE_;
87 $0: Remove unused authority records
88
89 This script removes authority records that do not have any biblio
90 records attached to them.
91
92 If the --aut option is supplied, only authority records of that
93 particular type will be checked for usage.  --aut can be repeated.
94
95 If --aut is not supplied, all authority records will be checked.
96
97 Use --test to perform a test run.  This script does not ask the
98 operator to confirm the deletion of each authority record.
99
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.
104
105 _USAGE_
106 }