From e84d6cbc2545e35fcafb55a3d5dc60b9ec34debd Mon Sep 17 00:00:00 2001 From: Xavier Leune Date: Tue, 17 Nov 2026 15:25:09 +0100 Subject: [PATCH] Assainit les sitemaps des talks et des news MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sitemap.talks.xml listait tous les speakers de tous les CFP (une ligne par speaker et par événement, candidats refusés compris) et tous les événements, d'où des en double et des facettes sans aucune conférence. Un talk planifié sur plusieurs créneaux y apparaissait aussi plusieurs fois, avec pour lastmod sa date de soumission au CFP. Les facettes speaker et événement sont désormais dérivées des seuls talks publiés (passés et visibles dans l'historique), dédoublonnées (label de speaker normalisé, titre d'événement), sans changefreq daily ni priorité 1 pour les événements. Le lastmod d'un talk est la fin de son créneau. sitemap.news.xml n'ajoutait que la dernière page de la liste des news (borne de départ erronée), page orpheline sans intérêt : seuls les articles y figurent désormais. --- .../Model/Repository/SpeakerRepository.php | 36 ++++ .../Subscriber/SitemapXmlSubscriber.php | 145 ++++++++++------ .../features/PublicSite/Sitemaps.feature | 6 + .../Subscriber/SitemapXmlSubscriberTest.php | 159 ++++++++++++++++++ 4 files changed, 293 insertions(+), 53 deletions(-) create mode 100644 tests/integration/AppBundle/Subscriber/SitemapXmlSubscriberTest.php diff --git a/sources/AppBundle/Event/Model/Repository/SpeakerRepository.php b/sources/AppBundle/Event/Model/Repository/SpeakerRepository.php index 5a92ad0df..771520e8c 100644 --- a/sources/AppBundle/Event/Model/Repository/SpeakerRepository.php +++ b/sources/AppBundle/Event/Model/Repository/SpeakerRepository.php @@ -40,6 +40,42 @@ public function getSpeakersByTalk(Talk $talk) return $query->query($this->getCollection(new HydratorSingleObject())); } + /** + * Conférenciers liés à au moins une des sessions données, triés par identifiant. + * + * @param list $talkIds + * + * @return list + */ + public function getSpeakersByTalkIds(array $talkIds): array + { + if ($talkIds === []) { + return []; + } + + $placeholders = []; + $params = []; + foreach ($talkIds as $index => $talkId) { + $placeholders[] = ':talk' . $index; + $params['talk' . $index] = $talkId; + } + + $query = $this->getPreparedQuery(sprintf( + 'SELECT DISTINCT c.conferencier_id, c.id_forum, c.genre, c.nom, c.prenom, c.email, c.societe, + c.biographie, c.twitter, c.user_github, c.photo, c.bluesky, c.mastodon, c.linkedin + FROM afup_conferenciers c + INNER JOIN afup_conferenciers_sessions cs ON cs.conferencier_id = c.conferencier_id + WHERE cs.session_id IN (%s) + ORDER BY c.conferencier_id ASC', + implode(', ', $placeholders), + ))->setParams($params); + + /** @var list $speakers */ + $speakers = iterator_to_array($query->query($this->getCollection(new HydratorSingleObject())), false); + + return $speakers; + } + /** * Retrieve speakers with a scheduled talk for a given event * @param bool $returnTalksThatWillBePublished diff --git a/sources/AppBundle/Subscriber/SitemapXmlSubscriber.php b/sources/AppBundle/Subscriber/SitemapXmlSubscriber.php index 49bc304bc..4d408004f 100644 --- a/sources/AppBundle/Subscriber/SitemapXmlSubscriber.php +++ b/sources/AppBundle/Subscriber/SitemapXmlSubscriber.php @@ -7,12 +7,11 @@ use Afup\Site\Corporate\Feuille; use AppBundle\Association\Model\CompanyMember; use AppBundle\Association\Model\Repository\CompanyMemberRepository; -use AppBundle\Controller\Website\News\ListAction; +use AppBundle\Event\Entity\Repository\PlanningRepository; use AppBundle\Event\Model\Event; use AppBundle\Event\Model\Repository\EventRepository; use AppBundle\Event\Model\Repository\SpeakerRepository; use AppBundle\Event\Model\Repository\TalkRepository; -use AppBundle\Event\Model\Speaker; use AppBundle\Event\Model\Talk; use AppBundle\Site\Entity\Repository\FeuilleRepository; use AppBundle\Site\Entity\Repository\ArticleRepository; @@ -29,6 +28,7 @@ class SitemapXmlSubscriber implements EventSubscriberInterface public function __construct( private readonly UrlGeneratorInterface $urlGenerator, private readonly TalkRepository $talkRepository, + private readonly PlanningRepository $planningRepository, private readonly SpeakerRepository $speakerRepository, private readonly EventRepository $eventRepository, private readonly ArticleRepository $articleRepository, @@ -45,31 +45,54 @@ public static function getSubscribedEvents(): array public function populate(SitemapPopulateEvent $event): void { - $this->registerTalksUrls($event->getUrlContainer()); - $this->registerSpeakersTalksUrls($event->getUrlContainer()); - $this->registerEventsTalksUrls($event->getUrlContainer()); + $publishedTalks = $this->getPublishedTalks(); + + $this->registerTalksUrls($event->getUrlContainer(), $publishedTalks); + $this->registerSpeakersTalksUrls($event->getUrlContainer(), $publishedTalks); + $this->registerEventsTalksUrls($event->getUrlContainer(), $publishedTalks); $this->registerNewsUrls($event->getUrlContainer()); $this->registerMembers($event->getUrlContainer()); $this->registerDefaultPages($event->getUrlContainer()); } - public function registerTalksUrls(UrlContainerInterface $urls): void + /** + * Talks passés visibles dans l'historique, indexés par identifiant + * (un talk planifié sur plusieurs créneaux n'apparaît qu'une fois). + * + * @return array + */ + private function getPublishedTalks(): array { - /** @var Talk[] $talks */ - $talks = $this->talkRepository->getAllPastTalks(new \DateTime()); + $publishedTalks = []; - foreach ($talks as $talk) { - if (!$talk->isDisplayedOnHistory()) { - continue; + /** @var iterable $pastTalks */ + $pastTalks = $this->talkRepository->getAllPastTalks(new \DateTime()); + + foreach ($pastTalks as $talk) { + $talkId = $talk->getId(); + if ($talkId !== null && $talk->isDisplayedOnHistory()) { + $publishedTalks[$talkId] = $talk; } + } + + return $publishedTalks; + } + /** + * @param array $publishedTalks + */ + private function registerTalksUrls(UrlContainerInterface $urls, array $publishedTalks): void + { + $plannings = $this->planningRepository->findIndexedByTalkIds(array_keys($publishedTalks)); + + foreach ($publishedTalks as $talkId => $talk) { $url = new UrlConcrete( $this->urlGenerator->generate( 'talks_show', - ['id' => $talk->getId(), 'slug' => $talk->getSlug()], + ['id' => $talkId, 'slug' => $talk->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL, ), - $talk->getSubmittedOn(), + $plannings[$talkId]->end ?? null, ); $urls->addUrl($url, 'talks'); @@ -87,42 +110,70 @@ public function registerTalksUrls(UrlContainerInterface $urls): void } } - public function registerSpeakersTalksUrls(UrlContainerInterface $urls): void + /** + * Facettes speaker de la liste des talks : uniquement les speakers ayant + * au moins un talk publié, une seule URL par label normalisé. + * + * @param array $publishedTalks + */ + private function registerSpeakersTalksUrls(UrlContainerInterface $urls, array $publishedTalks): void { - /** @var Speaker[] $speakers */ - $speakers = $this->speakerRepository->getAll(); - foreach ($speakers as $speaker) { - $url = new UrlConcrete( - $this->urlGenerator->generate( - 'talks_list', - ['fR' => ['speakers.label' => [$speaker->getLabel()]]], - UrlGeneratorInterface::ABSOLUTE_URL, + $labels = []; + foreach ($this->speakerRepository->getSpeakersByTalkIds(array_keys($publishedTalks)) as $speaker) { + $label = self::normalizeLabel($speaker->getLabel()); + if ($label !== '') { + $labels[$label] ??= $speaker->getLabel(); + } + } + + foreach ($labels as $label) { + $urls->addUrl( + new UrlConcrete( + $this->urlGenerator->generate( + 'talks_list', + ['fR' => ['speakers.label' => [$label]]], + UrlGeneratorInterface::ABSOLUTE_URL, + ), ), + 'talks', ); - $urls->addUrl($url, 'talks'); } } - public function registerEventsTalksUrls(UrlContainerInterface $urls): void + /** + * Facettes événement de la liste des talks : uniquement les événements + * ayant au moins un talk publié, une seule URL par titre. + * + * @param array $publishedTalks + */ + private function registerEventsTalksUrls(UrlContainerInterface $urls, array $publishedTalks): void { - /** @var Event[] $events */ - $events = $this->eventRepository->getAll(); - foreach ($events as $event) { - $url = new UrlConcrete( - $this->urlGenerator->generate( - 'talks_list', - ['fR' => ['event.title' => [$event->getTitle()]]], - UrlGeneratorInterface::ABSOLUTE_URL, + $eventIds = array_map(static fn(Talk $talk): ?int => $talk->getForumId(), $publishedTalks); + + $registeredTitles = []; + /** @var Event $event */ + foreach ($this->eventRepository->getAll() as $event) { + $title = $event->getTitle(); + if (!in_array($event->getId(), $eventIds, true) || in_array($title, $registeredTitles, true)) { + continue; + } + $registeredTitles[] = $title; + + $urls->addUrl( + new UrlConcrete( + $this->urlGenerator->generate( + 'talks_list', + ['fR' => ['event.title' => [$title]]], + UrlGeneratorInterface::ABSOLUTE_URL, + ), + $event->getDateEnd(), ), - $event->getDateEnd(), - UrlConcrete::CHANGEFREQ_DAILY, - 1, + 'talks', ); - $urls->addUrl($url, 'talks'); } } - public function registerNewsUrls(UrlContainerInterface $urls): void + private function registerNewsUrls(UrlContainerInterface $urls): void { $news = $this->articleRepository->findAllPublishedArticles(); @@ -141,23 +192,11 @@ public function registerNewsUrls(UrlContainerInterface $urls): void 'news', ); } + } - $total = $this->articleRepository->countPublishedArticles([]); - $byPage = ListAction::ARTICLES_PER_PAGE; - $lastPage = ceil($total / $byPage); - - for ($page = max($lastPage, 1); $page <= $lastPage; $page++) { - $urls->addUrl( - new UrlConcrete( - $this->urlGenerator->generate( - 'news_list', - ['page' => $page], - UrlGeneratorInterface::ABSOLUTE_URL, - ), - ), - 'news', - ); - } + private static function normalizeLabel(string $label): string + { + return (string) preg_replace('/\s+/u', ' ', trim($label)); } private function registerMembers(UrlContainerInterface $urls): void diff --git a/tests/behat/features/PublicSite/Sitemaps.feature b/tests/behat/features/PublicSite/Sitemaps.feature index b982edecb..4940a13dc 100644 --- a/tests/behat/features/PublicSite/Sitemaps.feature +++ b/tests/behat/features/PublicSite/Sitemaps.feature @@ -16,6 +16,11 @@ Feature: Site Public - Sitemaps And the response should contain "https://apachephptest:80/talks/?fR%5Bspeakers.label%5D%5B0%5D=Geoffrey%20BACHELET" And the response should contain "https://apachephptest:80/talks/?fR%5Bevent.title%5D%5B0%5D=forum" And the response should contain "https://apachephptest:80/talks/" + # lastmod = fin du créneau de planning, et non date de soumission au CFP + And the response should contain "https://apachephptest:80/talks/6-revolutionons-php2020-09-15T10:40:00+02:00" + # Speaker et événement sans talk publié : pas de facette + And the response should not contain "speakers.label%5D%5B0%5D=John%20DOE" + And the response should not contain "event.title%5D%5B0%5D=AFUP%20Day%20Lyon" @reloadDbWithTestData Scenario: On accède sitemap video @@ -26,6 +31,7 @@ Feature: Site Public - Sitemaps Scenario: On accède sitemap news Given I am on "/sitemap.news.xml" And the response should contain "https://apachephptest:80/news/1-les-videos-du-forum-2018-en-ligne" + And the response should not contain "?page=" @reloadDbWithTestData Scenario: On accède sitemap members diff --git a/tests/integration/AppBundle/Subscriber/SitemapXmlSubscriberTest.php b/tests/integration/AppBundle/Subscriber/SitemapXmlSubscriberTest.php new file mode 100644 index 000000000..1e9545d0a --- /dev/null +++ b/tests/integration/AppBundle/Subscriber/SitemapXmlSubscriberTest.php @@ -0,0 +1,159 @@ +get(Connection::class); + + $this->insertEvent($connection, 10, 'PHP Tour 2015'); + $this->insertEvent($connection, 20, 'Forum PHP 2016'); + $this->insertEvent($connection, 30, 'Forum PHP 2030'); + + // Talk publié, avec un speaker au nom vide (entrée de test) à ignorer + $this->insertTalk($connection, 1, 10, 'Premier talk', true); + $this->insertPlanning($connection, 1, 10, self::PAST_START, self::PAST_END); + $this->insertSpeaker($connection, 1, 10, 'Jane', 'Doe'); + $this->insertSpeaker($connection, 2, 10, '', ''); + $this->linkSpeaker($connection, 1, 1); + $this->linkSpeaker($connection, 1, 2); + + // Talk publié sur deux créneaux, par la même personne inscrite à un autre CFP + $this->insertTalk($connection, 2, 20, 'Second talk', true); + $this->insertPlanning($connection, 2, 20, self::PAST_START, self::PAST_END); + $this->insertPlanning($connection, 2, 20, self::PAST_START + 3600, self::PAST_END + 3600); + $this->insertSpeaker($connection, 3, 20, 'Jane', 'DOE'); + $this->linkSpeaker($connection, 2, 3); + + // Talk refusé : ni le speaker ni l'événement ne doivent apparaître + $this->insertTalk($connection, 3, 30, 'Talk refusé', false); + $this->insertSpeaker($connection, 4, 30, 'Refused', 'Speaker'); + $this->linkSpeaker($connection, 3, 4); + + // Clinique passée : non visible dans l'historique + $this->insertTalk($connection, 4, 30, 'Clinique', true, Talk::TYPE_CLINIC); + $this->insertPlanning($connection, 4, 30, self::PAST_START, self::PAST_END); + $this->insertSpeaker($connection, 5, 30, 'Clinic', 'Speaker'); + $this->linkSpeaker($connection, 4, 5); + + $talksUrls = $this->populate()['talks']; + $talksLocs = array_map(static fn(UrlConcrete $url): string => $url->getLoc(), $talksUrls); + $talksUrlsByLoc = array_combine($talksLocs, $talksUrls); + + self::assertSame(array_values(array_unique($talksLocs)), $talksLocs, 'Aucune en double dans sitemap.talks.xml'); + + self::assertSame( + [ + 'http://localhost/talks/1-premier-talk', + 'http://localhost/talks/2-second-talk', + 'http://localhost/talks/?fR%5Bspeakers.label%5D%5B0%5D=Jane%20DOE', + 'http://localhost/talks/?fR%5Bevent.title%5D%5B0%5D=PHP%20Tour%202015', + 'http://localhost/talks/?fR%5Bevent.title%5D%5B0%5D=Forum%20PHP%202016', + ], + $talksLocs, + ); + + $talkUrl = $talksUrlsByLoc['http://localhost/talks/1-premier-talk']; + self::assertSame(self::PAST_END, $talkUrl->getLastmod()?->getTimestamp(), 'lastmod = fin du créneau de planning'); + + $eventUrl = $talksUrlsByLoc['http://localhost/talks/?fR%5Bevent.title%5D%5B0%5D=PHP%20Tour%202015']; + self::assertNull($eventUrl->getChangefreq()); + self::assertNull($eventUrl->getPriority()); + } + + /** + * @return array> URLs par section, dans l'ordre d'ajout (doublons compris) + */ + private function populate(): array + { + $container = new class implements UrlContainerInterface { + /** @var array> */ + public array $urls = []; + + public function addUrl(Url $url, string $section): void + { + if ($url instanceof UrlConcrete) { + $this->urls[$section][] = $url; + } + } + }; + + self::getContainer()->get(SitemapXmlSubscriber::class)->populate( + new SitemapPopulateEvent($container, self::getContainer()->get(UrlGeneratorInterface::class)), + ); + + return $container->urls; + } + + private function insertEvent(Connection $connection, int $id, string $title): void + { + $connection->insert('afup_forum', [ + 'id' => $id, + 'titre' => $title, + 'path' => 'event-' . $id, + 'date_debut' => '2016-01-01', + 'date_fin' => '2016-01-02', + ]); + } + + private function insertTalk(Connection $connection, int $id, int $eventId, string $title, bool $scheduled, int $type = Talk::TYPE_FULL_LONG): void + { + $connection->insert('afup_sessions', [ + 'session_id' => $id, + 'id_forum' => $eventId, + 'date_soumission' => '2015-06-01', + 'titre' => $title, + 'abstract' => 'Abstract de test', + 'genre' => $type, + 'plannifie' => $scheduled ? 1 : 0, + ]); + } + + private function insertPlanning(Connection $connection, int $talkId, int $eventId, int $start, int $end): void + { + $connection->insert('afup_forum_planning', [ + 'id_session' => $talkId, + 'id_forum' => $eventId, + 'debut' => $start, + 'fin' => $end, + 'id_salle' => 1, + 'keynote' => 0, + ]); + } + + private function insertSpeaker(Connection $connection, int $id, int $eventId, string $firstname, string $lastname): void + { + $connection->insert('afup_conferenciers', [ + 'conferencier_id' => $id, + 'id_forum' => $eventId, + 'prenom' => $firstname, + 'nom' => $lastname, + 'email' => sprintf('speaker%d@example.com', $id), + ]); + } + + private function linkSpeaker(Connection $connection, int $talkId, int $speakerId): void + { + $connection->insert('afup_conferenciers_sessions', [ + 'session_id' => $talkId, + 'conferencier_id' => $speakerId, + ]); + } +}