<commit_before>package EnsEMBL::Web::Object::HelpRecord;

use strict;

use EnsEMBL::Web::Exceptions;

use base qw(EnsEMBL::Web::Object::DbFrontend);

sub record_type {
  ## Gets the type of the record(s) requested for i.e. - glossary, view, movie, faq
  my $self = shift;

  my $type = lc $self->hub->function;
  return $type && grep({$type eq $_} qw(glossary view movie faq)) ? $type : undef;
}

sub fetch_for_display {
  my $self   = shift;
  return $self->SUPER::fetch_for_display(@_) if $self->hub->param('id');

  my $type   = $self->record_type;
  $self->SUPER::fetch_for_display({'query' => ['type' => $type]}) if $type;

  my $rose_objects  = $self->rose_objects;
  my $order_by_1    = {qw(glossary word view ensembl_object movie title faq category)}->{$type};
  my $order_by_2    = {qw(view ensembl_action faq question)}->{$type} || 0;

  $self->rose_objects([ sort { ($a->virtual_column_value($order_by_1) cmp $b->virtual_column_value($order_by_1)) || $order_by_2 && ($a->virtual_column_value($order_by_2) cmp $b->virtual_column_value($order_by_2))} @$rose_objects ]) if $rose_objects;
}

sub fetch_for_list {
  return shift->fetch_for_display(@_);
}

sub get_help_images_dir {
  ## Returns the absolute address of the directory that contains help images
  my $sd = shift->hub->species_defs;
  return $sd->ENSEMBL_SERVERROOT.'/htdocs'.$sd->ENSEMBL_HELP_IMAGE_ROOT;
}

