<commit_before><?

session_start();
require('config.php');

$tables = array();

function lt_table($tag, $title, $query, $options = array()) {
  global $lt_settings;
  global $tables;
  global $basename; // Set by lt_print_block()
  global $block_options; // Set by lt_print_block()

  if (!$basename) { // lt_table run from data.php
    $table = array();
    $table['tag'] = $tag;
    $table['title'] = $title;
    $table['query'] = $query;
    $table['options'] = $options;
    $tables[] = $table;
    return;
  }

  if (empty($tag)) {
    print "<p>Table in block $basename has no tag specified</p>";
    return;
  }
  if (empty($title)) {
    print "<p>Table $tag in block $basename has no title specified</p>";
    return;
  }
  if (empty($query)) {
    print "<p>No query specified for table $tag in block $basename</p>";
    return;
  }

  if (!empty($block_options['params'])) $params = $block_options['params'];
  elseif (!empty($options['params'])) {
    if (is_numeric($options['params'])) $params = array();
    elseif (is_array($options['params'])) {
      $params = array();
      foreach ($options['params'] as $param) {
        if (!empty($_GET[$param])) $params[] = $_GET[$param];
        else {
          print "<p>Table $tag in block $basename requires $param parameter</p>";
          return;
        }
      }
    }
  }
  else $params = array();
  if (!empty($options['classes']['div'])) $divclasses = 'lt-div ' . $options['classes']['div'];
  else $divclasses = 'lt-div';

  if (!empty($options['embed'])) {
    $data = lt_query($query, $params);
    if (isset($data['error'])) {
      print '<p>Query for table ' . $table['title'] . ' in block ' . $basename . ' returned error: ' . $data['error'] . '</p>';
      return;
    }
    $data['options'] = $options;
    if (empty($lt_settings['checksum']) || ($lt_settings['checksum'] == 'php')) $data['crc'] = crc32(json_encode($data['rows']));
    elseif ($lt_settings['checksum'] == 'psql') {
      $data['crc'] = lt_query_single("SELECT md5(string_agg(q::text, '')) FROM ($query) AS q)");
      if (strpos($data['crc'], 'Error:') === 0) {
        print '<p>Checksum query for table ' . $table['title'] . ' in block ' . $basename . ' returned error: ' . substr($data['crc'], 6);
        return;
      }
    }
    $data['title'] = $title;
    $data['block'] = $basename;
    $data['tag'] = $tag;

    if (empty($params)) {
      if (!empty($options['params'])) print ' <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-embedded="' . "\n" . chunk_split(base64_encode(json_encode($data)), 79, "\n") . '" data-params="-">Loading table ' . $title . "...</div>\n";
      else print '  <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-embedded="' . "\n" . chunk_split(base64_encode(json_encode($data)), 79, "\n") . '">Loading table ' . $title . "...</div>\n";
    }
    else {
      $data['params'] = base64_encode(json_encode($params));
      print ' <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-embedded="' . "\n" . chunk_split(base64_encode(json_encode($data)), 79, "\n") . '" data-params="' . base64_encode(json_encode($params)) . '">Loading table ' . $title . "...</div>\n";
    }
  }
  elseif (empty($params)) {
    if (!empty($options['params'])) print '  <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-params="-">Loading table ' . $title . "...</div>\n";
    else print '  <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '">Loading table ' . $title . "...</div>\n";
  }
  else print '  <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-params="' . base64_encode(json_encode($params)) . '">Loading table ' . $title . "...</div>\n";
}

function lt_calendar($tag, $queries, $options = array()) {
  global $lt_settings;
  global $tables;
  global $basename;

  if (!$basename) { // run from data.php
    $table = array();
    $table['tag'] = $tag;
    $table['queries'] = $queries;
    $table['options'] = $options;
    $tables[] = $table;
  }
}

