<commit_before><?php

namespace Liip\TranslationBundle\Import;

use Liip\TranslationBundle\DependencyInjection\Configuration;
use Liip\TranslationBundle\Persistence\PersistenceInterface;
use Liip\TranslationBundle\Repository\UnitRepository;
use Liip\TranslationBundle\Translation\Translator;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * To be completed
 *
 * This file is part of the LiipTranslationBundle. For more information concerning
 * the bundle, see the README.md file at the project root.
 *
 * @package Liip\TranslationBundle\Import
 * @version 0.0.1
 *
 * @license http://opensource.org/licenses/MIT MIT License
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2013, Liip, http://www.liip.ch
 */
class FileImporter
{
    /** @var UnitRepository $repository */
    protected $repository;
    /** @var \Symfony\Component\HttpFoundation\Session|\Symfony\Component\HttpFoundation\Session\Session $session */
    protected $session;
    /** @var Translator $translator */
    protected $translator;

    /**
     * @param UnitRepository $repository
     * @param \Symfony\Component\HttpFoundation\Session|\Symfony\Component\HttpFoundation\Session\Session $session
     * @param Translator $translator
     */
    public function __construct(UnitRepository $repository, $session, Translator $translator)
    {
        $this->repository = $repository;
        $this->session = $session;
        $this->translator = $translator;
    }

    /**
     * Return the UploadedFile original extension. This is here for
     * compatibility reason with Symfony 2.0.
     *
     * @param UploadedFile $file
     * @return string
     */
    protected function getFileExtension(UploadedFile $file)
    {
        if(method_exists($file, 'getClientOriginalExtension')) {
            return $file->getClientOriginalExtension();
        } else {
            return pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
        }
    }

    /**
     * Take care of uploaded files (including zip) for importing resources
     *
     * @param UploadedFile $file
     */
    public function handleUploadedFile(UploadedFile $file)
    {
        if ($this->getFileExtension($file) === 'zip') {
            $tempFolder = sys_get_temp_dir().md5(time());
            mkdir($tempFolder);
            $zip = new \ZipArchive;
            $zip->open($file->getRealPath());
            $zip->extractTo($tempFolder);
            $zip->close();
            foreach(scandir($tempFolder) as $path) {
                if (is_file($tempFolder.'/'.$path)) {
                    $this->import($tempFolder.'/'.$path);
                }
            }
            unset($tempFolder);
        }
        else {
            $this->import($file->getRealPath(), $file->getClientOriginalName());
        }
    }

    /**
     * Add a file to the current import buffer
     *
     * @param string $filePath   The path to the file
     * @param string $fileName   Optional, the filename to parse to extract resources data
     * @throws \RuntimeException
     */
    protected function import($filePath, $fileName = null)
    {
        // Filename parsing
        if ($fileName == null){
            $fileName = basename($filePath);
        }
        if (!preg_match('/\w+\.\w+\.\w+/', $fileName)){
            throw new \RuntimeException("Invalid filename [$fileName], all translation files must be named: domain.locale.format (ex: messages.en.yml)");
        }
        list($domain, $locale, $format) = explode('.', $fileName, 3);
        $catalogue = $this->translator->loadResource(array(
            'format' => $format,
            'locale' => $locale,
            'domain' => $domain,
            'path' => $filePath
        ));

        // Merge with existing entries
        $translations = $this->getCurrentTranslations();
        if (!array_key_exists($locale, $translations)){
            $translations[$locale] = array('new' => array(), 'updated' => array());
        }
        foreach($catalogue->all() as $domain => $messages) {
            foreach ($messages as $key => $value) {
                if ($trans = $this->repository->findTranslation($domain, $key, $locale)) {
                    if ($trans !== $value) {
                        $translations[$locale]['updated'][$domain][$key] = array('old' => $trans, 'new' => $value);
                    }
                } else {
                    $translations[$locale]['new'][$domain][$key] = $value;
                }
            }
        }

        $this->session->set(Configuration::SESSION_PREFIX.'import-list', $translations);
    }

