From f130c705c10c88a9ab2878c422068e4d1cdf86b9 Mon Sep 17 00:00:00 2001 From: Bernardo Gonzalez Kriegel Date: Thu, 30 Jan 2020 11:30:30 -0300 Subject: [PATCH] Bug 13897: Process description, multiline values and SQL statements in YAML files This patch adds 3 features: 1) Display description of YAML files at install time for frameworks, and fixes it's encoding. 2) Enable use of multiline values, field required 3) Process SQL statements declared in YAML files With this features we can process files with the following generic YAML strucure: --- description: - "File description" tables: - table_name: translatable: [ title, content ] multiline: [ content ] rows: - title: "Example title" content: - "Content:" - "" - "This is the content." id: 1 value: ~ sql_statements: - "UPDATE table_name SET value ='empty' WHERE value IS NULL" ... * file description is now inside the YAML, can have multiple lines. This attribute is expected in all YAML files. * translatable attribute in table declare fields that can be translated * multiline attribute in table declare fields that can have multiple lines of content, they are joined using '\r\n' before insert into database. This is useful to express fields like 'news' content, and to simplify it's translation. '\r\n' is used for correct display in Windows clients. * sql_statements allows to add multiple SQL sentences, not insertions normally, that are executed in order. This features are not needed for the example file of this patch, but will be used in new bugs. To test: 1) Use the same test plan of first patch. Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- C4/Installer.pm | 29 ++++++++++++++----- installer/data/mysql/en/optional/auth_val.yml | 5 ++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/C4/Installer.pm b/C4/Installer.pm index 3737a3a4e4..2e096279d3 100644 --- a/C4/Installer.pm +++ b/C4/Installer.pm @@ -150,11 +150,17 @@ sub marc_framework_sql_list { my %cell; my @frameworklist; map { - my $name = substr( $_, 0, -4 ); # FIXME: restricted to 3 letter extension - open my $fh, "<:encoding(UTF-8)", "$dir/$requirelevel/$name.txt"; - my $line = <$fh>; - $line = Encode::encode('UTF-8', $line) unless ( Encode::is_utf8($line) ); - my @lines = split /\n/, $line; + my ( $name, $ext ) = split /\./, $_; + my @lines; + if ( $ext =~ /yml/ ) { + my $yaml = LoadFile("$dir/$requirelevel/$name\.$ext"); + @lines = map { Encode::decode('UTF-8', $_) } @{ $yaml->{'description'} }; + } else { + open my $fh, "<:encoding(UTF-8)", "$dir/$requirelevel/$name.txt"; + my $line = <$fh>; + $line = Encode::encode('UTF-8', $line) unless ( Encode::is_utf8($line) ); + @lines = split /\n/, $line; + } my $mandatory = ($requirelevel =~ /(mandatory|requi|oblig|necess)/i); push @frameworklist, { @@ -231,7 +237,7 @@ sub sample_data_sql_list { my @lines; if ( $ext =~ /yml/ ) { my $yaml = LoadFile("$dir/$requirelevel/$name\.$ext"); - @lines = @{ $yaml->{'description'} }; + @lines = map { Encode::decode('UTF-8', $_) } @{ $yaml->{'description'} }; } else { open my $fh, "<:encoding(UTF-8)", "$dir/$requirelevel/$name.txt"; my $line = <$fh>; @@ -484,11 +490,20 @@ sub load_sql { my $placeholders = join ",", map { "?" } @columns; # '?,..,?' string my $query = "INSERT INTO $table_name ( $fields ) VALUES ( $placeholders )"; my $sth = $dbh->prepare($query); + my @multiline = @{ $table->{$table_name}->{'multiline'} }; # to check multiline values; foreach my $row ( @rows ) { - my @values = map { $row->{$_} } @columns; + my @values = map { + my $col = $_; + ( @multiline and grep { $_ eq $col } @multiline ) + ? join "\r\n", @{$row->{$col}} # join multiline values + : $row->{$col}; + } @columns; $sth->execute( @values ); } } + for my $statement ( @{ $yaml->{'sql_statements'} } ) { # extra SQL statements + $dbh->do($statement); + } }; } if ($@){ diff --git a/installer/data/mysql/en/optional/auth_val.yml b/installer/data/mysql/en/optional/auth_val.yml index d303cfe35f..2c662a343d 100644 --- a/installer/data/mysql/en/optional/auth_val.yml +++ b/installer/data/mysql/en/optional/auth_val.yml @@ -25,6 +25,7 @@ description: tables: - authorised_values: translatable: [ lib ] + multiline: [] rows: # Reasons for acceptance or rejection of suggestions in acquisitions - category: "SUGGEST" @@ -45,6 +46,7 @@ tables: - authorised_values: translatable: [ lib, lib_opac ] + multiline: [] rows: # Desired formats for requesting new materials - category: "SUGGEST_FORMAT" @@ -74,6 +76,7 @@ tables: - authorised_values : translatable: [ lib ] + multiline: [] rows: # availability statuses - category: "LOST" @@ -181,6 +184,7 @@ tables: - authorised_values: translatable: [ lib, lib_opac ] + multiline: [] rows: # OPAC Suggestions reasons - category: "OPAC_SUG" @@ -195,6 +199,7 @@ tables: - authorised_values: translatable: [ lib ] + multiline: [] rows: # Report groups - category: "REPORT_GROUP" -- 2.20.1