From 5db7e99395e7d1f3f3ee04a27b3f3c8ac55564c4 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 23 Jun 2016 05:12:39 +0100 Subject: [PATCH] Bug 11921: Restore memcached infos to koha-conf - Koha::Config Move C4::Context->read_config_file to Koha::Config That permits to reuse it from Koha::Cache without needing C4::Context (and introduce a circular deps). TODO: Add decent POD to Koha::Config Signed-off-by: Chris Cormack Signed-off-by: Tomas Cohen Arazi --- C4/Context.pm | 91 +++++++++------------------------------------------------- Koha/Cache.pm | 25 +++++++++++----- Koha/Caches.pm | 2 +- Koha/Config.pm | 55 +++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 85 deletions(-) create mode 100644 Koha/Config.pm diff --git a/C4/Context.pm b/C4/Context.pm index 728964e..1f1e9a1 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -90,7 +90,6 @@ BEGIN { use Encode; use ZOOM; -use XML::Simple; use Koha::Caches; use POSIX (); use DateTime::TimeZone; @@ -100,6 +99,7 @@ use Carp; use C4::Boolean; use C4::Debug; use Koha; +use Koha::Config; use Koha::Config::SysPref; use Koha::Config::SysPrefs; @@ -168,66 +168,9 @@ environment variable to the pathname of a configuration file to use. # Zconn # A connection object for the Zebra server -# Koha's main configuration file koha-conf.xml -# is searched for according to this priority list: -# -# 1. Path supplied via use C4::Context '/path/to/koha-conf.xml' -# 2. Path supplied in KOHA_CONF environment variable. -# 3. Path supplied in INSTALLED_CONFIG_FNAME, as long -# as value has changed from its default of -# '__KOHA_CONF_DIR__/koha-conf.xml', as happens -# when Koha is installed in 'standard' or 'single' -# mode. -# 4. Path supplied in CONFIG_FNAME. -# -# The first entry that refers to a readable file is used. - -use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml"; - # Default config file, if none is specified - -my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml'; - # path to config file set by installer - # __KOHA_CONF_DIR__ is set by rewrite-confg.PL - # when Koha is installed in 'standard' or 'single' - # mode. If Koha was installed in 'dev' mode, - # __KOHA_CONF_DIR__ is *not* rewritten; instead - # developers should set the KOHA_CONF environment variable - $context = undef; # Initially, no context is set @context_stack = (); # Initially, no saved contexts - -=head2 read_config_file - -Reads the specified Koha config file. - -Returns an object containing the configuration variables. The object's -structure is a bit complex to the uninitiated ... take a look at the -koha-conf.xml file as well as the XML::Simple documentation for details. Or, -here are a few examples that may give you what you need: - -The simple elements nested within the element: - - my $pass = $koha->{'config'}->{'pass'}; - -The elements: - - my $listen = $koha->{'listen'}->{'biblioserver'}->{'content'}; - -The elements nested within the element: - - my $ccl2rpn = $koha->{'server'}->{'biblioserver'}->{'cql2rpn'}; - -Returns undef in case of error. - -=cut - -sub read_config_file { # Pass argument naming config file to read - my $koha = XMLin(shift, keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'], suppressempty => ''); - - return $koha; # Return value: ref-to-hash holding the configuration -} - =head2 db_scheme2dbi my $dbd_driver_name = C4::Context::db_schema2dbi($scheme); @@ -292,38 +235,32 @@ sub new { undef $conf_fname unless (defined $conf_fname && -s $conf_fname); # Figure out a good config file to load if none was specified. - if (!defined($conf_fname)) - { - # If the $KOHA_CONF environment variable is set, use - # that. Otherwise, use the built-in default. - if (exists $ENV{"KOHA_CONF"} and $ENV{'KOHA_CONF'} and -s $ENV{"KOHA_CONF"}) { - $conf_fname = $ENV{"KOHA_CONF"}; - } elsif ($INSTALLED_CONFIG_FNAME !~ /__KOHA_CONF_DIR/ and -s $INSTALLED_CONFIG_FNAME) { - # NOTE: be careful -- don't change __KOHA_CONF_DIR in the above - # regex to anything else -- don't want installer to rewrite it - $conf_fname = $INSTALLED_CONFIG_FNAME; - } elsif (-s CONFIG_FNAME) { - $conf_fname = CONFIG_FNAME; - } else { + unless ( defined $conf_fname ) { + $conf_fname = Koha::Config->guess_koha_conf; + unless ( $conf_fname ) { warn "unable to locate Koha configuration file koha-conf.xml"; return; } } my $conf_cache = Koha::Caches->get_instance('config'); - if ( $conf_cache ) { - $self = $conf_cache->get_from_cache('kohaconf'); + my $config_from_cache; + if ( $conf_cache->cache ) { + $config_from_cache = $conf_cache->get_from_cache('koha_conf'); } unless ( %$self ) { - $self = read_config_file($conf_fname); + $self = Koha::Config->read_from_file($conf_fname); } - if ( $conf_cache ) { + + if ( $config_from_cache ) { + $self = $config_from_cache; + } elsif ( $conf_cache->memcached_cache ) { # FIXME it may be better to use the memcached servers from the config file # to cache it $conf_cache->set_in_cache('koha_conf', $self) } unless ( exists $self->{config} or defined $self->{config} ) { - warn "read_config_file($conf_fname) returned undef"; + warn "The config file ($conf_fname) has not been parsed correctly"; return; } @@ -466,7 +403,7 @@ with this method. =cut -my $syspref_cache = Koha::Caches->get_instance(); +my $syspref_cache = Koha::Caches->get_instance('syspref'); my $use_syspref_cache = 1; sub preference { my $self = shift; diff --git a/Koha/Cache.pm b/Koha/Cache.pm index efd3f29..eda115e 100644 --- a/Koha/Cache.pm +++ b/Koha/Cache.pm @@ -39,10 +39,12 @@ use strict; use warnings; use Carp; use Module::Load::Conditional qw(can_load); -use Koha::Cache::Object; use Sereal::Encoder; use Sereal::Decoder; +use Koha::Cache::Object; +use Koha::Config; + use base qw(Class::Accessor); __PACKAGE__->mk_ro_accessors( @@ -69,21 +71,30 @@ Create a new Koha::Cache object. This is required for all cache-related function =cut sub new { - my ( $class, $self, $subnamespace ) = @_; + my ( $class, $self, $params ) = @_; $self->{'default_type'} = $self->{cache_type} - || $ENV{CACHING_SYSTEM} + || $ENV{CACHING_SYSTEM} # DELME What about this? || 'memcached'; + my $subnamespace = $params->{subnamespace} // ''; + $ENV{DEBUG} && carp "Default caching system: $self->{'default_type'}"; $self->{'timeout'} ||= 0; - $self->{'namespace'} ||= $ENV{MEMCACHED_NAMESPACE} || C4::Context->config('memcached_namespace') || 'koha'; + # Should we continue to support MEMCACHED ENV vars? + $self->{'namespace'} ||= $ENV{MEMCACHED_NAMESPACE}; + my @servers = split /,/, $ENV{MEMCACHED_SERVERS} || ''; + unless ( $self->{namespace} and @servers ) { + my $koha_config = Koha::Config->read_from_file( Koha::Config->guess_koha_conf() ); + $self->{namespace} ||= $koha_config->{config}{memcached_namespace} || 'koha'; + @servers ||= split /,/, $koha_config->{config}{memcached_servers}; + } $self->{namespace} .= ":$subnamespace:"; if ( $self->{'default_type'} eq 'memcached' && can_load( modules => { 'Cache::Memcached::Fast' => undef } ) - && _initialize_memcached($self) + && _initialize_memcached($self, @servers) && defined( $self->{'memcached_cache'} ) ) { $self->{'cache'} = $self->{'memcached_cache'}; @@ -115,8 +126,8 @@ sub new { } sub _initialize_memcached { - my ($self) = @_; - my @servers = split /,/, $ENV{MEMCACHED_SERVERS} || C4::Context->config('memcached_servers') || ''; + my ($self, @servers) = @_; + return unless @servers; $ENV{DEBUG} diff --git a/Koha/Caches.pm b/Koha/Caches.pm index 338ccd2..808b392 100644 --- a/Koha/Caches.pm +++ b/Koha/Caches.pm @@ -7,7 +7,7 @@ our $singleton_caches; sub get_instance { my ($class, $subnamespace) = @_; $subnamespace //= ''; - $singleton_caches->{$subnamespace} = Koha::Cache->new({}, $subnamespace) unless $singleton_caches->{$subnamespace}; + $singleton_caches->{$subnamespace} = Koha::Cache->new({}, { subnamespace => $subnamespace } ) unless $singleton_caches->{$subnamespace}; return $singleton_caches->{$subnamespace}; } diff --git a/Koha/Config.pm b/Koha/Config.pm new file mode 100644 index 0000000..cef87ec --- /dev/null +++ b/Koha/Config.pm @@ -0,0 +1,55 @@ +package Koha::Config; + +use Modern::Perl; +use XML::Simple; + +# Default config file, if none is specified +use constant CONFIG_FNAME => "/etc/koha/koha-conf.xml"; + +# path to config file set by installer +# __KOHA_CONF_DIR__ is set by rewrite-confg.PL +# when Koha is installed in 'standard' or 'single' +# mode. If Koha was installed in 'dev' mode, +# __KOHA_CONF_DIR__ is *not* rewritten; instead +# developers should set the KOHA_CONF environment variable +my $INSTALLED_CONFIG_FNAME = '__KOHA_CONF_DIR__/koha-conf.xml'; + +# Should not be called outside of C4::Context or Koha::Cache +# use C4::Context->config instead +sub read_from_file { + my ( $class, $file ) = @_; + return XMLin($file, keyattr => ['id'], forcearray => ['listen', 'server', 'serverinfo'], suppressempty => ''); +} + +# Koha's main configuration file koha-conf.xml +# is searched for according to this priority list: +# +# 1. Path supplied via use C4::Context '/path/to/koha-conf.xml' +# 2. Path supplied in KOHA_CONF environment variable. +# 3. Path supplied in INSTALLED_CONFIG_FNAME, as long +# as value has changed from its default of +# '__KOHA_CONF_DIR__/koha-conf.xml', as happens +# when Koha is installed in 'standard' or 'single' +# mode. +# 4. Path supplied in CONFIG_FNAME. +# +# The first entry that refers to a readable file is used. + +sub guess_koha_conf { + + # If the $KOHA_CONF environment variable is set, use + # that. Otherwise, use the built-in default. + my $conf_fname; + if ( exists $ENV{"KOHA_CONF"} and $ENV{'KOHA_CONF'} and -s $ENV{"KOHA_CONF"} ) { + $conf_fname = $ENV{"KOHA_CONF"}; + } elsif ( $INSTALLED_CONFIG_FNAME !~ /__KOHA_CONF_DIR/ and -s $INSTALLED_CONFIG_FNAME ) { + # NOTE: be careful -- don't change __KOHA_CONF_DIR in the above + # regex to anything else -- don't want installer to rewrite it + $conf_fname = $INSTALLED_CONFIG_FNAME; + } elsif ( -s CONFIG_FNAME ) { + $conf_fname = CONFIG_FNAME; + } + return $conf_fname; +} + +1; -- 2.7.4