From 485277f9f9e8a4f0435454d81e2c9e749c9e5739 Mon Sep 17 00:00:00 2001 From: Axel Amghar Date: Thu, 23 May 2019 11:48:01 +0200 Subject: [PATCH] Bug 22639: Elasticsearch ability to export mappings and facets settings With this patch you can download search fields , facets order, biblios and authorities table in search engine configuration Test plan: - apply the patch - make sure you have "elasticsearch" as value for the syspref "SearchEngine" - go to search engine configuration in the admin page - in the search fields part, you have a button "Download ... table" - download in (.csv), (.tab), (.ods) - verify that you have the same table in your file than in the search fields table - make the same test for : biblios table : facet order table : authorities table - Add some mappings, modify some fields, change the facets order - Save your modifications - Then dowload again - Your new files must be modified --- admin/searchengine/elasticsearch/mappings.pl | 146 +++++++++++++++++++++ .../admin/searchengine/elasticsearch/mappings.tt | 126 +++++++++++++++++- 2 files changed, 267 insertions(+), 5 deletions(-) diff --git a/admin/searchengine/elasticsearch/mappings.pl b/admin/searchengine/elasticsearch/mappings.pl index f91399a..244e0f0 100755 --- a/admin/searchengine/elasticsearch/mappings.pl +++ b/admin/searchengine/elasticsearch/mappings.pl @@ -21,12 +21,16 @@ use Scalar::Util qw(looks_like_number); use List::Util qw( first ); use C4::Koha; use C4::Output; +use C4::Context; use C4::Auth; +use Text::CSV::Encoded; +use C4::Reports::Guided; use Koha::SearchEngine::Elasticsearch; use Koha::SearchEngine::Elasticsearch::Indexer; use Koha::SearchMarcMaps; use Koha::SearchFields; +use Koha::Util::OpenDocument; use Try::Tiny; @@ -151,6 +155,138 @@ elsif( $op eq 'reset_confirmed' ) { elsif( $op eq 'reset_confirm' ) { $template->param( reset_confirm => 1 ); } +elsif ($op eq 'download'){ + my $format = $input->param("format"); + my $table = $input->param("table"); + my ($type,$reportfilename, $csv, $ods_fh, $ods_filepath); + + if ($format eq "tab"){ + $type = 'application/octet-stream'; + + } elsif ($format eq "csv"){ + $type = 'application/csv'; + my $delimiter = C4::Context->preference('delimiter') || ','; + $delimiter = "\t" if ($delimiter eq 'tabulation'); + $csv = Text::CSV::Encoded->new({ encoding_out => 'UTF-8', sep_char => $delimiter}); + $csv or die "Text::CSV::Encoded->new({binary => 1}) FAILED: " . Text::CSV::Encoded->error_diag(); + + } elsif ($format eq "ods"){ + $type = 'application/vnd.oasis.opendocument.spreadsheet'; + $ods_fh = File::Temp->new( UNLINK => 0 ); + $ods_filepath = $ods_fh->filename; + } + + #search_fields var + my (@search_field_names, @search_fileds_labels, @search_fileds_types, @search_field_weights); + #facet_var + my (@display_facet, @label_facet); + #biblios and authorities var + my (@sort, @facet, @suggestible, @marc_field); + + my @header_row; + if ($table eq "search_fields"){ + $reportfilename = "search_fields_table"; + @search_field_names = $input->multi_param("search_field_name"); + @search_fileds_labels = $input->multi_param("search_field_label"); + @search_fileds_types = $input->multi_param("search_field_type"); + @search_field_weights = $input->multi_param("search_field_weight"); + @header_row = ("Name\t\t\t","Label\t\t\t","Type","Weight"); + + } elsif ($table eq "facet"){ + $reportfilename = "facet_order_table"; + @search_field_names = $input->multi_param("search_fields_facet"); + @label_facet = $input->multi_param("label_facet"); + @display_facet = $input->multi_param("display"); + @header_row = ("Search field","Label\t\t","Display"); + + } else { + $reportfilename = $table . "_table"; + @search_field_names = $input->multi_param("search_field_names_". $table); + @sort = $input->multi_param("sort_". $table); + @facet = $input->multi_param("facet_". $table); + @suggestible = $input->multi_param("suggestible_". $table); + @marc_field = $input->multi_param("marc_field_". $table); + @header_row = ("Search field\t","Sortable","Facetable","Suggestible","Mapping"); + } + + my $i = 0; + my ($content, $ods_content); + + if ($format eq 'tab') { + $reportfilename .= ".tab"; + $content .= join("\t\t", @header_row) . "\n"; + $content = Encode::decode('UTF-8', $content); + + foreach my $search_field_name (@search_field_names){ + my @row; + if ($table eq "search_fields"){ + $search_field_name .= ' ' while (length($search_field_name)< 25); + $search_fileds_labels[$i] .= ' ' while (length($search_fileds_labels[$i])< 25); + @row = ($search_field_name, $search_fileds_labels[$i], $search_fileds_types[$i], $search_field_weights[$i]); + } elsif ($table eq "facet"){ + $search_field_name .= ' ' while (length($search_field_name)< 15); + $label_facet[$i] .= ' ' while (length($label_facet[$i])< 20); + @row = ($search_field_name, $label_facet[$i], $display_facet[$i]); + } else { + $search_field_name .= ' ' while (length($search_field_name)< 20); + $sort[$i] .= ' ' while (length($sort[$i])< 15); + $facet[$i] .= ' ' while (length($facet[$i])< 15); + $suggestible[$i] .= ' ' while (length($suggestible[$i])< 15); + @row = ($search_field_name,$sort[$i],$facet[$i],$suggestible[$i],$marc_field[$i]); + } + $content .= join("\t\t", @row) . "\n"; + $i++; + } + } elsif( $format eq 'csv' ) { + $reportfilename .= ".csv"; + if ($csv->combine(@header_row)) { + $content .= Encode::decode('UTF-8', $csv->string()) . "\n"; + foreach my $search_field_name (@search_field_names){ + my @row; + if ($table eq "search_fields"){ + @row = ($search_field_name,$search_fileds_labels[$i],$search_fileds_types[$i],$search_field_weights[$i]); + } elsif ($table eq "facet"){ + @row = ($search_field_name, $label_facet[$i], $display_facet[$i]); + } else { + @row = ($search_field_name,$sort[$i],$facet[$i],$suggestible[$i],$marc_field[$i]); + } + if ($csv->combine(@row)) { + $content .= $csv->string() . "\n"; + } + $i++; + } + } + } elsif ( $format eq 'ods' ) { + $reportfilename .= ".ods"; + my @headers = @header_row; + push @$ods_content, \@headers; + + foreach my $search_field_name (@search_field_names){ + my @content_row; + if ($table eq "search_fields"){ + push @content_row, Encode::encode( 'UTF8', $search_field_name ), Encode::encode( 'UTF8', $search_fileds_labels[$i] ) , $search_fileds_types[$i],$search_field_weights[$i]; + } elsif ($table eq "facet"){ + push @content_row, Encode::encode( 'UTF8', $search_field_name ), Encode::encode( 'UTF8', $label_facet[$i] ) , $display_facet[$i]; + } else { + push @content_row, Encode::encode( 'UTF8', $search_field_name ), $sort[$i] , $facet[$i], $suggestible[$i], Encode::encode( 'UTF8', $marc_field[$i] ); + } + push @$ods_content, \@content_row; + $i++; + } + generate_ods($ods_filepath, $ods_content); + binmode(STDOUT); + open $ods_fh, '<', $ods_filepath; + $content .= $_ while <$ods_fh>; + unlink $ods_filepath; + } + + print $input->header( + -type => $type, + -attachment=> $reportfilename + ); + print $content; + exit; +} my @indexes; @@ -215,11 +351,21 @@ while ( my $search_field = $search_fields->next ) { push @all_search_fields, $search_field_unblessed; } +my @extensions = ("csv","tab","ods"); +my @formats_and_extensions =("Semicolon separated test (.csv)","Tab separated text (.tab)","Open Document Spreadsheet (.ods)"); +my @formats; +my $j = 0; +foreach my $extension (@extensions){ + push (@formats , { extension => $extension, format_and_extension =>$formats_and_extensions[$j] }); + $j++; +} + $template->param( indexes => \@indexes, all_search_fields => \@all_search_fields, facetable_fields => \@facetable_fields, messages => \@messages, + formats => \@formats, ); output_html_with_http_headers $input, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt index 33df1fb..018859f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt @@ -26,6 +26,18 @@ return new_line; } + function create_input(value, name){ + $("#save_or_reset").append('' ); + } + + function create_submit(format, table_name){ + $(".remove").remove(); + create_input("download","op"); + create_input(format,"format"); + create_input(table_name,"table"); + $("#save_or_reset").submit(); + } + $(document).ready(function() { $("#tabs").tabs(); $('.delete').click(function() { @@ -56,12 +68,43 @@ $("#facet_biblios > table").tableDnD( { onDragClass: "dragClass", } ); + + $("#edit").click(function () { + $(".remove").remove(); + create_input("edit","op"); + $("#save_or_reset").submit(); + }); + $("#reset_confirm").click(function () { + $(".remove").remove(); + create_input("reset_confirm","op"); + $("#save_or_reset").submit(); + }); + + [% FOREACH format IN formats %] + $("#link_[% format.extension | html %]").click( function () { + create_submit("[% format.extension | html %]","search_fields"); + }); + $("#link_[% format.extension | html %]_facet").click( function () { + create_submit("[% format.extension | html %]","facet"); + }); + + [% FOREACH index IN indexes %] + $("#link_[% index.index_name | html %]_[% format.extension | html %]").click( function () { + create_submit("[% format.extension | html %]","[% index.index_name | html %]"); + }); + [% END %] + [% END %] }); @@ -142,7 +185,7 @@ a.add, a.delete { [% END %] -
+
+
+ + + +
+ + +

+

+ @@ -222,8 +279,43 @@ a.add, a.delete {
+ [% FOREACH index IN indexes %]
+
+ + + +
+ + +
+ + [% IF index.index_name == "biblios" %] +
+ + +
+ [% END %] +
+

+ + [% FOREACH mappings IN index.mappings %] + + + + + + [% END %] + @@ -357,27 +449,50 @@ a.add, a.delete { @@ -389,12 +504,13 @@ a.add, a.delete { [% END %] -

- - -

+ +

+ + +

-- 2.7.4
[% f.name | html %] + [% SWITCH f.name %] [% CASE 'author' %]Authors + + [% CASE 'itype' %]Item Types + + [% CASE 'location' %]Locations + + [% CASE 'su-geo' %]Places + + [% CASE 'title-series' %]Series + + [% CASE 'subject' %]Topics + + [% CASE 'ccode' %]Collections + + [% CASE 'holdingbranch' %]Holding libraries + + [% CASE 'homebranch' %]Home libraries + + [% CASE 'ln' %]Language + [% CASE %][% f | html %] [% END %] + [% IF f.facet_order %] + [% ELSE %] + [% END %]