<commit_before><?php

namespace Ojs\ApiBundle\Controller;

use Doctrine\ORM\EntityManager;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Ojs\JournalBundle\Entity\Journal;
use Ojs\UserBundle\Entity\User;
use Ojs\JournalBundle\Entity\Institution;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\Get;
use Elastica\Query;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * PublicSearchRest may contain similar actions with SearchRest
 */
class PublicSearchRestController extends FOSRestController
{

    /**
     *
     * @ApiDoc(
     *  resource=true,
     *  description="search Institutions",
     *  parameters={
     * {
     *          "name"="q",
     *          "dataType"="string",
     *          "required"="true",
     *          "description"="search term"
     *      },
     *      {
     *          "name"="verified",
     *          "dataType"="boolean",
     *          "required"="false",
     *          "description"="list only verified or not"
     *      },
     *      {
     *          "name"="page",
     *          "dataType"="integer",
     *          "required"="false",
     *          "description"="limit"
     *      }
     *  }
     * )
     * @Get("/public/search/institution")
     *
     * @param  Request $request
     * @return array
     */
    public function getInstitutionsAction(Request $request)
    {
        #$limit = $request->get('limit');
        #$verified = $request->get('verified');
        $q = $request->get('q');
        $search = $this->container->get('fos_elastica.index.search.institution');

        $prefix = new Query\Prefix();
        $prefix->setPrefix('name', strtolower($q));
        $qe = new Query();
        $qe->setQuery($prefix);

        $results = $search->search($prefix);
        $data = [];
        foreach ($results as $result) {
            $data[] = array_merge(array('id' => $result->getId()), $result->getData());
        }

        return $data;
    }    /**
     *
     * @ApiDoc(
     *  resource=true,
     *  description="search Institutions for autocomplete",
     *  parameters={
     * {
     *          "name"="q",
     *          "dataType"="string",
     *          "required"="true",
     *          "description"="search term"
     *      },
     *      {
     *          "name"="verified",
     *          "dataType"="boolean",
     *          "required"="false",
     *          "description"="list only verified or not"
     *      },
     *      {
     *          "name"="page",
     *          "dataType"="integer",
     *          "required"="false",
     *          "description"="limit"
     *      }
     *  }
     * )
     * @Get("/public/search/institute")
     *
     * @param  Request $request
     * @return array
     */
    public function getInstitutesAction(Request $request)
    {
        $q = $request->get('q');
        $search = $this->container->get('fos_elastica.index.search.institution');

        $prefix = new Query\Prefix();
        $prefix->setPrefix('name', strtolower($q));
        $qe = new Query();
        $qe->setQuery($prefix);

        $results = $search->search($prefix);

        $data = [];
        foreach ($results as $result) {
            $data[] = [
                'id' => $result->getId(),
                'text' => $result->getData()['name'],
            ];
        }

        return $data;
    }
    /**
     * @param $id
     * @return Response
     * @throws \Doctrine\ORM\ORMException
     * @ApiDoc(
     *                                    resource=true,
     *                                    description="get institution by id"
     *                                    )
     * @Get("/public/institution/get/{id}")
     */
    public function getInstitutionAction($id)
    {
        /** @var EntityManager $em */
        $em = $this->getDoctrine()->getManager();
        /** @var Institution  $institution */
        $institution = $em->find('OjsJournalBundle:Institution', $id);
        if ($institution) {
            return JsonResponse::create(['id' => $id, 'text' => $institution->getName()]);
        }
        throw new NotFoundHttpException();
    }

    /**
     * @ApiDoc(
     *  resource=true,
     *  description="search Institutions",
     *  parameters={
     * {
     *          "name"="q",
     *          "dataType"="string",
     *          "required"="true",
     *          "description"="search term"
     *      },
     *      {
     *          "name"="page",
     *          "dataType"="integer",
     *          "required"="false",
     *          "description"="limit"
     *      }
     *  }
     * )
     * @Get("/public/search/tags")
     *
     * @return array
     */
    public function getTagsAction(Request $request)
    {
        #$limit = 12;
        $q = $request->get('q');
        $search = $this->container->get('fos_elastica.index.search');

        $prefix = new Query\Prefix();
        $prefix->setPrefix('tags', strtolower($q));
        $qe = new Query();
        $qe->setQuery($prefix);

        $results = $search->search($prefix);
        $data = [];
        foreach ($results as $result) {
            foreach(explode(',',$result->getData()['tags']) as $tag){
                $data[] = $tag;
            }
        }

        return $data;
    }

    /**
     * @param  Request $request
     * @ApiDoc(
     *                          resource=true,
     *                          description="search users",
     *                          parameters={
     *                          {
     *                          "name"="q",
     *                          "dataType"="string",<commit_msg>edit public tag api according to typeahead<commit_after><?php

namespace Ojs\ApiBundle\Controller;

use Doctrine\ORM\EntityManager;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Ojs\JournalBundle\Entity\Journal;
use Ojs\UserBundle\Entity\User;
use Ojs\JournalBundle\Entity\Institution;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\Get;
use Elastica\Query;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * PublicSearchRest may contain similar actions with SearchRest
 */
class PublicSearchRestController extends FOSRestController
{

