From 3e027a2ab90b9a61bd1a04ce4ae239ce771cbc68 Mon Sep 17 00:00:00 2001 From: Blou Date: Fri, 27 Oct 2023 17:22:39 -0400 Subject: [PATCH] Bug 35250: remove usages of Koha::Config::Syspref(s) from C4::Context Use straigth SQL queries to access systempreferences instead of relying on the ORM, thus removing all direct and indirect dependencies to Koha::Object To remove all dependencies between C4::Context and the db object model, we move the calls to set_preference and delete_prefence directly to the class object Koha::Config::SysPrefs C4::Context::preference remains but uses straight SQL C4::Context::set_preference moves to Koha::Config::SysPrefs::set_preference C4::Context::delete_preference moves to Koha::Config::SysPrefs::delete_preference --- C4/Context.pm | 90 ++--------------------------------------- Koha/Config/SysPrefs.pm | 86 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 89 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 0d8bce7458..7f4b4d8096 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -41,9 +41,8 @@ use File::Spec; use List::MoreUtils qw(any); use Koha::Caches; -use Koha::Config::SysPref; -use Koha::Config::SysPrefs; use Koha::Config; +use Koha::Database; use Koha; =head1 NAME @@ -300,7 +299,7 @@ with this method. =cut -my $use_syspref_cache = 1; +our $use_syspref_cache = 1; sub preference { my $self = shift; my $var = shift; # The system preference to return @@ -316,9 +315,7 @@ sub preference { return $cached_var if defined $cached_var; } - my $syspref; - eval { $syspref = Koha::Config::SysPrefs->find( lc $var ) }; - my $value = $syspref ? $syspref->value() : undef; + my ($value) = $self->dbh()->selectrow_array("SELECT value FROM systempreferences WHERE variable like '$var'"); if ( $use_syspref_cache ) { my $syspref_cache = Koha::Caches->get_instance('syspref'); @@ -413,87 +410,6 @@ sub clear_syspref_cache { $syspref_cache->flush_all; } -=head2 set_preference - - C4::Context->set_preference( $variable, $value, [ $explanation, $type, $options ] ); - -This updates a preference's value both in the systempreferences table and in -the sysprefs cache. If the optional parameters are provided, then the query -becomes a create. It won't update the parameters (except value) for an existing -preference. - -=cut - -sub set_preference { - my ( $self, $variable, $value, $explanation, $type, $options ) = @_; - - my $variable_case = $variable; - $variable = lc $variable; - - my $syspref = Koha::Config::SysPrefs->find($variable); - $type = - $type ? $type - : $syspref ? $syspref->type - : undef; - - $value = 0 if ( $type && $type eq 'YesNo' && $value eq '' ); - - # force explicit protocol on OPACBaseURL - if ( $variable eq 'opacbaseurl' && $value && substr( $value, 0, 4 ) !~ /http/ ) { - $value = 'http://' . $value; - } - - if ($syspref) { - $syspref->set( - { ( defined $value ? ( value => $value ) : () ), - ( $explanation ? ( explanation => $explanation ) : () ), - ( $type ? ( type => $type ) : () ), - ( $options ? ( options => $options ) : () ), - } - )->store; - } else { - $syspref = Koha::Config::SysPref->new( - { variable => $variable_case, - value => $value, - explanation => $explanation || undef, - type => $type, - options => $options || undef, - } - )->store(); - } - - if ( $use_syspref_cache ) { - my $syspref_cache = Koha::Caches->get_instance('syspref'); - $syspref_cache->set_in_cache( "syspref_$variable", $value ); - } - - return $syspref; -} - -=head2 delete_preference - - C4::Context->delete_preference( $variable ); - -This deletes a system preference from the database. Returns a true value on -success. Failure means there was an issue with the database, not that there -was no syspref of the name. - -=cut - -sub delete_preference { - my ( $self, $var ) = @_; - - if ( Koha::Config::SysPrefs->find( $var )->delete ) { - if ( $use_syspref_cache ) { - my $syspref_cache = Koha::Caches->get_instance('syspref'); - $syspref_cache->clear_from_cache("syspref_$var"); - } - - return 1; - } - return 0; -} - =head2 csv_delimiter $delimiter = C4::Context->csv_delimiter; diff --git a/Koha/Config/SysPrefs.pm b/Koha/Config/SysPrefs.pm index 8b08d71a32..15694434d8 100644 --- a/Koha/Config/SysPrefs.pm +++ b/Koha/Config/SysPrefs.pm @@ -19,9 +19,9 @@ package Koha::Config::SysPrefs; use Modern::Perl; +use C4::Context; -use Koha::Database; - +use Koha::Caches; use Koha::Config::SysPref; use base qw(Koha::Objects); @@ -36,6 +36,88 @@ Koha::Config::SysPrefs - Koha System Preference object set class =cut +=head2 set_preference + + Koha::Config::SysPrefs->set_preference( $variable, $value, [ $explanation, $type, $options ] ); + +This updates a preference's value both in the systempreferences table and in +the sysprefs cache. If the optional parameters are provided, then the query +becomes a create. It won't update the parameters (except value) for an existing +preference. + +=cut + +sub set_preference { + my ( $self, $variable, $value, $explanation, $type, $options ) = @_; + + my $variable_case = $variable; + $variable = lc $variable; + + my $syspref = Koha::Config::SysPrefs->find($variable); + $type = + $type ? $type + : $syspref ? $syspref->type + : undef; + + $value = 0 if ( $type && $type eq 'YesNo' && $value eq '' ); + + # force explicit protocol on OPACBaseURL + if ( $variable eq 'opacbaseurl' && $value && substr( $value, 0, 4 ) !~ /http/ ) { + $value = 'http://' . $value; + } + + if ($syspref) { + $syspref->set( + { ( defined $value ? ( value => $value ) : () ), + ( $explanation ? ( explanation => $explanation ) : () ), + ( $type ? ( type => $type ) : () ), + ( $options ? ( options => $options ) : () ), + } + )->store; + } else { + $syspref = Koha::Config::SysPref->new( + { variable => $variable_case, + value => $value, + explanation => $explanation || undef, + type => $type, + options => $options || undef, + } + )->store(); + } + + if ( $C4::Context::use_syspref_cache ) { + my $syspref_cache = Koha::Caches->get_instance('syspref'); + $syspref_cache->set_in_cache( "syspref_$variable", $value ); + } + + return $syspref; +} + +=head2 delete_preference + + Koha::Config::SysPrefs->delete_preference( $variable ); + +This deletes a system preference from the database. Returns a true value on +success. Failure means there was an issue with the database, not that there +was no syspref of the name. + +=cut + +sub delete_preference { + my ( $self, $var ) = @_; + + if ( Koha::Config::SysPrefs->find( $var )->delete ) { + if ( $C4::Context::use_syspref_cache ) { + my $syspref_cache = Koha::Caches->get_instance('syspref'); + $syspref_cache->clear_from_cache("syspref_$var"); + } + return 1; + } + return 0; +} + + + =head3 type =cut -- 2.34.1