<commit_before><?php
/* This file is part of the XP framework's port "Dialog"
 *
 * $Id$ 
 */
  require('lang.base.php');
  xp::sapi('cli');
  uses(
    'io.Folder',
    'io.File',
    'io.FileUtil',
    'util.Date',
    'util.log.Logger',
    'util.log.ConsoleAppender', 
    'de.thekid.dialog.SingleShot',
    'de.thekid.dialog.io.ShotProcessor',
    'de.thekid.dialog.io.IndexCreator'
  );

  define('ENTRIES_PER_PAGE',  5);
  define('DESCRIPTION_EXT',  '.txt');
  
  define('DATA_FOLDER',       dirname(__FILE__).'/../data/');
  define('IMAGE_FOLDER',      dirname(__FILE__).'/../doc_root/shots/');
  
  // {{{ main
  $param= &new ParamString();
  if (!$param->exists(1) || $param->exists('help', '?')) {
    Console::writeLine(<<<__
Imports a directory of images into an album
--------------------------------------------------------------------------------
Usage:
  php add_singleshot.php <<image_file>> [<<options>>]

Options:
  --debug, -d     Turns on debugging (default: off)
  --title, -t     Set shot title (default: origin file name)
  --desc, -E      Set description in case description file does not exist
  --date, -D      Set album date (default: origin file's creation date)
__
    );
    exit(1);
  }

  // Check origin folder
  $origin= &new File($param->value(1));
  if (!$origin->exists()) {
    Console::writeLinef(
      'The specified file "%s" does not exist', 
      $origin->getURI()
    );
    exit(2);
  }

  // Calculate shot name
  $filename= substr($origin->getFilename(), 0, strpos($origin->getFilename(), '.'));
  $name= preg_replace('/[^a-z0-9-]/i', '_', $filename);
  
  // Create destination folder if it doesn't exist yet
  $destination= &new Folder(IMAGE_FOLDER);
  try(); {
    $destination->exists() || $destination->create(0755);
  } if (catch('IOException', $e)) {
    $e->printStackTrace();
    exit(-1);
  }

  Console::writeLine('===> Starting import at ', date('r'));

  // Set up processor
  $processor= &new ShotProcessor();
  $processor->setOutputFolder($destination);
  
  // Check if debugging output is wanted
  $cat= NULL;
  if ($param->exists('debug')) {
    $l= &Logger::getInstance();
    $cat= &$l->getCategory();
    $cat->addAppender(new ConsoleAppender());
    $processor->setTrace($cat);
  }
  
  with ($shot= &new SingleShot()); {
    $shot->setName($name);
    $shot->setFileName($origin->getFilename());
    $shot->setTitle($param->value('title', 't', $origin->filename));
    $shot->setDate(new Date($param->value('date', 'D', $origin->createdAt())));

    // Read the introductory text from [filename].txt if existant
    if (is_file($df= $filename.DESCRIPTION_EXT)) {
      $shot->setDescription(file_get_contents($df));
    } else {
      $shot->setDescription($param->value('desc', 'E', ''));
    }

    try(); {
      $image= &$processor->albumImageFor($origin->getURI());
    } if (catch('ImagingException', $e)) {
      $e->printStackTrace();
      exit(-2);
    }
    
    $shot->setImage($image);
  }

  // Save shot
  $serialized= &new File(DATA_FOLDER.$name.'.dat');
  $cat && $cat->debug($shot);
  try(); {
    FileUtil::setContents($serialized, serialize($shot));
  } if (catch('IOException', $e)) {
    $e->printStackTrace();
    exit(-1);
  }

  // Regenerate indexes
  $index= &IndexCreator::forFolder(new Folder(DATA_FOLDER));
  $index->setEntriesPerPage(ENTRIES_PER_PAGE);
  $index->setTrace($cat);
  $index->regenerate();

  Console::writeLine('===> Finished at ', date('r'));
  // }}}
?>
<commit_msg>- QA: Fix inline comment<commit_after><?php
/* This file is part of the XP framework's port "Dialog"
 *
 * $Id$ 
 */
  require('lang.base.php');
  xp::sapi('cli');
  uses(
    'io.Folder',
    'io.File',
    'io.FileUtil',
    'util.Date',
    'util.log.Logger',
    'util.log.ConsoleAppender', 
    'de.thekid.dialog.SingleShot',
    'de.thekid.dialog.io.ShotProcessor',
    'de.thekid.dialog.io.IndexCreator'
  );

  define('ENTRIES_PER_PAGE',  5);
  define('DESCRIPTION_EXT',  '.txt');
  
  define('DATA_FOLDER',       dirname(__FILE__).'/../data/');
  define('IMAGE_FOLDER',      dirname(__FILE__).'/../doc_root/shots/');
  
  // {{{ main
  $param= &new ParamString();
  if (!$param->exists(1) || $param->exists('help', '?')) {
    Console::writeLine(<<<__
Imports a directory of images into an album
--------------------------------------------------------------------------------
Usage:
  php add_singleshot.php <<image_file>> [<<options>>]

Options:
  --debug, -d     Turns on debugging (default: off)
  --title, -t     Set shot title (default: origin file name)
  --desc, -E      Set description in case description file does not exist
  --date, -D      Set album date (default: origin file's creation date)
__
    );
    exit(1);
  }

  // Check origin file
  $origin= &new File($param->value(1));
  if (!$origin->exists()) {
    Console::writeLinef(
      'The specified file "%s" does not exist', 
      $origin->getURI()
    );
    exit(2);
  }

  // Calculate shot name
  $filename= substr($origin->getFilename(), 0, strpos($origin->getFilename(), '.'));
  $name= preg_replace('/[^a-z0-9-]/i', '_', $filename);
  
  // Create destination folder if it doesn't exist yet
  $destination= &new Folder(IMAGE_FOLDER);
  try(); {
    $destination->exists() || $destination->create(0755);
  } if (catch('IOException', $e)) {
    $e->printStackTrace();
    exit(-1);
  }

  Console::writeLine('===> Starting import at ', date('r'));

  // Set up processor
  $processor= &new ShotProcessor();
  $processor->setOutputFolder($destination);
  
  // Check if debugging output is wanted
  $cat= NULL;
  if ($param->exists('debug')) {
    $l= &Logger::getInstance();
    $cat= &$l->getCategory();
    $cat->addAppender(new ConsoleAppender());
    $processor->setTrace($cat);
  }
  
  with ($shot= &new SingleShot()); {
    $shot->setName($name);
    $shot->setFileName($origin->getFilename());
    $shot->setTitle($param->value('title', 't', $origin->filename));
    $shot->setDate(new Date($param->value('date', 'D', $origin->createdAt())));

    // Read the introductory text from [filename].txt if existant
    if (is_file($df= $filename.DESCRIPTION_EXT)) {
      $shot->setDescription(file_get_contents($df));
    } else {
      $shot->setDescription($param->value('desc', 'E', ''));
    }

    try(); {
      $image= &$processor->albumImageFor($origin->getURI());
    } if (catch('ImagingException', $e)) {
      $e->printStackTrace();
      exit(-2);
    }
    
    $shot->setImage($image);
  }

  // Save shot
  $serialized= &new File(DATA_FOLDER.$name.'.dat');
  $cat && $cat->debug($shot);
  try(); {
    FileUtil::setContents($serialized, serialize($shot));
  } if (catch('IOException', $e)) {
    $e->printStackTrace();
    exit(-1);
  }

  // Regenerate indexes
  $index= &IndexCreator::forFolder(new Folder(DATA_FOLDER));
  $index->setEntriesPerPage(ENTRIES_PER_PAGE);
  $index->setTrace($cat);
  $index->regenerate();

  Console::writeLine('===> Finished at ', date('r'));
  // }}}
?>

{'fraction_non_alphanumeric': 0.22339968494916226, 'fraction_numerical': 0.004296147787483889, 'mean_word_length': 3.0985915492957745, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 89, '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': '1325', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2301, 'n_words': 468}