    /**
     *
     * @ApiDoc(
     *  resource=true,
     *  description="search Institutions",
     *  parameters={
     * {
     *          "name"="q",
     *          "dataType"="string",
     *          "required"="true",
     *          "description"="search term"
     *      },
     *      {
     *          "name"="verified",
     *          "dataType"="boolean",
     *          "required"="false",
     *          "description"="list only verified or not"
     *      },
     *      {
     *          "name"="page",
     *          "dataType"="integer",
     *          "required"="false",
     *          "description"="limit"
     *      }
     *  }
     * )
     * @Get("/public/search/institution")
     *
     * @param  Request $request
     * @return array
     */
    public function getInstitutionsAction(Request $request)
    {
        #$limit = $request->get('limit');
        #$verified = $request->get('verified');
        $q = $request->get('q');
        $search = $this->container->get('fos_elastica.index.search.institution');

        $prefix = new Query\Prefix();
        $prefix->setPrefix('name', strtolower($q));
        $qe = new Query();
        $qe->setQuery($prefix);

        $results = $search->search($prefix);
        $data = [];
        foreach ($results as $result) {
            $data[] = array_merge(array('id' => $result->getId()), $result->getData());
        }

        return $data;
    }    /**
     *
     * @ApiDoc(
     *  resource=true,
     *  description="search Institutions for autocomplete",
     *  parameters={
     * {
     *          "name"="q",
     *          "dataType"="string",
     *          "required"="true",
     *          "description"="search term"
     *      },
     *      {
     *          "name"="verified",
     *          "dataType"="boolean",
     *          "required"="false",
     *          "description"="list only verified or not"
     *      },
     *      {
     *          "name"="page",
     *          "dataType"="integer",
     *          "required"="false",
     *          "description"="limit"
     *      }
     *  }
     * )
     * @Get("/public/search/institute")
     *
     * @param  Request $request
     * @return array
     */
    public function getInstitutesAction(Request $request)
    {
        $q = $request->get('q');
        $search = $this->container->get('fos_elastica.index.search.institution');

        $prefix = new Query\Prefix();
        $prefix->setPrefix('name', strtolower($q));
        $qe = new Query();
        $qe->setQuery($prefix);

        $results = $search->search($prefix);

        $data = [];
        foreach ($results as $result) {
            $data[] = [
                'id' => $result->getId(),
                'text' => $result->getData()['name'],
            ];
        }

        return $data;
    }
    /**
     * @param $id
     * @return Response
     * @throws \Doctrine\ORM\ORMException
     * @ApiDoc(
     *                                    resource=true,
     *                                    description="get institution by id"
     *                                    )
     * @Get("/public/institution/get/{id}")
     */
    public function getInstitutionAction($id)
    {
        /** @var EntityManager $em */
        $em = $this->getDoctrine()->getManager();
        /** @var Institution  $institution */
        $institution = $em->find('OjsJournalBundle:Institution', $id);
        if ($institution) {
            return JsonResponse::create(['id' => $id, 'text' => $institution->getName()]);
        }
        throw new NotFoundHttpException();
    }

    /**
     * @ApiDoc(
     *  resource=true,
     *  description="search Institutions",
     *  parameters={
     * {
     *          "name"="q",
     *          "dataType"="string",
     *          "required"="true",
     *          "description"="search term"
     *      },
     *      {
     *          "name"="page",
     *          "dataType"="integer",
     *          "required"="false",
     *          "description"="limit"
     *      }
     *  }
     * )
     * @Get("/public/search/tags")
     *
     * @return array
     */
    public function getTagsAction(Request $request)
    {
        #$limit = 12;
        $q = $request->get('q');
        $search = $this->container->get('fos_elastica.index.search');

        $prefix = new Query\Prefix();
        $prefix->setPrefix('tags', strtolower($q));
        $qe = new Query();
        $qe->setQuery($prefix);

        $results = $search->search($prefix);
        $data = [];
        foreach ($results as $result) {
            foreach(explode(',',$result->getData()['tags']) as $tag){
                $data[] = ['text'=>$tag];
            }
        }

        return $data;
    }

    /**
     * @param  Request $request
     * @ApiDoc(
     *                          resource=true,
     *                          description="search users",
     *                          parameters={
     *                          {
     *                          "name"="q",
     *                          "dataType"="string",
{'fraction_non_alphanumeric': 0.15990392039118126, 'fraction_numerical': 0.00034314146006691256, 'mean_word_length': 1.6191866996180633, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3630854', 'n_tokens_mistral': 3402, 'n_tokens_neox': 3362, 'n_words': 521}