Bug 17600: Standardize our EXPORT_OK
[koha.git] / admin / searchengine / elasticsearch / mappings.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use CGI;
20 use Scalar::Util qw( looks_like_number );
21 use List::Util qw( first );
22 use C4::Output;
23 use C4::Auth;
24 use C4::Log;
25
26 use Koha::SearchEngine::Elasticsearch;
27 use Koha::SearchEngine::Elasticsearch::QueryBuilder;
28 use Koha::SearchMarcMaps;
29 use Koha::SearchFields;
30 use Koha::Caches;
31
32 use Try::Tiny qw( catch try );
33 use Module::Load::Conditional qw( can_load );
34
35
36 my $input = CGI->new;
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
38     {
39         template_name   => 'admin/searchengine/elasticsearch/mappings.tt',
40         query           => $input,
41         type            => 'intranet',
42         flagsrequired   => { parameters => 'manage_search_engine_config' },
43     }
44 );
45
46 unless ( can_load( modules => { 'Koha::SearchEngine::Elasticsearch::Indexer' => undef } ) ) {
47     output_and_exit( $input, $cookie, $template, 'missing_es_modules');
48 }
49
50
51 my $index = $input->param('index') || 'biblios';
52 my $op    = $input->param('op')    || 'list';
53 my @messages;
54 push @messages, { type => 'message', code => 'elasticsearch_disabled' }
55   if ( C4::Context->preference('SearchEngine') ne 'Elasticsearch' );
56
57 my $database = Koha::Database->new();
58 my $schema   = $database->schema;
59
60 my $marc_type = lc C4::Context->preference('marcflavour');
61
62 my @index_names = ($Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX, $Koha::SearchEngine::Elasticsearch::AUTHORITIES_INDEX);
63
64 my $update_mappings = sub {
65     for my $index_name (@index_names) {
66         my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
67         try {
68             $indexer->update_mappings();
69         } catch {
70             my $conf = $indexer->get_elasticsearch_params();
71             push @messages, {
72                 type => 'error',
73                 code => 'error_on_update_es_mappings',
74                 message => $_[0],
75                 index => $conf->{index_name},
76             };
77         };
78     }
79 };
80
81 my $search_fields_aliases = {};
82 while ( my ( $key, $value ) = each(%{Koha::SearchEngine::Elasticsearch::QueryBuilder->get_index_field_convert}) ) {
83     my $field_aliases = $search_fields_aliases->{$value};
84     $field_aliases = [] unless $field_aliases;
85     push @$field_aliases, $key;
86     $search_fields_aliases->{$value} = $field_aliases;
87 }
88
89 if ( $op eq 'edit' ) {
90
91     $schema->storage->txn_begin;
92
93     my @field_name = $input->multi_param('search_field_name');
94     my @field_label = $input->multi_param('search_field_label');
95     my @field_type = $input->multi_param('search_field_type');
96     my @field_weight = $input->multi_param('search_field_weight');
97     my @field_staff_client = $input->multi_param('search_field_staff_client');
98     my @field_opac = $input->multi_param('search_field_opac');
99
100     my @index_name          = $input->multi_param('mapping_index_name');
101     my @search_field_name   = $input->multi_param('mapping_search_field_name');
102     my @mapping_sort        = $input->multi_param('mapping_sort');
103     my @mapping_facet       = $input->multi_param('mapping_facet');
104     my @mapping_suggestible = $input->multi_param('mapping_suggestible');
105     my @mapping_search      = $input->multi_param('mapping_search');
106     my @mapping_marc_field  = $input->multi_param('mapping_marc_field');
107     my @faceted_field_names = $input->multi_param('display_facet');
108
109     eval {
110
111         for my $i ( 0 .. scalar(@field_name) - 1 ) {
112             my $field_name = $field_name[$i];
113             my $field_label = $field_label[$i];
114             my $field_type = $field_type[$i];
115             my $field_weight = $field_weight[$i];
116             my $field_staff_client = $field_staff_client[$i];
117             my $field_opac = $field_opac[$i];
118
119             my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
120             $search_field->label($field_label);
121             $search_field->type($field_type);
122
123             if (!length($field_weight)) {
124                 $search_field->weight(undef);
125             }
126             elsif ($field_weight <= 0 || !looks_like_number($field_weight)) {
127                 push @messages, { type => 'error', code => 'invalid_field_weight', 'weight' => $field_weight };
128             }
129             else {
130                 $search_field->weight($field_weight);
131             }
132             $search_field->staff_client($field_staff_client ? 1 : 0);
133             $search_field->opac($field_opac ? 1 : 0);
134
135             my $facet_order = first { $faceted_field_names[$_] eq $field_name } 0 .. $#faceted_field_names;
136             $search_field->facet_order(defined $facet_order ? $facet_order + 1 : undef);
137             $search_field->store;
138         }
139
140         Koha::SearchMarcMaps->search( { marc_type => $marc_type, } )->delete;
141         my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields();
142         my @facetable_field_names = map { $_->name } @facetable_fields;
143
144         my $mandatory_before = Koha::SearchFields->search({mandatory=>1})->count;
145         my $mandatory_after  = 0;
146         my %seen_fields;
147         for my $i ( 0 .. scalar(@index_name) - 1 ) {
148             my $index_name          = $index_name[$i];
149             my $search_field_name   = $search_field_name[$i];
150             my $mapping_marc_field  = $mapping_marc_field[$i];
151             my $mapping_facet       = $mapping_facet[$i];
152             $mapping_facet = ( grep { $_ eq $search_field_name } @facetable_field_names ) ? $mapping_facet : 0;
153             my $mapping_suggestible = $mapping_suggestible[$i];
154             my $mapping_sort        = $mapping_sort[$i] eq 'undef' ? undef : $mapping_sort[$i];
155             my $mapping_search      = $mapping_search[$i];
156
157             my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' });
158             $mandatory_after++ if $search_field->mandatory && !defined $seen_fields{$search_field_name};
159             $seen_fields{$search_field_name} = 1;
160             # TODO Check mapping format
161             my $marc_field = Koha::SearchMarcMaps->find_or_create({
162                 index_name => $index_name,
163                 marc_type => $marc_type,
164                 marc_field => $mapping_marc_field
165             });
166             $search_field->add_to_search_marc_maps($marc_field, {
167                 facet => $mapping_facet,
168                 suggestible => $mapping_suggestible,
169                 sort => $mapping_sort,
170                 search => $mapping_search
171             });
172         }
173         push @messages, { type => 'error', code => 'missing_mandatory_fields' } if $mandatory_after < $mandatory_before;
174     };
175     if ($@ || @messages) {
176         push @messages, { type => 'error', code => 'error_on_update', message => $@, };
177         $schema->storage->txn_rollback;
178     } else {
179         push @messages, { type => 'message', code => 'success_on_update' };
180
181         C4::Log::logaction( 'SEARCHENGINE', 'EDIT_MAPPINGS', undef, q{} );
182
183         $schema->storage->txn_commit;
184
185         Koha::SearchEngine::Elasticsearch->clear_search_fields_cache();
186
187         $update_mappings->();
188     }
189 }
190 elsif( $op eq 'reset_confirmed' ) {
191     Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
192     push @messages, { type => 'message', code => 'success_on_reset' };
193     C4::Log::logaction( 'SEARCHENGINE', 'RESET_MAPPINGS', undef, q{} );
194 }
195 elsif( $op eq 'reset_confirm' ) {
196     $template->param( reset_confirm => 1 );
197 }
198
199 my @indexes;
200
201 for my $index_name (@index_names) {
202     my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
203     if (!$indexer->is_index_status_ok) {
204         my $conf = $indexer->get_elasticsearch_params();
205         if ($indexer->is_index_status_reindex_required) {
206             push @messages, {
207                 type => 'error',
208                 code => 'reindex_required',
209                 index => $conf->{index_name},
210             };
211         }
212         elsif($indexer->is_index_status_recreate_required) {
213             push @messages, {
214                 type => 'error',
215                 code => 'recreate_required',
216                 index => $conf->{index_name},
217             };
218         }
219     }
220 }
221
222 my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields();
223 for my $index_name (@index_names) {
224     my $search_fields = Koha::SearchFields->search(
225         {
226             'search_marc_map.index_name' => $index_name,
227             'search_marc_map.marc_type' => $marc_type,
228         },
229         {
230             join => { search_marc_to_fields => 'search_marc_map' },
231             '+select' => [
232                 'search_marc_to_fields.facet',
233                 'search_marc_to_fields.suggestible',
234                 'search_marc_to_fields.sort',
235                 'search_marc_to_fields.search',
236                 'search_marc_map.marc_field'
237             ],
238             '+as' => [
239                 'facet',
240                 'suggestible',
241                 'sort',
242                 'search',
243                 'marc_field'
244             ],
245             order_by => { -asc => [qw/name marc_field/] }
246          }
247      );
248
249     my @mappings;
250     my @facetable_field_names = map { $_->name } @facetable_fields;
251
252     while ( my $s = $search_fields->next ) {
253         my $name = $s->name;
254         push @mappings, {
255             search_field_name  => $name,
256             search_field_label => $s->label,
257             search_field_type  => $s->type,
258             search_field_mandatory  => $s->mandatory,
259             marc_field         => $s->get_column('marc_field'),
260             sort               => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
261             suggestible        => $s->get_column('suggestible'),
262             search             => $s->get_column('search'),
263             facet              => $s->get_column('facet'),
264             is_facetable       => ( grep { $_ eq $name } @facetable_field_names ) ? 1 : 0,
265         };
266     }
267
268     push @indexes, { index_name => $index_name, mappings => \@mappings };
269 }
270
271 my $search_fields = Koha::SearchFields->search( {}, { order_by => ['name'] } );
272 my @all_search_fields;
273 while ( my $search_field = $search_fields->next ) {
274     my $search_field_unblessed = $search_field->unblessed;
275     $search_field_unblessed->{mapped_biblios} = 1 if $search_field->is_mapped_biblios;
276     $search_field_unblessed->{aliases} = $search_fields_aliases->{$search_field_unblessed->{name}};
277     push @all_search_fields, $search_field_unblessed;
278 }
279
280 $template->param(
281     indexes           => \@indexes,
282     all_search_fields => \@all_search_fields,
283     facetable_fields  => \@facetable_fields,
284     messages          => \@messages,
285 );
286
287 output_html_with_http_headers $input, $cookie, $template->output;