<commit_before># OpenXPKI::Config
#
# Written 2012 by <NAME> for the OpenXPKI project
# Copyright (C) 2012 by The OpenXPKI Project
#

package OpenXPKI::Config;

use strict;
use warnings;
use English;
use Moose;
use OpenXPKI::Config::Backend;
use OpenXPKI::Exception;
use OpenXPKI::Debug;
use OpenXPKI::Server::Context qw( CTX );
use Data::Dumper;

# Make sure the underlying connector is recent
use Connector 1.08;

extends 'Connector::Multi';

has '+BASECONNECTOR' => ( required => 0 );

has '_head_version' => (
    is => 'rw',
    isa => 'Str',
    required => 0,
    default => '',
);


around BUILDARGS => sub {
    my $orig = shift;
    my $class = shift;
    return $class->$orig( { BASECONNECTOR => OpenXPKI::Config::Backend->new() } );
};

before '_route_call' => sub {

    my $self = shift;
    my $call = shift;
    my $path = shift;
    my $location;

    # Location can be a string or an array
    if (ref $path eq "ARRAY") {
        $location = @{$path}[0];
        ##! 8: 'Location was array - shifted: ' . Dumper $location
    } else {
        $location = $path;
    }

    ##! 16: "_route_call interception on $location "
    # system or realm acces - no prefix
    if ( substr ($location, 0, 6) eq 'system' || substr($location, 0, 5) eq 'realm' ) {
        ##! 16: "_route_call: system or explicit realm value, reset connector offsets"
        $self->_config()->{''}->PREFIX('');
    } else {
        my $session = CTX('session');
        my $pki_realm = $session->get_pki_realm();
        ##! 16: "_route_call: realm value, set prefix to " . $pki_realm
        $self->_config()->{''}->PREFIX( "realm.$pki_realm" );
    }

    ##! 8: 'Full path: ' . Dumper $path
};

sub get_version {
    my $self = shift;
    return '';
    ##! 16: 'Config version requested ' . Dumper( $self->BASECONNECTOR()->version() )
    #return $self->BASECONNECTOR()->version();
}

sub get_head_version {
    my $self = shift;
    return '';
    #return $self->_head_version();
}

sub update_head {
    my $self = shift;

    return '';

    my $head_id = $self->BASECONNECTOR()->fetch_head_commit();

    # if the head version has evolved, update the session context
    ##! 32: sprintf 'My head: %s,  Repo head: %s ',  $self->_head_version(), $head_id
    if ( $self->_head_version() ne $head_id ) {
        ##! 16: 'Advance to head commit ' . $head_id
        $self->_head_version( $head_id );

        CTX('log')->log(
            MESSAGE  => "system config advanced to new head commit: $head_id",
            PRIORITY => "info",
            FACILITY => "system",
        );

        return 1;
    }
    return;
}

sub walkQueryPoints {

    my $self = shift;
    my $prefix = shift;
    my $query = shift;
    my $params = shift;

    my $call;

    if (ref $params eq 'HASH') {
        $call = $params->{call};
        undef $params->{call};
    } elsif ($params) {
        $call = $params;
    } else {
        $call = 'get';
    }

    ##! 16: " Walk resolvers at $prefix with $call "

    my $result;

    my @path_prefix = $self->_build_path( $prefix );
    my @prep_query = $self->_build_path( $query );

    foreach my $resolver (  $self->get_list( [ @path_prefix, 'resolvers' ] ) ) {
        ##! 32: 'Ask Resolver ' . $prefix.'.'.$resolver.'.'.$query
        $result = $self->$call( [ @path_prefix, $resolver, @prep_query ] , $params );
        return { 'VALUE' => $result, 'SOURCE' => $resolver } if ($result);
    }
    return;
}

