From 165a11a90d7805b8a8af52b801630bf121b75be6 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 26 Jan 2026 16:03:27 +0100 Subject: [PATCH] Bug 41746: Move to Koha::Devel::Sysprefs --- Koha/Devel/Sysprefs.pm | 97 +++++++++++++++++++++++++++++++++ t/db_dependent/check_sysprefs.t | 68 ++--------------------- 2 files changed, 103 insertions(+), 62 deletions(-) create mode 100644 Koha/Devel/Sysprefs.pm diff --git a/Koha/Devel/Sysprefs.pm b/Koha/Devel/Sysprefs.pm new file mode 100644 index 00000000000..cf550cf1a58 --- /dev/null +++ b/Koha/Devel/Sysprefs.pm @@ -0,0 +1,97 @@ +package Koha::Devel::Sysprefs; + +use Modern::Perl; +use File::Slurp qw(read_file); + +=head1 NAME + +Koha::Devel::Sysprefs + +=head1 DESCRIPTION + +Handle system preferences operations for developers. + +=cut + +=head1 API + +=cut + +=head2 new + +my $syspref_handler = Koha::Devel::Sysprefs->new(); + +Constructor + +=cut + +sub new { + my ( $class, $args ) = @_; + $args ||= {}; + + unless ( $args->{filepath} ) { + $args->{filepath} = sprintf "%s/installer/data/mysql/mandatory/sysprefs.sql", + C4::Context->config('intranetdir'); + } + my $self = bless $args, $class; + return $self; +} + +=head2 get_sysprefs_from_file + +my @sysprefs = $syspref_handler->get_sysprefs_from_file(); + +Return an array of sysprefs from the SQL file used to populate the system preferences DB table. + +=cut + +sub get_sysprefs_from_file { + my ($self) = @_; + my @sysprefs; + my @lines = read_file( $self->{filepath} ) or die "Can't open $self->{filepath}: $!"; + for my $line (@lines) { + chomp $line; + next if $line =~ /^INSERT INTO /; # first line + next if $line =~ /^;$/; # last line + next if $line =~ /^--/; # Comment line + if ( + $line =~ m/ + '(?[^'\\]*(?:\\.[^'\\]*)*)',\s* + '(?[^'\\]*(?:\\.[^'\\]*)*)',\s* + (?NULL|'(?[^'\\]*(?:\\.[^'\\]*)*)'),\s* + (?NULL|'(?[^'\\]*(?:\\.[^'\\]*)*)'),\s* + (?NULL|'(?[^'\\]*(?:\\.[^'\\]*)*)') + /xms + ) + { + my $variable = $+{variable}; + my $value = $+{value}; + my $options = $+{options_content}; + my $explanation = $+{explanation_content}; + my $type = $+{type_content}; + + if ($options) { + $options =~ s/\\'/'/g; + $options =~ s/\\\\/\\/g; + } + if ($explanation) { + $explanation =~ s/\\'/'/g; + $explanation =~ s/\\n/\n/g; + } + + # FIXME Explode if already exists? + push @sysprefs, { + variable => $variable, + value => $value, + options => $options, + explanation => $explanation, + type => $type, + }; + } else { + die "$line does not match"; + } + } + return @sysprefs; +} + +1; diff --git a/t/db_dependent/check_sysprefs.t b/t/db_dependent/check_sysprefs.t index afe4eccc504..0d3a82c4bc9 100755 --- a/t/db_dependent/check_sysprefs.t +++ b/t/db_dependent/check_sysprefs.t @@ -19,20 +19,19 @@ use Modern::Perl; -use File::Slurp qw(read_file); -use C4::Context; use Array::Utils qw(array_minus); use Test::NoWarnings; use Test::More tests => 3; +use C4::Context; + +use Koha::Devel::Sysprefs; + our $dbh = C4::Context->dbh; -my $intranetdir = C4::Context->config('intranetdir'); -my $root_dir = $intranetdir . '/installer/data/mysql/mandatory'; -my $syspref_filepath = "$root_dir/sysprefs.sql"; +my $intranetdir = C4::Context->config('intranetdir'); -my @lines = read_file($syspref_filepath) or die "Can't open $syspref_filepath: $!"; -my @sysprefs_in_file = get_sysprefs_from_file(@lines); +my @sysprefs_in_file = Koha::Devel::Sysprefs->new->get_sysprefs_from_file(); subtest 'Compare database with sysprefs.sql file' => sub { ok( scalar(@sysprefs_in_file), "Found sysprefs" ); @@ -70,61 +69,6 @@ subtest 'Compare sysprefs.sql with YAML files' => sub { } }; -# Get sysprefs from SQL file populating sysprefs table with INSERT statement. -# -# Example: -# INSERT INTO `systempreferences` (variable,value,explanation,options,type) -# VALUES('AmazonLocale','US','Use to set the Locale of your Amazon.com Web Services', -# 'US|CA|DE|FR|JP|UK','Choice') -# -sub get_sysprefs_from_file { - my @lines = @_; - my @sysprefs; - for my $line (@lines) { - chomp $line; - next if $line =~ /^INSERT INTO /; # first line - next if $line =~ /^;$/; # last line - next if $line =~ /^--/; # Comment line - if ( - $line =~ m/ - '(?[^'\\]*(?:\\.[^'\\]*)*)',\s* - '(?[^'\\]*(?:\\.[^'\\]*)*)',\s* - (?NULL|'(?[^'\\]*(?:\\.[^'\\]*)*)'),\s* - (?NULL|'(?[^'\\]*(?:\\.[^'\\]*)*)'),\s* - (?NULL|'(?[^'\\]*(?:\\.[^'\\]*)*)') - /xms - ) - { - my $variable = $+{variable}; - my $value = $+{value}; - my $options = $+{options_content}; - my $explanation = $+{explanation_content}; - my $type = $+{type_content}; - - if ($options) { - $options =~ s/\\'/'/g; - $options =~ s/\\\\/\\/g; - } - if ($explanation) { - $explanation =~ s/\\'/'/g; - $explanation =~ s/\\n/\n/g; - } - - # FIXME Explode if already exists? - push @sysprefs, { - variable => $variable, - value => $value, - options => $options, - explanation => $explanation, - type => $type, - }; - } else { - die "$line does not match"; - } - } - return @sysprefs; -} - # Get system preferences from YAML files sub get_syspref_from_yaml { my @prefs; -- 2.43.0