<commit_before>package Unison::WWW::Page;
use CBT::debug;
CBT::debug::identify_file() if ($CBT::debug::trace_uses);

use warnings;

use base Exporter;
use CGI qw( -debug -nosticky -newstyle_urls);
push(@ISA, 'CGI');
#BEGIN { (-t 0) || eval "use CGI::Carp qw(fatalsToBrowser)" }

use Unison;
use Unison::Exceptions;
use Unison::WWW::utilities qw( text_wrap );
use Error qw(:try);

#WARNING: strict must be last, unsure why
use strict;



sub page_connect ($);
sub infer_pseq_id ($);

our $infer_pseq_id = 0;


sub import {
  my $self = shift;
  for (@_) {
	$infer_pseq_id=1 if ($_ eq 'infer_pseq_id');
  }
}


sub new {
  my $class = shift;
  my $self = $class->SUPER::new( @_ );
  $self->{starttime} = time;
  my $v = $self->Vars();
  $v->{debug} = 0 unless defined $v->{debug};


  try {
	page_connect($self);
  }	catch Unison::Exception with {
	my $ex_text = defined $_[0] ? CGI::escapeHTML($_[0]) : 'no exception information';
	my $env = join('', map( { "<br><code>$_: ".(defined $ENV{$_}?$ENV{$_}:'<i>undef</i>')."</code>\n" }
							qw(REMOTE_USER KRB5CCNAME) ));

	__PACKAGE__->die('Unison Connection Failed', 
					 '<pre>'.$ex_text.'</pre>',
					 '<p>',
					 '<hr>Kerberos and user information:',
					 $env
					);
  };


  if (not exists $v->{pseq_id} and $infer_pseq_id) {
	my @st = grep {exists $v->{$_}} qw(q pseq_id seq md5 alias);
	if (@st > 1) {
	  $self->die("please don't provide more than one search term",
				 sprintf('You provided criteria for %d terms (%s)',
						 $#st+1, join(',',@st) ));
	}
	$v->{pseq_id} = infer_pseq_id($self);
	if (not defined $v->{pseq_id}) {
	  $self->die("couldn't infer pseq_id from arguments");
	}

	# hereafter, we don't want these polluting our variables
	delete $v->{'q'};
	delete $v->{alias};
	delete $v->{md5};
	delete $v->{seq};
  }

  $self->{userprefs} = $self->{unison}->get_userprefs();
  $self->{readonly} = 1;
  $self->{readonly} = 0 if ($self->{unison}->{dbname} eq 'csb-dev' 
							and $self->{unison}->{username} =~ m/^(rkh|cavs)$/
							and exists $v->{update} );

  # if we've made it this far, we'll eventually get a page out
  $self->start_html;

  return $self;
}


sub page_connect ($) {
  my $self = shift;
  my $v = $self->Vars();

  # choose database based on port unless explicitly set
  # SERVER_PORT is always set, EXCEPT when debugging from the command line
  if (not exists $v->{dbname}) {
	my ($port) = $ENV{SERVER_PORT} || 80;
	$v->{dbname} = ($port == 8080) ? 'csb-dev'
	  			 : ($port == 8040) ? 'csb-stage'
	             :                   'csb';
  }

  # establish session authentication, preferably via kerberos
  # must connect to host 'csb' for Kerberos authentication
  if (exists $ENV{REMOTE_USER} and -f "/tmp/krb5cc_$ENV{REMOTE_USER}") {
	$v->{username} = $ENV{REMOTE_USER};
	$ENV{KRB5CCNAME}="FILE:/tmp/krb5cc_$v->{username}";
	$v->{host} = 'csb';
  }

  #else: $v->{username} = 'PUBLIC' unless defined $v->{username};


  # NOTE: password=><PASSWORD> works for PUBLIC and krb auth
  $self->{unison} = new Unison( username => $v->{username},
								password => <PASSWORD>,
								host => $v->{host},
								dbname => $v->{dbname} );<commit_msg>* minor changes for move to host csb and krb5 auth<commit_after>package Unison::WWW::Page;
use CBT::debug;
CBT::debug::identify_file() if ($CBT::debug::trace_uses);

use warnings;

use base Exporter;
use CGI qw( -debug -nosticky -newstyle_urls);
push(@ISA, 'CGI');
#BEGIN { (-t 0) || eval "use CGI::Carp qw(fatalsToBrowser)" }

use Unison;
use Unison::Exceptions;
use Unison::WWW::utilities qw( text_wrap );
use Error qw(:try);

#WARNING: strict must be last, unsure why
use strict;



sub page_connect ($);
sub infer_pseq_id ($);

our $infer_pseq_id = 0;


sub import {
  my $self = shift;
  for (@_) {
	$infer_pseq_id=1 if ($_ eq 'infer_pseq_id');
  }
}


sub new {
  my $class = shift;
  my $self = $class->SUPER::new( @_ );
  $self->{starttime} = time;
  my $v = $self->Vars();
  $v->{debug} = 0 unless defined $v->{debug};


  try {
	page_connect($self);
  }	catch Unison::Exception with {
	my $ex_text = defined $_[0] ? CGI::escapeHTML($_[0]) : 'no exception information';
	my $env = join('', map( { "<br><code>$_: ".(defined $ENV{$_}?$ENV{$_}:'<i>undef</i>')."</code>\n" }
							qw(REMOTE_USER KRB5CCNAME) ));

	__PACKAGE__->die('Unison Connection Failed', 
					 '<pre>'.$ex_text.'</pre>',
					 '<p>',
					 '<hr>Kerberos and user information:',
					 $env
					);
  };


  if (not exists $v->{pseq_id} and $infer_pseq_id) {
	my @st = grep {exists $v->{$_}} qw(q pseq_id seq md5 alias);
	if (@st > 1) {
	  $self->die("please don't provide more than one search term",
				 sprintf('You provided criteria for %d terms (%s)',
						 $#st+1, join(',',@st) ));
	}
	$v->{pseq_id} = infer_pseq_id($self);
	if (not defined $v->{pseq_id}) {
	  $self->die("couldn't infer pseq_id from arguments");
	}

	# hereafter, we don't want these polluting our variables
	delete $v->{'q'};
	delete $v->{alias};
	delete $v->{md5};
	delete $v->{seq};
  }

  $self->{userprefs} = $self->{unison}->get_userprefs();
  $self->{readonly} = 1;
  $self->{readonly} = 0 if ($self->{unison}->{dbname} eq 'csb-dev' 
							and $self->{unison}->{username} =~ m/^(rkh|cavs)$/
							and exists $v->{update} );

  $self->{tmproot} = exists $ENV{'DOCUMENT_ROOT'} ? "$ENV{'DOCUMENT_ROOT'}" : '';
  $self->{tmpdir} = "$self->{tmproot}/tmp";

  # if we've made it this far, we'll eventually get a page out
  $self->start_html;

  return $self;
}


sub page_connect ($) {
  my $self = shift;
  my $v = $self->Vars();

  # choose database based on port unless explicitly set
  # SERVER_PORT is always set, EXCEPT when debugging from the command line
  if (not exists $v->{dbname}) {
	my ($port) = $ENV{SERVER_PORT} || 8080;
	$v->{dbname} = ($port == 8000) ? 'csb'
	  			 : ($port == 8040) ? 'csb-stage'
	             :                   'csb-dev';
  }

  # establish session authentication, preferably via kerberos
  # must connect to host 'csb' for Kerberos authentication
  if (exists $ENV{REMOTE_USER} and -f "/tmp/krb5cc_$ENV{REMOTE_USER}") {
	$v->{username} = $ENV{REMOTE_USER};
	$ENV{KRB5CCNAME}="FILE:/tmp/krb5cc_$v->{username}";
	$v->{host} = 'csb';
  } else {
	$v->{username} = `id -nu`
	#$v->{username} = 'PUBLIC' unless defined $v->{username};
  }



  # NOTE: password=><PASSWORD> works for PUBLIC and krb auth
  $self->{unison} = new Unison( username => $v->{username},
								password => <PASSWORD>,
								host => $v->{host},
								dbname => $v->{dbname} );
{'fraction_non_alphanumeric': 0.20073948544138037, 'fraction_numerical': 0.00847327068248344, 'mean_word_length': 3.5686136523574947, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 119, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '205468', 'n_tokens_mistral': 2798, 'n_tokens_neox': 2504, 'n_words': 621}