From 3a87fdfc738a5f2e0bbbd45c270112013ed9361e Mon Sep 17 00:00:00 2001 From: Xavier Leune Date: Tue, 17 Nov 2026 17:29:36 +0100 Subject: [PATCH 1/2] =?UTF-8?q?Calcule=20le=20VideoObject=20des=20pages=20?= =?UTF-8?q?talk=20c=C3=B4t=C3=A9=20PHP,=20avec=20langue=20et=20transcripti?= =?UTF-8?q?on?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit La #2470 a ajouté un JSON-LD VideoObject écrit directement dans le template. Il est remplacé par VideoObjectJsonLd, testé : - la description est convertie en texte brut (balises retirées, entités HTML décodées, espaces normalisés) au lieu d'un simple striptags ; - uploadDate vient du début du créneau du talk, toujours présent, et non plus des dates de l'événement, qui donnaient la date du jour quand elles étaient vides ; - inLanguage et transcript sont renseignés quand ils existent ; - l'encodage JSON_HEX_TAG empêche un texte de fermer la balise script. --- .../Controller/Website/Talks/ShowAction.php | 3 + .../Event/Talk/VideoObjectJsonLd.php | 74 +++++++++++++++ templates/site/talks/show.html.twig | 15 +-- tests/behat/bootstrap/FeatureContext.php | 61 ++++++++++++ tests/behat/features/PublicSite/Talks.feature | 21 ++++ .../Event/Talk/VideoObjectJsonLdTest.php | 95 +++++++++++++++++++ 6 files changed, 257 insertions(+), 12 deletions(-) create mode 100644 sources/AppBundle/Event/Talk/VideoObjectJsonLd.php create mode 100644 tests/unit/AppBundle/Event/Talk/VideoObjectJsonLdTest.php diff --git a/sources/AppBundle/Controller/Website/Talks/ShowAction.php b/sources/AppBundle/Controller/Website/Talks/ShowAction.php index deda565b9..f0728b8ca 100644 --- a/sources/AppBundle/Controller/Website/Talks/ShowAction.php +++ b/sources/AppBundle/Controller/Website/Talks/ShowAction.php @@ -8,6 +8,7 @@ use AppBundle\Event\Model\Repository\EventRepository; use AppBundle\Event\Model\Repository\SpeakerRepository; use AppBundle\Event\Model\Repository\TalkRepository; +use AppBundle\Event\Talk\VideoObjectJsonLd; use AppBundle\Joindin\JoindinComments; use AppBundle\Subtitles\Parser; use AppBundle\Twig\ViewRenderer; @@ -23,6 +24,7 @@ public function __construct( private readonly SpeakerRepository $speakerRepository, private readonly PlanningRepository $planningRepository, private readonly EventRepository $eventRepository, + private readonly VideoObjectJsonLd $videoObjectJsonLd, ) {} public function __invoke(int $id, string $slug): Response @@ -50,6 +52,7 @@ public function __invoke(int $id, string $slug): Response 'speakers' => $speakers, 'comments' => $comments, 'transcript' => $parsedContent, + 'video_object' => $this->videoObjectJsonLd->getData($talk, $planning->start, $parsedContent), ]); } } diff --git a/sources/AppBundle/Event/Talk/VideoObjectJsonLd.php b/sources/AppBundle/Event/Talk/VideoObjectJsonLd.php new file mode 100644 index 000000000..69d17d6e2 --- /dev/null +++ b/sources/AppBundle/Event/Talk/VideoObjectJsonLd.php @@ -0,0 +1,74 @@ + : + * JSON_HEX_TAG encode < et >, si bien qu'un texte contenant « » ne peut pas fermer la balise. + */ + public const int JSON_FLAGS = JSON_HEX_TAG | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE; + + /** + * @param array $transcript les phrases renvoyées par AppBundle\Subtitles\Parser + * + * @return array|null null quand le talk n'a pas de vidéo ou pas de date de créneau + */ + public function getData(Talk $talk, ?\DateTimeInterface $start, array $transcript): ?array + { + if (!$talk->hasYoutubeId() || null === $start) { + return null; + } + + $youtubeId = (string) $talk->getYoutubeId(); + + $data = [ + '@context' => 'https://schema.org', + '@type' => 'VideoObject', + 'name' => $talk->getTitle(), + ]; + + $description = $this->toPlainText((string) $talk->getDescription()); + if ('' !== $description) { + $data['description'] = $description; + } + + $data += [ + 'thumbnailUrl' => sprintf('https://img.youtube.com/vi/%s/hqdefault.jpg', $youtubeId), + 'uploadDate' => $start->format(\DateTimeInterface::ATOM), + 'embedUrl' => sprintf('https://www.youtube.com/embed/%s', $youtubeId), + 'contentUrl' => (string) $talk->getYoutubeUrl(), + ]; + + if (null !== $talk->getLanguageCode()) { + $data['inLanguage'] = $talk->getLanguageCode(); + } + + $transcriptText = $this->toPlainText(implode(' ', array_map( + static fn(array $cue): string => is_string($cue['text']) ? $cue['text'] : '', + $transcript, + ))); + if ('' !== $transcriptText) { + $data['transcript'] = $transcriptText; + } + + return $data; + } + + private function toPlainText(string $html): string + { + $text = html_entity_decode(strip_tags($html), ENT_QUOTES | ENT_HTML5, 'UTF-8'); + + return trim((string) preg_replace('/\s+/u', ' ', $text)); + } +} diff --git a/templates/site/talks/show.html.twig b/templates/site/talks/show.html.twig index 70432bbe8..91d09e5d6 100644 --- a/templates/site/talks/show.html.twig +++ b/templates/site/talks/show.html.twig @@ -17,23 +17,14 @@ {% if talk.hasYoutubeId %} - {% else %} {% endif %} + {% if video_object %} + + {% endif %} {% endblock %} {% block content %} diff --git a/tests/behat/bootstrap/FeatureContext.php b/tests/behat/bootstrap/FeatureContext.php index ada57e0da..5927597d6 100644 --- a/tests/behat/bootstrap/FeatureContext.php +++ b/tests/behat/bootstrap/FeatureContext.php @@ -8,6 +8,7 @@ use Behat\Behat\Context\Context; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use Behat\Gherkin\Node\PyStringNode; +use Behat\Gherkin\Node\TableNode; use Behat\Hook\AfterScenario; use Behat\Hook\BeforeScenario; use Behat\Mink\Driver\BrowserKitDriver; @@ -351,4 +352,64 @@ public function openMenu(string $text): void $node->click(); } + + #[Then('the JSON-LD :type should have the following properties:')] + public function assertJsonLdHasProperties(string $type, TableNode $expectedProperties): void + { + $data = $this->findJsonLd($type); + + if (null === $data) { + throw new ExpectationException(sprintf('No JSON-LD of type "%s" found', $type), $this->minkContext->getSession()->getDriver()); + } + + foreach ($expectedProperties->getRowsHash() as $property => $expectedValue) { + $actualValue = $data[$property] ?? null; + if (!is_scalar($actualValue) || (string) $actualValue !== $expectedValue) { + throw new ExpectationException( + sprintf('JSON-LD "%s" property "%s" is %s, "%s" expected', $type, $property, json_encode($actualValue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $expectedValue), + $this->minkContext->getSession()->getDriver(), + ); + } + } + } + + #[Then('the JSON-LD :type property :property should match :regex')] + public function assertJsonLdPropertyMatches(string $type, string $property, string $regex): void + { + $actualValue = $this->findJsonLd($type)[$property] ?? null; + + if (!is_string($actualValue) || !preg_match($regex, $actualValue)) { + throw new ExpectationException( + sprintf('JSON-LD "%s" property "%s" is %s, it does not match "%s"', $type, $property, json_encode($actualValue, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), $regex), + $this->minkContext->getSession()->getDriver(), + ); + } + } + + #[Then('the response should not contain a JSON-LD :type')] + public function assertNoJsonLd(string $type): void + { + if (null !== $this->findJsonLd($type)) { + throw new ExpectationException(sprintf('A JSON-LD of type "%s" was found', $type), $this->minkContext->getSession()->getDriver()); + } + } + + /** + * Décode chaque '); + $talk->setAbstract('Résumé </script> échappé en HTML'); + $talk->setYoutubeId('abc'); + + $data = new VideoObjectJsonLd()->getData($talk, new \DateTime(), [ + ['start' => 0.0, 'text' => 'Texte </script>'], + ]); + self::assertIsArray($data); + + $json = json_encode($data, VideoObjectJsonLd::JSON_FLAGS); + + self::assertIsString($json); + self::assertStringNotContainsStringIgnoringCase(' Date: Thu, 19 Nov 2026 11:18:47 +0100 Subject: [PATCH 2/2] Ajoute l'URL de la page au VideoObject et retire contentUrl MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - url : l'URL canonique de la page du talk, reprise de la #2470. - contentUrl est retiré : Google y attend le fichier vidéo lui-même, pas la page YouTube ; embedUrl suffit pour une vidéo YouTube. - description est obligatoire pour Google : sans résumé, on reprend le titre au lieu d'omettre la propriété. - Les steps Behat JSON-LD échouent si la page contient deux blocs du même type, cas d'un bloc en double après un merge. --- .../Controller/Website/Talks/ShowAction.php | 3 +- .../Event/Talk/VideoObjectJsonLd.php | 10 +++---- tests/behat/bootstrap/FeatureContext.php | 11 +++++-- tests/behat/features/PublicSite/Talks.feature | 2 +- .../Event/Talk/VideoObjectJsonLdTest.php | 29 ++++++++++++++----- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/sources/AppBundle/Controller/Website/Talks/ShowAction.php b/sources/AppBundle/Controller/Website/Talks/ShowAction.php index f0728b8ca..a76363ca9 100644 --- a/sources/AppBundle/Controller/Website/Talks/ShowAction.php +++ b/sources/AppBundle/Controller/Website/Talks/ShowAction.php @@ -14,6 +14,7 @@ use AppBundle\Twig\ViewRenderer; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; final class ShowAction extends AbstractController { @@ -52,7 +53,7 @@ public function __invoke(int $id, string $slug): Response 'speakers' => $speakers, 'comments' => $comments, 'transcript' => $parsedContent, - 'video_object' => $this->videoObjectJsonLd->getData($talk, $planning->start, $parsedContent), + 'video_object' => $this->videoObjectJsonLd->getData($talk, $planning->start, $parsedContent, $this->generateUrl('talks_show', ['id' => $talk->getId(), 'slug' => $talk->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL)), ]); } } diff --git a/sources/AppBundle/Event/Talk/VideoObjectJsonLd.php b/sources/AppBundle/Event/Talk/VideoObjectJsonLd.php index 69d17d6e2..7d208e686 100644 --- a/sources/AppBundle/Event/Talk/VideoObjectJsonLd.php +++ b/sources/AppBundle/Event/Talk/VideoObjectJsonLd.php @@ -21,10 +21,11 @@ /** * @param array $transcript les phrases renvoyées par AppBundle\Subtitles\Parser + * @param string $url URL absolue de la page du talk * * @return array|null null quand le talk n'a pas de vidéo ou pas de date de créneau */ - public function getData(Talk $talk, ?\DateTimeInterface $start, array $transcript): ?array + public function getData(Talk $talk, ?\DateTimeInterface $start, array $transcript, string $url): ?array { if (!$talk->hasYoutubeId() || null === $start) { return null; @@ -38,16 +39,15 @@ public function getData(Talk $talk, ?\DateTimeInterface $start, array $transcrip 'name' => $talk->getTitle(), ]; + // description est obligatoire pour Google : sans résumé, on reprend le titre. $description = $this->toPlainText((string) $talk->getDescription()); - if ('' !== $description) { - $data['description'] = $description; - } + $data['description'] = '' !== $description ? $description : $talk->getTitle(); $data += [ 'thumbnailUrl' => sprintf('https://img.youtube.com/vi/%s/hqdefault.jpg', $youtubeId), 'uploadDate' => $start->format(\DateTimeInterface::ATOM), 'embedUrl' => sprintf('https://www.youtube.com/embed/%s', $youtubeId), - 'contentUrl' => (string) $talk->getYoutubeUrl(), + 'url' => $url, ]; if (null !== $talk->getLanguageCode()) { diff --git a/tests/behat/bootstrap/FeatureContext.php b/tests/behat/bootstrap/FeatureContext.php index 5927597d6..fadecb622 100644 --- a/tests/behat/bootstrap/FeatureContext.php +++ b/tests/behat/bootstrap/FeatureContext.php @@ -395,7 +395,8 @@ public function assertNoJsonLd(string $type): void } /** - * Décode chaque