sub get_scalar_as_list {
    my $self = shift;
    my $path = shift;
    my @values;
    my $meta = $self->get_meta( $path );
    ##! 16: 'node meta ' . Dumper $meta
    if ($meta && $meta->{TYPE} eq 'list') {
        @values = $self->get_list( $path );
    } else {
        my $val = ( $self->get( $path ) );
        @values = ( $val ) if (defined $val);<commit_msg>Improve handling of undef values in Config layer<commit_after># OpenXPKI::Config
#
# Written 2012 by <NAME> for the OpenXPKI project
# Copyright (C) 2012 by The OpenXPKI Project
#

package OpenXPKI::Config;

use strict;
use warnings;
use English;
use Moose;
use OpenXPKI::Config::Backend;
use OpenXPKI::Exception;
use OpenXPKI::Debug;
use OpenXPKI::Server::Context qw( CTX );
use Data::Dumper;

# Make sure the underlying connector is recent
use Connector 1.08;

extends 'Connector::Multi';

has '+BASECONNECTOR' => ( required => 0 );

has '_head_version' => (
    is => 'rw',
    isa => 'Str',
    required => 0,
    default => '',
);


around BUILDARGS => sub {
    my $orig = shift;
    my $class = shift;
    return $class->$orig( { BASECONNECTOR => OpenXPKI::Config::Backend->new() } );
};

before '_route_call' => sub {

    my $self = shift;
    my $call = shift;
    my $path = shift;
    my $location;

    # Location can be a string or an array
    if (ref $path eq "ARRAY") {
        $location = @{$path}[0];
        ##! 8: 'Location was array - shifted: ' . Dumper $location
    } else {
        $location = $path;
    }

    ##! 16: "_route_call interception on $location "
    # system or realm acces - no prefix
    if ( substr ($location, 0, 6) eq 'system' || substr($location, 0, 5) eq 'realm' ) {
        ##! 16: "_route_call: system or explicit realm value, reset connector offsets"
        $self->_config()->{''}->PREFIX('');
    } else {
        my $session = CTX('session');
        my $pki_realm = $session->get_pki_realm();
        ##! 16: "_route_call: realm value, set prefix to " . $pki_realm
        $self->_config()->{''}->PREFIX( "realm.$pki_realm" );
    }

    ##! 8: 'Full path: ' . Dumper $path
};

sub get_version {
    my $self = shift;
    return '';
    ##! 16: 'Config version requested ' . Dumper( $self->BASECONNECTOR()->version() )
    #return $self->BASECONNECTOR()->version();
}

sub get_head_version {
    my $self = shift;
    return '';
    #return $self->_head_version();
}

sub update_head {
    my $self = shift;

    return '';

    my $head_id = $self->BASECONNECTOR()->fetch_head_commit();

    # if the head version has evolved, update the session context
    ##! 32: sprintf 'My head: %s,  Repo head: %s ',  $self->_head_version(), $head_id
    if ( $self->_head_version() ne $head_id ) {
        ##! 16: 'Advance to head commit ' . $head_id
        $self->_head_version( $head_id );

        CTX('log')->log(
            MESSAGE  => "system config advanced to new head commit: $head_id",
            PRIORITY => "info",
            FACILITY => "system",
        );

        return 1;
    }
    return;
}

sub walkQueryPoints {

    my $self = shift;
    my $prefix = shift;
    my $query = shift;
    my $params = shift;

    my $call;

    if (ref $params eq 'HASH') {
        $call = $params->{call};
        undef $params->{call};
    } elsif ($params) {
        $call = $params;
    } else {
        $call = 'get';
    }

    ##! 16: " Walk resolvers at $prefix with $call "

    my $result;

    my @path_prefix = $self->_build_path( $prefix );
    my @prep_query = $self->_build_path( $query );

    foreach my $resolver (  $self->get_list( [ @path_prefix, 'resolvers' ] ) ) {
        ##! 32: 'Ask Resolver ' . $prefix.'.'.$resolver.'.'.$query
        $result = $self->$call( [ @path_prefix, $resolver, @prep_query ] , $params );
        return { 'VALUE' => $result, 'SOURCE' => $resolver } if ($result);
    }
    return;
}

sub get_scalar_as_list {
    my $self = shift;
    my $path = shift;
    my @values;
    my $meta = $self->get_meta( $path );

    return unless(defined $meta);

    ##! 16: 'node meta ' . Dumper $meta
    if ($meta->{TYPE} eq 'list') {
        @values = $self->get_list( $path );
    } elsif ($meta->{TYPE} eq 'scalar') {
        my $val = ( $self->get( $path ) );
        @values = ( $val ) if (defined $val);
    } else {
        CTX('log')->log(
            MESSAGE  => "get_scalar_as_list got invalid node type",
            PRIORITY => "error",
            FACILITY => "system",
        );
{'fraction_non_alphanumeric': 0.168292992016221, 'fraction_numerical': 0.009884678747940691, 'mean_word_length': 2.5469662921348313, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1981799', 'n_tokens_mistral': 2790, 'n_tokens_neox': 2672, 'n_words': 747}