    /**
     * Return the current buffer
     *
     * @return array
     */
    public function getCurrentTranslations()
    {
        return $this->session->get(Configuration::SESSION_PREFIX.'import-list', array());<commit_msg>fix value comparison upon import<commit_after><?php

namespace Liip\TranslationBundle\Import;

use Liip\TranslationBundle\DependencyInjection\Configuration;
use Liip\TranslationBundle\Persistence\PersistenceInterface;
use Liip\TranslationBundle\Repository\UnitRepository;
use Liip\TranslationBundle\Translation\Translator;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * To be completed
 *
 * This file is part of the LiipTranslationBundle. For more information concerning
 * the bundle, see the README.md file at the project root.
 *
 * @package Liip\TranslationBundle\Import
 * @version 0.0.1
 *
 * @license http://opensource.org/licenses/MIT MIT License
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2013, Liip, http://www.liip.ch
 */
class FileImporter
{
    /** @var UnitRepository $repository */
    protected $repository;
    /** @var \Symfony\Component\HttpFoundation\Session|\Symfony\Component\HttpFoundation\Session\Session $session */
    protected $session;
    /** @var Translator $translator */
    protected $translator;

    /**
     * @param UnitRepository $repository
     * @param \Symfony\Component\HttpFoundation\Session|\Symfony\Component\HttpFoundation\Session\Session $session
     * @param Translator $translator
     */
    public function __construct(UnitRepository $repository, $session, Translator $translator)
    {
        $this->repository = $repository;
        $this->session = $session;
        $this->translator = $translator;
    }

    /**
     * Return the UploadedFile original extension. This is here for
     * compatibility reason with Symfony 2.0.
     *
     * @param UploadedFile $file
     * @return string
     */
    protected function getFileExtension(UploadedFile $file)
    {
        if(method_exists($file, 'getClientOriginalExtension')) {
            return $file->getClientOriginalExtension();
        } else {
            return pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION);
        }
    }

    /**
     * Take care of uploaded files (including zip) for importing resources
     *
     * @param UploadedFile $file
     */
    public function handleUploadedFile(UploadedFile $file)
    {
        if ($this->getFileExtension($file) === 'zip') {
            $tempFolder = sys_get_temp_dir().md5(time());
            mkdir($tempFolder);
            $zip = new \ZipArchive;
            $zip->open($file->getRealPath());
            $zip->extractTo($tempFolder);
            $zip->close();
            foreach(scandir($tempFolder) as $path) {
                if (is_file($tempFolder.'/'.$path)) {
                    $this->import($tempFolder.'/'.$path);
                }
            }
            unset($tempFolder);
        }
        else {
            $this->import($file->getRealPath(), $file->getClientOriginalName());
        }
    }

    /**
     * Add a file to the current import buffer
     *
     * @param string $filePath   The path to the file
     * @param string $fileName   Optional, the filename to parse to extract resources data
     * @throws \RuntimeException
     */
    protected function import($filePath, $fileName = null)
    {
        // Filename parsing
        if ($fileName == null){
            $fileName = basename($filePath);
        }
        if (!preg_match('/\w+\.\w+\.\w+/', $fileName)){
            throw new \RuntimeException("Invalid filename [$fileName], all translation files must be named: domain.locale.format (ex: messages.en.yml)");
        }
        list($domain, $locale, $format) = explode('.', $fileName, 3);
        $catalogue = $this->translator->loadResource(array(
            'format' => $format,
            'locale' => $locale,
            'domain' => $domain,
            'path' => $filePath
        ));

        // Merge with existing entries
        $translations = $this->getCurrentTranslations();
        if (!array_key_exists($locale, $translations)){
            $translations[$locale] = array('new' => array(), 'updated' => array());
        }
        foreach($catalogue->all() as $domain => $messages) {
            foreach ($messages as $key => $value) {
                if ($trans = $this->repository->findTranslation($domain, $key, $locale)) {
                    if ($trans->getValue() !== $value) {
                        $translations[$locale]['updated'][$domain][$key] = array('old' => $trans, 'new' => $value);
                    }
                } else {
                    $translations[$locale]['new'][$domain][$key] = $value;
                }
            }
        }

        $this->session->set(Configuration::SESSION_PREFIX.'import-list', $translations);
    }

    /**
     * Return the current buffer
     *
     * @return array
     */
    public function getCurrentTranslations()
    {
        return $this->session->get(Configuration::SESSION_PREFIX.'import-list', array());
{'fraction_non_alphanumeric': 0.13817883174212092, 'fraction_numerical': 0.0022584950210450673, 'mean_word_length': 2.4062937062937064, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2606620', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2621, 'n_words': 591}