From 1edb6a783254200a4f3902254064da31a4727fa0 Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Fri, 8 Nov 2024 11:15:44 +0100 Subject: [PATCH] Bug 38384: Inline plugin loading into C4::Context's BEGIN block --- C4/Context.pm | 87 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 6 deletions(-) diff --git a/C4/Context.pm b/C4/Context.pm index 3f009603fe..8164da044b 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -21,12 +21,87 @@ use Modern::Perl; use vars qw($AUTOLOAD $context); BEGIN { - # Calling get_enabled_plugins here esnures that all plugin - # modules are loaded before use and will not trigger - # a database connection reset - Koha::Plugins->get_enabled_plugins(); + my $enable_plugins = 0; + if ( exists $ENV{'KOHA_CONF'} && -e $ENV{'KOHA_CONF'} ) { + require XML::Simple; + $enable_plugins = + XML::Simple->new->XMLin( $ENV{'KOHA_CONF'}, ForceArray => 0, KeyAttr => [] )->{'config'}->{'enable_plugins'} + // 0; + } + + if ($enable_plugins) { + require Koha::Cache::Memory::Lite; + my $enabled_plugins = Koha::Cache::Memory::Lite->get_from_cache('enabled_plugins'); + + if ( !$enabled_plugins ) { + require Koha::Config; + my $config = Koha::Config->get_instance; + + # Database connection setup + my $driver = $config->get('db_scheme') eq 'Pg' ? 'Pg' : 'mysql'; + my $dsn = sprintf( + 'dbi:%s:database=%s;host=%s;port=%s', + $driver, + $config->get("database_test") || $config->get("database"), + $config->get("hostname"), + $config->get("port") || q{}, + ); + my $attr = { RaiseError => 1, PrintError => 1 }; + if ( $driver eq 'mysql' && ( $config->get("tls") // q{} ) eq 'yes' ) { + $dsn .= sprintf( + ';mysql_ssl=1;mysql_ssl_client_key=%s;mysql_ssl_client_cert=%s;mysql_ssl_ca_file=%s', + $config->get('key'), $config->get('cert'), $config->get('ca'), + ); + $attr->{mysql_enable_utf8} = 1; + } + + require DBI; + my $dbh = DBI->connect( $dsn, $config->get("user"), $config->get("pass"), $attr ); + + if ($dbh) { + my $tz = $config->timezone // q{}; + my @queries; + push @queries, "SET NAMES 'utf8mb4'" if $driver eq 'mysql'; + push @queries, qq{SET time_zone = "$tz"} if $tz && $driver eq 'mysql' && $tz ne 'local'; + push @queries, qq{SET TIME ZONE = "$tz"} if $tz && $driver eq 'Pg'; + push @queries, 'set client_encoding = \'UTF8\'' if $driver eq 'Pg'; + if ( $driver eq 'mysql' ) { + my $sql_mode = + ( $config->get('strict_sql_modes') || $ENV{KOHA_TESTING} || ( $ENV{_} // q{} ) =~ m{prove}smx ) + ? 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION' + : 'IGNORE_SPACE,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'; + push @queries, qq{SET sql_mode = '$sql_mode'}; + } + + $dbh->do($_) for grep { $_ } @queries; + + # Retrieve enabled plugin classes + my $sth = $dbh->prepare( + q{SELECT plugin_class FROM plugin_data WHERE plugin_key = ' __ENABLED__ ' AND plugin_value = 1}); + $sth->execute(); + my @plugin_classes = map { $_->[0] } @{ $sth->fetchall_arrayref() }; + $sth->finish(); + $dbh->disconnect(); + + # Load and instantiate plugins + $enabled_plugins = []; + foreach my $plugin_class (@plugin_classes) { + next + if !Module::Load::Conditional::can_load( + modules => { $plugin_class => undef }, + nocache => 1 + ); + if ( my $plugin = eval { $plugin_class->new() } ) { + push @{$enabled_plugins}, $plugin; + } + } + + Koha::Cache::Memory::Lite->set_in_cache( 'enabled_plugins', $enabled_plugins ); + } + } + } - if ( $ENV{'HTTP_USER_AGENT'} ) { # Only hit when plack is not enabled + if ( $ENV{'HTTP_USER_AGENT'} ) { # Only hit when plack is not enabled # Redefine multi_param if cgi version is < 4.08 # Remove the "CGI::param called in list context" warning in this case @@ -38,7 +113,7 @@ BEGIN { $CGI::LIST_CONTEXT_WARN = 0; } } -}; +} use Carp qw( carp ); use DateTime::TimeZone; -- 2.47.0