<commit_before><?php

namespace PHPOrchestra\FrontBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
//use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class OrchestraGenerateSitemapCommand extends ContainerAwareCommand
{
    /**
     * Configure command
     */
    protected function configure()
    {
        $this
            ->setName('orchestra:sitemap:generate')
            ->setDescription('Generate sitemaps')
            ->addOption('siteId', null, InputOption::VALUE_OPTIONAL, 'If set, will only generate sitemap for this site');
    }

    /**
     * Execute command
     * 
     * @param InputInterface  $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($siteId = $input->getOption('siteId')) {
            $this->generateSitemap($siteId, $output);
        } else {
            $sites = array('1', '3', '6', '9');
            foreach ($sites as $siteId) {
                $this->generateSitemap($siteId, $output);
            }
        }

        $output->writeln("<info>Done.</info>");
    }

    /**
     * Generate sitemap for siteId
     * 
     * @param string          $siteId
     * @param OutputInterface $output
     */
    protected function generateSitemap($siteId, OutputInterface $output)
    {
        $output->writeln("<info>Generating sitemap for siteId " . $siteId . "</info>");

        $filename = 'sitemap.' . $siteId . '.xml';
        $text = 'random xml content';

        file_put_contents('web/' . $filename, $text);
        $output->writeln("<comment>-> " . $filename . " generated</comment>\n");

        return true;
    }
}
<commit_msg>First xml version<commit_after><?php

namespace PHPOrchestra\FrontBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use PHPOrchestra\ModelInterface\Model\SiteInterface;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;
use PHPOrchestra\ModelInterface\Model\NodeInterface;

class OrchestraGenerateSitemapCommand extends ContainerAwareCommand
{
    /**
     * Configure command
     */
    protected function configure()
    {
        $this
            ->setName('orchestra:sitemap:generate')
            ->setDescription('Generate sitemaps')
            ->addOption('siteId', null, InputOption::VALUE_REQUIRED, 'If set, will only generate sitemap for this site');
    }

    /**
     * Execute command
     * 
     * @param InputInterface  $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($siteId = $input->getOption('siteId')) {

            $site = $this->getContainer()->get('php_orchestra_model.repository.site')->findOneBySiteId($siteId);
            $this->generateSitemap($site, $output);

        } else {

            $siteCollection = $this->getContainer()->get('php_orchestra_model.repository.site')->findByDeleted(false);
            if ($siteCollection) {
                foreach ($siteCollection as $site) {
                    $this->generateSitemap($site, $output);
                }
            }
        }

        $output->writeln("<info>Done.</info>");
    }

    /**
     * Generate sitemap for $site
     * 
     * @param SiteInterface   $site
     * @param OutputInterface $output
     */
    protected function generateSitemap(SiteInterface $site, OutputInterface $output)
    {
        $output->writeln("<info>Generating sitemap for site " . $site->getSiteId() . " on domain " . $site->getDomain() . "</info>");

        $nodes = $this->getSitemapNodesFromSite($site);
        $filename = 'sitemap.' . $site->getDomain() . '.xml';

        $encoders = array(new XmlEncoder(), new JsonEncoder());
        $normalizers = array(new GetSetMethodNormalizer());
        $serializer = new Serializer($normalizers, $encoders);
 
        file_put_contents('web/' . $filename, $serializer->serialize($nodes, 'xml'));
        $output->writeln("<comment>-> " . $filename . " generated</comment>\n");

        return true;
    }

    /**
     * Return an array of sitemapNodes for $site
     * 
     * @param SiteInterface $site
     * 
     * @return array
     */
    protected function getSitemapNodesFromSite(SiteInterface $site)
    {
        $nodes = array();

        // TODO : récupérer les noeuds en version published uniquement + vision publique
        $nodesCollection = $this->getContainer()->get('php_orchestra_model.repository.node')
            ->findLastVersionBySiteId(NodeInterface::TYPE_DEFAULT, $site->getSiteId());

        if ($nodesCollection) {
            foreach($nodesCollection as $node) {
                if ($lastmod = $node->getUpdatedAt())
                    $lastmod = $lastmod->format('Y-m-d');

                $nodes[] = array(
                    'loc' => $site->getDomain() . '/' . $node->getPath(),
                    'lastmod' => $lastmod,
                    'changefreq' => $node->getSitemapChangefreq(),
                    'priority' => $node->getSitemapPriority()
                );
            }
        }

        return $nodes;
    }
}

{'fraction_non_alphanumeric': 0.13386666666666666, 'fraction_numerical': 0.0007111111111111111, 'mean_word_length': 2.572063492063492, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '1083053', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1556, 'n_words': 297}