Bug 23336: (follow-up) Fix failing test
[koha.git] / misc / migration_tools / checkNonIndexedBiblios.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 BibLibre
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 # Small script that checks if each biblio in the DB is properly indexed
21 # if it is not and if you use -z the not-indexed biblios are inserted in zebraqueue
22 # To test just ommit the -z option you will have the biblionumber of non-indexed biblios and the total
23
24 use strict;
25
26 # Koha modules used
27 use Koha::Script;
28 use C4::Context;
29 use Getopt::Long qw( GetOptions );
30
31 use Koha::SearchEngine::Search;
32
33 my ( $help, $confirm, $zebraqueue, $silent,$stealth );
34
35 GetOptions(
36     'c' => \$confirm,
37     'h' => \$help,
38     'z' => \$zebraqueue,
39     's' => \$silent,
40     'st' => \$stealth
41 );
42
43 if ( $help || ( !$confirm ) ) {
44     print_usage();
45     exit 0;
46 }
47
48 my $dbh = C4::Context->dbh;
49 my $i = 0;
50 my $count = 0;
51
52 # flushes output
53 $| = 1;
54
55 my $sth = $dbh->prepare("SELECT biblionumber FROM biblio");
56 my $sth_insert = $dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,operation,server,done) VALUES (?,'specialUpdate','biblioserver',0)");
57
58 my $searcher = Koha::SearchEngine::Search->new({index => 'biblios'});
59
60 # We get all biblios
61 $sth->execute;
62 my ($nbhits);
63 # We check for each biblio
64 while ( my ($biblionumber) = $sth->fetchrow ) {
65     (undef,undef,$nbhits) = $searcher->simple_search_compat("Local-number=$biblionumber");
66     print "biblionumber $biblionumber not indexed\n" unless $nbhits || $stealth;
67 # If -z option we put the biblio in zebraqueue
68     if ($zebraqueue && !$nbhits){
69         $sth_insert->execute($biblionumber);
70         print "$biblionumber inserted in zebraqueue\n" unless $stealth;
71     }
72     $i++;
73     print "$i done\n" unless $i % 1000 || $silent || $stealth;
74     $count++ unless $nbhits;
75 }
76
77 if ($count > 0 && $zebraqueue){
78     print "\t$count bibliorecords not indexed and inserted in zebraqueue\n";
79 }
80 else{
81     print "\t$count bibliorecords not indexed\n";
82 }
83
84 sub print_usage {
85     print <<_USAGE_;
86 $0: This script takes all biblios and checks if they are indexed in zebra using biblionumber search.
87
88 parameters:
89 \th this help screen
90 \tc confirm (without this parameter, you get the help screen)
91 \tz inserts a signal in zebraqueue to force indexing of non indexed biblios otherwise you have only the check
92 \ts silent throws no warnings except for non indexed records. Otherwise throws a warn every 1000 biblios to show progress
93 \tst stealth do not print warnings for non indexed records and do not warn every 1000
94
95 Syntax:
96 \t./batchCheckNonIndexedBiblios.pl -h (or without arguments => shows this screen)
97 \t./batchCheckNonIndexedBiblios.pl -c (c like confirm => checks all records (may be long)
98 \t./batchCheckNonIndexedBiblios.pl -z (z like zebraqueue => inserts in zebraqueue. Without => test only, changes nothing in DB just warns)
99 \t./batchCheckNonIndexedBiblios.pl -s (s like silent => don't throws a warn every 1000 biblios to show progress)
100 \t./batchCheckNonIndexedBiblios.pl -st (s like stealth => don't throws a warn every 1000 biblios to show progress and no warn for the non indexed biblionumbers, just the total)
101 _USAGE_
102 }