<commit_before><?php
namespace org\dokuwiki\translatorBundle\Services\Repository;

use Monolog\Logger;
use org\dokuwiki\translatorBundle\Services\Git\GitException;
use org\dokuwiki\translatorBundle\Services\GitHub\GitHubServiceException;
use org\dokuwiki\translatorBundle\Services\Language\LanguageParseException;
use org\dokuwiki\translatorBundle\Services\Repository\Behavior\RepositoryBehavior;
use Symfony\Component\DependencyInjection\Container;
use org\dokuwiki\translatorBundle\Entity\TranslationUpdateEntity;
use org\dokuwiki\translatorBundle\Services\Git\GitRepository;
use org\dokuwiki\translatorBundle\Services\Git\GitService;
use org\dokuwiki\translatorBundle\Services\Language\LanguageManager;
use org\dokuwiki\translatorBundle\Entity\RepositoryEntity;
use Doctrine\ORM\EntityManager;
use org\dokuwiki\translatorBundle\Services\Language\LocalText;
use Symfony\Component\Filesystem\Filesystem;

abstract class Repository {

    private $dataFolder;
    private $basePath = null;

    /**
     * @var GitRepository
     */
    private $git = null;

    /**
     * @var RepositoryEntity Database representation
     */
    protected $entity;

    /**
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * @var RepositoryStats
     */
    protected $repositoryStats;

    /**
     * @var GitService
     */
    private $gitService;

    /**
     * @var RepositoryBehavior
     */
    private $behavior;

    /**
     * @var Logger
     */
    private $logger;


    public function __construct($dataFolder, EntityManager $entityManager, $entity, RepositoryStats $repositoryStats,
                GitService $gitService, RepositoryBehavior $behavior, Logger $logger) {
        $this->dataFolder = $dataFolder;
        $this->entityManager = $entityManager;
        $this->entity = $entity;
        $this->repositoryStats = $repositoryStats;
        $this->gitService = $gitService;
        $this->behavior = $behavior;
        $this->logger = $logger;
    }

    public function update() {
        $path = $this->buildBasePath();
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        try {
            if ($this->isLocked()) return;
            $this->lock();
            $changed = $this->updateFromRemote();
            if ($changed) {
                $this->updateLanguage();
            }
            $this->entity->setLastUpdate(intval(time()));
            $this->entity->setErrorCount(0);
        } catch (RepositoryNotUpdatedException $e) {
            $this->entity->setErrorCount($this->entity->getErrorCount() + 1);
            $this->logger->warn(sprintf('Repository %d not updated. Error count is %d. Error: %s',
                    $this->entity->getId(), $this->entity->getErrorCount(), $e->getPrevious()->getMessage()));
        }
        $this->entityManager->flush($this->entity);
        $this->unlock();
    }

    /**
     * Update local repository
     * @throws RepositoryNotUpdatedException
     * @return boolean true if the repository is changed.
     */<commit_msg>now catch error on fork an unavailible repository, closes #1<commit_after><?php
namespace org\dokuwiki\translatorBundle\Services\Repository;

use Github\Exception\RuntimeException;
use Monolog\Logger;
use org\dokuwiki\translatorBundle\Services\Git\GitException;
use org\dokuwiki\translatorBundle\Services\GitHub\GitHubServiceException;
use org\dokuwiki\translatorBundle\Services\Language\LanguageParseException;
use org\dokuwiki\translatorBundle\Services\Repository\Behavior\RepositoryBehavior;
use Symfony\Component\DependencyInjection\Container;
use org\dokuwiki\translatorBundle\Entity\TranslationUpdateEntity;
use org\dokuwiki\translatorBundle\Services\Git\GitRepository;
use org\dokuwiki\translatorBundle\Services\Git\GitService;
use org\dokuwiki\translatorBundle\Services\Language\LanguageManager;
use org\dokuwiki\translatorBundle\Entity\RepositoryEntity;
use Doctrine\ORM\EntityManager;
use org\dokuwiki\translatorBundle\Services\Language\LocalText;
use Symfony\Component\Filesystem\Filesystem;

abstract class Repository {

    private $dataFolder;
    private $basePath = null;

    /**
     * @var GitRepository
     */
    private $git = null;

    /**
     * @var RepositoryEntity Database representation
     */
    protected $entity;

    /**
     * @var EntityManager
     */
    protected $entityManager;

    /**
     * @var RepositoryStats
     */
    protected $repositoryStats;

    /**
     * @var GitService
     */
    private $gitService;

    /**
     * @var RepositoryBehavior
     */
    private $behavior;

    /**
     * @var Logger
     */
    private $logger;


    public function __construct($dataFolder, EntityManager $entityManager, $entity, RepositoryStats $repositoryStats,
                GitService $gitService, RepositoryBehavior $behavior, Logger $logger) {
        $this->dataFolder = $dataFolder;
        $this->entityManager = $entityManager;
        $this->entity = $entity;
        $this->repositoryStats = $repositoryStats;
        $this->gitService = $gitService;
        $this->behavior = $behavior;
        $this->logger = $logger;
    }

    public function update() {
        $path = $this->buildBasePath();
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        try {
            if ($this->isLocked()) return;
            $this->lock();
            $changed = $this->updateFromRemote();
            if ($changed) {
                $this->updateLanguage();
            }
            $this->entity->setLastUpdate(intval(time()));
            $this->entity->setErrorCount(0);
        } catch (RepositoryNotUpdatedException $e) {
            $this->increaseErrorCount($e);
        } catch (RuntimeException $e) {
            $this->increaseErrorCount($e);
        }
        $this->entityManager->flush($this->entity);
        $this->unlock();
    }

    private function increaseErrorCount(\Exception $e) {
        $this->entity->setErrorCount($this->entity->getErrorCount() + 1);
        $this->logger->warn(sprintf('Repository %d not updated. Error count is %d. Error: %s',
            $this->entity->getId(), $this->entity->getErrorCount(), $e->getPrevious()->getMessage()));
    }

    /**
     * Update local repository
     * @throws RepositoryNotUpdatedException
     * @return boolean true if the repository is changed.
     */
{'fraction_non_alphanumeric': 0.12550988390335738, 'fraction_numerical': 0.0020395356134295575, 'mean_word_length': 2.8706739526411655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '3835883', 'n_tokens_mistral': 1823, 'n_tokens_neox': 1743, 'n_words': 326}