<commit_before><?php

/*
 * This file is part of Packagist.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Packagist\WebBundle\Package;

use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface;
use Packagist\WebBundle\Entity\Author;
use Packagist\WebBundle\Entity\Package;
use Packagist\WebBundle\Entity\Tag;
use Packagist\WebBundle\Entity\Version;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class Updater
{
    /**
     * Doctrine
     * @var RegistryInterface
     */
    protected $doctrine;

    /**
     * Supported link types
     * @var array
     */
    protected $supportedLinkTypes = array(
        'require'   => 'RequireLink',
        'conflict'  => 'ConflictLink',
        'provide'   => 'ProvideLink',
        'replace'   => 'ReplaceLink',
        'recommend' => 'RecommendLink',
        'suggest'   => 'SuggestLink',
    );

    /**
     * Constructor
     *
     * @param RegistryInterface $doctrine
     */
    public function __construct(RegistryInterface $doctrine)
    {
        $this->doctrine = $doctrine;
    }

    /**
     * Update a project
     *
     * @param PackageInterface $package
     * @param boolean $clearExistingVersions
     * @param DateTime $start
     */
    public function update(Package $package, RepositoryInterface $repository, $clearExistingVersions = false, \DateTime $start = null)
    {
        if (null === $start) {
            $start = new \DateTime();
        }

        $versions = $repository->getPackages();
        $em = $this->doctrine->getEntityManager();

        usort($versions, function ($a, $b) {
            return version_compare($a->getVersion(), $b->getVersion());
        });

        $versionRepository = $this->doctrine->getRepository('PackagistWebBundle:Version');

        if ($clearExistingVersions) {
            foreach ($package->getVersions() as $version) {
                $versionRepository->remove($version);
            }

            $em->flush();
            $em->refresh($package);
        }

        foreach ($versions as $version) {
            $this->updateInformation($package, $version);
            $em->flush();
        }

        // remove outdated -dev versions
        foreach ($package->getVersions() as $version) {
            if ($version->getDevelopment() && $version->getUpdatedAt() < $start) {
                $versionRepository->remove($version);
            }
        }

        $package->setUpdatedAt(new \DateTime);
        $package->setCrawledAt(new \DateTime);<commit_msg>Add 8days delay before pruning old dev versions<commit_after><?php

/*
 * This file is part of Packagist.
 *
 * (c) <NAME> <<EMAIL>>
 *     <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Packagist\WebBundle\Package;

use Composer\Package\PackageInterface;
use Composer\Repository\RepositoryInterface;
use Packagist\WebBundle\Entity\Author;
use Packagist\WebBundle\Entity\Package;
use Packagist\WebBundle\Entity\Tag;
use Packagist\WebBundle\Entity\Version;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class Updater
{
    /**
     * Doctrine
     * @var RegistryInterface
     */
    protected $doctrine;

    /**
     * Supported link types
     * @var array
     */
    protected $supportedLinkTypes = array(
        'require'   => 'RequireLink',
        'conflict'  => 'ConflictLink',
        'provide'   => 'ProvideLink',
        'replace'   => 'ReplaceLink',
        'recommend' => 'RecommendLink',
        'suggest'   => 'SuggestLink',
    );

    /**
     * Constructor
     *
     * @param RegistryInterface $doctrine
     */
    public function __construct(RegistryInterface $doctrine)
    {
        $this->doctrine = $doctrine;
    }

    /**
     * Update a project
     *
     * @param PackageInterface $package
     * @param boolean $clearExistingVersions
     * @param DateTime $start
     */
    public function update(Package $package, RepositoryInterface $repository, $clearExistingVersions = false, \DateTime $start = null)
    {
        if (null === $start) {
            $start = new \DateTime();
        }
        $pruneDate = clone $start;
        $pruneDate->modify('-8days');

        $versions = $repository->getPackages();
        $em = $this->doctrine->getEntityManager();

        usort($versions, function ($a, $b) {
            return version_compare($a->getVersion(), $b->getVersion());
        });

        $versionRepository = $this->doctrine->getRepository('PackagistWebBundle:Version');

        if ($clearExistingVersions) {
            foreach ($package->getVersions() as $version) {
                $versionRepository->remove($version);
            }

            $em->flush();
            $em->refresh($package);
        }

        foreach ($versions as $version) {
            $this->updateInformation($package, $version);
            $em->flush();
        }

        // remove outdated -dev versions
        foreach ($package->getVersions() as $version) {
            if ($version->getDevelopment() && $version->getUpdatedAt() < $pruneDate) {
                $versionRepository->remove($version);
            }
        }

        $package->setUpdatedAt(new \DateTime);
        $package->setCrawledAt(new \DateTime);
{'fraction_non_alphanumeric': 0.13673506446341022, 'fraction_numerical': 0.0003631741420010895, 'mean_word_length': 2.4021000617665225, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 74, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2262953', 'n_tokens_mistral': 1578, 'n_tokens_neox': 1526, 'n_words': 334}