diff --git a/sources/AppBundle/Controller/Website/Talks/ShowAction.php b/sources/AppBundle/Controller/Website/Talks/ShowAction.php index deda565b9..a76363ca9 100644 --- a/sources/AppBundle/Controller/Website/Talks/ShowAction.php +++ b/sources/AppBundle/Controller/Website/Talks/ShowAction.php @@ -8,11 +8,13 @@ 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; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; final class ShowAction extends AbstractController { @@ -23,6 +25,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 +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, $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 new file mode 100644 index 000000000..7d208e686 --- /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 + * @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, string $url): ?array + { + if (!$talk->hasYoutubeId() || null === $start) { + return null; + } + + $youtubeId = (string) $talk->getYoutubeId(); + + $data = [ + '@context' => 'https://schema.org', + '@type' => 'VideoObject', + 'name' => $talk->getTitle(), + ]; + + // description est obligatoire pour Google : sans résumé, on reprend le titre. + $description = $this->toPlainText((string) $talk->getDescription()); + $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), + 'url' => $url, + ]; + + 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..fadecb622 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,69 @@ 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::URL); + self::assertIsArray($data); + + $json = json_encode($data, VideoObjectJsonLd::JSON_FLAGS); + + self::assertIsString($json); + self::assertStringNotContainsStringIgnoringCase('setTitle('Un talk sans résumé'); + $talk->setAbstract('

'); + $talk->setYoutubeId('abc'); + + $data = new VideoObjectJsonLd()->getData($talk, new \DateTime(), [], self::URL); + + self::assertIsArray($data); + self::assertSame('Un talk sans résumé', $data['description']); + } +}