Skip to content
Draft
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
18 changes: 0 additions & 18 deletions phpstan-baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -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\\>\\|T of object, string given\\.$#',
'identifier' => 'argument.type',
Expand Down
36 changes: 29 additions & 7 deletions sources/AppBundle/Controller/Website/Talks/ListAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
48 changes: 48 additions & 0 deletions sources/AppBundle/Event/Model/Repository/SpeakerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions sources/AppBundle/Event/Model/Repository/TalkRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
90 changes: 90 additions & 0 deletions sources/AppBundle/Event/Talk/TalkListQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace AppBundle\Event\Talk;

/**
* Paramètres d'URL de la liste des talks (`/talks/`), tels qu'écrits par
* l'urlSync d'instantsearch.js v1 (`htdocs/js/talk/list.js`).
*
* Seules `/talks/` et une facette unique (speaker ou événement) sont des pages
* à indexer ; toute recherche, pagination ou autre filtre en est une déclinaison.
*/
final readonly class TalkListQuery
{
public const string FACET_SPEAKER = 'speakers.label';
public const string FACET_EVENT = 'event.title';

/**
* Paramètres de l'état Algolia, hors `q`, `p` et `fR` traités à part :
* `hPP` (résultats par page), `idx` (index), `is_v` (version d'instantsearch),
* `dFR` (facettes disjonctives), `nR` (filtres numériques), `fE` (exclusions),
* `hFR` (facettes hiérarchiques), `tR` (tags).
*/
private const array SEARCH_STATE_PARAMETERS = ['hPP', 'idx', 'is_v', 'dFR', 'nR', 'fE', 'hFR', 'tR'];

/**
* @param array<string, mixed> $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<string>
*/
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<array-key, mixed>
*/
private function getFacetRefinements(): array
{
$refinements = $this->query['fR'] ?? [];

return is_array($refinements) ? $refinements : ['' => $refinements];
}
}
3 changes: 3 additions & 0 deletions templates/site/talks/list.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

{% block metas %}
{{ parent() }}
{% if noindex %}
<meta name="robots" content="noindex, follow">
{% endif %}
<meta name="algolia_appid" content="{{ algolia_app_id }}" />
<meta name="algolia_apikey" content="{{ algolia_api_key }}" />

Expand Down
75 changes: 61 additions & 14 deletions tests/behat/features/PublicSite/Talks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/\" />"
When I go to "/talks/?p=1"
And the response should contain "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/\" />"
When I go to "/talks/?q=&hPP=7&idx=afup_talks&p=-1"
And the response should contain "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/\" />"
And the response should not contain "<meta name=\"robots\""
When I go to "/talks/?p=0"
Then the response should contain "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/\" />"
And the response should not contain "<meta name=\"robots\""

@reloadDbWithTestData
Scenario: Accès à la liste des vidéos d'un évènement en particulier
When I go to "/talks/?fR[event.title][0]=forum"
Then the response status code should be 200
And I should see "forum les vidéos"
And the response should contain "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/?fR%5Bevent.title%5D%5B0%5D=forum\" />"
And the response should not contain "<meta name=\"robots\""

@reloadDbWithTestData
Scenario: Évènement sans talk publié
When I go to "/talks/?fR[event.title][0]=AFUP Day Lyon"
Then the response status code should be 404

@reloadDbWithTestData
Scenario: Évènement inconnu
When I go to "/talks/?fR[event.title][0]=AFUP Day 2019 Lyon"
Then I should see "AFUP Day 2019 Lyon les vidéos"
And the response should contain "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/?fR%5Bevent.title%5D%5B0%5D=AFUP%20Day%202019%20Lyon\" />"
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 "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/?fR%5Bevent.title%5D%5B0%5D=AFUP%20Day%202019%20Lyon\" />"
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 "<url>"
Then the response status code should be 200
And the response should contain "<meta name=\"robots\" content=\"noindex, follow\">"
And the response should contain "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/<canonical>\" />"

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
Expand All @@ -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 "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/?fR%5Bspeakers.label%5D%5B0%5D=Geoffrey%20BACHELET\" />"
And the response should not contain "<meta name=\"robots\""

@reloadDbWithTestData
Scenario: Libellé de speaker avec des espaces superflus
When I go to "/talks/?fR[speakers.label][0]=%20Geoffrey%20%20%20BACHELET%20"
Then the response status code should be 200
And the response should not contain "<meta name=\"robots\""

@reloadDbWithTestData
Scenario: Speaker sans talk publié
When I go to "/talks/?fR[speakers.label][0]=John DOE"
Then the response status code should be 404

@reloadDbWithTestData
Scenario: Speaker inconnu
When I go to "/talks/?fR[speakers.label][0]=Un Speaker"
Then I should see "Les vidéos de Un Speaker"
And the response should contain "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/?fR%5Bspeakers.label%5D%5B0%5D=Un%20Speaker\" />"
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 "<link rel=\"canonical\" href=\"https://apachephptest:80/talks/?fR%5Bspeakers.label%5D%5B0%5D=Un%20Speaker\" />"
Then the response status code should be 404
Loading
Loading