<commit_before><?php

namespace CtSearchBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use \CtSearchBundle\CtSearchBundle;
use CtSearchBundle\Classes\IndexManager;
use \CtSearchBundle\Classes\Processor;
use \Symfony\Component\HttpFoundation\Response;

class SearchPageController extends Controller {

  /**
   * @Route("/search-pages", name="search-pages")
   */
  public function listSearchPagesAction(Request $request) {
    $searchPages = IndexManager::getInstance()->getSearchPages($this);
    return $this->render('ctsearch/search-page.html.twig', array(
        'title' => $this->get('translator')->trans('Search pages'),
        'main_menu_item' => 'search-pages',
        'searchPages' => $searchPages
    ));
  }

  /**
   * @Route("/search-pages/add", name="search-page-add")
   */
  public function addSearchPageAction(Request $request) {
    return $this->handleAddOrEditSearchPage($request);
  }

  /**
   * @Route("/search-pages/edit", name="search-page-edit")
   */
  public function editSearchPageAction(Request $request) {
    return $this->handleAddOrEditSearchPage($request, $request->get('id'));
  }

  /**
   * @Route("/search-pages/delete", name="search-page-delete")
   */
  public function deleteSearchPageAction(Request $request) {
    if ($request->get('id') != null) {
      IndexManager::getInstance()->deleteSearchPage($request->get('id'));
      CtSearchBundle::addSessionMessage($this, 'status', $this->get('translator')->trans('Search page has been deleted'));
    } else {
      CtSearchBundle::addSessionMessage($this, 'error', $this->get('translator')->trans('No id provided'));
    }
    return $this->redirect($this->generateUrl('search-pages'));
  }

