Skip to content

Commit

Permalink
Merge pull request #1538 from stakovicz/feat-1180-less-sql-query-bett…
Browse files Browse the repository at this point in the history
…er-sitemap

afupf#1180 allègement des requêtes SQL et ajout sitemap.xml
  • Loading branch information
stakovicz authored Sep 15, 2024
2 parents a41ed89 + 0de8920 commit 01b9d54
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ services:
tags:
- {name: form.type}

AppBundle\Subscriber\TalksSitemapSubscriber:
AppBundle\Subscriber\TalksAndNewsSitemapSubscriber:
arguments:
- "@router"
- "@ting"
Expand Down
27 changes: 11 additions & 16 deletions sources/AppBundle/Controller/HtmlSitemapController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
9 changes: 9 additions & 0 deletions sources/AppBundle/Site/Model/Repository/ArticleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

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;
use Presta\SitemapBundle\Sitemap\Url\UrlConcrete;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class TalksSitemapSubscriber implements EventSubscriberInterface
class TalksAndNewsSitemapSubscriber implements EventSubscriberInterface
{
/** @var RepositoryFactory */
private $ting;
Expand All @@ -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)
Expand All @@ -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'
);
}
}
}

0 comments on commit 01b9d54

Please sign in to comment.