<commit_before><?php

/**
 * This file is covered by the AGPLv3 license, which can be found at the LICENSE file in the root of this project.
 * @copyright 2017 subtitulamos.tv
 */

// TODO: Handle zero width space (\U200B), special "", single "..." utf8 codepoint, etc

namespace App\Controllers;

use \Doctrine\ORM\EntityManager;
use \Slim\Views\Twig;
use App\Entities\Subtitle;
use App\Entities\Sequence;
use App\Services\Auth;
use App\Services\Langs;
use App\Services\Translation;

use Respect\Validation\Validator as v;

// TODO: Extract half this logic into a translation service
class TranslationController
{
    /**
     * Number of sequences that are displayed per page
     * @var int
     */
    const SEQUENCES_PER_PAGE = 20;

    public function newTranslation($request, $response, EntityManager $em, \Slim\Router $router)
    {
        $episodeID = $request->getParsedBodyParam('episode', 0);
        $lang = (int)$request->getParsedBodyParam('lang', 0);
        if (!Langs::existsId($lang)) {
            $response->getBody()->write("Lang is invalid");
            return $response->withStatus(400);
        }

        if (!v::numeric()->positive()->validate($episodeID)) {
            $response->getBody()->write("Version is invalid");
            return $response->withStatus(400);
        }

        $version = $em->createQuery("SELECT v FROM App:Version v WHERE v.episode = :epid ORDER BY v.id DESC")
            ->setParameter("epid", (int)$episodeID)
            ->setMaxResults(1)
            ->getOneOrNullResult();

        if (!$version) {
            $response->getBody()->write("Version doesn't exist");
            return $response->withStatus(412);
        }

        $base = null;
        foreach ($version->getSubtitles() as $sub) {
            if ($sub->isDirectUpload()) {
                $base = $sub;
            }

            if ($sub->getLang() == $lang) {
                // Lang already started!
                $response->getBody()->write("This version already has this lang");
                return $response->withStatus(412);
            }
        }

        // All good, create a new sub in the right lang
        $sub = new Subtitle();
        $sub->setLang($lang);
        $sub->setVersion($version);
        $sub->setProgress(0); // TODO: This progress could be more than 0% if sequences are autofilled
        $sub->setDirectUpload(false);
        $sub->setUploadTime(new \DateTime());

        // Autofill sequences
        foreach ($base->getSequences() as $sequence) {
            if (Translation::containsCreditsText($sequence->getText())) {
                // Autoblock and replace with our credits
                $nseq = clone $sequence;
                $nseq->setSubtitle($sub);
                //$nseq->setAuthor(null); // TODO: Change author to ModBot
                $nseq->setText('CREDITS AUTOREPLACE');
                $nseq->setLocked(true);
                $em->persist($nseq);
            } else {
                $blankSequence = Translation::getBlankSequenceConfidence($sequence);

                if ($blankSequence > 0) {
                    $nseq = clone $sequence;
                    //$nseq->setAuthor(null); // TODO: Change author to ModBot
                    $nseq->setSubtitle($sub);
                    $nseq->setText(' '); //Blank
                    $nseq->setLocked($blankSequence >= 95 ? 1 : 0); // Only lock if we're sure
                    $em->persist($nseq);
                }
            }
        }

        $em->persist($sub);
        $em->flush();

        return $response
                ->withStatus(200)
                ->withHeader('Location', $router->pathFor("translation", ["id" => $sub->getId()]));
    }

    public function view($id, $request, $response, EntityManager $em, Twig $twig)
    {
        $sub = $em->createQuery("SELECT s, v, e FROM App:Subtitle s JOIN s.version v JOIN v.episode e WHERE s.id = :id")<commit_msg>Autofill credits with a subtitulamos.tv line instead of a generic placeholder<commit_after><?php

/**
 * This file is covered by the AGPLv3 license, which can be found at the LICENSE file in the root of this project.
 * @copyright 2017 subtitulamos.tv
 */

// TODO: Handle zero width space (\U200B), special "", single "..." utf8 codepoint, etc

namespace App\Controllers;

use \Doctrine\ORM\EntityManager;
use \Slim\Views\Twig;
use App\Entities\Subtitle;
use App\Entities\Sequence;
use App\Services\Auth;
use App\Services\Langs;
use App\Services\Translation;

use Respect\Validation\Validator as v;

// TODO: Extract half this logic into a translation service
class TranslationController
{
    /**
     * Number of sequences that are displayed per page
     * @var int
     */
    const SEQUENCES_PER_PAGE = 20;

    public function newTranslation($request, $response, EntityManager $em, \Slim\Router $router)
    {
        $episodeID = $request->getParsedBodyParam('episode', 0);
        $lang = (int)$request->getParsedBodyParam('lang', 0);
        if (!Langs::existsId($lang)) {
            $response->getBody()->write("Lang is invalid");
            return $response->withStatus(400);
        }

        if (!v::numeric()->positive()->validate($episodeID)) {
            $response->getBody()->write("Version is invalid");
            return $response->withStatus(400);
        }

        $version = $em->createQuery("SELECT v FROM App:Version v WHERE v.episode = :epid ORDER BY v.id DESC")
            ->setParameter("epid", (int)$episodeID)
            ->setMaxResults(1)
            ->getOneOrNullResult();

        if (!$version) {
            $response->getBody()->write("Version doesn't exist");
            return $response->withStatus(412);
        }

        $base = null;
        foreach ($version->getSubtitles() as $sub) {
            if ($sub->isDirectUpload()) {
                $base = $sub;
            }

            if ($sub->getLang() == $lang) {
                // Lang already started!
                $response->getBody()->write("This version already has this lang");
                return $response->withStatus(412);
            }
        }

        // All good, create a new sub in the right lang
        $sub = new Subtitle();
        $sub->setLang($lang);
        $sub->setVersion($version);
        $sub->setProgress(0); // TODO: This progress could be more than 0% if sequences are autofilled
        $sub->setDirectUpload(false);
        $sub->setUploadTime(new \DateTime());

        // Autofill sequences
        foreach ($base->getSequences() as $sequence) {
            if (Translation::containsCreditsText($sequence->getText())) {
                // Autoblock and replace with our credits
                $nseq = clone $sequence;
                $nseq->setSubtitle($sub);
                //$nseq->setAuthor(null); // TODO: Change author to ModBot
                $nseq->setText('subtitulamos.tv');
                $nseq->setLocked(true);
                $em->persist($nseq);
            } else {
                $blankSequence = Translation::getBlankSequenceConfidence($sequence);

                if ($blankSequence > 0) {
                    $nseq = clone $sequence;
                    //$nseq->setAuthor(null); // TODO: Change author to ModBot
                    $nseq->setSubtitle($sub);
                    $nseq->setText(' '); //Blank
                    $nseq->setLocked($blankSequence >= 95 ? 1 : 0); // Only lock if we're sure
                    $em->persist($nseq);
                }
            }
        }

        $em->persist($sub);
        $em->flush();

        return $response
                ->withStatus(200)
                ->withHeader('Location', $router->pathFor("translation", ["id" => $sub->getId()]));
    }

    public function view($id, $request, $response, EntityManager $em, Twig $twig)
    {
        $sub = $em->createQuery("SELECT s, v, e FROM App:Subtitle s JOIN s.version v JOIN v.episode e WHERE s.id = :id")
{'fraction_non_alphanumeric': 0.13978766430738118, 'fraction_numerical': 0.00910010111223458, 'mean_word_length': 2.048151001540832, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 103, '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': '5312530', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2158, 'n_words': 566}