function lt_print_block($block, $params = array(), $options = array()) {
  global $lt_settings;
  global $basename;
  global $block_options;

  $basename = $block;
  $block_options = $options;

  if ($lt_settings['security'] == 'php') {
    if (empty($lt_settings['allowed_blocks_query'])) {
      print "Configuration sets security to 'php' but no allowed_blocks_query defined";
      return;
    }
    if (!($res = $dbh->query($lt_settings['allowed_blocks_query']))) {
      $err = $dbh->errorInfo();
      print "Allowed-blocks query returned error: " . $err[2];
      return;
    }
    $allowed_blocks = $res->fetchAll(PDO::FETCH_COLUMN, 0);
    if (!in_array($basename, $allowed_blocks)) {
      print "Block $basename is not in our list of allowed blocks";
      return;
    }
  }

  if (is_array($lt_settings['blocks_dir'])) $dirs = $lt_settings['blocks_dir'];
  else $dirs[] = $lt_settings['blocks_dir'];

  foreach($dirs as $dir) {
    if (file_exists($dir . $basename . '.html')) {
      readfile($dir . $basename . '.html');
      return;
    }
    if (function_exists('yaml_parse_file') && file_exists($dir . $basename . '.yml')) {
      $yaml = yaml_parse_file($dir . $basename . '.yml', -1);
      if ($yaml === false) print("<p>YAML syntax error in block $basename</p>");
      else {
        foreach ($yaml as $table) {
          lt_table($table[0], $table[1], $table[2], isset($table[3])?$table[3]:array());
        }
      }
      return;
    }
    if (file_exists($dir . $basename . '.php')) {
      if (!empty($params)) {
        $block_options['params'] = $params;
        print '<div id="block_' . $basename . '_' . base64_encode(json_encode($params)) . '"';
      }
      else print '<div id="block_' . $basename . '"';
      if (!empty($block_options['style'])) print ' style="' . $block_options['style'] . '"';
      print ">\n";
      if (eval(file_get_contents($dir . $basename . '.php')) === FALSE) print "<p>PHP syntax error in block $basename</p>";
      print "</div>\n";
      return;
    }
  }

  print "Block $basename not found in blocks_dir " . implode(", ", $dirs);
}

