]> git.koha-community.org Git - koha.git/blob - Koha/Indexer/Indexing.pm
Bug 11081: Port Koha::Contrib::Tamil indexer into Koha code base
[koha.git] / Koha / Indexer / Indexing.pm
1 package Koha::Indexer::Indexing;
2
3 use Moose;
4
5 use Modern::Perl;
6 use utf8;
7 use Carp;
8 use Koha::Indexer::RecordReader;
9 use Koha::Indexer::RecordWriter;
10 use AnyEvent::Processor::Conversion;
11 use File::Path;
12 use IO::File;
13 use C4::Context;
14
15
16 with 'MooseX::Getopt';
17
18
19 has source => (
20     is      => 'rw',
21     isa     => 'Koha::RecordType',
22     default => 'biblio'
23 );
24
25 has select => (
26     is       => 'rw',
27     isa      => 'Koha::RecordSelect',
28     required => 1,
29     default  => 'all',
30 );
31
32 has directory => (
33     is      => 'rw',
34     isa     => 'Str',
35     default => './koha-index',
36 );
37
38 has keep => ( is => 'rw', isa => 'Bool', default => 0 );
39
40 has verbose => ( is => 'rw', isa => 'Bool', default => 0 );
41
42 has help => (
43     is      => 'rw',
44     isa     => 'Bool',
45     default => 0,
46     traits  => [ 'NoGetopt' ],
47 );
48
49 has blocking => (
50     is      => 'rw',
51     isa     => 'Bool',
52     default => 0,
53     traits  => [ 'NoGetopt' ],
54 );
55
56
57 sub run {
58     my $self = shift;
59
60     # Is it a full indexing of all Koha DB records?
61     my $is_full_indexing = $self->select =~ /all/i;
62
63     # Is it biblio indexing (if not it's authority)
64     my $is_biblio_indexing = $self->source =~ /biblio/i;
65
66     # STEP 1: All biblio records are exported in a directory
67
68     unless ( -d $self->directory ) {
69         mkdir $self->directory
70             or die "Unable to create directory: " . $self->directory;
71     }
72     my $from_dir = $self->directory . "/" . $self->source;
73     mkdir $from_dir;
74     for my $dir ( ( "$from_dir/update", "$from_dir/delete") ) {
75         rmtree( $dir ) if -d $dir;
76         mkdir $dir;
77     }
78
79     # DOM indexing? otherwise GRS-1
80     my $is_dom = $self->source eq 'biblio'
81                  ? 'zebra_bib_index_mode'
82                  : 'zebra_auth_index_mode';
83     $is_dom = C4::Context->config($is_dom) || '';
84     $is_dom = $is_dom =~ /dom/i ? 1 : 0;
85
86     # STEP 1.1: Records to update
87     say "Exporting records to update" if $self->verbose;
88     my $exporter = AnyEvent::Processor::Conversion->new(
89         reader => Koha::Indexer::RecordReader->new(
90             source => $self->source,
91             select => $is_full_indexing ? 'all' : 'queue_update',
92             xml    => '1'
93         ),
94         writer => Koha::Indexer::RecordWriter->new(
95             fh => IO::File->new( "$from_dir/update/records", '>:encoding(utf8)' ),
96             valid => $is_dom ),
97         blocking    => $self->blocking,
98         verbose     => $self->verbose,
99     );
100     $exporter->run();
101
102     # STEP 1.2: Record to delete, if zebraqueue
103     if ( ! $is_full_indexing ) {
104         say "Exporting records to delete" if $self->verbose;
105         $exporter = AnyEvent::Processor::Conversion->new(
106             reader => Koha::Indexer::RecordReader->new(
107                 source => $self->source,
108                 select => 'queue_delete',
109                 xml    => '1'
110             ),
111             writer => Koha::Indexer::RecordWriter->new(
112                 fh => IO::File->new( "$from_dir/delete/records", '>:encoding(utf8)' ),
113                 valid => $is_dom ),
114             blocking    => $self->blocking,
115             verbose     => $self->verbose,
116         );
117         $exporter->run();
118     }
119
120     # STEP 2: Run zebraidx
121
122     my $cmd;
123     my $zconfig  = C4::Context->zebraconfig(
124        $is_biblio_indexing ? 'biblioserver' : 'authorityserver')->{config};
125     my $db_name  = $is_biblio_indexing ? 'biblios' : 'authorities';
126     my $cmd_base = "zebraidx -c " . $zconfig;
127     $cmd_base   .= " -n" if $is_full_indexing; # No shadow: no indexing daemon
128     $cmd_base   .= $self->verbose ? " -v warning,log" : " -v none";
129     $cmd_base   .= " -g marcxml";
130     $cmd_base   .= " -d $db_name";
131
132     if ( $is_full_indexing ) {
133         $cmd = "$cmd_base init";
134         say $cmd if $self->verbose;
135         system( $cmd );
136     }
137
138     $cmd = "$cmd_base update $from_dir/update";
139     say $cmd if $self->verbose;
140     system( $cmd );
141
142     if ( ! $is_full_indexing ) {
143         $cmd = "$cmd_base adelete $from_dir/delete";
144         say $cmd if $self->verbose;
145         system( $cmd );
146         my $cmd = "$cmd_base commit";
147         say $cmd if $self->verbose;
148         system( $cmd );
149     }
150
151     rmtree( $self->directory ) unless $self->keep;
152 }
153
154
155 no Moose;
156 __PACKAGE__->meta->make_immutable;
157
158 __END__
159 =pod
160
161 =head1 SYNOPSIS
162
163  my $indexer = Koha::Indexer->new(
164    source => 'biblio',
165    select => 'queue'
166  );
167  $indexer->run();
168
169  my $indexer = Koha::Indexer->new(
170    source    => 'authority',
171    select    => 'all',
172    directory => '/tmp',
173    verbose   => 1,
174  );
175  $indexer->run();
176
177 =head1 DESCRIPTION
178
179 Indexes Koha biblio/authority records, full indexing or queued record indexing.
180
181
182 =head1 Methods
183
184 =over
185
186 =item run
187
188 Runs the indexing task.
189
190 =back
191
192
193 =cut
194
195 1;