<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ContentBundle\Command;

use Darvin\ContentBundle\Entity\SlugMapItem;
use Darvin\ContentBundle\Slug\SlugMapItemFactory;
use Darvin\Utils\Mapping\MetadataFactoryInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * Slug map rebuild command
 */
class SlugMapRebuildCommand extends Command
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    /**
     * @var \Darvin\Utils\Mapping\MetadataFactoryInterface
     */
    private $metadataFactory;

    /**
     * @var \Darvin\ContentBundle\Slug\SlugMapItemFactory
     */
    private $slugMapItemFactory;

    /**
     * @param string                                         $name               Command name
     * @param \Doctrine\ORM\EntityManager                    $em                 Entity manager
     * @param \Darvin\Utils\Mapping\MetadataFactoryInterface $metadataFactory    Metadata factory
     * @param \Darvin\ContentBundle\Slug\SlugMapItemFactory  $slugMapItemFactory Slug map item factory
     */
    public function __construct($name, EntityManager $em, MetadataFactoryInterface $metadataFactory, SlugMapItemFactory $slugMapItemFactory)
    {
        parent::__construct($name);

        $this->em = $em;
        $this->metadataFactory = $metadataFactory;
        $this->slugMapItemFactory = $slugMapItemFactory;
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setDescription('Rebuilds slug map.');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);

        $this->truncateSlugMap();

        /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $doctrineMeta */
        foreach ($this->em->getMetadataFactory()->getAllMetadata() as $doctrineMeta) {
            if ($doctrineMeta->getReflectionClass()->isAbstract()) {
                continue;
            }

            $extendedMeta = $this->metadataFactory->getExtendedMetadata($doctrineMeta->getName());

            if (!isset($extendedMeta['slugs']) || empty($extendedMeta['slugs'])) {
                continue;
            }

            $entities = $this->em->getRepository($doctrineMeta->getName())->findAll();

            foreach ($entities as $entity) {
                foreach ($this->slugMapItemFactory->createItems($entity, $extendedMeta['slugs'], $doctrineMeta) as $slugMapItem) {
                    $this->em->persist($slugMapItem);
                }

                $io->comment($doctrineMeta->getName().' '.implode('', $doctrineMeta->getIdentifierValues($entity)));
            }
        }

        $this->em->flush();
    }

    private function truncateSlugMap()
    {
        $tableName = $this->em->getClassMetadata(SlugMapItem::class)->getTableName();

        $connection = $this->em->getConnection();
        $connection->exec($connection->getDriver()->getDatabasePlatform()->getTruncateTableSQL($tableName));
    }
}
<commit_msg>Disable foreign key checks before slug map rebuilding.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\ContentBundle\Command;

use Darvin\ContentBundle\Entity\SlugMapItem;
use Darvin\ContentBundle\Slug\SlugMapItemFactory;
use Darvin\Utils\Mapping\MetadataFactoryInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
 * Slug map rebuild command
 */
class SlugMapRebuildCommand extends Command
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    private $em;

    /**
     * @var \Darvin\Utils\Mapping\MetadataFactoryInterface
     */
    private $metadataFactory;

    /**
     * @var \Darvin\ContentBundle\Slug\SlugMapItemFactory
     */
    private $slugMapItemFactory;

    /**
     * @param string                                         $name               Command name
     * @param \Doctrine\ORM\EntityManager                    $em                 Entity manager
     * @param \Darvin\Utils\Mapping\MetadataFactoryInterface $metadataFactory    Metadata factory
     * @param \Darvin\ContentBundle\Slug\SlugMapItemFactory  $slugMapItemFactory Slug map item factory
     */
    public function __construct($name, EntityManager $em, MetadataFactoryInterface $metadataFactory, SlugMapItemFactory $slugMapItemFactory)
    {
        parent::__construct($name);

        $this->em = $em;
        $this->metadataFactory = $metadataFactory;
        $this->slugMapItemFactory = $slugMapItemFactory;
    }

    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setDescription('Rebuilds slug map.');
    }

    /**
     * {@inheritdoc}
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $io = new SymfonyStyle($input, $output);

        $this->truncateSlugMap();

        /** @var \Doctrine\ORM\Mapping\ClassMetadataInfo $doctrineMeta */
        foreach ($this->em->getMetadataFactory()->getAllMetadata() as $doctrineMeta) {
            if ($doctrineMeta->getReflectionClass()->isAbstract()) {
                continue;
            }

            $extendedMeta = $this->metadataFactory->getExtendedMetadata($doctrineMeta->getName());

            if (!isset($extendedMeta['slugs']) || empty($extendedMeta['slugs'])) {
                continue;
            }

            $entities = $this->em->getRepository($doctrineMeta->getName())->findAll();

            foreach ($entities as $entity) {
                foreach ($this->slugMapItemFactory->createItems($entity, $extendedMeta['slugs'], $doctrineMeta) as $slugMapItem) {
                    $this->em->persist($slugMapItem);
                }

                $io->comment($doctrineMeta->getName().' '.implode('', $doctrineMeta->getIdentifierValues($entity)));
            }
        }

        $this->em->flush();
    }

    private function truncateSlugMap()
    {
        $tableName = $this->em->getClassMetadata(SlugMapItem::class)->getTableName();

        $connection = $this->em->getConnection();
        $connection->exec('SET foreign_key_checks = 0');
        $connection->exec($connection->getDriver()->getDatabasePlatform()->getTruncateTableSQL($tableName));
    }
}

{'fraction_non_alphanumeric': 0.12982954545454545, 'fraction_numerical': 0.001278409090909091, 'mean_word_length': 2.690251572327044, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 80, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '798130', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1893, 'n_words': 344}