function lt_query($query, $params = array(), $id = 0) {
  global $dbh;
  $ret = array();

  $start = microtime(TRUE);
  if (empty($params)) {
    if (!($res = $dbh->query($query))) {
      $err = $dbh->errorInfo();
      $ret['error'] = $err[2];
      return $ret;<commit_msg>Only wrap block in a div when `wrapperdiv` option is set<commit_after><?

session_start();
require('config.php');

$tables = array();

function lt_table($tag, $title, $query, $options = array()) {
  global $lt_settings;
  global $tables;
  global $basename; // Set by lt_print_block()
  global $block_options; // Set by lt_print_block()

  if (!$basename) { // lt_table run from data.php
    $table = array();
    $table['tag'] = $tag;
    $table['title'] = $title;
    $table['query'] = $query;
    $table['options'] = $options;
    $tables[] = $table;
    return;
  }

  if (empty($tag)) {
    print "<p>Table in block $basename has no tag specified</p>";
    return;
  }
  if (empty($title)) {
    print "<p>Table $tag in block $basename has no title specified</p>";
    return;
  }
  if (empty($query)) {
    print "<p>No query specified for table $tag in block $basename</p>";
    return;
  }

  if (!empty($block_options['params'])) $params = $block_options['params'];
  elseif (!empty($options['params'])) {
    if (is_numeric($options['params'])) $params = array();
    elseif (is_array($options['params'])) {
      $params = array();
      foreach ($options['params'] as $param) {
        if (!empty($_GET[$param])) $params[] = $_GET[$param];
        else {
          print "<p>Table $tag in block $basename requires $param parameter</p>";
          return;
        }
      }
    }
  }
  else $params = array();
  if (!empty($options['classes']['div'])) $divclasses = 'lt-div ' . $options['classes']['div'];
  else $divclasses = 'lt-div';

  if (!empty($options['embed'])) {
    $data = lt_query($query, $params);
    if (isset($data['error'])) {
      print '<p>Query for table ' . $table['title'] . ' in block ' . $basename . ' returned error: ' . $data['error'] . '</p>';
      return;
    }
    $data['options'] = $options;
    if (empty($lt_settings['checksum']) || ($lt_settings['checksum'] == 'php')) $data['crc'] = crc32(json_encode($data['rows']));
    elseif ($lt_settings['checksum'] == 'psql') {
      $data['crc'] = lt_query_single("SELECT md5(string_agg(q::text, '')) FROM ($query) AS q)");
      if (strpos($data['crc'], 'Error:') === 0) {
        print '<p>Checksum query for table ' . $table['title'] . ' in block ' . $basename . ' returned error: ' . substr($data['crc'], 6);
        return;
      }
    }
    $data['title'] = $title;
    $data['block'] = $basename;
    $data['tag'] = $tag;

    if (empty($params)) {
      if (!empty($options['params'])) print ' <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-embedded="' . "\n" . chunk_split(base64_encode(json_encode($data)), 79, "\n") . '" data-params="-">Loading table ' . $title . "...</div>\n";
      else print '  <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-embedded="' . "\n" . chunk_split(base64_encode(json_encode($data)), 79, "\n") . '">Loading table ' . $title . "...</div>\n";
    }
    else {
      $data['params'] = base64_encode(json_encode($params));
      print ' <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-embedded="' . "\n" . chunk_split(base64_encode(json_encode($data)), 79, "\n") . '" data-params="' . base64_encode(json_encode($params)) . '">Loading table ' . $title . "...</div>\n";
    }
  }
  elseif (empty($params)) {
    if (!empty($options['params'])) print '  <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-params="-">Loading table ' . $title . "...</div>\n";
    else print '  <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '">Loading table ' . $title . "...</div>\n";
  }
  else print '  <div id="' . $tag . '" class="' . $divclasses . '" data-source="' . $basename . ':' . $tag . '" data-params="' . base64_encode(json_encode($params)) . '">Loading table ' . $title . "...</div>\n";
}

function lt_calendar($tag, $queries, $options = array()) {
  global $lt_settings;
  global $tables;
  global $basename;

  if (!$basename) { // run from data.php
    $table = array();
    $table['tag'] = $tag;
    $table['queries'] = $queries;
    $table['options'] = $options;
    $tables[] = $table;
  }
}

function lt_print_block($block, $params = array(), $options = array()) {
  global $lt_settings;
  global $basename;
  global $block_options;

  $basename = $block;
  $block_options = $options;

  if ($lt_settings['security'] == 'php') {
    if (empty($lt_settings['allowed_blocks_query'])) {
      print "Configuration sets security to 'php' but no allowed_blocks_query defined";
      return;
    }
    if (!($res = $dbh->query($lt_settings['allowed_blocks_query']))) {
      $err = $dbh->errorInfo();
      print "Allowed-blocks query returned error: " . $err[2];
      return;
    }
    $allowed_blocks = $res->fetchAll(PDO::FETCH_COLUMN, 0);
    if (!in_array($basename, $allowed_blocks)) {
      print "Block $basename is not in our list of allowed blocks";
      return;
    }
  }

  if (is_array($lt_settings['blocks_dir'])) $dirs = $lt_settings['blocks_dir'];
  else $dirs[] = $lt_settings['blocks_dir'];

  if (!empty($options['wrapperdiv']) && $options['wrapperdiv']) {
    if (!empty($params)) print '<div id="block_' . $basename . '_' . base64_encode(json_encode($params)) . '"';
    else print '<div id="block_' . $basename . '"';
    if (!empty($block_options['style'])) print ' style="' . $block_options['style'] . '"';
    print ">\n";
  }
  foreach($dirs as $dir) {
    if (file_exists($dir . $basename . '.html')) {
      readfile($dir . $basename . '.html');
      if (!empty($options['wrapperdiv']) && $options['wrapperdiv']) print "</div>\n";
      return;
    }
    if (function_exists('yaml_parse_file') && file_exists($dir . $basename . '.yml')) {
      $yaml = yaml_parse_file($dir . $basename . '.yml', -1);
      if ($yaml === false) print("<p>YAML syntax error in block $basename</p>");
      else {
        foreach ($yaml as $table) {
          lt_table($table[0], $table[1], $table[2], isset($table[3])?$table[3]:array());
        }
      }
      if (!empty($options['wrapperdiv']) && $options['wrapperdiv']) print "</div>\n";
      return;
    }
    if (file_exists($dir . $basename . '.php')) {
      if (!empty($params)) $block_options['params'] = $params;
      if (eval(file_get_contents($dir . $basename . '.php')) === FALSE) print "<p>PHP syntax error in block $basename</p>";
      if (!empty($options['wrapperdiv']) && $options['wrapperdiv']) print "</div>\n";
      return;
    }
  }

  print "Block $basename not found in blocks_dir " . implode(", ", $dirs);
}

function lt_query($query, $params = array(), $id = 0) {
  global $dbh;
  $ret = array();

  $start = microtime(TRUE);
  if (empty($params)) {
    if (!($res = $dbh->query($query))) {
      $err = $dbh->errorInfo();
      $ret['error'] = $err[2];
      return $ret;
{'fraction_non_alphanumeric': 0.2264540891644523, 'fraction_numerical': 0.005166814289932094, 'mean_word_length': 3.1370992366412214, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 73, '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': '55489', 'n_tokens_mistral': 4664, 'n_tokens_neox': 4544, 'n_words': 1039}