Tamil/Indexer.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 158 CC 21 SCAR 56.9
CC
21
Cov
0%
run
462.0
BEGIN
2.0
BEGIN
2.0
CRAP 294.7
1 package Koha::Contrib::Tamil::Indexer;
2 # ABSTRACT: Class doing Zebra Koha indexing
3 $Koha::Contrib::Tamil::Indexer::VERSION = '0.074';
4 use Moose;
5  
6 use 5.010;
7 use utf8;
8 use Carp;
9 use Koha::Contrib::Tamil::Koha;
10 use Koha::Contrib::Tamil::RecordReader;
11 use Koha::Contrib::Tamil::RecordWriter::File::Marcxml;
12 use AnyEvent::Processor::Conversion;
13 use File::Path;
14 use IO::File;
15 use Locale::TextDomain 'Koha-Contrib-Tamil';
16  
17  
18 with 'MooseX::Getopt';
19  
20 has koha => (
21 is => 'rw',
22 isa => 'Koha::Contrib::Tamil::Koha',
23 required => 0,
24 traits => [ 'NoGetopt' ],
25 );
26  
27 has conf => (
28 is => 'rw',
29 isa => 'Str',
30 trigger => sub {
31 my ( $self, $file ) = @_;
32 $self->koha( Koha::Contrib::Tamil::Koha->new( conf_file => $file ) );
33 return $file;
34 },
35 );
36  
37 has source => (
38 is => 'rw',
39 isa => 'Koha::RecordType',
40 default => 'biblio'
41 );
42  
43 has select => (
44 is => 'rw',
45 isa => 'Koha::RecordSelect',
46 required => 1,
47 default => 'all',
48 );
49  
50 has directory => (
51 is => 'rw',
52 isa => 'Str',
53 default => './koha-index',
54 );
55  
56 has keep => ( is => 'rw', isa => 'Bool', default => 0 );
57  
58 has verbose => ( is => 'rw', isa => 'Bool', default => 0 );
59  
60 has help => (
61 is => 'rw',
62 isa => 'Bool',
63 default => 0,
64 traits => [ 'NoGetopt' ],
65 );
66  
67 has blocking => (
68 is => 'rw',
69 isa => 'Bool',
70 default => 0,
71 traits => [ 'NoGetopt' ],
72 );
73  
74  
75  
76 sub run {
77 my $self = shift;
78  
79 # Is it a full indexing of all Koha DB records?
80 my $is_full_indexing = $self->select =~ /all/i;
81  
82 # Is it biblio indexing (if not it's authority)
83 my $is_biblio_indexing = $self->source =~ /biblio/i;
84  
85 $self->koha( Koha::Contrib::Tamil::Koha->new() ) unless $self->koha;
86  
87 # STEP 1: All biblio records are exported in a directory
88  
89 unless ( -d $self->directory ) {
90 mkdir $self->directory
91 or die "Unable to create directory: " . $self->directory;
92 }
93 my $from_dir = $self->directory . "/" . $self->source;
94 mkdir $from_dir;
95 for my $dir ( ( "$from_dir/update", "$from_dir/delete") ) {
96 rmtree( $dir ) if -d $dir;
97 mkdir $dir;
98 }
99  
100 # DOM indexing? otherwise GRS-1
101 # Par défaut DOM depuis 18.11 avec dispartion des param zebra_bib_index_mode
102 my $is_dom = $self->source eq 'biblio' ? 'zebra_bib_index_mode' : 'zebra_auth_index_mode';
103 $is_dom = $self->koha->conf->{config}->{$is_dom} || '';
104 $is_dom = $is_dom =~ /grs/i ? 0 : 1;
105  
106 # STEP 1.1: Records to update
107 print __"Exporting records to update", "\n" if $self->verbose;
108 my $exporter = AnyEvent::Processor::Conversion->new(
109 reader => Koha::Contrib::Tamil::RecordReader->new(
110 koha => $self->koha,
111 source => $self->source,
112 select => $is_full_indexing ? 'all' : 'queue_update',
113 xml => '1'
114 ),
115 writer => Koha::Contrib::Tamil::RecordWriter::File::Marcxml->new(
116 fh => IO::File->new( "$from_dir/update/records", '>:encoding(utf8)' ),
117 valid => $is_dom ),
118 blocking => $self->blocking,
119 verbose => $self->verbose,
120 );
121 $exporter->run();
122  
123 # STEP 1.2: Record to delete, if zebraqueue
124 if ( ! $is_full_indexing ) {
125 print __"Exporting records to delete", "\n" if $self->verbose;
126 $exporter = AnyEvent::Processor::Conversion->new(
127 reader => Koha::Contrib::Tamil::RecordReader->new(
128 koha => $self->koha,
129 source => $self->source,
130 select => 'queue_delete',
131 xml => '1'
132 ),
133 writer => Koha::Contrib::Tamil::RecordWriter::File::Marcxml->new(
134 fh => IO::File->new( "$from_dir/delete/records", '>:utf8' ),
135 valid => $is_dom ),
136 blocking => $self->blocking,
137 verbose => $self->verbose,
138 );
139 $exporter->run();
140 }
141  
142 # STEP 2: Run zebraidx
143  
144 my $cmd;
145 my $zconfig = $self->koha->conf->{server}->{
146 $is_biblio_indexing ? 'biblioserver' : 'authorityserver' }->{config};
147 my $db_name = $is_biblio_indexing ? 'biblios' : 'authorities';
148 my $cmd_base = "zebraidx -c " . $zconfig;
149 $cmd_base .= " -n" if $is_full_indexing; # No shadow: no indexing daemon
150 $cmd_base .= $self->verbose ? " -v warning,log" : " -v none";
151 $cmd_base .= " -g marcxml";
152 $cmd_base .= " -d $db_name";
153  
154 if ( $is_full_indexing ) {
155 $cmd = "$cmd_base init";
156 print "$cmd\n" if $self->verbose;
157 system( $cmd );
158 }
159  
160 $cmd = "$cmd_base update $from_dir/update";
161 print "$cmd\n" if $self->verbose;
162 system( $cmd );
163  
164 if ( ! $is_full_indexing ) {
165 $cmd = "$cmd_base adelete $from_dir/delete";
166 print "$cmd\n" if $self->verbose;
167 system( $cmd );
168 my $cmd = "$cmd_base commit";
169 print "$cmd\n" if $self->verbose;
170 system( $cmd );
171 }
172  
173 rmtree( $self->directory ) unless $self->keep;
174 }
175  
176  
177 no Moose;
178 __PACKAGE__->meta->make_immutable;
179  
180 =pod
181  
182 =encoding UTF-8
183  
184 =head1 NAME
185  
186 Koha::Contrib::Tamil::Indexer - Class doing Zebra Koha indexing
187  
188 =head1 VERSION
189  
190 version 0.074
191  
192 =head1 METHODS
193  
194 =head2 run
195  
196 Runs the indexing task.
197  
198 =HEAD1 SYNOPSIS
199  
200 my $indexer = Koha::Contrib::Tamil::Indexer->new(
201 source => 'biblio',
202 select => 'queue'
203 );
204 $indexer->run();
205  
206 my $indexer = Koha::Contrib::Tamil::Indexer->new(
207 source => 'authority',
208 select => 'all',
209 directory => '/tmp',
210 verbose => 1,
211 );
212 $indexer->run();
213  
214 =HEAD1 DESCRIPTION
215  
216 Indexes Koha biblio/authority records, full indexing or queued record indexing.
217  
218 =head1 AUTHOR
219  
220 Frédéric Demians <f.demians@tamil.fr>
221  
222 =head1 COPYRIGHT AND LICENSE
223  
224 This software is Copyright (c) 2025 by Fréderic Démians.
225  
226 This is free software, licensed under:
227  
228 The GNU General Public License, Version 3, June 2007
229  
230 =cut
231  
232 __END__