Bug 24663: [19.05.x] Remove authnotrequired if set to 0
[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::Koha;
23 use C4::Output;
24 use C4::Auth;
25
26 use Koha::SearchEngine::Elasticsearch;
27 use Koha::SearchEngine::Elasticsearch::Indexer;
28 use Koha::SearchMarcMaps;
29 use Koha::SearchFields;
30
31 use Try::Tiny;
32
33 my $input = new CGI;
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
35     {   template_name   => 'admin/searchengine/elasticsearch/mappings.tt',
36         query           => $input,
37         type            => 'intranet',
38         flagsrequired   => { parameters => 'manage_search_engine_config' },
39     }
40 );
41
42 my $index = $input->param('index') || 'biblios';
43 my $op    = $input->param('op')    || 'list';
44 my @messages;
45
46 my $database = Koha::Database->new();
47 my $schema   = $database->schema;
48
49 my $marc_type = lc C4::Context->preference('marcflavour');
50
51 my @index_names = ($Koha::SearchEngine::Elasticsearch::BIBLIOS_INDEX, $Koha::SearchEngine::Elasticsearch::AUTHORITIES_INDEX);
52
53 my $update_mappings = sub {
54     for my $index_name (@index_names) {
55         my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
56         try {
57             $indexer->update_mappings();
58         } catch {
59             my $conf = $indexer->get_elasticsearch_params();
60             push @messages, {
61                 type => 'error',
62                 code => 'error_on_update_es_mappings',
63                 message => $_[0],
64                 index => $conf->{index_name},
65             };
66         };
67     }
68 };
69
70 if ( $op eq 'edit' ) {
71
72     $schema->storage->txn_begin;
73
74     my @field_name = $input->multi_param('search_field_name');
75     my @field_label = $input->multi_param('search_field_label');
76     my @field_type = $input->multi_param('search_field_type');
77     my @field_weight = $input->multi_param('search_field_weight');
78
79     my @index_name          = $input->multi_param('mapping_index_name');
80     my @search_field_name  = $input->multi_param('mapping_search_field_name');
81     my @mapping_sort        = $input->multi_param('mapping_sort');
82     my @mapping_facet       = $input->multi_param('mapping_facet');
83     my @mapping_suggestible = $input->multi_param('mapping_suggestible');
84     my @mapping_marc_field  = $input->multi_param('mapping_marc_field');
85     my @faceted_field_names = $input->multi_param('display_facet');
86
87     eval {
88
89         for my $i ( 0 .. scalar(@field_name) - 1 ) {
90             my $field_name = $field_name[$i];
91             my $field_label = $field_label[$i];
92             my $field_type = $field_type[$i];
93             my $field_weight = $field_weight[$i];
94
95             my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
96             $search_field->label($field_label);
97             $search_field->type($field_type);
98
99             if (!length($field_weight)) {
100                 $search_field->weight(undef);
101             }
102             elsif ($field_weight <= 0 || !looks_like_number($field_weight)) {
103                 push @messages, { type => 'error', code => 'invalid_field_weight', 'weight' => $field_weight };
104             }
105             else {
106                 $search_field->weight($field_weight);
107             }
108
109             my $facet_order = first { $faceted_field_names[$_] eq $field_name } 0 .. $#faceted_field_names;
110             $search_field->facet_order(defined $facet_order ? $facet_order + 1 : undef);
111             $search_field->store;
112         }
113
114         Koha::SearchMarcMaps->search( { marc_type => $marc_type, } )->delete;
115         my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields();
116         my @facetable_field_names = map { $_->name } @facetable_fields;
117
118         for my $i ( 0 .. scalar(@index_name) - 1 ) {
119             my $index_name          = $index_name[$i];
120             my $search_field_name  = $search_field_name[$i];
121             my $mapping_marc_field  = $mapping_marc_field[$i];
122             my $mapping_facet       = $mapping_facet[$i];
123             my $mapping_suggestible = $mapping_suggestible[$i];
124             my $mapping_sort        = $mapping_sort[$i];
125             $mapping_sort = undef if $mapping_sort eq 'undef';
126             $mapping_facet = ( grep {/^$search_field_name$/} @facetable_field_names ) ? $mapping_facet : 0;
127
128             my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' });
129             # TODO Check mapping format
130             my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $marc_type, marc_field => $mapping_marc_field });
131             $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping_facet, suggestible => $mapping_suggestible, sort => $mapping_sort } );
132
133         }
134     };
135     if ($@) {
136         push @messages, { type => 'error', code => 'error_on_update', message => $@, };
137         $schema->storage->txn_rollback;
138     } else {
139         push @messages, { type => 'message', code => 'success_on_update' };
140         $schema->storage->txn_commit;
141         $update_mappings->();
142     }
143 }
144 elsif( $op eq 'reset_confirmed' ) {
145     Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
146     push @messages, { type => 'message', code => 'success_on_reset' };
147 }
148 elsif( $op eq 'reset_confirm' ) {
149     $template->param( reset_confirm => 1 );
150 }
151
152
153 my @indexes;
154
155 for my $index_name (@index_names) {
156     my $indexer = Koha::SearchEngine::Elasticsearch::Indexer->new({ index => $index_name });
157     if (!$indexer->is_index_status_ok) {
158         my $conf = $indexer->get_elasticsearch_params();
159         if ($indexer->is_index_status_reindex_required) {
160             push @messages, {
161                 type => 'error',
162                 code => 'reindex_required',
163                 index => $conf->{index_name},
164             };
165         }
166         elsif($indexer->is_index_status_recreate_required) {
167             push @messages, {
168                 type => 'error',
169                 code => 'recreate_required',
170                 index => $conf->{index_name},
171             };
172         }
173     }
174 }
175
176 my @facetable_fields = Koha::SearchEngine::Elasticsearch->get_facetable_fields();
177 for my $index_name (@index_names) {
178     my $search_fields = Koha::SearchFields->search(
179         { 'search_marc_map.index_name' => $index_name, 'search_marc_map.marc_type' => $marc_type, },
180         {   join => { search_marc_to_fields => 'search_marc_map' },
181             '+select' => [ 'search_marc_to_fields.facet', 'search_marc_to_fields.suggestible', 'search_marc_to_fields.sort', 'search_marc_map.marc_field' ],
182             '+as'     => [ 'facet',                       'suggestible',                       'sort',                       'marc_field' ],
183             order_by => { -asc => [qw/name marc_field/] }
184         }
185     );
186
187     my @mappings;
188     my @facetable_field_names = map { $_->name } @facetable_fields;
189
190     while ( my $s = $search_fields->next ) {
191         my $name = $s->name;
192         push @mappings,
193           { search_field_name  => $name,
194             search_field_label => $s->label,
195             search_field_type  => $s->type,
196             marc_field         => $s->get_column('marc_field'),
197             sort               => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
198             suggestible        => $s->get_column('suggestible'),
199             facet              => $s->get_column('facet'),
200             is_facetable       => ( grep {/^$name$/} @facetable_field_names ) ? 1 : 0,
201           };
202     }
203
204     push @indexes, { index_name => $index_name, mappings => \@mappings };
205 }
206
207 my $search_fields = Koha::SearchFields->search( {}, { order_by => ['name'] } );
208 my @all_search_fields;
209 while ( my $search_field = $search_fields->next ) {
210     my $search_field_unblessed = $search_field->unblessed;
211     $search_field_unblessed->{mapped_biblios} = 1 if $search_field->is_mapped_biblios;
212     push @all_search_fields, $search_field_unblessed;
213 }
214
215 $template->param(
216     indexes           => \@indexes,
217     all_search_fields => \@all_search_fields,
218     facetable_fields  => \@facetable_fields,
219     messages          => \@messages,
220 );
221
222 output_html_with_http_headers $input, $cookie, $template->output;