From 0de89208c89a53320e22697585ed77a2177b8717 Mon Sep 17 00:00:00 2001 From: Albin Date: Fri, 13 Sep 2024 19:15:20 +0200 Subject: [PATCH] =?UTF-8?q?afupf#1180=20all=C3=A8gement=20des=20requ=C3=AA?= =?UTF-8?q?tes=20SQL=20et=20ajout=20sitemap.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/config/services.yml | 2 +- .../Controller/HtmlSitemapController.php | 27 ++++++++----------- .../Model/Repository/ArticleRepository.php | 9 +++++++ ....php => TalksAndNewsSitemapSubscriber.php} | 25 ++++++++++++++++- 4 files changed, 45 insertions(+), 18 deletions(-) rename sources/AppBundle/Subscriber/{TalksSitemapSubscriber.php => TalksAndNewsSitemapSubscriber.php} (67%) diff --git a/app/config/services.yml b/app/config/services.yml index 837558121..a69739745 100644 --- a/app/config/services.yml +++ b/app/config/services.yml @@ -640,7 +640,7 @@ services: tags: - {name: form.type} - AppBundle\Subscriber\TalksSitemapSubscriber: + AppBundle\Subscriber\TalksAndNewsSitemapSubscriber: arguments: - "@router" - "@ting" diff --git a/sources/AppBundle/Controller/HtmlSitemapController.php b/sources/AppBundle/Controller/HtmlSitemapController.php index 50dc3f2f1..fae85fae1 100644 --- a/sources/AppBundle/Controller/HtmlSitemapController.php +++ b/sources/AppBundle/Controller/HtmlSitemapController.php @@ -79,25 +79,20 @@ private function members(): array private function news(): array { - $itemPerPage = 100; - $page = 1; - $repository = $this->get('ting')->get(ArticleRepository::class); $news = []; - do { - $newsList = $repository->findPublishedNews($page++, $itemPerPage, []); - foreach ($newsList as $newsItem) { - $url = $this->urlGenerator->generate('news_display', [ - 'code' => $newsItem->getSlug(), - ]); - - $news[] = [ - 'name' => $newsItem->getTitle(), - 'url' => $url - ]; - } - } while (count($newsList) >= $itemPerPage); + $newsList = $repository->findAllPublishedNews(); + foreach ($newsList as $newsItem) { + $url = $this->urlGenerator->generate('news_display', [ + 'code' => $newsItem->getSlug(), + ]); + + $news[] = [ + 'name' => $newsItem->getTitle(), + 'url' => $url + ]; + } return $news; } diff --git a/sources/AppBundle/Site/Model/Repository/ArticleRepository.php b/sources/AppBundle/Site/Model/Repository/ArticleRepository.php index 9740fbb9a..8dcffbcb7 100644 --- a/sources/AppBundle/Site/Model/Repository/ArticleRepository.php +++ b/sources/AppBundle/Site/Model/Repository/ArticleRepository.php @@ -85,6 +85,15 @@ public function findPublishedNews($page, $itemsPerPage, array $filters) return $query->query($this->getCollection(new HydratorSingleObject())); } + public function findAllPublishedNews(array $filters = []) + { + list($sql, $params) = $this->getSqlPublishedNews($filters); + + return $this->getPreparedQuery($sql) + ->setParams($params) + ->query($this->getCollection(new HydratorSingleObject())); + } + public function findPrevious(Article $article) { if (null === ($publishedAt = $article->getPublishedAt())) { diff --git a/sources/AppBundle/Subscriber/TalksSitemapSubscriber.php b/sources/AppBundle/Subscriber/TalksAndNewsSitemapSubscriber.php similarity index 67% rename from sources/AppBundle/Subscriber/TalksSitemapSubscriber.php rename to sources/AppBundle/Subscriber/TalksAndNewsSitemapSubscriber.php index b1877fc4d..0ce2ca21b 100644 --- a/sources/AppBundle/Subscriber/TalksSitemapSubscriber.php +++ b/sources/AppBundle/Subscriber/TalksAndNewsSitemapSubscriber.php @@ -4,6 +4,8 @@ use AppBundle\Event\Model\Repository\TalkRepository; use AppBundle\Event\Model\Talk; +use AppBundle\Site\Model\Article; +use AppBundle\Site\Model\Repository\ArticleRepository; use CCMBenchmark\TingBundle\Repository\RepositoryFactory; use Presta\SitemapBundle\Event\SitemapPopulateEvent; use Presta\SitemapBundle\Service\UrlContainerInterface; @@ -11,7 +13,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -class TalksSitemapSubscriber implements EventSubscriberInterface +class TalksAndNewsSitemapSubscriber implements EventSubscriberInterface { /** @var RepositoryFactory */ private $ting; @@ -35,6 +37,7 @@ public static function getSubscribedEvents() public function populate(SitemapPopulateEvent $event) { $this->registerTalksUrls($event->getUrlContainer()); + $this->registerNewsUrls($event->getUrlContainer()); } public function registerTalksUrls(UrlContainerInterface $urls) @@ -58,4 +61,24 @@ public function registerTalksUrls(UrlContainerInterface $urls) } } } + + public function registerNewsUrls(UrlContainerInterface $urls) + { + $news = $this->ting->get(ArticleRepository::class)->findAllPublishedNews(); + + /** @var Article $article */ + foreach ($news as $article) { + $urls->addUrl( + new UrlConcrete( + $this->urlGenerator->generate( + 'news_display', + ['code' => $article->getSlug(),], + UrlGeneratorInterface::ABSOLUTE_URL + ), + $article->getPublishedAt() + ), + 'news' + ); + } + } }