  private function handleAddOrEditSearchPage($request, $id = null) {
    if ($id == null) { //Add
      $searchPage = new \CtSearchBundle\Classes\SearchPage('', '', '{}', json_encode(json_decode('{"fields": {"title":"", "url":"", "excerp_fields":[]}, "aggregations":[], "more_like_this_fields": []}'), JSON_PRETTY_PRINT));
    } else { //Edit
      $searchPage = IndexManager::getInstance()->getSearchPage($request->get('id'));
      $searchPage->setDefinition(json_encode($searchPage->getDefinition(), JSON_PRETTY_PRINT));
      $searchPage->setConfig(json_encode($searchPage->getConfig(), JSON_PRETTY_PRINT));
    }
    $info = IndexManager::getInstance()->getElasticInfo();
    $indexChoices = array(
      '' => $this->get('translator')->trans('Select index'),
    );
    foreach ($info as $k => $data) {
      $indexChoices[$k] = $k;
    }
    $form = $this->createFormBuilder($searchPage)
      ->add('id', 'hidden')
      ->add('name', 'text', array(
        'label' => $this->get('translator')->trans('Search page name'),
        'required' => true,
      ))
      ->add('indexName', 'choice', array(
        'label' => $this->get('translator')->trans('Index name'),
        'choices' => $indexChoices,
        'required' => true
      ))
      ->add('definition', 'textarea', array(
        'label' => $this->get('translator')->trans('JSON Definition'),
        'required' => true
      ))
      ->add('config', 'textarea', array(
        'label' => $this->get('translator')->trans('Configuration (JSON)'),
        'required' => true
      ))
      ->add('save', 'submit', array('label' => $this->get('translator')->trans('Save')))
      ->getForm();
    $form->handleRequest($request);
    if ($form->isValid()) {
      if (json_decode($searchPage->getDefinition()) == null || json_decode($searchPage->getConfig()) == null) {
        CtSearchBundle::addSessionMessage($this, 'error', $this->get('translator')->trans('JSON parsing failed.'));
      } else {
        IndexManager::getInstance()->saveSearchPage($form->getData());
        if ($id == null) {
          CtSearchBundle::addSessionMessage($this, 'status', $this->get('translator')->trans('New search page has been added successfully'));<commit_msg>Migrating search page controller to s3<commit_after><?php

namespace CtSearchBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use \CtSearchBundle\CtSearchBundle;
use CtSearchBundle\Classes\IndexManager;
use \CtSearchBundle\Classes\Processor;
use \Symfony\Component\HttpFoundation\Response;

class SearchPageController extends Controller {

  /**
   * @Route("/search-pages", name="search-pages")
   */
  public function listSearchPagesAction(Request $request) {
    $searchPages = IndexManager::getInstance()->getSearchPages($this);
    return $this->render('ctsearch/search-page.html.twig', array(
        'title' => $this->get('translator')->trans('Search pages'),
        'main_menu_item' => 'search-pages',
        'searchPages' => $searchPages
    ));
  }

  /**
   * @Route("/search-pages/add", name="search-page-add")
   */
  public function addSearchPageAction(Request $request) {
    return $this->handleAddOrEditSearchPage($request);
  }

  /**
   * @Route("/search-pages/edit", name="search-page-edit")
   */
  public function editSearchPageAction(Request $request) {
    return $this->handleAddOrEditSearchPage($request, $request->get('id'));
  }

  /**
   * @Route("/search-pages/delete", name="search-page-delete")
   */
  public function deleteSearchPageAction(Request $request) {
    if ($request->get('id') != null) {
      IndexManager::getInstance()->deleteSearchPage($request->get('id'));
      CtSearchBundle::addSessionMessage($this, 'status', $this->get('translator')->trans('Search page has been deleted'));
    } else {
      CtSearchBundle::addSessionMessage($this, 'error', $this->get('translator')->trans('No id provided'));
    }
    return $this->redirect($this->generateUrl('search-pages'));
  }

  private function handleAddOrEditSearchPage($request, $id = null) {
    if ($id == null) { //Add
      $searchPage = new \CtSearchBundle\Classes\SearchPage('', '', '{}', json_encode(json_decode('{"fields": {"title":"", "url":"", "excerp_fields":[]}, "aggregations":[], "more_like_this_fields": []}'), JSON_PRETTY_PRINT));
    } else { //Edit
      $searchPage = IndexManager::getInstance()->getSearchPage($request->get('id'));
      $searchPage->setDefinition(json_encode($searchPage->getDefinition(), JSON_PRETTY_PRINT));
      $searchPage->setConfig(json_encode($searchPage->getConfig(), JSON_PRETTY_PRINT));
    }
    $info = IndexManager::getInstance()->getElasticInfo();
    $indexChoices = array(
      $this->get('translator')->trans('Select index') => '',
    );
    foreach ($info as $k => $data) {
      $indexChoices[$k] = $k;
    }
    $form = $this->createFormBuilder($searchPage)
      ->add('id', HiddenType::class)
      ->add('name', TextType::class, array(
        'label' => $this->get('translator')->trans('Search page name'),
        'required' => true,
      ))
      ->add('indexName', ChoiceType::class, array(
        'label' => $this->get('translator')->trans('Index name'),
        'choices' => $indexChoices,
        'required' => true
      ))
      ->add('definition', TextareaType::class, array(
        'label' => $this->get('translator')->trans('JSON Definition'),
        'required' => true
      ))
      ->add('config', TextareaType::class, array(
        'label' => $this->get('translator')->trans('Configuration (JSON)'),
        'required' => true
      ))
      ->add('save', SubmitType::class, array('label' => $this->get('translator')->trans('Save')))
      ->getForm();
    $form->handleRequest($request);
    if ($form->isValid()) {
      if (json_decode($searchPage->getDefinition()) == null || json_decode($searchPage->getConfig()) == null) {
        CtSearchBundle::addSessionMessage($this, 'error', $this->get('translator')->trans('JSON parsing failed.'));
      } else {
        IndexManager::getInstance()->saveSearchPage($form->getData());
        if ($id == null) {
          CtSearchBundle::addSessionMessage($this, 'status', $this->get('translator')->trans('New search page has been added successfully'));
{'fraction_non_alphanumeric': 0.19014417395414795, 'fraction_numerical': 0.0001181753722524226, 'mean_word_length': 4.25, 'pattern_counts': {'":': 12, '<': 5, '<?xml version=': 0, '>': 139, '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': '673064', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2450, 'n_words': 398}