<commit_before>package Mappings;

use strict;
use warnings;

use Text::CSV;
use URI::Split  qw( uri_split uri_join );



sub new {
    my $class    = shift;
    my $csv_file = shift;
    
    my $self = {};
    bless $self, $class;
    
    $self->{'csv'} = Text::CSV->new({ binary => 1 });
    open $self->{'csv_handle'}, '<:encoding(utf8)', $csv_file
        or return undef;
    
    $self->{'column_names'} = $self->read_column_names();
    return unless scalar @{$self->{'column_names'}};
    return unless $self->has_mandatory_columns();
    
    return $self;
}

sub entire_csv_as_nginx_config {
    my $self = shift;
    
    my %configs;
    while ( my( $host, $map, $line ) = $self->row_as_nginx_config() ) {
        $configs{$host}{$map} = []
            unless defined $configs{$host};
        push @{ $configs{$host}{$map} }, $line;
    }
    
    foreach my $host ( keys %configs ) {
        foreach my $map ( keys %{ $configs{$host} } ) {
            $configs{$host}{$map} = join '', sort @{ $configs{$host}{$map} };
        }
    }
    
    return \%configs;
}
sub row_as_nginx_config {
    my $self = shift;
    my $row  = $self->get_row();
    
    return unless defined $row;
    
    my $status  = $row->{'Status'};
    my $new_url = $row->{'New Url'};
    
    my( $scheme, $host, $path, $query, $frag ) = uri_split $row->{'Old Url'};

    my $old_url = uri_join undef, undef, $path, $query, $frag;
        
        # strip potential trailing whitespace
        $new_url =~ s{\s+$}{};
        $old_url =~ s{\s+$}{};
    
    if ( 'www.direct.gov.uk' eq $host ) {
        return( $host, "location = $old_url { return 410; }\n" )
            if '410' eq $status && length $old_url;
        return( $host, "location = $old_url { return 301 $new_url; }\n" )
            if '301' eq $status && length $old_url && length $new_url;
        
        return(
            $host,
            "# invalid entry: status='$status' old='$row->{'Old Url'}' new='$new_url'\n"
        );
    } 

    if ( 'www.businesslink.gov.uk' eq $host ) {
        #get operational part of the url        <commit_msg>Only sort location blocks in nginx output.<commit_after>package Mappings;

use strict;
use warnings;

use Text::CSV;
use URI::Split  qw( uri_split uri_join );



sub new {
    my $class    = shift;
    my $csv_file = shift;
    
    my $self = {};
    bless $self, $class;
    
    $self->{'csv'} = Text::CSV->new({ binary => 1 });
    open $self->{'csv_handle'}, '<:encoding(utf8)', $csv_file
        or return undef;
    
    $self->{'column_names'} = $self->read_column_names();
    return unless scalar @{$self->{'column_names'}};
    return unless $self->has_mandatory_columns();
    
    return $self;
}

sub entire_csv_as_nginx_config {
    my $self = shift;
    
    my %configs;
    while ( my( $host, $map, $line ) = $self->row_as_nginx_config() ) {
        $configs{$host}{$map} = []
            unless defined $configs{$host};
        push @{ $configs{$host}{$map} }, $line;
    }
    
    foreach my $host ( keys %configs ) {
        foreach my $map ( keys %{ $configs{$host} } ) {
            if ( 'location' eq $map ) {
                # locations need to be sorted for b-tree insert efficiency
                $configs{$host}{$map} = join '', sort @{ $configs{$host}{$map} };
            }
            else {
                $configs{$host}{$map} = join '', @{ $configs{$host}{$map} };
            }
        }
    }
    
    return \%configs;
}
sub row_as_nginx_config {
    my $self = shift;
    my $row  = $self->get_row();
    
    return unless defined $row;
    
    my $status  = $row->{'Status'};
    my $new_url = $row->{'New Url'};
    
    my( $scheme, $host, $path, $query, $frag ) = uri_split $row->{'Old Url'};

    my $old_url = uri_join undef, undef, $path, $query, $frag;
        
        # strip potential trailing whitespace
        $new_url =~ s{\s+$}{};
        $old_url =~ s{\s+$}{};
    
    if ( 'www.direct.gov.uk' eq $host ) {
        return( $host, 'location', "location = $old_url { return 410; }\n" )
            if '410' eq $status && length $old_url;
        return( $host, 'location', "location = $old_url { return 301 $new_url; }\n" )
            if '301' eq $status && length $old_url && length $new_url;
        
        return(
            $host,
            'location',
            "# invalid entry: status='$status' old='$row->{'Old Url'}' new='$new_url'\n"
        );
    } 

    if ( 'www.businesslink.gov.uk' eq $host ) {
        #get operational part of the url        
{'fraction_non_alphanumeric': 0.1736540158870256, 'fraction_numerical': 0.00617828773168579, 'mean_word_length': 2.1566852367688023, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3548926', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1556, 'n_words': 384}