<commit_before>=head1 LICENSE

Copyright [1999-2016] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

package EnsEMBL::Web::DBSQL::ConfigAdaptor;

use strict;

use Data::Dumper;
use DBI;
use Digest::MD5 qw(md5_hex);
use HTML::Entities qw(encode_entities decode_entities);

use EnsEMBL::Web::Controller;

my $DBH_SESSION; # package database handle for persistence
my $DBH_USER;

sub new {
  my ($class, $hub) = @_;
  my $species_defs  = $hub->species_defs;
  my $user          = $hub->user;
  my $self          = {
    hub        => $hub,
    session_id => $hub->session->create_session_id,
    user_id    => $user ? $user->id : undef,
    group_ids  => [ $user ? map($_->group_id, $user->get_groups) : (), @{$species_defs->ENSEMBL_DEFAULT_USER_GROUPS || []} ], 
    servername => $species_defs->ENSEMBL_SERVERNAME,
    site_type  => $species_defs->ENSEMBL_SITETYPE,
    version    => $species_defs->ENSEMBL_VERSION,
    cache_tags => [],
  };
  
  bless $self, $class;
  
  $self->dbh;
  
  return $self;
}

sub hub          { return $_[0]{'hub'};        }
sub user_id      { return $_[0]{'user_id'};    }
sub servername   { return $_[0]{'servername'}; }
sub site_type    { return $_[0]{'site_type'};  }
sub version      { return $_[0]{'version'};    }
sub cache_tags   { return $_[0]{'cache_tags'}; }
sub group_ids    { return $_[0]{'group_ids'};  }
sub admin_groups { return $_[0]{'admin_groups'} ||= { map { $_->group_id => $_ } $_[0]->hub->user->find_admin_groups }; }
sub session_id   { return $_[0]{'session_id'}   ||= $_[0]->hub->session->create_session_id; }

sub dbh {
  my $self = shift;
  my $dbhs = {};

  for (qw(session user)) {
    my $method    = "_dbh_$_";
    my $dbh       = $self->$method or next;
    $dbhs->{$_}   = $dbh;
  }

  return keys %$dbhs ? $dbhs : undef;
}

sub _dbh_session {
  return $DBH_SESSION if $DBH_SESSION and $DBH_SESSION->ping;

  my $self  = shift;
  my $db    = $self->hub->species_defs->session_db;

  # try and get session db connection
  eval {
    $DBH_SESSION = DBI->connect(sprintf('DBI:mysql:database=%s;host=%s;port=%s', $db->{'NAME'}, $db->{'HOST'}, $db->{'PORT'}), $db->{'USER'}, $db->{'PASS'});
  };

  EnsEMBL::Web::Controller->disconnect_on_request_finish($DBH_SESSION);

  return $DBH_SESSION || undef;
}

sub _dbh_user {
  return $DBH_USER if $DBH_USER and $DBH_USER->ping;

  my $self  = shift;
  my $hub   = $self->hub;

  return unless $hub->users_plugin_available;

  my $db = $hub->species_defs->accounts_db;

  # try and get accounts db connection
  eval {
    $DBH_USER = DBI->connect(sprintf('DBI:mysql:database=%s;host=%s;port=%s', $db->{'NAME'}, $db->{'HOST'}, $db->{'PORT'}), $db->{'USER'}, $db->{'PASS'});
  };

  EnsEMBL::Web::Controller->disconnect_on_request_finish($DBH_USER);

  return $DBH_USER || undef;
}

sub record_type_query {
  my $self   = shift;
  my @ids    = grep $_, $self->session_id, $self->user_id;
  my @groups = @{$self->group_ids};
  my $where  = '(cd.record_type = "session" AND cd.record_type_id = ?)';
     $where .= qq{ OR (cd.record_type = "user" AND cd.record_type_id = ?)} if scalar @ids == 2;
     $where .= sprintf qq{ OR (cd.record_type = "group" AND cd.record_type_id IN (%s))}, join ',', map '?', @groups if scalar @groups;
     $where  = "($where) AND cd.site_type = ?";
  
  return ($where, @ids, @groups, $self->site_type);
}

