Bug 2720 - Overdues which debar automatically should undebar automatically when returned
[koha.git] / misc / migration_tools / rebuild_solr.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 BibLibre SARL
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use Data::Dumper;
22 use Getopt::Long;
23 use LWP::Simple;
24 use XML::Simple;
25
26 use C4::Context;
27 use C4::Search;
28 use Koha::SearchEngine::Index;
29
30 $|=1; # flushes output
31
32 if ( C4::Context->preference("SearchEngine") ne 'Solr' ) {
33     warn "System preference 'SearchEngine' not equal 'Solr'.";
34     warn "We can not indexing";
35     exit(1);
36 }
37
38 #Setup
39
40 my ( $reset, $number, $recordtype, $biblionumbers, $optimize, $info, $want_help );
41 GetOptions(
42     'r'   => \$reset,
43     'n:s' => \$number,
44     't:s' => \$recordtype,
45     'w:s' => \$biblionumbers,
46     'o'   => \$optimize,
47     'i'   => \$info,
48     'h|help' => \$want_help,
49 );
50 my $debug = C4::Context->preference("DebugLevel");
51 my $index_service = Koha::SearchEngine::Index->new;
52 my $solrurl = $index_service->searchengine->config->SolrAPI;
53
54 my $ping = &ping_command;
55 if (!defined $ping) {
56     print "SolrAPI = $solrurl\n";
57     print "Solr is Down\n";
58     exit(1);
59 }
60
61 #Script
62
63 &print_help if ($want_help);
64 &print_info if ($info);
65 if ($reset){
66   if ($recordtype){
67       &reset_index("recordtype:".$recordtype);
68   } else {
69       &reset_index("*:*");
70   }
71 }
72
73 if (defined $biblionumbers){
74     if (not defined $recordtype) { print "You must specify a recordtype\n"; exit 1;}
75     &index_biblio($_) for split ',', $biblionumbers;
76 } elsif  (defined $recordtype) {
77     &index_data;
78     &optimise_index;
79 }
80
81 if ($optimize) {
82     &optimise_index;
83 }
84
85 #Functions
86
87 sub index_biblio {
88     my ($biblionumber) = @_;
89     $index_service->index_record($recordtype, [ $biblionumber ] );
90 }
91
92 sub index_data {
93     my $dbh = C4::Context->dbh;
94         $dbh->do('SET NAMES UTF8;');
95
96     my $query;
97     if ( $recordtype eq 'biblio' ) {
98       $query = "SELECT biblionumber FROM biblio ORDER BY biblionumber";
99     } elsif ( $recordtype eq 'authority' ) {
100       $query = "SELECT authid FROM auth_header ORDER BY authid";
101     }
102     $query .= " LIMIT $number" if $number;
103
104     my $sth = $dbh->prepare( $query );
105     $sth->execute();
106
107     $index_service->index_record($recordtype, [ map { $_->[0] } @{ $sth->fetchall_arrayref } ] );
108
109     $sth->finish;
110 }
111
112 sub reset_index {
113     &reset_command;
114     &commit_command;
115     $debug eq '2' && &count_all_docs eq 0 && warn  "Index cleaned!"
116 }
117
118 sub commit_command {
119     my $commiturl = "/update?stream.body=%3Ccommit/%3E";
120     my $urlreturns = get $solrurl.$commiturl;
121 }
122
123 sub ping_command {
124     my $pingurl = "/admin/ping";
125     my $urlreturns = get $solrurl.$pingurl;
126 }
127
128 sub reset_command {
129     my ($query) = @_;
130     my $deleteurl = "/update?stream.body=%3Cdelete%3E%3Cquery%3E".$query."%3C/query%3E%3C/delete%3E";
131     my $urlreturns = get $solrurl.$deleteurl;
132 }
133
134 sub optimise_index {
135     $index_service->optimize;
136 }
137
138 sub count_all_docs {
139     my $queryurl = "/select/?q=*:*";
140     my $urlreturns = get $solrurl.$queryurl;
141     my $xmlsimple = XML::Simple->new();
142     my $data = $xmlsimple->XMLin($urlreturns);
143     return $data->{result}->{numFound};
144 }
145
146 sub print_info {
147     my $count = &count_all_docs;
148     print <<_USAGE_;
149 SolrAPI = $solrurl
150 How many indexed documents = $count;
151 _USAGE_
152 }
153
154 sub print_help {
155     print <<_USAGE_;
156 $0: reindex biblios and/or authorities in Solr.
157
158 Use this batch job to reindex all biblio or authority records in your Koha database.  This job is useful only if you are using Solr search engine.
159
160 Parameters:
161     -t biblio               index bibliographic records
162
163     -t authority            index authority records
164
165     -r                      clear Solr index before adding records to index - use this option carefully!
166
167     -n 100                  index 100 first records
168
169     -n "100,2"              index 2 records after 100th (101 and 102)
170
171     -w 101                  index biblio with biblionumber equals 101
172
173     -o                      launch optimize command at the end of indexing
174
175     -i                      gives solr install information: SolrAPI value and count all documents indexed
176
177     --help or -h            show this message.
178 _USAGE_
179 }