From 37a61f05d02836dc02299370ee9a3109c9d46027 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 9 Mar 2026 15:07:43 +0100 Subject: [PATCH] Bug 24712: Maintenance script to load installer data When new strings are translated in the yml files of the installer (auth values, notice templates, etc.) we don't update the database. This patch adds a maintenance script to add new entries, or update the existing rows. The script misc/maintenance/load_installer_data.pl takes 4 parameters: --file: to provide a yml file to load --table and --lang to define a file to load (eg. authorised_values and fr-FR) --force to replace the existing rows Without --force only new entries will be added Examples: perl misc/maintenance/load_installer_data.pl --table letter --lang es-ES perl misc/maintenance/load_installer_data.pl --table sample_notices --lang es-ES perl misc/maintenance/load_installer_data.pl --table letter --lang es-ES --force You can also use authorised_values, account_credit_types and account_debit_types --- C4/Installer.pm | 60 ++++++++++++++++----- misc/maintenance/load_installer_data.pl | 70 +++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 14 deletions(-) create mode 100755 misc/maintenance/load_installer_data.pl diff --git a/C4/Installer.pm b/C4/Installer.pm index 7dd124fba53..75dc7b0276d 100644 --- a/C4/Installer.pm +++ b/C4/Installer.pm @@ -526,6 +526,47 @@ sub set_languages_syspref { C4::Context->clear_syspref_cache(); } +=head2 get_sql_statements_from_yml + + my @sql_statements = $installer->get_sql_statements_from_yml($filename[, $split]); + +Load the YML file passed in parameter and return the list of statements found. + +If $split is passed, each row will be inserted one by one. + +=cut + +sub get_sql_statements_from_yml { + my ( $self, $filename, $split ) = @_; + my $yaml = YAML::XS::LoadFile($filename); # Load YAML + my @statements; + for my $table ( @{ $yaml->{'tables'} } ) { + my $query_info = process_yml_table($table); + my $query = $query_info->{query}; + my $placeholders = $query_info->{placeholders}; + my $values = $query_info->{values}; + + if ($split) { + $query .= join ', ', ($placeholders); + for my $value (@$values) { + push @statements, [ $query, $value ]; + } + } else { + + # Doing only 1 INSERT query for the whole table + my @all_rows_values = map { @$_ } @$values; + $query .= join ', ', ($placeholders) x scalar @$values; + push @statements, [ $query, \@all_rows_values ]; + } + } + + # extra SQL statements + if ( $yaml->{sql_statements} ) { + push @statements, [ map { [ $_, undef, undef ] } @{ $yaml->{'sql_statements'} } ]; + } + return @statements; +} + =head2 process_yml_table my $query_info = $installer->process_yml_table($table); @@ -592,20 +633,11 @@ sub load_sql { }; } else { # YAML files eval { - my $yaml = YAML::XS::LoadFile($filename); # Load YAML - for my $table ( @{ $yaml->{'tables'} } ) { - my $query_info = process_yml_table($table); - my $query = $query_info->{query}; - my $placeholders = $query_info->{placeholders}; - my $values = $query_info->{values}; - - # Doing only 1 INSERT query for the whole table - my @all_rows_values = map { @$_ } @$values; - $query .= join ', ', ($placeholders) x scalar @$values; - $dbh->do( $query, undef, @all_rows_values ); - } - for my $statement ( @{ $yaml->{'sql_statements'} } ) { # extra SQL statements - $dbh->do($statement); + my @statements = $self->get_sql_statements_from_yml($filename); + for my $statement (@statements) { + my $sql = $statement->[0]; + my $variables = $statement->[1]; + $dbh->do( $sql, undef, @$variables ); } }; } diff --git a/misc/maintenance/load_installer_data.pl b/misc/maintenance/load_installer_data.pl new file mode 100755 index 00000000000..3b323f9b1d0 --- /dev/null +++ b/misc/maintenance/load_installer_data.pl @@ -0,0 +1,70 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Koha::Script; +use Getopt::Long; +use Try::Tiny; +use C4::Context; +use C4::Installer; + +my ( $filepath, $help, $table, $lang, $all, $force ); +GetOptions( + 'help|h' => \$help, + 'filepath=s' => \$filepath, + 'table=s' => \$table, + 'lang=s' => \$lang, + 'force' => \$force, +); + +my $dbh = C4::Context->dbh; +my $installer = C4::Installer->new; + +my $table_mapping = { + authorised_values => 'auth_values', + account_credit_types => 'account_credit_types', + account_debit_types => 'account_debit_types', + letter => 'sample_notices', +}; + +unless ($filepath) { + die "table and lang are mandatory" unless $table || $lang; + + my $filename = $table; + if ( exists $table_mapping->{$table} ) { + $filename = $table_mapping->{$table}; + } + $filepath = sprintf 'installer/data/mysql/%s/mandatory/%s.yml', $lang, $filename; +} + +die "File does not exist ($filepath)" unless -f $filepath; + +my $inserted = 0; +my @sql_statements = $installer->get_sql_statements_from_yml( $filepath, 1 ); +for my $statement (@sql_statements) { + my $sql = $statement->[0]; + if ($force) { + $sql =~ s/INSERT INTO/REPLACE INTO/; + } + my $variables = $statement->[1]; + try { + $inserted += $dbh->do( $sql, undef, @$variables ); + } catch { + }; +} +say "$inserted rows inserted"; -- 2.43.0