sub all_configs {
  my $self = shift;
  
  if (!$self->{'all_configs'}) {
    $self->{'all_configs'} = $self->filtered_configs;<commit_msg>No need to connect to user db if user is not logged in<commit_after>=head1 LICENSE

Copyright [1999-2016] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

package EnsEMBL::Web::DBSQL::ConfigAdaptor;

use strict;

use Data::Dumper;
use DBI;
use Digest::MD5 qw(md5_hex);
use HTML::Entities qw(encode_entities decode_entities);

use EnsEMBL::Web::Controller;

my $DBH_SESSION; # package database handle for persistence
my $DBH_USER;

sub new {
  my ($class, $hub) = @_;
  my $species_defs  = $hub->species_defs;
  my $user          = $hub->user;
  my $self          = {
    hub        => $hub,
    session_id => $hub->session->create_session_id,
    user_id    => $user ? $user->id : undef,
    group_ids  => [ $user ? map($_->group_id, $user->get_groups) : (), @{$species_defs->ENSEMBL_DEFAULT_USER_GROUPS || []} ], 
    servername => $species_defs->ENSEMBL_SERVERNAME,
    site_type  => $species_defs->ENSEMBL_SITETYPE,
    version    => $species_defs->ENSEMBL_VERSION,
    cache_tags => [],
  };
  
  bless $self, $class;
  
  $self->dbh;
  
  return $self;
}

sub hub          { return $_[0]{'hub'};        }
sub user_id      { return $_[0]{'user_id'};    }
sub servername   { return $_[0]{'servername'}; }
sub site_type    { return $_[0]{'site_type'};  }
sub version      { return $_[0]{'version'};    }
sub cache_tags   { return $_[0]{'cache_tags'}; }
sub group_ids    { return $_[0]{'group_ids'};  }
sub admin_groups { return $_[0]{'admin_groups'} ||= { map { $_->group_id => $_ } $_[0]->hub->user->find_admin_groups }; }
sub session_id   { return $_[0]{'session_id'}   ||= $_[0]->hub->session->create_session_id; }

sub dbh {
  my $self = shift;
  my $dbhs = {};

  for (qw(session user)) {
    my $method    = "_dbh_$_";
    my $dbh       = $self->$method or next;
    $dbhs->{$_}   = $dbh;
  }

  return keys %$dbhs ? $dbhs : undef;
}

sub _dbh_session {
  return $DBH_SESSION if $DBH_SESSION and $DBH_SESSION->ping;

  my $self  = shift;
  my $db    = $self->hub->species_defs->session_db;

  # try and get session db connection
  eval {
    $DBH_SESSION = DBI->connect(sprintf('DBI:mysql:database=%s;host=%s;port=%s', $db->{'NAME'}, $db->{'HOST'}, $db->{'PORT'}), $db->{'USER'}, $db->{'PASS'});
  };

  EnsEMBL::Web::Controller->disconnect_on_request_finish($DBH_SESSION);

  return $DBH_SESSION || undef;
}

sub _dbh_user {
  return $DBH_USER if $DBH_USER and $DBH_USER->ping;

  my $self  = shift;
  my $hub   = $self->hub;

  return unless $hub->users_plugin_available;
  return unless $hub->user;

  my $db = $hub->species_defs->accounts_db;

  # try and get accounts db connection
  eval {
    $DBH_USER = DBI->connect(sprintf('DBI:mysql:database=%s;host=%s;port=%s', $db->{'NAME'}, $db->{'HOST'}, $db->{'PORT'}), $db->{'USER'}, $db->{'PASS'});
  };

  EnsEMBL::Web::Controller->disconnect_on_request_finish($DBH_USER);

  return $DBH_USER || undef;
}

sub record_type_query {
  my $self   = shift;
  my @ids    = grep $_, $self->session_id, $self->user_id;
  my @groups = @{$self->group_ids};
  my $where  = '(cd.record_type = "session" AND cd.record_type_id = ?)';
     $where .= qq{ OR (cd.record_type = "user" AND cd.record_type_id = ?)} if scalar @ids == 2;
     $where .= sprintf qq{ OR (cd.record_type = "group" AND cd.record_type_id IN (%s))}, join ',', map '?', @groups if scalar @groups;
     $where  = "($where) AND cd.site_type = ?";
  
  return ($where, @ids, @groups, $self->site_type);
}

sub all_configs {
  my $self = shift;
  
  if (!$self->{'all_configs'}) {
    $self->{'all_configs'} = $self->filtered_configs;
{'fraction_non_alphanumeric': 0.16914201546581564, 'fraction_numerical': 0.006628206701853443, 'mean_word_length': 3.773286467486819, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 124, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '358045', 'n_tokens_mistral': 3034, 'n_tokens_neox': 2946, 'n_words': 726}