diff --git a/phpstan-baseline.php b/phpstan-baseline.php index 9bd71bd97..8e98bb947 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -4171,24 +4171,6 @@ 'count' => 1, 'path' => __DIR__ . '/sources/AppBundle/Controller/Website/Talks/JoindinAction.php', ]; -$ignoreErrors[] = [ - 'message' => '#^Binary operation "\\." between \'Les vidéos de \' and mixed results in an error\\.$#', - 'identifier' => 'binaryOp.invalid', - 'count' => 1, - 'path' => __DIR__ . '/sources/AppBundle/Controller/Website/Talks/ListAction.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Binary operation "\\." between mixed and \' les vidéos\' results in an error\\.$#', - 'identifier' => 'binaryOp.invalid', - 'count' => 1, - 'path' => __DIR__ . '/sources/AppBundle/Controller/Website/Talks/ListAction.php', -]; -$ignoreErrors[] = [ - 'message' => '#^Cannot access offset 0 on mixed\\.$#', - 'identifier' => 'offsetAccess.nonOffsetAccessible', - 'count' => 2, - 'path' => __DIR__ . '/sources/AppBundle/Controller/Website/Talks/ListAction.php', -]; $ignoreErrors[] = [ 'message' => '#^Parameter \\#1 \\$objectOrClass of class ReflectionClass constructor expects class\\-string\\\\|T of object, string given\\.$#', 'identifier' => 'argument.type', diff --git a/sources/AppBundle/Controller/Website/Talks/ListAction.php b/sources/AppBundle/Controller/Website/Talks/ListAction.php index f2a90c3ba..6fae73069 100644 --- a/sources/AppBundle/Controller/Website/Talks/ListAction.php +++ b/sources/AppBundle/Controller/Website/Talks/ListAction.php @@ -5,6 +5,9 @@ namespace AppBundle\Controller\Website\Talks; use AppBundle\Antennes\AntenneRepository; +use AppBundle\Event\Model\Repository\SpeakerRepository; +use AppBundle\Event\Model\Repository\TalkRepository; +use AppBundle\Event\Talk\TalkListQuery; use AppBundle\Twig\ViewRenderer; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\DependencyInjection\Attribute\Autowire; @@ -21,29 +24,48 @@ public function __construct( #[Autowire(env: 'ALGOLIA_FRONTEND_API_KEY')] private readonly string $algoliaFrontendApikey, private readonly AntenneRepository $antennesRepository, + private readonly SpeakerRepository $speakerRepository, + private readonly TalkRepository $talkRepository, ) {} public function __invoke(Request $request): Response { + $query = new TalkListQuery($request->query->all()); + $speakerLabels = $query->getFacetValues(TalkListQuery::FACET_SPEAKER); + $eventTitles = $query->getFacetValues(TalkListQuery::FACET_EVENT); + + $now = new \DateTimeImmutable(); + foreach ($speakerLabels as $label) { + if (!$this->speakerRepository->hasPublishedTalkForLabel($label, $now)) { + throw $this->createNotFoundException('Aucun talk publié pour ce speaker'); + } + } + foreach ($eventTitles as $eventTitle) { + if (!$this->talkRepository->hasPublishedTalkForEventTitle($eventTitle, $now)) { + throw $this->createNotFoundException('Aucun talk publié pour cet événement'); + } + } + $title = 'Historique des conférences de l\'AFUP'; $canonical = $this->generateUrl('talks_list', referenceType: UrlGeneratorInterface::ABSOLUTE_URL); - if (isset($request->query->all('fR')['speakers.label'][0])) { - $label = $request->query->all('fR')['speakers.label'][0]; + if (isset($speakerLabels[0])) { + $label = $speakerLabels[0]; $title = 'Les vidéos de ' . $label; $canonical = $this->generateUrl('talks_list', [ - 'fR' => ['speakers.label' => [$label]], + 'fR' => [TalkListQuery::FACET_SPEAKER => [$label]], ], UrlGeneratorInterface::ABSOLUTE_URL); - } elseif (isset($request->query->all('fR')['event.title'][0])) { - $label = $request->query->all('fR')['event.title'][0]; - $title = $request->query->all('fR')['event.title'][0] . ' les vidéos'; + } elseif (isset($eventTitles[0])) { + $label = $eventTitles[0]; + $title = $label . ' les vidéos'; $canonical = $this->generateUrl('talks_list', [ - 'fR' => ['event.title' => [$label]], + 'fR' => [TalkListQuery::FACET_EVENT => [$label]], ], UrlGeneratorInterface::ABSOLUTE_URL); } return $this->view->render('site/talks/list.html.twig', [ 'title' => $title, 'canonical' => $canonical, + 'noindex' => !$query->isIndexable(), 'antennes' => $this->antennesRepository->getAllSortedByLabels(), 'algolia_app_id' => $this->algoliaAppId, 'algolia_api_key' => $this->algoliaFrontendApikey, diff --git a/sources/AppBundle/Event/Model/Repository/SpeakerRepository.php b/sources/AppBundle/Event/Model/Repository/SpeakerRepository.php index 5a92ad0df..c196dfdbd 100644 --- a/sources/AppBundle/Event/Model/Repository/SpeakerRepository.php +++ b/sources/AppBundle/Event/Model/Repository/SpeakerRepository.php @@ -40,6 +40,54 @@ public function getSpeakersByTalk(Talk $talk) return $query->query($this->getCollection(new HydratorSingleObject())); } + /** + * Un speaker dont le libellé (`Speaker::getLabel()`) correspond à celui donné + * a-t-il au moins un talk publié ? Les variations d'espaces sont tolérées + * (espaces en bord de chaîne, espaces multiples). + * + * Le SQL ne fait qu'un pré-filtre, compatible MySQL 5.7 (pas de REGEXP_REPLACE) : + * prénom et nom concaténés sans espaces, tabulations ni retours à la ligne, + * comparés sans tenir compte de la casse. La comparaison finale reprend + * `Speaker::getLabel()` pour rester fidèle aux libellés indexés dans Algolia. + */ + public function hasPublishedTalkForLabel(string $label, \DateTimeInterface $publishedBefore): bool + { + $label = self::normalizeLabel($label); + if ($label === '') { + return false; + } + + $query = $this->getPreparedQuery( + "SELECT c.conferencier_id, c.prenom, c.nom + FROM afup_conferenciers c + WHERE UPPER(REPLACE(REPLACE(REPLACE(REPLACE(CONCAT_WS('', c.prenom, c.nom), ' ', ''), '\t', ''), '\n', ''), '\r', '')) = UPPER(:compact_label) + AND EXISTS ( + SELECT 1 + FROM afup_conferenciers_sessions cs + INNER JOIN afup_sessions talk ON talk.session_id = cs.session_id + INNER JOIN afup_forum_planning planning ON planning.id_session = talk.session_id + WHERE cs.conferencier_id = c.conferencier_id AND " . TalkRepository::PUBLISHED_TALK_CONDITION . ' + )', + )->setParams([ + 'compact_label' => str_replace(' ', '', $label), + 'published_before' => $publishedBefore->format('U'), + ]); + + /** @var Speaker $speaker */ + foreach ($query->query($this->getCollection(new HydratorSingleObject())) as $speaker) { + if (self::normalizeLabel($speaker->getLabel()) === $label) { + return true; + } + } + + return false; + } + + private static function normalizeLabel(string $label): string + { + return (string) preg_replace('/\s+/u', ' ', trim($label)); + } + /** * Retrieve speakers with a scheduled talk for a given event * @param bool $returnTalksThatWillBePublished diff --git a/sources/AppBundle/Event/Model/Repository/TalkRepository.php b/sources/AppBundle/Event/Model/Repository/TalkRepository.php index ef8d029ae..27cb85cf9 100644 --- a/sources/AppBundle/Event/Model/Repository/TalkRepository.php +++ b/sources/AppBundle/Event/Model/Repository/TalkRepository.php @@ -27,6 +27,17 @@ */ class TalkRepository extends Repository implements MetadataInitializer { + /** + * Condition SQL d'un talk publié : créneau terminé avant `:published_before` + * (timestamp) et `Talk::isDisplayedOnHistory()`, c'est-à-dire planifié + * (`plannifie = 1`) et ni clinique ni présentation des speakers. + * + * Attend les alias `talk` (afup_sessions) et `planning` (afup_forum_planning). + */ + public const string PUBLISHED_TALK_CONDITION = 'talk.plannifie = 1' + . ' AND talk.genre NOT IN (' . Talk::TYPE_CLINIC . ', ' . Talk::TYPE_SPEAKER_INTRODUCTIONS . ')' + . ' AND planning.fin <= :published_before'; + public function getNumberOfTalksByEvent(Event|int $event, ?\DateTime $since = null) { return $this->getNumberOfTalksByEventAndLanguage($event, null, $since); @@ -380,6 +391,27 @@ public function getScheduledTalksByEvent(int $eventId): CollectionInterface return $query->query($this->getCollection(new HydratorSingleObject())); } + /** + * L'événement de ce titre exact a-t-il au moins un talk publié ? + * Comme l'index Algolia, l'événement est celui du créneau de planning. + */ + public function hasPublishedTalkForEventTitle(string $eventTitle, \DateTimeInterface $publishedBefore): bool + { + $query = $this->getPreparedQuery( + 'SELECT 1 + FROM afup_forum event + INNER JOIN afup_forum_planning planning ON planning.id_forum = event.id + INNER JOIN afup_sessions talk ON talk.session_id = planning.id_session + WHERE event.titre = :event_title AND ' . self::PUBLISHED_TALK_CONDITION . ' + LIMIT 1', + )->setParams([ + 'event_title' => $eventTitle, + 'published_before' => $publishedBefore->format('U'), + ]); + + return $query->query($this->getCollection(new HydratorArray()))->count() > 0; + } + public function getAllPastTalks(\DateTime $dateTime) { $query = $this->getPreparedQuery( diff --git a/sources/AppBundle/Event/Talk/TalkListQuery.php b/sources/AppBundle/Event/Talk/TalkListQuery.php new file mode 100644 index 000000000..2f8e910d4 --- /dev/null +++ b/sources/AppBundle/Event/Talk/TalkListQuery.php @@ -0,0 +1,90 @@ + $query + */ + public function __construct(private array $query) {} + + /** + * Valeurs de la facette donnée dans `fR`. Une valeur qui n'est pas une + * chaîne est renvoyée vide, pour qu'elle ne corresponde à rien. + * + * @return list + */ + public function getFacetValues(string $facet): array + { + $values = $this->getFacetRefinements()[$facet] ?? []; + if (!is_array($values)) { + $values = [$values]; + } + + return array_values(array_map( + static fn(mixed $value): string => is_string($value) ? $value : '', + $values, + )); + } + + public function isIndexable(): bool + { + $search = $this->query['q'] ?? ''; + if (!is_string($search) || trim($search) !== '') { + return false; + } + + $page = $this->query['p'] ?? 0; + if ((!is_string($page) && !is_int($page)) || (int) $page >= 1) { + return false; + } + + foreach (self::SEARCH_STATE_PARAMETERS as $parameter) { + if (array_key_exists($parameter, $this->query)) { + return false; + } + } + + $refinements = $this->getFacetRefinements(); + $refinementsCount = 0; + foreach ($refinements as $facet => $values) { + if ($facet !== self::FACET_SPEAKER && $facet !== self::FACET_EVENT) { + return false; + } + $refinementsCount += is_array($values) ? count($values) : 1; + } + + return $refinementsCount <= 1; + } + + /** + * @return array + */ + private function getFacetRefinements(): array + { + $refinements = $this->query['fR'] ?? []; + + return is_array($refinements) ? $refinements : ['' => $refinements]; + } +} diff --git a/templates/site/talks/list.html.twig b/templates/site/talks/list.html.twig index 75f629649..089f97752 100644 --- a/templates/site/talks/list.html.twig +++ b/templates/site/talks/list.html.twig @@ -17,6 +17,9 @@ {% block metas %} {{ parent() }} + {% if noindex %} + + {% endif %} diff --git a/tests/behat/features/PublicSite/Talks.feature b/tests/behat/features/PublicSite/Talks.feature index db8a89bac..72b889da4 100644 --- a/tests/behat/features/PublicSite/Talks.feature +++ b/tests/behat/features/PublicSite/Talks.feature @@ -6,20 +6,52 @@ Feature: Site Public - Talks When I follow "Vidéos" Then I should see "Historique des conférences de l'AFUP" And the response should contain "" - When I go to "/talks/?p=1" - And the response should contain "" - When I go to "/talks/?q=&hPP=7&idx=afup_talks&p=-1" - And the response should contain "" + And the response should not contain "" + And the response should not contain "" + And the response should not contain "" - When I go to "/talks/?q=&hPP=7&idx=afup_talks&p=0&fR[event.title][0]=AFUP Day 2019 Lyon" - Then I should see "AFUP Day 2019 Lyon les vidéos" - And the response should contain "" + Then the response status code should be 404 + + @reloadDbWithTestData + Scenario Outline: Une recherche, une page ou un filtre de la liste des vidéos n'est pas indexé + When I go to "" + Then the response status code should be 200 + And the response should contain "" + And the response should contain "\" />" + Examples: + | url | canonical | + | /talks/?q=symfony | | + | /talks/?p=1 | | + | /talks/?hPP=7 | | + | /talks/?idx=afup_talks | | + | /talks/?is_v=1 | | + | /talks/?dFR[type.label][0]=Conférence | | + | /talks/?nR[event.start_date][>=][0]=0 | | + | /talks/?fE[type.label][0]=Atelier | | + | /talks/?hFR[category][0]=php | | + | /talks/?tR=php | | + | /talks/?fR[has_video][0]=true | | + | /talks/?q=&hPP=7&idx=afup_talks&p=0&fR[event.title][0]=forum | ?fR%5Bevent.title%5D%5B0%5D=forum | + | /talks/?fR[event.title][0]=forum&fR[speakers.label][0]=Geoffrey BACHELET | ?fR%5Bspeakers.label%5D%5B0%5D=Geoffrey%20BACHELET | + | /talks/?fR[speakers.label][0]=Geoffrey BACHELET&fR[speakers.label][1]=Adrien GALLOU | ?fR%5Bspeakers.label%5D%5B0%5D=Geoffrey%20BACHELET | @reloadDbWithTestData Scenario: Affichage des posts Bluesky sur la page d'un talk @@ -29,9 +61,24 @@ Feature: Site Public - Talks @reloadDbWithTestData Scenario: Accès à la liste des vidéos d'un speaker + When I go to "/talks/?fR[speakers.label][0]=Geoffrey BACHELET" + Then the response status code should be 200 + And I should see "Les vidéos de Geoffrey BACHELET" + And the response should contain "" + And the response should not contain "" - When I go to "/talks/?q=&hPP=7&idx=afup_talks&p=0&fR[event.title][0]=AFUP Day 2019 Lyon&fR[speakers.label][0]=Un Speaker" - Then I should see "Les vidéos de Un Speaker" - And the response should contain "" + Then the response status code should be 404 diff --git a/tests/unit/AppBundle/Event/Talk/TalkListQueryTest.php b/tests/unit/AppBundle/Event/Talk/TalkListQueryTest.php new file mode 100644 index 000000000..ebecb8f00 --- /dev/null +++ b/tests/unit/AppBundle/Event/Talk/TalkListQueryTest.php @@ -0,0 +1,79 @@ + $query + */ + #[DataProvider('indexableQueries')] + public function testIsIndexable(array $query): void + { + self::assertTrue(new TalkListQuery($query)->isIndexable()); + } + + /** + * @return iterable}> + */ + public static function indexableQueries(): iterable + { + yield 'liste nue' => [[]]; + yield 'facette speaker' => [['fR' => ['speakers.label' => ['Geoffrey BACHELET']]]]; + yield 'facette événement' => [['fR' => ['event.title' => ['forum']]]]; + yield 'recherche vide' => [['q' => '', 'fR' => ['event.title' => ['forum']]]]; + yield 'première page' => [['p' => '0']]; + yield 'page négative' => [['p' => '-1']]; + yield 'paramètre de suivi' => [['utm_source' => 'newsletter']]; + } + + /** + * @param array $query + */ + #[DataProvider('notIndexableQueries')] + public function testIsNotIndexable(array $query): void + { + self::assertFalse(new TalkListQuery($query)->isIndexable()); + } + + /** + * @return iterable}> + */ + public static function notIndexableQueries(): iterable + { + yield 'recherche' => [['q' => 'symfony']]; + yield 'recherche tableau' => [['q' => ['symfony']]]; + yield 'deuxième page' => [['p' => '1']]; + yield 'page tableau' => [['p' => ['1']]]; + yield 'deux facettes' => [['fR' => ['speakers.label' => ['Geoffrey BACHELET'], 'event.title' => ['forum']]]]; + yield 'deux valeurs de facette' => [['fR' => ['speakers.label' => ['Geoffrey BACHELET', 'Adrien GALLOU']]]]; + yield 'autre facette' => [['fR' => ['has_video' => ['true']]]]; + yield 'facette non tableau' => [['fR' => 'forum']]; + yield 'hPP' => [['hPP' => '7']]; + yield 'idx' => [['idx' => 'afup_talks']]; + yield 'is_v' => [['is_v' => '1']]; + yield 'dFR' => [['dFR' => ['type.label' => ['Conférence']]]]; + yield 'nR' => [['nR' => ['event.start_date' => ['>=' => ['0']]]]]; + yield 'fE' => [['fE' => ['type.label' => ['Atelier']]]]; + yield 'hFR' => [['hFR' => ['category' => ['php']]]]; + yield 'tR' => [['tR' => 'php']]; + } + + public function testGetFacetValues(): void + { + $query = new TalkListQuery(['fR' => [ + 'speakers.label' => ['Geoffrey BACHELET', ['tableau']], + 'event.title' => 'forum', + ]]); + + self::assertSame(['Geoffrey BACHELET', ''], $query->getFacetValues(TalkListQuery::FACET_SPEAKER)); + self::assertSame(['forum'], $query->getFacetValues(TalkListQuery::FACET_EVENT)); + self::assertSame([], new TalkListQuery([])->getFacetValues(TalkListQuery::FACET_SPEAKER)); + } +}