Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afupf#1180 allègement des requêtes SQL et ajout sitemap.xml #1538

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
);
}
}
}