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