<commit_before><?php
require '../vendor/autoload.php';

$f3= \Base::instance();
$f3->config($_ENV['ORDURE_CONFIG'] ?: '../config.ini');

$f3->set('DBH', new DB\SQL($f3->get('db.dsn'),
                           $f3->get('db.user'),
                           $f3->get('db.password'),
                           array(
                             \PDO::MYSQL_ATTR_LOCAL_INFILE => true,
                             \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
                           )));

// Add @markdown() function for templates
$f3->set('markdown', function($text) {
  $text= Template::instance()->resolve($text);
  return Markdown::instance()->convert($text);
});

$f3->set('item_style_color', function($text) {
  if (preg_match('/^color:(.+)/', $text, $m)) {
    $r= hexdec(substr($m[1], 0, 2));
    $g= hexdec(substr($m[1], 2, 2));
    $b= hexdec(substr($m[1], 4, 2));
    // Calculate whether to use black or white text for bgcolor
    return 'background: #' . $m[1] . '; color: #' .
      ((($r * 0.2126 + $g * 0.7152 + $b * 0.0722) > 179) ? '000' : 'fff');
  } else {
    return '';
  }
});

$f3->set('includeTemplate', function($template) {
  echo Template::instance()->render("$template.html");
});

$f3->set('includeFragment', function($template) {
  $db= \Base::instance()->get('DBH');

  $page= new DB\SQL\Mapper($db, 'page');

  if ($page->load(array('slug=?', $template))) {
    $text= Template::instance()->resolve($page->content);
    echo Markdown::instance()->convert($text);
  } else {
    echo "Couldn't find '$template'";
  }
});

// if DEBUG, allow access to /info
if ($f3->get('DEBUG')) {
  $f3->route('GET|HEAD /info', function ($f3) {
    phpinfo();
  });
}

$f3->set('ONERROR', function ($f3) {
  if ($f3->get('AJAX')) {
    echo json_encode($f3->get('ERROR'));
  } else {
    $db= $f3->get('DBH');

    $code= $f3->get('ERROR.code');

    if ($code == "404") {
      $path= $f3->get('PATH');

      if ($path == '/') die("Confused. Hang on.");

      $catalog= $f3->get('CATALOG');
      if (!strncmp($path, '/' . $catalog . '/', strlen($catalog)+2)) {
        $path= substr($path, strlen($catalog)+2);
        $redir= new DB\SQL\Mapper($db, 'catalog_redirect');
        if ($redir->load(array('source LIKE ?', $path . '%'))) {
          $q= $f3->get('QUERY');
          $f3->reroute('/' . $catalog . '/' . $redir->dest . ($q ? "?$q" : ""));
        }
      } else {
        $redir= new DB\SQL\Mapper($db, 'redirect');
<commit_msg>Flip how we use source on catalog redirects<commit_after><?php
require '../vendor/autoload.php';

$f3= \Base::instance();
$f3->config($_ENV['ORDURE_CONFIG'] ?: '../config.ini');

$f3->set('DBH', new DB\SQL($f3->get('db.dsn'),
                           $f3->get('db.user'),
                           $f3->get('db.password'),
                           array(
                             \PDO::MYSQL_ATTR_LOCAL_INFILE => true,
                             \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
                           )));

// Add @markdown() function for templates
$f3->set('markdown', function($text) {
  $text= Template::instance()->resolve($text);
  return Markdown::instance()->convert($text);
});

$f3->set('item_style_color', function($text) {
  if (preg_match('/^color:(.+)/', $text, $m)) {
    $r= hexdec(substr($m[1], 0, 2));
    $g= hexdec(substr($m[1], 2, 2));
    $b= hexdec(substr($m[1], 4, 2));
    // Calculate whether to use black or white text for bgcolor
    return 'background: #' . $m[1] . '; color: #' .
      ((($r * 0.2126 + $g * 0.7152 + $b * 0.0722) > 179) ? '000' : 'fff');
  } else {
    return '';
  }
});

$f3->set('includeTemplate', function($template) {
  echo Template::instance()->render("$template.html");
});

$f3->set('includeFragment', function($template) {
  $db= \Base::instance()->get('DBH');

  $page= new DB\SQL\Mapper($db, 'page');

  if ($page->load(array('slug=?', $template))) {
    $text= Template::instance()->resolve($page->content);
    echo Markdown::instance()->convert($text);
  } else {
    echo "Couldn't find '$template'";
  }
});

// if DEBUG, allow access to /info
if ($f3->get('DEBUG')) {
  $f3->route('GET|HEAD /info', function ($f3) {
    phpinfo();
  });
}

$f3->set('ONERROR', function ($f3) {
  if ($f3->get('AJAX')) {
    echo json_encode($f3->get('ERROR'));
  } else {
    $db= $f3->get('DBH');

    $code= $f3->get('ERROR.code');

    if ($code == "404") {
      $path= $f3->get('PATH');

      if ($path == '/') die("Confused. Hang on.");

      $catalog= $f3->get('CATALOG');
      if (!strncmp($path, '/' . $catalog . '/', strlen($catalog)+2)) {
        $path= substr($path, strlen($catalog)+2);
        $redir= new DB\SQL\Mapper($db, 'catalog_redirect');
        if ($redir->load(array('? LIKE source', $path))) {
          $q= $f3->get('QUERY');
          $f3->reroute('/' . $catalog . '/' . $redir->dest . ($q ? "?$q" : ""));
        }
      } else {
        $redir= new DB\SQL\Mapper($db, 'redirect');

{'fraction_non_alphanumeric': 0.248687928946306, 'fraction_numerical': 0.023819136051675415, 'mean_word_length': 2.6407053637031592, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '1014699', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1760, 'n_words': 326}