sub get_help_images_list {
  ## Returns list of all the images in the help folder along with some extra info about each image
  my $self      = shift;
  my $dir       = $self->get_help_images_dir or throw exception('Images directory has not been configured. Please configure it in your plugins.');
  my $hub       = $self->hub;
  my $function  = $hub->function;
  my $root      = `pwd`;

  my %list;

  chdir $dir or throw exception("Error getting to images directory: $!");

  open ENTRIES, '<', 'CVS/Entries' or throw exception("Error reading CVS entries file: $!");
  my %cvs_entries;
  for (<ENTRIES>) {
    chop;
    my ($type, $name, $rev, $timestamp, $options, $tagdate) = split '/', $_;
    next if $type eq 'D';
    $cvs_entries{$name} = $tagdate && $tagdate =~ /^T(.+)/ ? $1 : undef;
  }
  close ENTRIES;

  opendir IMAGES_DIR, '.';

  for (readdir IMAGES_DIR) {
    next if -d; # skip directories

    $list{$_} = {
      'name'      => $_,
      'writable'  => -W || 0,
      'size'      => -s,
      'cvs'       => exists $cvs_entries{$_} ? _get_cvs_status($_) : 'New',
      'tag'       => delete $cvs_entries{$_}
    };

    if ($list{$_}{'writable'} && $function eq 'View' && $hub->param('file') eq $_) {
      open IMG, "<$_" or throw exception("Error reading image $_: $!");
      my $ctx = Digest::MD5->new;
      $ctx->addfile (*IMG);
      $list{$_}{'md5'} = substr $ctx->hexdigest, 0, 8;
      close IMG;
    }
  }

  closedir IMAGES_DIR;

  for (keys %cvs_entries) { # if any file doesn't exist on file system but is in cvs entries
    $list{$_} = {
      'name'      => $_,
      'writable'  => 1,
      'missing'   => 1,
      'cvs'       => _get_cvs_status($_)
    };
  }

  chop  $root;
  chdir $root;

  ## validate actions for each file<commit_msg>add dimensions to the image info for constructing image embed code<commit_after>package EnsEMBL::Web::Object::HelpRecord;

use strict;

use EnsEMBL::Web::Exceptions;

use base qw(EnsEMBL::Web::Object::DbFrontend);

sub record_type {
  ## Gets the type of the record(s) requested for i.e. - glossary, view, movie, faq
  my $self = shift;

  my $type = lc $self->hub->function;
  return $type && grep({$type eq $_} qw(glossary view movie faq)) ? $type : undef;
}

sub fetch_for_display {
  my $self   = shift;
  return $self->SUPER::fetch_for_display(@_) if $self->hub->param('id');

  my $type   = $self->record_type;
  $self->SUPER::fetch_for_display({'query' => ['type' => $type]}) if $type;

  my $rose_objects  = $self->rose_objects;
  my $order_by_1    = {qw(glossary word view ensembl_object movie title faq category)}->{$type};
  my $order_by_2    = {qw(view ensembl_action faq question)}->{$type} || 0;

  $self->rose_objects([ sort { ($a->virtual_column_value($order_by_1) cmp $b->virtual_column_value($order_by_1)) || $order_by_2 && ($a->virtual_column_value($order_by_2) cmp $b->virtual_column_value($order_by_2))} @$rose_objects ]) if $rose_objects;
}

sub fetch_for_list {
  return shift->fetch_for_display(@_);
}

sub get_help_images_dir {
  ## Returns the absolute address of the directory that contains help images
  my $sd = shift->hub->species_defs;
  return $sd->ENSEMBL_SERVERROOT.'/htdocs'.$sd->ENSEMBL_HELP_IMAGE_ROOT;
}

sub get_help_images_list {
  ## Returns list of all the images in the help folder along with some extra info about each image
  my $self      = shift;
  my $dir       = $self->get_help_images_dir or throw exception('Images directory has not been configured. Please configure it in your plugins.');
  my $hub       = $self->hub;
  my $function  = $hub->function;
  my $root      = `pwd`;

  my %list;

  chdir $dir or throw exception("Error getting to images directory: $!");

  open ENTRIES, '<', 'CVS/Entries' or throw exception("Error reading CVS entries file: $!");
  my %cvs_entries;
  for (<ENTRIES>) {
    chop;
    my ($type, $name, $rev, $timestamp, $options, $tagdate) = split '/', $_;
    next if $type eq 'D';
    $cvs_entries{$name} = $tagdate && $tagdate =~ /^T(.+)/ ? $1 : undef;
  }
  close ENTRIES;

  opendir IMAGES_DIR, '.';

  for (readdir IMAGES_DIR) {
    next if -d; # skip directories

    $list{$_} = {
      'name'      => $_,
      'writable'  => -W || 0,
      'size'      => -s,
      'cvs'       => exists $cvs_entries{$_} ? _get_cvs_status($_) : 'New',
      'tag'       => delete $cvs_entries{$_}
    };

    if ($list{$_}{'writable'} && $function eq 'View' && $hub->param('file') eq $_) {
      open IMG, "<$_" or throw exception("Error reading image $_: $!");
      my $ctx = Digest::MD5->new;
      $ctx->addfile (*IMG);
      $list{$_}{'md5'} = substr $ctx->hexdigest, 0, 8;
      close IMG;

      if ($list{$_}{'cvs'} eq 'Up-to-date' && !$list{$_}{'tag'}) {
        `file $_` =~ /\s+([0-9]+)\s+x\s+([0-9]+)/;
        $list{$_}{'dim'} = {'x' => $1, 'y' => $2} if $1 && $2;
      }
    }
  }

  closedir IMAGES_DIR;

  for (keys %cvs_entries) { # if any file doesn't exist on file system but is in cvs entries
    $list{$_} = {
      'name'      => $_,
      'writable'  => 1,
      'missing'   => 1,
      'cvs'       => _get_cvs_status($_)
    };
  }

  chop  $root;
  chdir $root;

  ## validate actions for each file
{'fraction_non_alphanumeric': 0.17121928887532428, 'fraction_numerical': 0.00610407446970853, 'mean_word_length': 3.3090072320841553, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 83, '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': '1361235', 'n_tokens_mistral': 2445, 'n_tokens_neox': 2385, 'n_words': 612}