<commit_before><?php
include_once("database.php");

/*
$det = new Details();
header('Content-Type: application/json');
echo json_encode($det->get(array(), 'ESP'));
*/

/**
 * Details for a certain country.
 */
class Details {

  /**
   * Return the detailed info of a certain country.
   * @param $options Array containing the selected language under the variable 'language' and the
   *    API path under 'host'
   * @param $iso3 ISO3 of the country to load its detailed info.
   */
  public function get($options, $iso3) {
    $lang = $options->language;
    //$lang = 'en';
    $api = $options->host;

    $cached = $this->get_from_cache($lang, $iso3);
    if ($cached !== null)
      return $cached;

    $database = new DataBaseHelper();
    $connection = $database->open();

    $info = $database->query($connection, "country", array($lang, $iso3));
    $countries = $database->query($connection, "countries_without_region", array($lang));
    $topics = $database->query($connection, "topics_and_indicators_by_region", array($lang, $iso3));

    $database->close($connection);
    $result = $this->compose_data($info, $countries, $topics);
    apc_store($this->generate_cache_key($lang, $iso3), $result);
    return $result;
  }

  private function get_from_cache($lang, $iso3) {
    $key = $this->generate_cache_key($lang, $iso3);
    if (apc_exists($key) !== false)
      return apc_fetch($key);
    return null;
  }

  private function generate_cache_key($lang, $iso3) {
    return hash('md5', "country_details" . $lang . $iso3);
  }

  private function compose_data($info, $countries, $topics) {
    return array(
      "info" => $this->compose_info($info),
      "selectors" => array(
        "countries" => $this->compose_countries($countries)
      ),
      "topics" => $this->compose_topics($topics),
    );
  }

  private function compose_topics($data) {<commit_msg>Refactor the Detail model to use the new DatabaseHelper.<commit_after><?php
require_once(dirname(__FILE__) .'/../database/database_helper.php');


class Details {

  /**
   * Return the detailed info of a certain country.
   * @param $options Array containing the selected language under the variable 'language' and the
   *    API path under 'host'
   * @param $iso3 ISO3 of the country to load its detailed info.
   */
  public function get($options, $iso3) {
    $lang = $options->language;
    $api = $options->host;

    $cached = $this->get_from_cache($lang, $iso3);
    if ($cached !== null)
      return $cached;

    $database = new DataBaseHelper();
    $database->open();
    $safe_iso3 = $database->escape($iso3);
    $info = $database->query("country", array($lang, $safe_iso3));
    $countries = $database->query("countries_without_region", array($lang));
    $topics = $database->query("topics_and_indicators_by_region", array($lang, $safe_iso3));

    $database->close();
    $result = $this->compose_data($info, $countries, $topics);
    apc_store($this->generate_cache_key($lang, $iso3), $result);
    return $result;
  }

  private function get_from_cache($lang, $iso3) {
    $key = $this->generate_cache_key($lang, $iso3);
    if (apc_exists($key) !== false)
      return apc_fetch($key);
    return null;
  }

  private function generate_cache_key($lang, $iso3) {
    return hash('md5', "country_details" . $lang . $iso3);
  }

  private function compose_data($info, $countries, $topics) {
    return array(
      "info" => $this->compose_info($info),
      "selectors" => array(
        "countries" => $this->compose_countries($countries)
      ),
      "topics" => $this->compose_topics($topics),
    );
  }

  private function compose_topics($data) {
{'fraction_non_alphanumeric': 0.16443228454172368, 'fraction_numerical': 0.007113543091655267, 'mean_word_length': 3.8168642951251646, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 41, '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': '1115883', 'n_tokens_mistral': 1198, 'n_tokens_neox': 1175, 'n_words': 274}