<commit_before><?php

class Ajax_Controller extends Emeraldview_Template_Controller
{
  protected $theme;
  protected $availableLanguages;
  protected $language;
  protected $collection;
  protected $session;

  public function __construct()
  {
    $this->auto_render = false;

    if ( ! EmeraldviewConfig::get('greenstone_collection_dir') ) {
      $msg = 'Greenstone collection directory not specified '
           . 'in config/emeraldview.yml';
      throw new Exception( $msg );
    }

    Controller::__construct();

    $this->session = Session::instance();
    $this->theme = EmeraldviewConfig::get('default_theme', 'default');

    if ($this->session->get('language')) {
      $this->language = $this->session->get('language');
    }
    else {
      $this->language = EmeraldviewConfig::get('default_language', 'en');
    }

    $this->loadGettextDomain( $this->language );
  }

  protected function loadCollection( $collection_name )
  {
    $this->collection = Collection::factory( $collection_name );

    if (!$this->collection) {
      return false;
    }

    // override global defaults if collection config values set
    if (
      ! $this->session->get('language')
      && $this->collection->getConfig('default_language')
    ) {
      $this->language = $this->collection->getConfig('default_language');
    }

    if ( $this->collection->getConfig('theme') ) {
      $this->theme = $this->collection->getConfig('theme');
    }

    return $this->collection;
  }

  public function browse( $collection_name, $node_id )
  {
    $collection = $this->loadCollection( $collection_name );

    if ( ! $collection ) {
      // FIXME: 404 header (#19)
      exit;
    }

    $node = Node_Classifier::factory( $collection, $node_id );

    if ( ! $node ) {
      // FIXME: 404 header (#19)
      exit;
    }

    $page = $node->getRootNode()->getNodePage();

    $node_tree_formatter = new NodeTreeFormatter( $node, $page );

    $output = $node_tree_formatter->render();

    echo $output;
  }
}<commit_msg>add proper 404 support for AJAX controller; resolves #19<commit_after><?php

class Ajax_Controller extends Emeraldview_Template_Controller
{
  protected $theme;
  protected $availableLanguages;
  protected $language;
  protected $collection;
  protected $session;

  public function __construct()
  {
    $this->auto_render = false;

    if ( ! EmeraldviewConfig::get('greenstone_collection_dir') ) {
      $msg = 'Greenstone collection directory not specified '
           . 'in config/emeraldview.yml';
      throw new Exception( $msg );
    }

    Controller::__construct();

    $this->session = Session::instance();
    $this->theme = EmeraldviewConfig::get('default_theme', 'default');

    if ($this->session->get('language')) {
      $this->language = $this->session->get('language');
    }
    else {
      $this->language = EmeraldviewConfig::get('default_language', 'en');
    }

    $this->loadGettextDomain( $this->language );
  }

  protected function loadCollection( $collection_name )
  {
    $this->collection = Collection::factory( $collection_name );

    if (!$this->collection) {
      return false;
    }

    // override global defaults if collection config values set
    if (
      ! $this->session->get('language')
      && $this->collection->getConfig('default_language')
    ) {
      $this->language = $this->collection->getConfig('default_language');
    }

    if ( $this->collection->getConfig('theme') ) {
      $this->theme = $this->collection->getConfig('theme');
    }

    return $this->collection;
  }

  public function browse( $collection_name, $node_id )
  {
    $collection = $this->loadCollection( $collection_name );

    if ( ! $collection ) {
      header('HTTP/1.1 404 Not Found');
      exit;
    }

    $node = Node_Classifier::factory( $collection, $node_id );

    if ( ! $node ) {
      header('HTTP/1.1 404 Not Found');
      exit;
    }

    $page = $node->getRootNode()->getNodePage();

    $node_tree_formatter = new NodeTreeFormatter( $node, $page );

    $output = $node_tree_formatter->render();

    echo $output;
  }
}
{'fraction_non_alphanumeric': 0.15167032431114363, 'fraction_numerical': 0.0060960741282614, 'mean_word_length': 3.224510813594233, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, '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': '1774930', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1193, 'n_words': 242}